You are on page 1of 2

#include <glut.

h>

// Function to render (draw) the scene


void renderScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth
buffers
glLoadIdentity(); // Reset transformations

// Set the color to red


glColor3f(1.0f, 0.0f, 0.0f);

// Draw a line loop


glBegin(GL_LINE_LOOP);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();

// Set the color to green


glColor3f(0.0f, 1.0f, 0.0f);

// Draw a line strip


glBegin(GL_LINE_STRIP);
glVertex2f(-0.5f, 0.0f);
glVertex2f(0.0f, -0.5f);
glVertex2f(0.5f, 0.0f);
glVertex2f(0.0f, 0.5f);
glEnd();

// Set the color to blue


glColor3f(0.0f, 0.0f, 1.0f);

// Draw a triangle fan


glBegin(GL_TRIANGLE_FAN);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.2f, 0.2f);
glVertex2f(0.4f, 0.0f);
glVertex2f(0.2f, -0.2f);
glVertex2f(-0.2f, -0.2f);
glVertex2f(-0.4f, 0.0f);
glVertex2f(-0.2f, 0.2f);
glEnd();

// Set the color to yellow


glColor3f(1.0f, 1.0f, 0.0f);

// Draw a polygon
glBegin(GL_POLYGON);
glVertex2f(-0.7f, 0.7f);
glVertex2f(-0.7f, 0.4f);
glVertex2f(-0.4f, 0.4f);
glVertex2f(-0.4f, 0.7f);
glEnd();

glFlush(); // Flush the OpenGL buffers to display the shapes


}

// Function to handle window resizing


void resizeWindow(int width, int height) {
glViewport(0, 0, width, height); // Set the viewport to the entire window
}

int main(int argc, char** argv) {


glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); // Set the display
mode

glutInitWindowSize(800, 600); // Set the window size


glutCreateWindow("OpenGL Program"); // Create the window with a title

glutDisplayFunc(renderScene); // Set the render function


glutReshapeFunc(resizeWindow); // Set the resize function

glutMainLoop(); // Start the main loop

return 0;
}

You might also like