- #include <GL/glut.h>
- #include <stdlib.h>
- GLfloat bottomVertices[][3] = {
- {-0.5f, -0.5f, -0.5f},
- {-0.5f, 0.5f, -0.5f},
- {0.5f, -0.5f, -0.5f},
- {0.5f, 0.5f, -0.5f}
- };
- GLfloat topVertices[][3] = {
- {-0.25f, -0.25f, 0.5f},
- {-0.25f, 0.25f, 0.5f},
- {0.25f, -0.25f, 0.5f},
- {0.25f, 0.25f, 0.5f}
- };
- GLfloat colors[][3] = {
- {1.0f, 0.0f, 0.0f}, // red
- {1.0f, 1.0f, 0.0f}, // yellow
- {0.0f, 1.0f, 0.0f}, // green
- {0.0f, 1.0f, 1.0f}, // cyan
- {0.0f, 0.0f, 1.0f}, // blue
- {1.0f, 0.0f, 1.0f} // magenta
- };
- void drawFrustum()
- {
- glPushMatrix();
- glTranslatef(0.0f, 0.0f, -5.0f);
- glBegin(GL_QUADS);
- // Draw bottom face in red
- glColor3fv(colors[0]);
- glVertex3fv(bottomVertices[0]);
- glVertex3fv(bottomVertices[1]);
- glVertex3fv(bottomVertices[3]);
- glVertex3fv(bottomVertices[2]);
- // Draw top face in yellow
- glColor3fv(colors[1]);
- glVertex3fv(topVertices[0]);
- glVertex3fv(topVertices[1]);
- glVertex3fv(topVertices[3]);
- glVertex3fv(topVertices[2]);
- // Draw left face in green
- glColor3fv(colors[2]);
- glVertex3fv(bottomVertices[0]);
- glVertex3fv(bottomVertices[1]);
- glVertex3fv(topVertices[1]);
- glVertex3fv(topVertices[0]);
- // Draw right face in cyan
- glColor3fv(colors[3]);
- glVertex3fv(bottomVertices[2]);
- glVertex3fv(bottomVertices[3]);
- glVertex3fv(topVertices[3]);
- glVertex3fv(topVertices[2]);
- // Draw back face in blue
- glColor3fv(colors[4]);
- glVertex3fv(bottomVertices[0]);
- glVertex3fv(bottomVertices[2]);
- glVertex3fv(topVertices[2]);
- glVertex3fv(topVertices[0]);
- // Draw front face in magenta
- glColor3fv(colors[5]);
- glVertex3fv(topVertices[3]);
- glVertex3fv(bottomVertices[3]);
- glVertex3fv(bottomVertices[1]);
- glVertex3fv(topVertices[1]);
- glEnd();
- glPopMatrix();
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- drawFrustum();
- glutSwapBuffers();
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0);
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(500, 500);
- glutCreateWindow("Frustum");
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- return 0;
- }