Facebook
From Tiny Eider, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 121
  1. #include <GL/glut.h>
  2. #include <stdlib.h>
  3.  
  4. GLfloat bottomVertices[][3] = {
  5.     {-0.5f, -0.5f, -0.5f},
  6.     {-0.5f, 0.5f, -0.5f},
  7.     {0.5f, -0.5f, -0.5f},
  8.     {0.5f, 0.5f, -0.5f}
  9. };
  10.  
  11. GLfloat topVertices[][3] = {
  12.     {-0.25f, -0.25f, 0.5f},
  13.     {-0.25f, 0.25f, 0.5f},
  14.     {0.25f, -0.25f, 0.5f},
  15.     {0.25f, 0.25f, 0.5f}
  16. };
  17.  
  18. GLfloat colors[][3] = {
  19.     {1.0f, 0.0f, 0.0f}, // red
  20.     {1.0f, 1.0f, 0.0f}, // yellow
  21.     {0.0f, 1.0f, 0.0f}, // green
  22.     {0.0f, 1.0f, 1.0f}, // cyan
  23.     {0.0f, 0.0f, 1.0f}, // blue
  24.     {1.0f, 0.0f, 1.0f}  // magenta
  25. };
  26.  
  27. void drawFrustum()
  28. {
  29.     glPushMatrix();
  30.     glTranslatef(0.0f, 0.0f, -5.0f);
  31.  
  32.     glBegin(GL_QUADS);
  33.  
  34.     // Draw bottom face in red
  35.     glColor3fv(colors[0]);
  36.     glVertex3fv(bottomVertices[0]);
  37.     glVertex3fv(bottomVertices[1]);
  38.     glVertex3fv(bottomVertices[3]);
  39.     glVertex3fv(bottomVertices[2]);
  40.  
  41.     // Draw top face in yellow
  42.     glColor3fv(colors[1]);
  43.     glVertex3fv(topVertices[0]);
  44.     glVertex3fv(topVertices[1]);
  45.     glVertex3fv(topVertices[3]);
  46.     glVertex3fv(topVertices[2]);
  47.  
  48.     // Draw left face in green
  49.     glColor3fv(colors[2]);
  50.     glVertex3fv(bottomVertices[0]);
  51.     glVertex3fv(bottomVertices[1]);
  52.     glVertex3fv(topVertices[1]);
  53.     glVertex3fv(topVertices[0]);
  54.  
  55.     // Draw right face in cyan
  56.     glColor3fv(colors[3]);
  57.     glVertex3fv(bottomVertices[2]);
  58.     glVertex3fv(bottomVertices[3]);
  59.     glVertex3fv(topVertices[3]);
  60.     glVertex3fv(topVertices[2]);
  61.  
  62.     // Draw back face in blue
  63.     glColor3fv(colors[4]);
  64.     glVertex3fv(bottomVertices[0]);
  65.     glVertex3fv(bottomVertices[2]);
  66.     glVertex3fv(topVertices[2]);
  67.     glVertex3fv(topVertices[0]);
  68.  
  69.     // Draw front face in magenta
  70.     glColor3fv(colors[5]);
  71.     glVertex3fv(topVertices[3]);
  72.     glVertex3fv(bottomVertices[3]);
  73.     glVertex3fv(bottomVertices[1]);
  74.     glVertex3fv(topVertices[1]);
  75.  
  76.     glEnd();
  77.  
  78.     glPopMatrix();
  79. }
  80.  
  81. void display()
  82. {
  83.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  84.  
  85. glMatrixMode(GL_MODELVIEW);
  86. glLoadIdentity();
  87. drawFrustum();
  88. glutSwapBuffers();
  89. }
  90.  
  91. void reshape(int w, int h)
  92. {
  93. glViewport(0, 0, w, h);
  94. glMatrixMode(GL_PROJECTION);
  95. glLoadIdentity();
  96. gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0);
  97. }
  98.  
  99. int main(int argc, char **argv)
  100. {
  101. glutInit(&argc, argv);
  102. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  103. glutInitWindowSize(500, 500);
  104. glutCreateWindow("Frustum");
  105. glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  106.  
  107. glutDisplayFunc(display);
  108. glutReshapeFunc(reshape);
  109.  
  110. glutMainLoop();
  111. return 0;
  112. }
  113.