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.

185 lines
5.5 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. * picksquare.c
  39. * Use of multiple names and picking are demonstrated.
  40. * A 3x3 grid of squares is drawn. When the left mouse
  41. * button is pressed, all squares under the cursor position
  42. * have their color changed.
  43. */
  44. #include <windows.h>
  45. #include <stdio.h>
  46. #include <GL/gl.h>
  47. #include <GL/glu.h>
  48. #include "glaux.h"
  49. int board[3][3]; /* amount of color for each square */
  50. /* Clear color value for every square on the board */
  51. void myinit(void)
  52. {
  53. int i, j;
  54. for (i = 0; i < 3; i++)
  55. for (j = 0; j < 3; j ++)
  56. board[i][j] = 0;
  57. glClearColor (0.0, 0.0, 0.0, 0.0);
  58. }
  59. /* The nine squares are drawn. In selection mode, each
  60. * square is given two names: one for the row and the
  61. * other for the column on the grid. The color of each
  62. * square is determined by its position on the grid, and
  63. * the value in the board[][] array.
  64. */
  65. void drawSquares(GLenum mode)
  66. {
  67. GLuint i, j;
  68. for (i = 0; i < 3; i++) {
  69. if (mode == GL_SELECT)
  70. glLoadName (i);
  71. for (j = 0; j < 3; j ++) {
  72. if (mode == GL_SELECT)
  73. glPushName (j);
  74. glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0,
  75. (GLfloat) board[i][j]/3.0);
  76. glRecti (i, j, i+1, j+1);
  77. if (mode == GL_SELECT)
  78. glPopName ();
  79. }
  80. }
  81. }
  82. /* processHits() prints out the contents of the
  83. * selection array.
  84. */
  85. void processHits (GLint hits, GLuint buffer[])
  86. {
  87. unsigned int i, j;
  88. GLuint ii, jj, names, *ptr;
  89. printf ("hits = %d\n", hits);
  90. ptr = (GLuint *) buffer;
  91. for (i = 0; i < hits; i++) { /* for each hit */
  92. names = *ptr;
  93. printf (" number of names for this hit = %d\n", names); ptr++;
  94. printf (" z1 is %u;", *ptr); ptr++;
  95. printf (" z2 is %u\n", *ptr); ptr++;
  96. printf (" names are ");
  97. for (j = 0; j < names; j++) { /* for each name */
  98. printf ("%d ", *ptr);
  99. if (j == 0) /* set row and column */
  100. ii = *ptr;
  101. else if (j == 1)
  102. jj = *ptr;
  103. ptr++;
  104. }
  105. printf ("\n");
  106. board[ii][jj] = (board[ii][jj] + 1) % 3;
  107. }
  108. }
  109. /* pickSquares() sets up selection mode, name stack,
  110. * and projection matrix for picking. Then the
  111. * objects are drawn.
  112. */
  113. #define BUFSIZE 512
  114. void pickSquares(AUX_EVENTREC *event)
  115. {
  116. GLuint selectBuf[BUFSIZE];
  117. GLint hits;
  118. GLint viewport[4];
  119. int x, y;
  120. x = event->data[AUX_MOUSEX];
  121. y = event->data[AUX_MOUSEY];
  122. glGetIntegerv (GL_VIEWPORT, viewport);
  123. glSelectBuffer (BUFSIZE, selectBuf);
  124. (void) glRenderMode (GL_SELECT);
  125. glInitNames();
  126. glPushName(-1);
  127. glMatrixMode (GL_PROJECTION);
  128. glPushMatrix ();
  129. glLoadIdentity ();
  130. /* create 5x5 pixel picking region near cursor location */
  131. gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
  132. 5.0, 5.0, viewport);
  133. gluOrtho2D (0.0, 3.0, 0.0, 3.0);
  134. drawSquares (GL_SELECT);
  135. glMatrixMode (GL_PROJECTION);
  136. glPopMatrix ();
  137. glFlush ();
  138. hits = glRenderMode (GL_RENDER);
  139. processHits (hits, selectBuf);
  140. }
  141. void display(void)
  142. {
  143. glClear(GL_COLOR_BUFFER_BIT);
  144. drawSquares (GL_RENDER);
  145. glFlush();
  146. }
  147. void myReshape(GLsizei w, GLsizei h)
  148. {
  149. glViewport(0, 0, w, h);
  150. glMatrixMode(GL_PROJECTION);
  151. glLoadIdentity();
  152. gluOrtho2D (0.0, 3.0, 0.0, 3.0);
  153. glMatrixMode(GL_MODELVIEW);
  154. glLoadIdentity();
  155. }
  156. /* Main Loop
  157. * Open window with initial window size, title bar,
  158. * RGBA display mode, and handle input events.
  159. */
  160. int main(int argc, char** argv)
  161. {
  162. auxInitDisplayMode (AUX_SINGLE | AUX_RGB);
  163. auxInitPosition (0, 0, 100, 100);
  164. auxInitWindow (argv[0]);
  165. myinit ();
  166. auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, pickSquares);
  167. auxReshapeFunc (myReshape);
  168. auxMainLoop(display);
  169. }