home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 March / Chip_2000-03_cd.bin / zkuste / linux / opengl / Terry / OpenGL / Examples / Polygons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-24  |  3.8 KB  |  162 lines

  1. // OpenGL Tutorial
  2. // Polygons.c
  3.  
  4. /*************************************************************************
  5. This program illustrates the use of the depth buffer and depth testing.
  6. *************************************************************************/
  7.  
  8. // gcc -o Polygons  Polygons.c -lX11 -lMesaGL -lMesaGLU -lMesatk -lm
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include <gltk.h>
  14.  
  15. void reshape(int width, int height) {
  16.  
  17.     // Set the new viewport size
  18.     glViewport(0, 0, (GLint)width, (GLint)height);
  19.  
  20.     // Choose the projection matrix to be the matrix 
  21.     // manipulated by the following calls
  22.     glMatrixMode(GL_PROJECTION);
  23.  
  24.     // Set the projection matrix to be the identity matrix
  25.     glLoadIdentity();
  26.  
  27.     // Define the dimensions of the Orthographic Viewing Volume
  28.     glOrtho(-8.0, 8.0, -8.0, 8.0, -8.0, 8.0);
  29.  
  30.     // Choose the modelview matrix to be the matrix
  31.     // manipulated by further calls
  32.     glMatrixMode(GL_MODELVIEW);
  33. }
  34.  
  35. GLenum key_down(int key, GLenum state) {
  36.  
  37.     if ((key == TK_ESCAPE) || (key == TK_q) || (key == TK_Q))
  38.         tkQuit();
  39. }
  40.  
  41. void draw(void) {
  42.  
  43.     // Clear the RGB buffer and the depth buffer
  44.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  45.  
  46.     // Set the modelview matrix to be the identity matrix
  47.     glLoadIdentity();
  48.     // Translate and rotate the object
  49.     glTranslatef(-2.5, 0.0, 0.0);
  50.     glRotatef(-30, 1.0, 0.0, 0.0);
  51.     glRotatef(30, 0.0, 1.0, 0.0);
  52.     glRotatef(30, 0.0, 0.0, 1.0);
  53.  
  54.     glColor3f(1.0, 0.0, 1.0);
  55.  
  56.     // Draw the sides of the three-sided pyramid
  57.     glBegin(GL_TRIANGLE_FAN);
  58.         glVertex3d(0, 4, 0);
  59.         glVertex3d(0, -4, -4);
  60.         glVertex3d(-4, -4, 4);
  61.         glVertex3d(4, -4, 4);
  62.         glVertex3d(0, -4, -4);
  63.     glEnd();
  64.  
  65.     glColor3f(0.0, 1.0, 1.0);
  66.  
  67.     // Draw the base of the pyramid
  68.     glBegin(GL_TRIANGLES);
  69.         glVertex3d(0, -4, -4);
  70.         glVertex3d(-4, -4, 4);
  71.         glVertex3d(4, -4, 4);
  72.     glEnd();
  73.  
  74.     glLoadIdentity();
  75.     glTranslatef(2.5, 0.0, 0.0);
  76.     glRotatef(45, 1.0, 0.0, 0.0);
  77.     glRotatef(45, 0.0, 1.0, 0.0);
  78.     glRotatef(45, 0.0, 0.0, 1.0);
  79.  
  80.     glColor3f(0.0, 1.0, 0.0);
  81.  
  82.     // Draw the sides of the cube
  83.     glBegin(GL_QUAD_STRIP);
  84.         glVertex3d(3, 3, -3);
  85.         glVertex3d(3, -3, -3);
  86.         glVertex3d(-3, 3, -3);
  87.         glVertex3d(-3, -3, -3);
  88.         glVertex3d(-3, 3, 3);
  89.         glVertex3d(-3, -3, 3);
  90.         glVertex3d(3, 3, 3);
  91.         glVertex3d(3, -3, 3);
  92.         glVertex3d(3, 3, -3);
  93.         glVertex3d(3, -3, -3);
  94.     glEnd();
  95.  
  96.     glColor3f(0.0, 0.0, 1.0);
  97.  
  98.     // Draw the top and bottom of the cube
  99.     glBegin(GL_QUADS);
  100.         glVertex3d(-3, -3, -3);
  101.         glVertex3d(3, -3, -3);
  102.         glVertex3d(3, -3, 3);
  103.         glVertex3d(-3, -3, 3);
  104.  
  105.         glVertex3d(-3, 3, -3);
  106.         glVertex3d(3, 3, -3);
  107.         glVertex3d(3, 3, 3);
  108.         glVertex3d(-3, 3, 3);
  109.     glEnd();
  110.  
  111.  
  112.     // Flush the buffer to force drawing of all objects thus far
  113.     glFlush();
  114. }
  115.  
  116. void main(int argc, char **argv) {
  117.  
  118.     // Set top left corner of window to be at location (0, 0)
  119.     // Set the window size to be 500x500 pixels
  120.     tkInitPosition(0, 0, 500, 500);
  121.  
  122.     // Initialize the RGB and Depth buffers
  123.     tkInitDisplayMode(TK_RGB | TK_DEPTH);
  124.  
  125.     // Open a window, name it "Polygons"
  126.     if (tkInitWindow("Polygons") == GL_FALSE) {
  127.         tkQuit();
  128.     }
  129.  
  130.     // Set the clear color to black
  131.     glClearColor(0.0, 0.0, 0.0, 0.0);
  132.  
  133.     // Set the shading model
  134.     glShadeModel(GL_FLAT);
  135.  
  136.     // Set the polygon mode to fill
  137.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  138.  
  139.     // Enable depth testing for hidden line removal
  140.     glEnable(GL_DEPTH_TEST);
  141.  
  142.     // Assign reshape() to be the function called whenever
  143.     // an expose event occurs
  144.     tkExposeFunc(reshape);
  145.  
  146.     // Assign reshape() to be the function called whenever 
  147.     // a reshape event occurs
  148.     tkReshapeFunc(reshape);
  149.  
  150.     // Assign key_down() to be the function called whenever
  151.     // a key is pressed
  152.     tkKeyDownFunc(key_down);
  153.  
  154.     // Assign draw() to be the function called whenever a display
  155.     // event occurs, generally after a resize or expose event
  156.     tkDisplayFunc(draw);
  157.  
  158.     // Pass program control to tk's event handling code
  159.     // In other words, loop forever
  160.     tkExec();
  161. }
  162.