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

  1. // OpenGL Tutorial
  2. // Primitives.c
  3.  
  4. /*************************************************************************
  5. This example illustrates the different results of each of the primitive
  6. types applied to the same set of verticies.  Notice that the order in
  7. which the verticies are declared is very important.  Also notice that
  8. some primitives, when given an incorrect number of verticies, will
  9. ignore any extra verticies.  For example, GL_TRIANGLES only draws the
  10. triangle corresponding to verticies 1, 2, and 3.  Verticies 4 and 5 are
  11. ignored.
  12. *************************************************************************/
  13.  
  14. // gcc -o Primitives  Primitives.c -lX11 -lMesaGL -lMesaGLU -lMesatk -lm
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <gltk.h>
  20.  
  21. void expose(int width, int height) {
  22.  
  23.     // Clear the window
  24.     glClear(GL_COLOR_BUFFER_BIT);
  25. }
  26.  
  27. void reshape(int width, int height) {
  28.  
  29.     // Set the new viewport size
  30.     glViewport(0, 0, (GLint)width, (GLint)height);
  31.  
  32.     // Choose the projection matrix to be the matrix 
  33.     // manipulated by the following calls
  34.     glMatrixMode(GL_PROJECTION);
  35.  
  36.     // Set the projection matrix to be the identity matrix
  37.     glLoadIdentity();
  38.  
  39.     // Define the dimensions of the Orthographic Viewing Volume
  40.     glOrtho(-8.0, 8.0, -8.0, 8.0, -8.0, 8.0);
  41.  
  42.     // Choose the modelview matrix to be the matrix
  43.     // manipulated by further calls
  44.     glMatrixMode(GL_MODELVIEW);
  45.  
  46.     // Clear the window
  47.     glClear(GL_COLOR_BUFFER_BIT);
  48. }
  49.  
  50. GLenum key_down(int key, GLenum state) {
  51.  
  52.     if ((key == TK_ESCAPE) || (key == TK_q) || (key == TK_Q))
  53.         tkQuit();
  54. }
  55.  
  56. void draw(void) {
  57.  
  58.     // Set the drawing color
  59.     glColor3f(1.0, 1.0, 1.0);
  60.  
  61.     // Set the modelview matrix to be the identity matrix
  62.     glLoadIdentity();
  63.     // Translate the object
  64.     glTranslatef(-5.0, 5.0, 0.0);
  65.  
  66.     // Draw the verticies connected according to the GL_LINES primitive
  67.     glBegin(GL_LINES);
  68.  
  69.         // Vertex 1
  70.         glVertex2f(-1.0, 1.0);
  71.  
  72.         // Vertex 2
  73.         glVertex2f(2.0, 2.0);
  74.  
  75.         // Vertex 3
  76.         glVertex2f(0.0, 0.0);
  77.  
  78.         // Vertex 4
  79.         glVertex2f(1.0, -1.0);
  80.  
  81.         // Vertex 5
  82.         glVertex2f(-2.0, -2.0);
  83.  
  84.     glEnd();
  85.  
  86.     glLoadIdentity();
  87.     glTranslatef(0.0, 5.0, 0.0);
  88.  
  89.     glBegin(GL_LINE_STRIP);
  90.         glVertex2f(-1.0, 1.0);
  91.         glVertex2f(2.0, 2.0);
  92.         glVertex2f(0.0, 0.0);
  93.         glVertex2f(1.0, -1.0);
  94.         glVertex2f(-2.0, -2.0);
  95.     glEnd();
  96.  
  97.     glLoadIdentity();
  98.     glTranslatef(5.0, 5.0, 0.0);
  99.  
  100.     glBegin(GL_LINE_LOOP);
  101.         glVertex2f(-1.0, 1.0);
  102.         glVertex2f(2.0, 2.0);
  103.         glVertex2f(0.0, 0.0);
  104.         glVertex2f(1.0, -1.0);
  105.         glVertex2f(-2.0, -2.0);
  106.     glEnd();
  107.  
  108.     glLoadIdentity();
  109.     glTranslatef(-5.0, 0.0, 0.0);
  110.  
  111.     glBegin(GL_POLYGON);
  112.         glVertex2f(-1.0, 1.0);
  113.         glVertex2f(2.0, 2.0);
  114.         glVertex2f(0.0, 0.0);
  115.         glVertex2f(1.0, -1.0);
  116.         glVertex2f(-2.0, -2.0);
  117.     glEnd();
  118.  
  119.     glLoadIdentity();
  120.     glTranslatef(0.0, 0.0, 0.0);
  121.  
  122.     glBegin(GL_QUADS);
  123.         glVertex2f(-1.0, 1.0);
  124.         glVertex2f(2.0, 2.0);
  125.         glVertex2f(0.0, 0.0);
  126.         glVertex2f(1.0, -1.0);
  127.         glVertex2f(-2.0, -2.0);
  128.     glEnd();
  129.  
  130.     glLoadIdentity();
  131.     glTranslatef(5.0, 0.0, 0.0);
  132.  
  133.     glBegin(GL_QUAD_STRIP);
  134.         glVertex2f(-1.0, 1.0);
  135.         glVertex2f(2.0, 2.0);
  136.         glVertex2f(0.0, 0.0);
  137.         glVertex2f(1.0, -1.0);
  138.         glVertex2f(-2.0, -2.0);
  139.     glEnd();
  140.  
  141.     glLoadIdentity();
  142.     glTranslatef(-5.0, -5.0, 0.0);
  143.  
  144.     glBegin(GL_TRIANGLES);
  145.         glVertex2f(-1.0, 1.0);
  146.         glVertex2f(2.0, 2.0);
  147.         glVertex2f(0.0, 0.0);
  148.         glVertex2f(1.0, -1.0);
  149.         glVertex2f(-2.0, -2.0);
  150.     glEnd();
  151.  
  152.     glLoadIdentity();
  153.     glTranslatef(0.0, -5.0, 0.0);
  154.  
  155.     glBegin(GL_TRIANGLE_STRIP);
  156.         glVertex2f(-1.0, 1.0);
  157.         glVertex2f(2.0, 2.0);
  158.         glVertex2f(0.0, 0.0);
  159.         glVertex2f(1.0, -1.0);
  160.         glVertex2f(-2.0, -2.0);
  161.     glEnd();
  162.  
  163.     glLoadIdentity();
  164.     glTranslatef(5.0, -5.0, 0.0);
  165.  
  166.     glBegin(GL_TRIANGLE_FAN);
  167.         glVertex2f(-1.0, 1.0);
  168.         glVertex2f(2.0, 2.0);
  169.         glVertex2f(0.0, 0.0);
  170.         glVertex2f(1.0, -1.0);
  171.         glVertex2f(-2.0, -2.0);
  172.     glEnd();
  173.  
  174.     // Flush the buffer to force drawing of all objects thus far
  175.     glFlush();
  176. }
  177.  
  178. void main(int argc, char **argv) {
  179.  
  180.     // Set top left corner of window to be at location (0, 0)
  181.     // Set the window size to be 500x500 pixels
  182.     tkInitPosition(0, 0, 500, 500);
  183.  
  184.     // Open a window, name it "Primitives"
  185.     if (tkInitWindow("Primitives") == GL_FALSE) {
  186.         tkQuit();
  187.     }
  188.  
  189.     // Set the clear color to black
  190.     glClearColor(0.0, 0.0, 0.0, 0.0);
  191.  
  192.     // Assign expose() to be the function called whenever
  193.     // an expose event occurs
  194.     tkExposeFunc(expose);
  195.  
  196.     // Assign reshape() to be the function called whenever 
  197.     // a reshape event occurs
  198.     tkReshapeFunc(reshape);
  199.  
  200.     // Assign key_down() to be the function called whenever
  201.     // a key is pressed
  202.     tkKeyDownFunc(key_down);
  203.  
  204.     // Assign draw() to be the function called whenever a display
  205.     // event occurs, generally after a resize or expose event
  206.     tkDisplayFunc(draw);
  207.  
  208.     // Pass program control to tk's event handling code
  209.     // In other words, loop forever
  210.     tkExec();
  211. }
  212.