Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.5 KiB

  1. /*
  2. * "drawmode.c" - A test program demonstrating the use of glDepthFunc()
  3. * and glDrawMode().
  4. *
  5. * Press the 'd' key to toggle between GL_LESS and GL_GREATER depth
  6. * tests. Press the 'ESC' key to quit.
  7. */
  8. #include <GL/glaux.h>
  9. /*
  10. * These #define constants are provided for compatibility between MS Windows
  11. * and the rest of the world.
  12. *
  13. * CALLBACK and APIENTRY are function modifiers under MS Windows.
  14. */
  15. #ifndef WIN32
  16. # define CALLBACK
  17. # define APIENTRY
  18. #endif /* !WIN32 */
  19. GLenum depth_function = GL_LESS; /* Current depth function */
  20. /*
  21. * 'reshape_scene()' - Change the size of the scene...
  22. */
  23. void CALLBACK
  24. reshape_scene(GLsizei width, /* I - Width of the window in pixels */
  25. GLsizei height) /* I - Height of the window in pixels */
  26. {
  27. /*
  28. * Reset the current viewport and perspective transformation...
  29. */
  30. glViewport(0, 0, width, height);
  31. glMatrixMode(GL_PROJECTION);
  32. glLoadIdentity();
  33. gluPerspective(22.5, (float)width / (float)height, 0.1, 1000.0);
  34. glMatrixMode(GL_MODELVIEW);
  35. }
  36. /*
  37. * 'draw_scene()' - Draw a scene containing a cube with a sphere in front of
  38. * it.
  39. */
  40. void CALLBACK
  41. draw_scene(void)
  42. {
  43. static float red_light[4] = { 1.0, 0.0, 0.0, 1.0 };
  44. static float red_pos[4] = { 1.0, 1.0, 1.0, 0.0 };
  45. static float blue_light[4] = { 0.0, 0.0, 1.0, 1.0 };
  46. static float blue_pos[4] = { -1.0, -1.0, -1.0, 0.0 };
  47. /*
  48. * Enable drawing features that we need...
  49. */
  50. glEnable(GL_DEPTH_TEST);
  51. glEnable(GL_LIGHTING);
  52. glEnable(GL_LIGHT0);
  53. glEnable(GL_LIGHT1);
  54. glDepthFunc(depth_function);
  55. /*
  56. * Clear the color and depth buffers...
  57. */
  58. if (depth_function == GL_LESS)
  59. glClearDepth(1.0);
  60. else
  61. glClearDepth(0.0);
  62. glClearColor(0.0, 0.0, 0.0, 0.0);
  63. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  64. /*
  65. * Draw the cube and sphere in different colors...
  66. *
  67. * We have positioned two lights in this scene. The first is red and
  68. * located above, to the right, and behind the viewer. The second is blue
  69. * and located below, to the left, and in front of the viewer.
  70. */
  71. glLightfv(GL_LIGHT0, GL_DIFFUSE, red_light);
  72. glLightfv(GL_LIGHT0, GL_POSITION, red_pos);
  73. glLightfv(GL_LIGHT1, GL_DIFFUSE, blue_light);
  74. glLightfv(GL_LIGHT1, GL_POSITION, blue_pos);
  75. glDrawBuffer(GL_NONE); /* Draw the cutting plane */
  76. glShadeModel(GL_FLAT);
  77. glBegin(GL_LINES);
  78. glVertex3f(-10.0, -10.0, -20.0);
  79. glVertex3f(10.0, 10.0, -20.0);
  80. glEnd();
  81. glShadeModel(GL_SMOOTH);
  82. glBegin(GL_POLYGON);
  83. glVertex3f(-10.0, -10.0, -20.0);
  84. glVertex3f(-10.0, 10.0, -20.0);
  85. glVertex3f(10.0, 10.0, -20.0);
  86. glVertex3f(10.0, -10.0, -20.0);
  87. glEnd();
  88. glDrawBuffer(GL_FRONT);
  89. glPushMatrix();
  90. glTranslatef(-1.0, 0.0, -20.0);
  91. auxSolidSphere(1.0);
  92. glPopMatrix();
  93. glPushMatrix();
  94. glTranslatef(1.0, 0.0, -20.0);
  95. glRotatef(15.0, 0.0, 1.0, 0.0);
  96. glRotatef(15.0, 0.0, 0.0, 1.0);
  97. auxSolidCube(2.0);
  98. glPopMatrix();
  99. glFlush();
  100. }
  101. /*
  102. * 'toggle_depth()' - Toggle the depth function between GL_LESS and GL_GREATER.
  103. */
  104. void CALLBACK
  105. toggle_depth(void)
  106. {
  107. if (depth_function == GL_LESS)
  108. depth_function = GL_GREATER;
  109. else
  110. depth_function = GL_LESS;
  111. }
  112. /*
  113. * 'main()' - Initialize the window and display the scene until the user presses
  114. * the ESCape key.
  115. */
  116. void
  117. main(void)
  118. {
  119. auxInitDisplayMode(AUX_RGB | AUX_SINGLE | AUX_DEPTH16);
  120. auxInitWindow("Depth Function");
  121. auxKeyFunc(AUX_d, toggle_depth);
  122. auxReshapeFunc(reshape_scene);
  123. auxMainLoop(draw_scene);
  124. }
  125. /*
  126. * End of "depth.c".
  127. */