0% found this document useful (0 votes)
20 views3 pages

CG 2

The document contains code to demonstrate basic geometric operations on 2D objects in OpenGL. It includes functions to draw a square, triangle, rectangle, and polygon by specifying vertices and drawing the shapes with different colors. The main function initializes the window, display callback, and runs the OpenGL main loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

CG 2

The document contains code to demonstrate basic geometric operations on 2D objects in OpenGL. It includes functions to draw a square, triangle, rectangle, and polygon by specifying vertices and drawing the shapes with different colors. The main function initializes the window, display callback, and runs the OpenGL main loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

2.

Develop a program to demonstrate basic geometric operations on the 2D object


#include <GL/glut.h>
#include <math.h>
// Initial size of the window
int width = 500, height = 500;

// Function to initialize the OpenGL environment


void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Set background color to white
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-250, 250, -250, 250); // Set the coordinate system
}

// Function to draw a square


void drawSquare() {
glColor3f(1.0, 0.0, 0.0); // Set color to red
glBegin(GL_POLYGON);
glVertex2f(-150.0f, 50.0f);
glVertex2f(-50.0f, 50.0f);
glVertex2f(-50.0f, 150.0f);
glVertex2f(-150.0f, 150.0f);
glEnd();
}

// Function to draw a triangle


void drawTriangle() {
glColor3f(0.0, 1.0, 0.0); // Set color to green
glBegin(GL_TRIANGLES);
glVertex2f(50.0f, 50.0f);
glVertex2f(150.0f, 50.0f);
glVertex2f(100.0f, 150.0f);
glEnd();
}

Dept. of CSE,
// Function to draw a rectangle
void drawRectangle() {
glColor3f(0.0, 0.0, 1.0); // Set color to blue
glBegin(GL_POLYGON);
glVertex2f(-200.0f, -150.0f);
glVertex2f(-50.0f, -150.0f);
glVertex2f(-50.0f, -50.0f);
glVertex2f(-200.0f, -50.0f);
glEnd();
}

// Function to draw a polygon


void drawPolygon() {
glColor3f(1.0, 1.0, 0.0); // Set color to yellow
glBegin(GL_POLYGON);
glVertex2f(50.0f, -150.0f);
glVertex2f(100.0f, -150.0f);
glVertex2f(125.0f, -100.0f);
glVertex2f(100.0f, -50.0f);
glVertex2f(50.0f, -50.0f);
glVertex2f(25.0f, -100.0f);
glEnd();
}

// Function to display the objects


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the display

// Draw objects
drawSquare();
drawTriangle();
drawRectangle();
drawPolygon();

Dept. of CSE,
glFlush(); // Render now
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("Basic Geometric Operations on 2D Objects");

init();
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

Dept. of CSE,

You might also like