0% found this document useful (0 votes)
22 views5 pages

Develop A Program To Demonstrate Basic Geometric Operations On The 3D Object

The document provides an OpenGL program that demonstrates basic geometric operations on a 3D object, specifically a rotating cube. It includes functions for initialization, display, reshaping the window, and handling keyboard input to control the rotation angles. The program uses GLUT to create a window and render the cube with different colors on each face, allowing user interaction for rotation via keyboard commands.
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)
22 views5 pages

Develop A Program To Demonstrate Basic Geometric Operations On The 3D Object

The document provides an OpenGL program that demonstrates basic geometric operations on a 3D object, specifically a rotating cube. It includes functions for initialization, display, reshaping the window, and handling keyboard input to control the rotation angles. The program uses GLUT to create a window and render the cube with different colors on each face, allowing user interaction for rotation via keyboard commands.
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/ 5

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

#include <GL/glut.h>

float angleX = 0.0f;


float angleY = 0.0f;

void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glRotatef(angleX, 1.0f, 0.0f, 0.0f);
glRotatef(angleY, 0.0f, 1.0f, 0.0f);

// Draw Cube
glBegin(GL_QUADS);

// Front face
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);

// Back face
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);

// Top face
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
// Bottom face
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);

// Right face
glColor3f(1.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);

// Left face
glColor3f(0.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);

glEnd();

glPopMatrix();
glutSwapBuffers();
}

void reshape(int w, int h) {


glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard(unsigned char key, int x, int y) {


switch (key) {
case 'q':
angleX += 5.0f;
break;
case 'w':
angleX -= 5.0f;
break;
case 'a':
angleY += 5.0f;
break;
case 's':
angleY -= 5.0f;
break;
}
glutPostRedisplay();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("3D Geometric Operations");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Explanation

Sure, let's break down the provided OpenGL program:

1. **Initialization**:
- The program starts with including the necessary OpenGL header file `<GL/glut.h>`.
- Two global variables `angleX` and `angleY` are declared to store the rotation angles around the X and
Y axes, respectively.

2. **Init Function**:
- The `init` function initializes OpenGL settings.
- It sets the clear color to black, sets the projection matrix to a perspective projection with a field of
view of 45 degrees, near clipping plane at 1.0, far clipping plane at 100.0, and aspect ratio of 1.0.
- It sets the modelview matrix to the default view position using `gluLookAt`.

3. **Display Function**:
- The `display` function is called whenever the contents of the window need to be redrawn.
- It clears the color buffer and the depth buffer.
- It sets up the transformation for rotating the cube by applying the rotation transformation using
`glRotatef`.
- It then draws the cube using `glBegin` and `glEnd` pairs, specifying each face of the cube with
different colors.
- Finally, it swaps the buffers to display the updated frame.

4. **Reshape Function**:
- The `reshape` function is called whenever the window is resized.
- It sets the viewport and adjusts the projection matrix to maintain the correct aspect ratio.

5. **Keyboard Function**:
- The `keyboard` function handles keyboard input.
- It adjusts the rotation angles (`angleX` and `angleY`) based on the keys pressed ('q', 'w', 'a', 's') to
control the rotation of the cube.
- After adjusting the angles, it requests a redraw using `glutPostRedisplay`.

6. **Main Function**:
- The `main` function is the entry point of the program.
- It initializes GLUT, sets the display mode, window size, position, and creates the main window.
- It registers callback functions for display, reshape, and keyboard events.
- Finally, it enters the GLUT main loop with `glutMainLoop`, which continuously processes events
until the program exits.

This program creates a simple rotating cube in a window. You can control the rotation of the cube using
the keyboard keys mentioned above.

You might also like