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

  1. // OpenGL Tutorial
  2. // Light.c
  3.  
  4. /*************************************************************************
  5. This example is a modification Polygons_List.c to use lighting.  The
  6. three sided sphere was changed to a four-sided sphere to make
  7. calculating the normal vectors easier.
  8.  
  9. Notice in the source code the order in which normals are defined for
  10. GL_QUAD_STRIP.  In the GL_QUAD_STRIP section, Normal A applies to
  11. vertices 1, 2, 3, and 4; Normal B applies to vertices 3, 4, 5, and 6;
  12. Normal C applies to vertices 5, 6, 7, and 8; and Normal D applies to
  13. vertices 7, 8, 9, and 10.  Due to the way that OpenGL renders objects,
  14. the correct normal value must be set before the last vertex in the
  15. current object is specified so that the correct normal value is
  16. assigned to that object.
  17. *************************************************************************/
  18.  
  19. // gcc -o Light  Light.c -lX11 -lMesaGL -lMesaGLU -lMesatk -lm
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <gltk.h>
  25.  
  26. #define SPHERE 1
  27. #define CUBE 2
  28.  
  29. void reshape(int width, int height) {
  30.  
  31.     // Set the new viewport size
  32.     glViewport(0, 0, (GLint)width, (GLint)height);
  33.  
  34.     // Choose the projection matrix to be the matrix 
  35.     // manipulated by the following calls
  36.     glMatrixMode(GL_PROJECTION);
  37.  
  38.     // Set the projection matrix to be the identity matrix
  39.     glLoadIdentity();
  40.  
  41.     // Define the dimensions of the Orthographic Viewing Volume
  42.     glOrtho(-8.0, 8.0, -8.0, 8.0, -8.0, 8.0);
  43.  
  44.     // Choose the modelview matrix to be the matrix
  45.     // manipulated by further calls
  46.     glMatrixMode(GL_MODELVIEW);
  47. }
  48.  
  49. GLenum key_down(int key, GLenum state) {
  50.  
  51.     if ((key == TK_ESCAPE) || (key == TK_q) || (key == TK_Q))
  52.         tkQuit();
  53. }
  54.  
  55. void draw(void) {
  56.  
  57.     // Clear the RGB buffer and the depth buffer
  58.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  59.  
  60.     // Set the modelview matrix to be the identity matrix
  61.     glLoadIdentity();
  62.     // Translate and rotate the object
  63.     glTranslatef(-2.5, 0.0, 0.0);
  64.  
  65.     // Draw sphere
  66.     glCallList(SPHERE);
  67.  
  68.     glLoadIdentity();
  69.     glTranslatef(2.5, 0.0, 0.0);
  70.     glRotatef(45, 1.0, 0.0, 0.0);
  71.     glRotatef(45, 0.0, 1.0, 0.0);
  72.     glRotatef(45, 0.0, 0.0, 1.0);
  73.  
  74.     // Draw cube
  75.     glCallList(CUBE);
  76.  
  77.     // Flush the buffer to force drawing of all objects thus far
  78.     glFlush();
  79. }
  80.  
  81. void make_sphere() {
  82.  
  83.     GLint slices, stacks;
  84.     GLUquadricObj *quadObj;
  85.  
  86.     quadObj = gluNewQuadric();
  87.     slices = 16;
  88.     stacks = 10;
  89.  
  90.     glNewList(SPHERE, GL_COMPILE);
  91.  
  92.         glColor3f(1.0, 0.0, 1.0);
  93.         gluSphere(quadObj, 4.0, slices, stacks);
  94.     
  95.     glEndList();
  96. }
  97.  
  98.  
  99. void make_cube() {
  100.  
  101.     glNewList(CUBE, GL_COMPILE);
  102.  
  103.         glColor3f(0.0, 1.0, 0.0);
  104.     
  105.         // Draw the sides of the cube
  106.         glBegin(GL_QUAD_STRIP);
  107.  
  108.                         // Normal A
  109.             glNormal3d(0.0, 0.0, -1.0);
  110.  
  111.                         // Vertex 1
  112.             glVertex3d(3, 3, -3);
  113.                         // Vertex 2
  114.             glVertex3d(3, -3, -3);
  115.  
  116.                         // Vertex 3
  117.             glVertex3d(-3, 3, -3);
  118.                         // Vertex 4
  119.             glVertex3d(-3, -3, -3);
  120.  
  121.                         // Normal B
  122.             glNormal3d(-1.0, 0.0, 0.0);
  123.  
  124.                         // Vertex 5
  125.             glVertex3d(-3, 3, 3);
  126.                         // Vertex 6
  127.             glVertex3d(-3, -3, 3);
  128.  
  129.                         // Normal C
  130.             glNormal3d(0.0, 0.0, 1.0);
  131.  
  132.                         // Vertex 7
  133.             glVertex3d(3, 3, 3);
  134.                         // Vertex 8
  135.             glVertex3d(3, -3, 3);
  136.  
  137.                         // Normal D
  138.             glNormal3d(1.0, 0.0, 0.0);
  139.  
  140.                         // Vertex 9
  141.             glVertex3d(3, 3, -3);
  142.                         // Vertex 10
  143.             glVertex3d(3, -3, -3);
  144.         glEnd();
  145.     
  146.         glColor3f(0.0, 0.0, 1.0);
  147.     
  148.         // Draw the top and bottom of the cube
  149.         glBegin(GL_QUADS);
  150.  
  151.             glNormal3d(0.0, 1.0, 0.0);
  152.             glVertex3d(-3, -3, -3);
  153.             glVertex3d(3, -3, -3);
  154.             glVertex3d(3, -3, 3);
  155.             glVertex3d(-3, -3, 3);
  156.     
  157.             glNormal3d(0.0, 1.0, 0.0);
  158.             glVertex3d(-3, 3, -3);
  159.             glVertex3d(3, 3, -3);
  160.             glVertex3d(3, 3, 3);
  161.             glVertex3d(-3, 3, 3);
  162.         glEnd();
  163.  
  164.     glEndList();
  165. }
  166.  
  167. void main(int argc, char **argv) {
  168.  
  169.     GLfloat specular [] = { 1.0, 1.0, 1.0, 1.0 };
  170.     GLfloat shininess [] = { 100.0 };
  171.     GLfloat position [] = { 1.0, 1.0, 1.0, 0.0 };
  172.  
  173.     // Set top left corner of window to be at location (0, 0)
  174.     // Set the window size to be 500x500 pixels
  175.     tkInitPosition(0, 0, 500, 500);
  176.  
  177.     // Initialize the RGB and Depth buffers
  178.     tkInitDisplayMode(TK_RGB | TK_DEPTH);
  179.  
  180.     // Open a window, name it "Light"
  181.     if (tkInitWindow("Light") == GL_FALSE) {
  182.         tkQuit();
  183.     }
  184.  
  185.     // Set the clear color to black
  186.     glClearColor(0.0, 0.0, 0.0, 0.0);
  187.  
  188.     // Set the shading model
  189.     glShadeModel(GL_SMOOTH);
  190.  
  191.     // Set the polygon mode to fill
  192.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  193.  
  194.     // Enable depth testing for hidden line removal
  195.     glEnable(GL_DEPTH_TEST);
  196.  
  197.     // Define material properties of specular color and degree of 
  198.     // shininess.  Since this is only done once in this particular 
  199.     // example, it applies to all objects.  Material properties can 
  200.     // be set for individual objects, individual faces of the objects,
  201.     // individual vertices of the faces, etc... 
  202.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
  203.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
  204.  
  205.     // Set the GL_AMBIENT_AND_DIFFUSE color state variable to be the
  206.     // one referred to by all following calls to glColor
  207.     glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  208.     glEnable(GL_COLOR_MATERIAL);
  209.  
  210.     // Create a Directional Light Source
  211.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  212.     glEnable(GL_LIGHTING);
  213.     glEnable(GL_LIGHT0);
  214.  
  215.     // Create the display lists
  216.     make_sphere();
  217.     make_cube();
  218.  
  219.     // Assign reshape() to be the function called whenever
  220.     // an expose event occurs
  221.     tkExposeFunc(reshape);
  222.  
  223.     // Assign reshape() to be the function called whenever 
  224.     // a reshape event occurs
  225.     tkReshapeFunc(reshape);
  226.  
  227.     // Assign key_down() to be the function called whenever
  228.     // a key is pressed
  229.     tkKeyDownFunc(key_down);
  230.  
  231.     // Assign draw() to be the function called whenever a display
  232.     // event occurs, generally after a resize or expose event
  233.     tkDisplayFunc(draw);
  234.  
  235.     // Pass program control to tk's event handling code
  236.     // In other words, loop forever
  237.     tkExec();
  238. }
  239.