P3
P3
P3
#include <GL/glut.h>
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();
}
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.