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

  1. // OpenGL Tutorial
  2. // Points_Lines.c
  3.  
  4. /*************************************************************************
  5. This example illustrates the commands covered in the tutorial section
  6. Changing the State.
  7. *************************************************************************/
  8.  
  9. // gcc -o Points_Lines  Points_Lines.c -lX11 -lMesaGL -lMesaGLU -lMesatk -lm
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <gltk.h>
  15.  
  16. void expose(int width, int height) {
  17.  
  18.     // Clear the window
  19.     glClear(GL_COLOR_BUFFER_BIT);
  20. }
  21.  
  22. void reshape(int width, int height) {
  23.  
  24.     // Set the new viewport size
  25.     glViewport(0, 0, (GLint)width, (GLint)height);
  26.  
  27.     // Choose the projection matrix to be the matrix 
  28.     // manipulated by the following calls
  29.     glMatrixMode(GL_PROJECTION);
  30.  
  31.     // Set the projection matrix to be the identity matrix
  32.     glLoadIdentity();
  33.  
  34.     // Define the dimensions of the Orthographic Viewing Volume
  35.     glOrtho(-6.0, 6.0, -8.0, 8.0, -8.0, 8.0);
  36.  
  37.     // Choose the modelview matrix to be the matrix
  38.     // manipulated by further calls
  39.     glMatrixMode(GL_MODELVIEW);
  40.  
  41.     // Clear the window
  42.     glClear(GL_COLOR_BUFFER_BIT);
  43. }
  44.  
  45. GLenum key_down(int key, GLenum state) {
  46.  
  47.     if ((key == TK_ESCAPE) || (key == TK_q) || (key == TK_Q))
  48.         tkQuit();
  49. }
  50.  
  51. void draw(void) {
  52.  
  53.     // Set the modelview matrix to be the identity matrix
  54.     glLoadIdentity();
  55.     // Translate the object
  56.     glTranslatef(-2.5, 5.0, 0.0);
  57.  
  58.     // Set the color
  59.     glColor3f(1.0, 1.0, 0.0);
  60.     // Draw aliased points
  61.     glDisable(GL_POINT_SMOOTH);
  62.     // Set the point size
  63.     glPointSize(10.0);
  64.  
  65.     glBegin(GL_POINTS);
  66.         glVertex2f(-1.0, 1.0);
  67.         glVertex2f(2.0, 2.0);
  68.         glVertex2f(0.0, 0.0);
  69.         glVertex2f(1.0, -1.0);
  70.         glVertex2f(-2.0, -2.0);
  71.     glEnd();
  72.  
  73.     glLoadIdentity();
  74.     glTranslatef(2.5, 5.0, 0.0);
  75.  
  76.     glColor3f(1.0, 0.75, 0.0);
  77.     // Draw filtered points
  78.     glEnable(GL_POINT_SMOOTH);
  79.  
  80.     glBegin(GL_POINTS);
  81.         glVertex2f(-1.0, 1.0);
  82.         glVertex2f(2.0, 2.0);
  83.         glVertex2f(0.0, 0.0);
  84.         glVertex2f(1.0, -1.0);
  85.         glVertex2f(-2.0, -2.0);
  86.     glEnd();
  87.  
  88.     glLoadIdentity();
  89.     glTranslatef(-2.5, 0.0, 0.0);
  90.  
  91.     glColor3f(1.0, 1.0, 0.0);
  92.     // Draw aliased lines
  93.     glDisable(GL_LINE_SMOOTH);
  94.     // Disable line stippling
  95.     glDisable(GL_LINE_STIPPLE);
  96.     // Set the line width
  97.     glLineWidth(10.0);
  98.  
  99.     glBegin(GL_LINE_STRIP);
  100.         glVertex2f(-1.0, 1.0);
  101.         glVertex2f(2.0, 2.0);
  102.         glVertex2f(0.0, 0.0);
  103.         glVertex2f(1.0, -1.0);
  104.         glVertex2f(-2.0, -2.0);
  105.     glEnd();
  106.  
  107.     glLoadIdentity();
  108.     glTranslatef(2.5, 0.0, 0.0);
  109.  
  110.     glColor3f(1.0, 0.75, 0.0);
  111.     // Draw filtered lines
  112.     glEnable(GL_LINE_SMOOTH);
  113.  
  114.     glBegin(GL_LINE_STRIP);
  115.         glVertex2f(-1.0, 1.0);
  116.         glVertex2f(2.0, 2.0);
  117.         glVertex2f(0.0, 0.0);
  118.         glVertex2f(1.0, -1.0);
  119.         glVertex2f(-2.0, -2.0);
  120.     glEnd();
  121.  
  122.     glLoadIdentity();
  123.     glTranslatef(-2.5, -5.0, 0.0);
  124.  
  125.     glColor3f(0.0, 1.0, 0.0);
  126.     // Set the line width
  127.     glLineWidth(1.0);
  128.     // Enable line stippling
  129.     glEnable(GL_LINE_STIPPLE);
  130.     // Set the stippling pattern
  131.     glLineStipple(3, 0xAAAA);
  132.  
  133.     glBegin(GL_LINE_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(2.5, -5.0, 0.0);
  143.  
  144.     // Set the stippling pattern
  145.     glLineStipple(2, 0x0C0F);
  146.  
  147.     glBegin(GL_LINE_STRIP);
  148.         glVertex2f(-1.0, 1.0);
  149.         glVertex2f(2.0, 2.0);
  150.         glVertex2f(0.0, 0.0);
  151.         glVertex2f(1.0, -1.0);
  152.         glVertex2f(-2.0, -2.0);
  153.     glEnd();
  154.  
  155.     // Flush the buffer to force drawing of all objects thus far
  156.     glFlush();
  157. }
  158.  
  159. void main(int argc, char **argv) {
  160.  
  161.     // Set top left corner of window to be at location (0, 0)
  162.     // Set the window size to be 500x500 pixels
  163.     tkInitPosition(0, 0, 300, 400);
  164.  
  165.     // Open a window, name it "Points_Lines"
  166.     if (tkInitWindow("Points_Lines") == GL_FALSE) {
  167.         tkQuit();
  168.     }
  169.  
  170.     // Set the clear color to black
  171.     glClearColor(0.0, 0.0, 0.0, 0.0);
  172.  
  173.     // Set the shading model
  174.     glShadeModel(GL_FLAT);
  175.  
  176.     // Assign expose() to be the function called whenever
  177.     // an expose event occurs
  178.     tkExposeFunc(expose);
  179.  
  180.     // Assign reshape() to be the function called whenever 
  181.     // a reshape event occurs
  182.     tkReshapeFunc(reshape);
  183.  
  184.     // Assign key_down() to be the function called whenever
  185.     // a key is pressed
  186.     tkKeyDownFunc(key_down);
  187.  
  188.     // Assign draw() to be the function called whenever a display
  189.     // event occurs, generally after a resize or expose event
  190.     tkDisplayFunc(draw);
  191.  
  192.     // Pass program control to tk's event handling code
  193.     // In other words, loop forever
  194.     tkExec();
  195. }
  196.