Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

picksquare.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. * 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 <stdlib.h>
  45. #include <stdio.h>
  46. #include <GL/glut.h>
  47. int board[3][3]; /* amount of color for each square */
  48. /* Clear color value for every square on the board */
  49. void init(void)
  50. {
  51. int i, j;
  52. for (i = 0; i < 3; i++)
  53. for (j = 0; j < 3; j ++)
  54. board[i][j] = 0;
  55. glClearColor (0.0, 0.0, 0.0, 0.0);
  56. }
  57. /* The nine squares are drawn. In selection mode, each
  58. * square is given two names: one for the row and the
  59. * other for the column on the grid. The color of each
  60. * square is determined by its position on the grid, and
  61. * the value in the board[][] array.
  62. */
  63. void drawSquares(GLenum mode)
  64. {
  65. GLuint i, j;
  66. for (i = 0; i < 3; i++) {
  67. if (mode == GL_SELECT)
  68. glLoadName (i);
  69. for (j = 0; j < 3; j ++) {
  70. if (mode == GL_SELECT)
  71. glPushName (j);
  72. glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0,
  73. (GLfloat) board[i][j]/3.0);
  74. glRecti (i, j, i+1, j+1);
  75. if (mode == GL_SELECT)
  76. glPopName ();
  77. }
  78. }
  79. }
  80. /* processHits prints out the contents of the
  81. * selection array.
  82. */
  83. void processHits (GLint hits, GLuint buffer[])
  84. {
  85. unsigned int i, j;
  86. GLuint ii = 0, jj = 0, names, *ptr;
  87. printf ("hits = %d\n", hits);
  88. ptr = (GLuint *) buffer;
  89. for (i = 0; i < hits; i++) { /* for each hit */
  90. names = *ptr;
  91. printf (" number of names for this hit = %d\n", names); ptr++;
  92. printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
  93. printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
  94. printf (" names are ");
  95. for (j = 0; j < names; j++) { /* for each name */
  96. printf ("%d ", *ptr);
  97. if (j == 0) /* set row and column */
  98. ii = *ptr;
  99. else if (j == 1)
  100. jj = *ptr;
  101. ptr++;
  102. }
  103. printf ("\n");
  104. board[ii][jj] = (board[ii][jj] + 1) % 3;
  105. }
  106. }
  107. /* pickSquares() sets up selection mode, name stack,
  108. * and projection matrix for picking. Then the
  109. * objects are drawn.
  110. */
  111. #define BUFSIZE 512
  112. void pickSquares(int button, int state, int x, int y)
  113. {
  114. GLuint selectBuf[BUFSIZE];
  115. GLint hits;
  116. GLint viewport[4];
  117. if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
  118. return;
  119. glGetIntegerv (GL_VIEWPORT, viewport);
  120. glSelectBuffer (BUFSIZE, selectBuf);
  121. (void) glRenderMode (GL_SELECT);
  122. glInitNames();
  123. glPushName(0);
  124. glMatrixMode (GL_PROJECTION);
  125. glPushMatrix ();
  126. glLoadIdentity ();
  127. /* create 5x5 pixel picking region near cursor location */
  128. gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
  129. 5.0, 5.0, viewport);
  130. gluOrtho2D (0.0, 3.0, 0.0, 3.0);
  131. drawSquares (GL_SELECT);
  132. glMatrixMode (GL_PROJECTION);
  133. glPopMatrix ();
  134. glFlush ();
  135. hits = glRenderMode (GL_RENDER);
  136. processHits (hits, selectBuf);
  137. glutPostRedisplay();
  138. }
  139. void display(void)
  140. {
  141. glClear(GL_COLOR_BUFFER_BIT);
  142. drawSquares (GL_RENDER);
  143. glFlush();
  144. }
  145. void reshape(int w, int h)
  146. {
  147. glViewport(0, 0, w, h);
  148. glMatrixMode(GL_PROJECTION);
  149. glLoadIdentity();
  150. gluOrtho2D (0.0, 3.0, 0.0, 3.0);
  151. glMatrixMode(GL_MODELVIEW);
  152. glLoadIdentity();
  153. }
  154. /* ARGSUSED1 */
  155. void keyboard(unsigned char key, int x, int y)
  156. {
  157. switch (key) {
  158. case 27:
  159. exit(0);
  160. break;
  161. }
  162. }
  163. /* Main Loop */
  164. int main(int argc, char** argv)
  165. {
  166. glutInit(&argc, argv);
  167. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  168. glutInitWindowSize (100, 100);
  169. glutInitWindowPosition (100, 100);
  170. glutCreateWindow (argv[0]);
  171. init ();
  172. glutReshapeFunc (reshape);
  173. glutDisplayFunc(display);
  174. glutMouseFunc (pickSquares);
  175. glutKeyboardFunc (keyboard);
  176. glutMainLoop();
  177. return 0;
  178. }