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.

212 lines
6.8 KiB

  1. /*
  2. * (c) Copyright 1993, Silicon Graphics, Inc.
  3. * ALL RIGHTS RESERVED
  4. * Permission to use, copy, modify, and distribute this software for
  5. * any purpose and without fee is hereby granted, provided that the above
  6. * copyright notice appear in all copies and that both the copyright notice
  7. * and this permission notice appear in supporting documentation, and that
  8. * the name of Silicon Graphics, Inc. not be used in advertising
  9. * or publicity pertaining to distribution of the software without specific,
  10. * written prior permission.
  11. *
  12. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  16. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  21. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * US Government Users Restricted Rights
  26. * Use, duplication, or disclosure by the Government is subject to
  27. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. * clause at DFARS 252.227-7013 and/or in similar or successor
  30. * clauses in the FAR or the DOD or NASA FAR Supplement.
  31. * Unpublished-- rights reserved under the copyright laws of the
  32. * United States. Contractor/manufacturer is Silicon Graphics,
  33. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  34. *
  35. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36. */
  37. /*
  38. * select.c
  39. * This is an illustration of the selection mode and
  40. * name stack, which detects whether objects which collide
  41. * with a viewing volume. First, four triangles and a
  42. * rectangular box representing a viewing volume are drawn
  43. * (drawScene routine). The green triangle and yellow
  44. * triangles appear to lie within the viewing volume, but
  45. * the red triangle appears to lie outside it. Then the
  46. * selection mode is entered (selectObjects routine).
  47. * Drawing to the screen ceases. To see if any collisions
  48. * occur, the four triangles are called. In this example,
  49. * the green triangle causes one hit with the name 1, and
  50. * the yellow triangles cause one hit with the name 3.
  51. */
  52. #include <windows.h>
  53. #include <stdio.h>
  54. #include <GL/gl.h>
  55. #include <GL/glu.h>
  56. #include "glaux.h"
  57. /* draw a triangle with vertices at (x1, y1), (x2, y2)
  58. * and (x3, y3) at z units away from the origin.
  59. */
  60. void drawTriangle (GLfloat x1, GLfloat y1, GLfloat x2,
  61. GLfloat y2, GLfloat x3, GLfloat y3, GLfloat z)
  62. {
  63. glBegin (GL_TRIANGLES);
  64. glVertex3f (x1, y1, z);
  65. glVertex3f (x2, y2, z);
  66. glVertex3f (x3, y3, z);
  67. glEnd ();
  68. }
  69. /* draw a rectangular box with these outer x, y, and z values */
  70. void drawViewVolume (GLfloat x1, GLfloat x2, GLfloat y1,
  71. GLfloat y2, GLfloat z1, GLfloat z2)
  72. {
  73. glColor3f (1.0, 1.0, 1.0);
  74. glBegin (GL_LINE_LOOP);
  75. glVertex3f (x1, y1, -z1);
  76. glVertex3f (x2, y1, -z1);
  77. glVertex3f (x2, y2, -z1);
  78. glVertex3f (x1, y2, -z1);
  79. glEnd ();
  80. glBegin (GL_LINE_LOOP);
  81. glVertex3f (x1, y1, -z2);
  82. glVertex3f (x2, y1, -z2);
  83. glVertex3f (x2, y2, -z2);
  84. glVertex3f (x1, y2, -z2);
  85. glEnd ();
  86. glBegin (GL_LINES); /* 4 lines */
  87. glVertex3f (x1, y1, -z1);
  88. glVertex3f (x1, y1, -z2);
  89. glVertex3f (x1, y2, -z1);
  90. glVertex3f (x1, y2, -z2);
  91. glVertex3f (x2, y1, -z1);
  92. glVertex3f (x2, y1, -z2);
  93. glVertex3f (x2, y2, -z1);
  94. glVertex3f (x2, y2, -z2);
  95. glEnd ();
  96. }
  97. /* drawScene() draws 4 triangles and a wire frame
  98. * which represents the viewing volume.
  99. */
  100. void drawScene (void)
  101. {
  102. glMatrixMode (GL_PROJECTION);
  103. glLoadIdentity ();
  104. gluPerspective (40.0, 4.0/3.0, 0.01, 100.0);
  105. glMatrixMode (GL_MODELVIEW);
  106. glLoadIdentity ();
  107. gluLookAt (7.5, 7.5, 12.5, 2.5, 2.5, -5.0, 0.0, 1.0, 0.0);
  108. glColor3f (0.0, 1.0, 0.0); /* green triangle */
  109. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
  110. glColor3f (1.0, 0.0, 0.0); /* red triangle */
  111. drawTriangle (2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
  112. glColor3f (1.0, 1.0, 0.0); /* yellow triangles */
  113. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0);
  114. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0);
  115. drawViewVolume (0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
  116. }
  117. /* processHits() prints out the contents of the selection array.
  118. */
  119. void processHits (GLint hits, GLuint buffer[])
  120. {
  121. unsigned int i, j;
  122. GLuint names, *ptr;
  123. printf ("hits = %d\n", hits);
  124. ptr = (GLuint *) buffer;
  125. for (i = 0; i < hits; i++) { /* for each hit */
  126. names = *ptr;
  127. printf (" number of names for hit = %d\n", names); ptr++;
  128. printf (" z1 is %u;", *ptr); ptr++;
  129. printf (" z2 is %u\n", *ptr); ptr++;
  130. printf (" the name is ");
  131. for (j = 0; j < names; j++) { /* for each name */
  132. printf ("%d ", *ptr); ptr++;
  133. }
  134. printf ("\n");
  135. }
  136. }
  137. /* selectObjects() "draws" the triangles in selection mode,
  138. * assigning names for the triangles. Note that the third
  139. * and fourth triangles share one name, so that if either
  140. * or both triangles intersects the viewing/clipping volume,
  141. * only one hit will be registered.
  142. */
  143. #define BUFSIZE 512
  144. void selectObjects(void)
  145. {
  146. GLuint selectBuf[BUFSIZE];
  147. GLint hits, viewport[4];
  148. glSelectBuffer (BUFSIZE, selectBuf);
  149. (void) glRenderMode (GL_SELECT);
  150. glInitNames();
  151. glPushName(-1);
  152. glPushMatrix ();
  153. glMatrixMode (GL_PROJECTION);
  154. glLoadIdentity ();
  155. glOrtho (0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
  156. glMatrixMode (GL_MODELVIEW);
  157. glLoadIdentity ();
  158. glLoadName(1);
  159. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
  160. glLoadName(2);
  161. drawTriangle (2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
  162. glLoadName(3);
  163. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0);
  164. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0);
  165. glPopMatrix ();
  166. glFlush ();
  167. hits = glRenderMode (GL_RENDER);
  168. processHits (hits, selectBuf);
  169. }
  170. void myinit (void)
  171. {
  172. glDepthFunc(GL_LESS);
  173. glEnable(GL_DEPTH_TEST);
  174. glShadeModel(GL_FLAT);
  175. }
  176. void display(void)
  177. {
  178. glClearColor (0.0, 0.0, 0.0, 0.0);
  179. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  180. drawScene ();
  181. selectObjects ();
  182. glFlush();
  183. }
  184. /* Main Loop
  185. * Open window with initial window size, title bar,
  186. * RGBA display mode, depth buffer, and handle input events.
  187. */
  188. int main(int argc, char** argv)
  189. {
  190. auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH16);
  191. auxInitPosition (0, 0, 200, 200);
  192. auxInitWindow (argv[0]);
  193. myinit ();
  194. auxMainLoop(display);
  195. }