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.

180 lines
5.4 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. * pickdepth.c
  39. * Picking is demonstrated in this program. In
  40. * rendering mode, three overlapping rectangles are
  41. * drawn. When the left mouse button is pressed,
  42. * selection mode is entered with the picking matrix.
  43. * Rectangles which are drawn under the cursor position
  44. * are "picked." Pay special attention to the depth
  45. * value range, which is returned.
  46. */
  47. #include <windows.h>
  48. #include <stdio.h>
  49. #include <GL/gl.h>
  50. #include <GL/glu.h>
  51. #include "glaux.h"
  52. void 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 drawRects(GLenum mode)
  65. {
  66. if (mode == GL_SELECT)
  67. glLoadName (1);
  68. glBegin (GL_QUADS);
  69. glColor3f (1.0, 1.0, 0.0);
  70. glVertex3i (2, 0, 0);
  71. glVertex3i (2, 6, 0);
  72. glVertex3i (6, 6, 0);
  73. glVertex3i (6, 0, 0);
  74. glColor3f (0.0, 1.0, 1.0);
  75. glVertex3i (3, 2, -1);
  76. glVertex3i (3, 8, -1);
  77. glVertex3i (8, 8, -1);
  78. glVertex3i (8, 2, -1);
  79. glColor3f (1.0, 0.0, 1.0);
  80. glVertex3i (0, 2, -2);
  81. glVertex3i (0, 7, -2);
  82. glVertex3i (5, 7, -2);
  83. glVertex3i (5, 2, -2);
  84. glEnd ();
  85. }
  86. /* processHits() prints out the contents of the
  87. * selection array.
  88. */
  89. void processHits (GLint hits, GLuint buffer[])
  90. {
  91. unsigned int i, j;
  92. GLuint names, *ptr;
  93. printf ("hits = %d\n", hits);
  94. ptr = (GLuint *) buffer;
  95. for (i = 0; i < hits; i++) { /* for each hit */
  96. names = *ptr;
  97. printf (" number of names for hit = %d\n", names); ptr++;
  98. printf (" z1 is %u;", *ptr); ptr++;
  99. printf (" z2 is %u\n", *ptr); ptr++;
  100. printf (" the name is ");
  101. for (j = 0; j < names; j++) { /* for each name */
  102. printf ("%d ", *ptr); ptr++;
  103. }
  104. printf ("\n");
  105. }
  106. }
  107. /* pickRects() sets up selection mode, name stack,
  108. * and projection matrix for picking. Then the objects
  109. * are drawn.
  110. */
  111. #define BUFSIZE 512
  112. void pickRects(AUX_EVENTREC *event)
  113. {
  114. GLuint selectBuf[BUFSIZE];
  115. GLint hits;
  116. GLint viewport[4];
  117. int x, y;
  118. x = event->data[AUX_MOUSEX];
  119. y = event->data[AUX_MOUSEY];
  120. glGetIntegerv (GL_VIEWPORT, viewport);
  121. glSelectBuffer (BUFSIZE, selectBuf);
  122. (void) glRenderMode (GL_SELECT);
  123. glInitNames();
  124. glPushName(-1);
  125. glMatrixMode (GL_PROJECTION);
  126. glPushMatrix ();
  127. glLoadIdentity ();
  128. /* create 5x5 pixel picking region near cursor location */
  129. gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
  130. 5.0, 5.0, viewport);
  131. glOrtho (0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
  132. drawRects (GL_SELECT);
  133. glPopMatrix ();
  134. glFlush ();
  135. hits = glRenderMode (GL_RENDER);
  136. processHits (hits, selectBuf);
  137. }
  138. void display(void)
  139. {
  140. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  141. drawRects (GL_RENDER);
  142. glFlush();
  143. }
  144. void myReshape(GLsizei w, GLsizei h)
  145. {
  146. glViewport(0, 0, w, h);
  147. glMatrixMode(GL_PROJECTION);
  148. glLoadIdentity();
  149. glOrtho (0.0, 8.0, 0.0, 8.0, 0.0, 2.0);
  150. glMatrixMode(GL_MODELVIEW);
  151. glLoadIdentity();
  152. }
  153. /* Main Loop
  154. * Open window with initial window size, title bar,
  155. * RGBA display mode, depth buffer, and handle input events.
  156. */
  157. int main(int argc, char** argv)
  158. {
  159. auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH16);
  160. auxInitPosition (0, 0, 100, 100);
  161. auxInitWindow (argv[0]);
  162. myinit ();
  163. auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, pickRects);
  164. auxReshapeFunc (myReshape);
  165. auxMainLoop(display);
  166. }