Clone of mesa.
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.

select.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 1993-1997, 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(R) is a registered 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 <GL/glut.h>
  53. #include <stdlib.h>
  54. #include <stdio.h>
  55. /* draw a triangle with vertices at (x1, y1), (x2, y2)
  56. * and (x3, y3) at z units away from the origin.
  57. */
  58. void drawTriangle (GLfloat x1, GLfloat y1, GLfloat x2,
  59. GLfloat y2, GLfloat x3, GLfloat y3, GLfloat z)
  60. {
  61. glBegin (GL_TRIANGLES);
  62. glVertex3f (x1, y1, z);
  63. glVertex3f (x2, y2, z);
  64. glVertex3f (x3, y3, z);
  65. glEnd ();
  66. }
  67. /* draw a rectangular box with these outer x, y, and z values */
  68. void drawViewVolume (GLfloat x1, GLfloat x2, GLfloat y1,
  69. GLfloat y2, GLfloat z1, GLfloat z2)
  70. {
  71. glColor3f (1.0, 1.0, 1.0);
  72. glBegin (GL_LINE_LOOP);
  73. glVertex3f (x1, y1, -z1);
  74. glVertex3f (x2, y1, -z1);
  75. glVertex3f (x2, y2, -z1);
  76. glVertex3f (x1, y2, -z1);
  77. glEnd ();
  78. glBegin (GL_LINE_LOOP);
  79. glVertex3f (x1, y1, -z2);
  80. glVertex3f (x2, y1, -z2);
  81. glVertex3f (x2, y2, -z2);
  82. glVertex3f (x1, y2, -z2);
  83. glEnd ();
  84. glBegin (GL_LINES); /* 4 lines */
  85. glVertex3f (x1, y1, -z1);
  86. glVertex3f (x1, y1, -z2);
  87. glVertex3f (x1, y2, -z1);
  88. glVertex3f (x1, y2, -z2);
  89. glVertex3f (x2, y1, -z1);
  90. glVertex3f (x2, y1, -z2);
  91. glVertex3f (x2, y2, -z1);
  92. glVertex3f (x2, y2, -z2);
  93. glEnd ();
  94. }
  95. /* drawScene draws 4 triangles and a wire frame
  96. * which represents the viewing volume.
  97. */
  98. void drawScene (void)
  99. {
  100. glMatrixMode (GL_PROJECTION);
  101. glLoadIdentity ();
  102. gluPerspective (40.0, 4.0/3.0, 1.0, 100.0);
  103. glMatrixMode (GL_MODELVIEW);
  104. glLoadIdentity ();
  105. gluLookAt (7.5, 7.5, 12.5, 2.5, 2.5, -5.0, 0.0, 1.0, 0.0);
  106. glColor3f (0.0, 1.0, 0.0); /* green triangle */
  107. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
  108. glColor3f (1.0, 0.0, 0.0); /* red triangle */
  109. drawTriangle (2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
  110. glColor3f (1.0, 1.0, 0.0); /* yellow triangles */
  111. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0);
  112. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0);
  113. drawViewVolume (0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
  114. }
  115. /* processHits prints out the contents of the selection array
  116. */
  117. void processHits (GLint hits, GLuint buffer[])
  118. {
  119. unsigned int i, j;
  120. GLuint names, *ptr;
  121. printf ("hits = %d\n", hits);
  122. ptr = (GLuint *) buffer;
  123. for (i = 0; i < hits; i++) { /* for each hit */
  124. names = *ptr;
  125. printf (" number of names for hit = %d\n", names); ptr++;
  126. printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
  127. printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
  128. printf (" the name is ");
  129. for (j = 0; j < names; j++) { /* for each name */
  130. printf ("%d ", *ptr); ptr++;
  131. }
  132. printf ("\n");
  133. }
  134. }
  135. /* selectObjects "draws" the triangles in selection mode,
  136. * assigning names for the triangles. Note that the third
  137. * and fourth triangles share one name, so that if either
  138. * or both triangles intersects the viewing/clipping volume,
  139. * only one hit will be registered.
  140. */
  141. #define BUFSIZE 512
  142. void selectObjects(void)
  143. {
  144. GLuint selectBuf[BUFSIZE];
  145. GLint hits;
  146. glSelectBuffer (BUFSIZE, selectBuf);
  147. (void) glRenderMode (GL_SELECT);
  148. glInitNames();
  149. glPushName(0);
  150. glPushMatrix ();
  151. glMatrixMode (GL_PROJECTION);
  152. glLoadIdentity ();
  153. glOrtho (0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
  154. glMatrixMode (GL_MODELVIEW);
  155. glLoadIdentity ();
  156. glLoadName(1);
  157. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
  158. glLoadName(2);
  159. drawTriangle (2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
  160. glLoadName(3);
  161. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0);
  162. drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0);
  163. glPopMatrix ();
  164. glFlush ();
  165. hits = glRenderMode (GL_RENDER);
  166. processHits (hits, selectBuf);
  167. }
  168. void init (void)
  169. {
  170. glEnable(GL_DEPTH_TEST);
  171. glShadeModel(GL_FLAT);
  172. }
  173. void display(void)
  174. {
  175. glClearColor (0.0, 0.0, 0.0, 0.0);
  176. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  177. drawScene ();
  178. selectObjects ();
  179. glFlush();
  180. }
  181. /* ARGSUSED1 */
  182. void keyboard(unsigned char key, int x, int y)
  183. {
  184. switch (key) {
  185. case 27:
  186. exit(0);
  187. break;
  188. }
  189. }
  190. /* Main Loop */
  191. int main(int argc, char** argv)
  192. {
  193. glutInit(&argc, argv);
  194. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  195. glutInitWindowSize (200, 200);
  196. glutInitWindowPosition (100, 100);
  197. glutCreateWindow (argv[0]);
  198. init();
  199. glutDisplayFunc(display);
  200. glutKeyboardFunc(keyboard);
  201. glutMainLoop();
  202. return 0;
  203. }