Clone of mesa.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

pickdepth.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (c) Mark J. Kilgard, 1994. */
  2. /*
  3. * (c) Copyright 1993, Silicon Graphics, Inc.
  4. * ALL RIGHTS RESERVED
  5. * Permission to use, copy, modify, and distribute this software for
  6. * any purpose and without fee is hereby granted, provided that the above
  7. * copyright notice appear in all copies and that both the copyright notice
  8. * and this permission notice appear in supporting documentation, and that
  9. * the name of Silicon Graphics, Inc. not be used in advertising
  10. * or publicity pertaining to distribution of the software without specific,
  11. * written prior permission.
  12. *
  13. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  17. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  22. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25. *
  26. * US Government Users Restricted Rights
  27. * Use, duplication, or disclosure by the Government is subject to
  28. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30. * clause at DFARS 252.227-7013 and/or in similar or successor
  31. * clauses in the FAR or the DOD or NASA FAR Supplement.
  32. * Unpublished-- rights reserved under the copyright laws of the
  33. * United States. Contractor/manufacturer is Silicon Graphics,
  34. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  35. *
  36. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37. */
  38. /*
  39. * pickdepth.c
  40. * Picking is demonstrated in this program. In
  41. * rendering mode, three overlapping rectangles are
  42. * drawn. When the left mouse button is pressed,
  43. * selection mode is entered with the picking matrix.
  44. * Rectangles which are drawn under the cursor position
  45. * are "picked." Pay special attention to the depth
  46. * value range, which is returned.
  47. */
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <GL/glut.h>
  51. void
  52. myinit(void)
  53. {
  54. glClearColor(0.0, 0.0, 0.0, 0.0);
  55. glDepthFunc(GL_LESS);
  56. glEnable(GL_DEPTH_TEST);
  57. glShadeModel(GL_FLAT);
  58. glDepthRange(0.0, 1.0); /* The default z mapping */
  59. }
  60. /* The three rectangles are drawn. In selection mode,
  61. * each rectangle is given the same name. Note that
  62. * each rectangle is drawn with a different z value.
  63. */
  64. void
  65. drawRects(GLenum mode)
  66. {
  67. if (mode == GL_SELECT)
  68. glLoadName(1);
  69. glBegin(GL_QUADS);
  70. glColor3f(1.0, 1.0, 0.0);
  71. glVertex3i(2, 0, 0);
  72. glVertex3i(2, 6, 0);
  73. glVertex3i(6, 6, 0);
  74. glVertex3i(6, 0, 0);
  75. glEnd();
  76. if (mode == GL_SELECT)
  77. glLoadName(2);
  78. glBegin(GL_QUADS);
  79. glColor3f(0.0, 1.0, 1.0);
  80. glVertex3i(3, 2, -1);
  81. glVertex3i(3, 8, -1);
  82. glVertex3i(8, 8, -1);
  83. glVertex3i(8, 2, -1);
  84. glEnd();
  85. if (mode == GL_SELECT)
  86. glLoadName(3);
  87. glBegin(GL_QUADS);
  88. glColor3f(1.0, 0.0, 1.0);
  89. glVertex3i(0, 2, -2);
  90. glVertex3i(0, 7, -2);
  91. glVertex3i(5, 7, -2);
  92. glVertex3i(5, 2, -2);
  93. glEnd();
  94. }
  95. /* processHits() prints out the contents of the
  96. * selection array.
  97. */
  98. void
  99. processHits(GLint hits, GLuint buffer[])
  100. {
  101. unsigned int i, j;
  102. GLuint names, *ptr;
  103. printf("hits = %d\n", hits);
  104. ptr = (GLuint *) buffer;
  105. for (i = 0; i < hits; i++) { /* for each hit */
  106. names = *ptr;
  107. printf(" number of names for hit = %d\n", names);
  108. ptr++;
  109. printf(" z1 is %g;", (float) *ptr/0xffffffff);
  110. ptr++;
  111. printf(" z2 is %g\n", (float) *ptr/0xffffffff);
  112. ptr++;
  113. printf(" the name is ");
  114. for (j = 0; j < names; j++) { /* for each name */
  115. printf("%d ", *ptr);
  116. ptr++;
  117. }
  118. printf("\n");
  119. }
  120. }
  121. /* pickRects() sets up selection mode, name stack,
  122. * and projection matrix for picking. Then the objects
  123. * are drawn.
  124. */
  125. #define BUFSIZE 512
  126. void
  127. pickRects(int button, int state, int x, int y)
  128. {
  129. GLuint selectBuf[BUFSIZE];
  130. GLint hits;
  131. GLint viewport[4];
  132. if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
  133. return;
  134. glGetIntegerv(GL_VIEWPORT, viewport);
  135. glSelectBuffer(BUFSIZE, selectBuf);
  136. (void) glRenderMode(GL_SELECT);
  137. glInitNames();
  138. glPushName(-1);
  139. glMatrixMode(GL_PROJECTION);
  140. glPushMatrix();
  141. glLoadIdentity();
  142. /* create 5x5 pixel picking region near cursor location */
  143. gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y),
  144. 5.0, 5.0, viewport);
  145. glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
  146. drawRects(GL_SELECT);
  147. glPopMatrix();
  148. glFlush();
  149. hits = glRenderMode(GL_RENDER);
  150. processHits(hits, selectBuf);
  151. }
  152. void
  153. display(void)
  154. {
  155. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  156. drawRects(GL_RENDER);
  157. glutSwapBuffers();
  158. }
  159. void
  160. myReshape(int w, int h)
  161. {
  162. glViewport(0, 0, w, h);
  163. glMatrixMode(GL_PROJECTION);
  164. glLoadIdentity();
  165. glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
  166. glMatrixMode(GL_MODELVIEW);
  167. glLoadIdentity();
  168. }
  169. static void
  170. key(unsigned char k, int x, int y)
  171. {
  172. switch (k) {
  173. case 27: /* Escape */
  174. exit(0);
  175. break;
  176. default:
  177. return;
  178. }
  179. glutPostRedisplay();
  180. }
  181. /* Main Loop
  182. * Open window with initial window size, title bar,
  183. * RGBA display mode, depth buffer, and handle input events.
  184. */
  185. int
  186. main(int argc, char **argv)
  187. {
  188. glutInitWindowSize(200, 200);
  189. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  190. glutInit(&argc, argv);
  191. glutCreateWindow(argv[0]);
  192. myinit();
  193. glutMouseFunc(pickRects);
  194. glutReshapeFunc(myReshape);
  195. glutDisplayFunc(display);
  196. glutKeyboardFunc(key);
  197. glutMainLoop();
  198. return 0; /* ANSI C requires main to return int. */
  199. }