Rectangle
#include <GL/glut.h>
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
// Draw a blue rectangle
glBegin(GL_QUADS); // Start drawing a quadrilateral (rectangle)
glColor3f(0.0f, 0.0f, 1.0f); // Set color to blue
glVertex2f(-1.0f, -0.5f); // Bottom-left corner
glVertex2f(1.0f, -0.5f); // Bottom-right corner
glVertex2f(1.0f, 0.5f); // Top-right corner
glVertex2f(-1.0f, 0.5f); // Top-left corner
glEnd(); // End drawing the rectangle
glFlush(); // Ensure drawing commands are executed
}
// Main function (same as previous examples)
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("Blue Rectangle");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Square
#include <GL/glut.h>
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the background (Color buffer)
// Draw a green square
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f); // Set color to green
glVertex2f(-0.5f, -0.5f); // Bottom-left corner
glVertex2f(0.5f, -0.5f); // Bottom-right corner
glVertex2f(0.5f, 0.5f); // Top-right corner
glVertex2f(-0.5f, 0.5f); // Top-left corner
glEnd();
glFlush(); // Ensure drawing commands are executed immediately
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("Green Square"); // Create a window with title
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
Triangle
#include <GL/glut.h>
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
// Draw a green triangle
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 1.0f, 0.0f); // Set color to green
glVertex2f(-0.5f, -0.5f); // Bottom-left vertex
glVertex2f(0.5f, -0.5f); // Bottom-right vertex
glVertex2f(0.0f, 0.5f); // Top vertex
glEnd();
glFlush(); // Ensure drawing commands are executed
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("Green triangle"); // Create a window with title
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
Circle
#include <GL/glut.h>
#include <corecrt_math.h>
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen with the current background color
// Draw a red circle
glColor3f(1.0f, 0.0f, 0.0f); // Set the drawing color to red
glBegin(GL_POLYGON); // Start drawing a polygon (approximation of a circle)
// Generate vertices for a smooth circle outline
for (int i = 0; i < 360; i++) {
float angle = i * 3.14159 / 180; // Convert degrees to radians
glVertex2f(0.5f * cos(angle), 0.5f * sin(angle)); // Center the circle at (0, 0) with radius 0.5
}
glEnd(); // End drawing the polygon
glFlush(); // Ensure drawing commands are executed immediately
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("Red Circle");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Cube