home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 March / Chip_2000-03_cd.bin / zkuste / linux / opengl / Terry / OpenGL / Examples / Swing.c < prev   
Encoding:
C/C++ Source or Header  |  1997-02-03  |  5.2 KB  |  246 lines

  1. // OpenGL Tutorial
  2. // Swing.c
  3.  
  4. /*************************************************************************
  5. This program demonstrates animation.
  6. *************************************************************************/
  7.  
  8. // gcc -o Swing  Swing.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. #define SWING 1
  16. #define BAR 2
  17.  
  18. GLfloat     angle;        // angle of swing
  19. int        direction;    // direction of swing
  20.  
  21. void reshape(int width, int height) {
  22.  
  23.     // Set the new viewport size
  24.     glViewport(0, 0, (GLint)width, (GLint)height);
  25.  
  26.     // Choose the projection matrix to be the matrix 
  27.     // manipulated by the following calls
  28.     glMatrixMode(GL_PROJECTION);
  29.  
  30.     // Set the projection matrix to be the identity matrix
  31.     glLoadIdentity();
  32.  
  33.     // Define the dimensions of the Orthographic Viewing Volume
  34.     glOrtho(-50.0, 50.0, -50.0, 50.0, -150.0, 150.0);
  35.  
  36.     // Choose the modelview matrix to be the matrix
  37.     // manipulated by further calls
  38.     glMatrixMode(GL_MODELVIEW);
  39. }
  40.  
  41. GLenum key_down(int key, GLenum state) {
  42.  
  43.     if ((key == TK_ESCAPE) || (key == TK_q) || (key == TK_Q))
  44.         tkQuit();
  45. }
  46.  
  47. void draw(void) {
  48.  
  49.     // Clear the RGB buffer and the depth buffer
  50.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  51.  
  52.     // Set the modelview matrix to be the identity matrix
  53.     glLoadIdentity();
  54.  
  55.     // Rotate the bar and swing
  56.     glRotatef(60.0, 0.0, 1.0, 0.0);
  57.  
  58.     // Draw the bar
  59.     glCallList(BAR);
  60.  
  61.     // Translate and rotate the swing to make it `swing'
  62.     glTranslatef(0.0, 30.0, 0.0);
  63.     glRotatef(angle, 1.0, 0.0, 0.0);
  64.     glTranslatef(0.0, -30.0, 0.0);
  65.  
  66.     // Draw the swing
  67.     glCallList(SWING);
  68.  
  69.     // Flush the buffer to force drawing of all objects thus far
  70.     glFlush();
  71.  
  72.     // Swap the buffers
  73.     tkSwapBuffers();
  74.  
  75.     // Make the it swing back or forth depending upon direction
  76.     if (direction == 1)
  77.         angle++;
  78.     else
  79.         angle--;
  80.  
  81.     // Change the direction if 40 or -40 is reached
  82.     if (angle == -40.0) {
  83.         direction = 1;
  84.     }
  85.     if (angle == 40.0) {
  86.         direction = 0;
  87.     }
  88. }
  89.  
  90. void make_bar() {
  91.  
  92.     glNewList(BAR, GL_COMPILE);
  93.  
  94.         // Draw the pole
  95.         glBegin(GL_QUADS);
  96.  
  97.             glColor3f(1.0, 1.0, 0.0);
  98.  
  99.             // Bottom
  100.             glVertex3d(-50, 30, -1);
  101.             glVertex3d(50, 30, -1);
  102.             glVertex3d(50, 30, 1);
  103.             glVertex3d(-50, 30, 1);
  104.             // Top
  105.             glVertex3d(-50, 33, -1);
  106.             glVertex3d(50, 33, -1);
  107.             glVertex3d(50, 33, 1);
  108.             glVertex3d(-50, 33, 1);
  109.  
  110.             glColor3f(1.0, 0.65, 0.0);
  111.  
  112.             // Front
  113.             glVertex3d(-50, 33, 1);
  114.             glVertex3d(-50, 30, 1);
  115.             glVertex3d(50, 30, 1);
  116.             glVertex3d(50, 33, 1);
  117.             // Back
  118.             glVertex3d(50, 33, -1);
  119.             glVertex3d(50, 30, -1);
  120.             glVertex3d(-50, 30, -1);
  121.             glVertex3d(-50, 33, -1);
  122.  
  123.             glColor3f(1.0, 0.0, 0.0);
  124.  
  125.             // Left
  126.             glVertex3d(-50, 33, -1);
  127.             glVertex3d(-50, 30, -1);
  128.             glVertex3d(-50, 30, 1);
  129.             glVertex3d(-50, 33, 1);
  130.             // Right
  131.             glVertex3d(50, 33, 1);
  132.             glVertex3d(50, 30, 1);
  133.             glVertex3d(50, 30, -1);
  134.             glVertex3d(50, 33, -1);
  135.         glEnd();
  136.  
  137.     glEndList();
  138. }
  139.  
  140. void make_swing() {
  141.  
  142.     glNewList(SWING, GL_COMPILE);
  143.  
  144.         glColor3f(1.0, 0.0, 1.0);
  145.         glLineWidth(5.0);
  146.     
  147.         // Draw the ropes
  148.         glBegin(GL_LINES);
  149.             glVertex3d(-20, 30, 0);
  150.             glVertex3d(-20, -30, 0);
  151.             glVertex3d(20, 30, 0);
  152.             glVertex3d(20, -30, 0);
  153.         glEnd();
  154.  
  155.         glColor3f(0.0, 1.0, 0.0);
  156.     
  157.         // Draw the sides of the seat
  158.         glBegin(GL_QUAD_STRIP);
  159.             glVertex3d(20, -30, -10);
  160.             glVertex3d(20, -33, -10);
  161.             glVertex3d(-20, -30, -10);
  162.             glVertex3d(-20, -33, -10);
  163.             glVertex3d(-20, -30, 10);
  164.             glVertex3d(-20, -33, 10);
  165.             glVertex3d(20, -30, 10);
  166.             glVertex3d(20, -33, 10);
  167.             glVertex3d(20, -30, -10);
  168.             glVertex3d(20, -33, -10);
  169.         glEnd();
  170.     
  171.         glColor3f(0.0, 0.0, 1.0);
  172.     
  173.         // Draw the top and bottom of the seat
  174.         glBegin(GL_QUADS);
  175.             glVertex3d(-20, -33, -10);
  176.             glVertex3d(20, -33, -10);
  177.             glVertex3d(20, -33, 10);
  178.             glVertex3d(-20, -33, 10);
  179.     
  180.             glVertex3d(-20, -30, -10);
  181.             glVertex3d(20, -30, -10);
  182.             glVertex3d(20, -30, 10);
  183.             glVertex3d(-20, -30, 10);
  184.         glEnd();
  185.     
  186.     glEndList();
  187. }
  188.  
  189. void main(int argc, char **argv) {
  190.  
  191.     // Set top left corner of window to be at location (0, 0)
  192.     // Set the window size to be 500x500 pixels
  193.     tkInitPosition(0, 0, 500, 500);
  194.  
  195.     // Initialize the RGB and Depth buffers
  196.     tkInitDisplayMode(TK_RGB | TK_DEPTH | TK_DOUBLE);
  197.  
  198.     // Open a window, name it "Swing"
  199.     if (tkInitWindow("Swing") == GL_FALSE) {
  200.         tkQuit();
  201.     }
  202.  
  203.     // Set the clear color to black
  204.     glClearColor(0.0, 0.0, 0.0, 0.0);
  205.  
  206.     // Set the shading model
  207.     glShadeModel(GL_FLAT);
  208.  
  209.     // Set the polygon mode to fill
  210.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  211.  
  212.     // Enable depth testing for hidden line removal
  213.     glEnable(GL_DEPTH_TEST);
  214.  
  215.     // Create the display lists
  216.     make_bar();
  217.     make_swing();
  218.  
  219.     angle = -39.0;
  220.     direction = 1;
  221.  
  222.     // Assign reshape() to be the function called whenever
  223.     // an expose event occurs
  224.     tkExposeFunc(reshape);
  225.  
  226.     // Assign reshape() to be the function called whenever 
  227.     // a reshape event occurs
  228.     tkReshapeFunc(reshape);
  229.  
  230.     // Assign key_down() to be the function called whenever
  231.     // a key is pressed
  232.     tkKeyDownFunc(key_down);
  233.  
  234.     // Assign draw() to be the function called whenever a display
  235.     // event occurs, generally after a resize or expose event
  236.     tkDisplayFunc(draw);
  237.  
  238.     // Assign idle() to be the function called whenever there 
  239.     // are no events
  240.     tkIdleFunc(draw);
  241.  
  242.     // Pass program control to tk's event handling code
  243.     // In other words, loop forever
  244.     tkExec();
  245. }
  246.