Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

stencil.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /* stencil.c
  39. * This program draws two rotated tori in a window.
  40. * A diamond in the center of the window masks out part
  41. * of the scene. Within this mask, a different model
  42. * (a sphere) is drawn in a different color.
  43. */
  44. /*
  45. * !!! NOTE !!!
  46. *
  47. * This demo is poorly written. The stencil buffer should be
  48. * redrawn in display(), not in the myReshape() function.
  49. * The reason is if the window gets "damaged" then the stencil buffer
  50. * contents will be in an undefined state (myReshape is not called when
  51. * a window is damaged and needs to be redrawn). If the stencil buffer
  52. * contents are undefined, the results of display() are unpredictable.
  53. *
  54. * -Brian
  55. */
  56. #include <stdlib.h>
  57. #include <GL/glut.h>
  58. #define YELLOWMAT 1
  59. #define BLUEMAT 2
  60. void myinit (void)
  61. {
  62. GLfloat yellow_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
  63. GLfloat yellow_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  64. GLfloat blue_diffuse[] = { 0.1, 0.1, 0.7, 1.0 };
  65. GLfloat blue_specular[] = { 0.1, 1.0, 1.0, 1.0 };
  66. GLfloat position_one[] = { 1.0, 1.0, 1.0, 0.0 };
  67. glNewList(YELLOWMAT, GL_COMPILE);
  68. glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse);
  69. glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular);
  70. glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
  71. glEndList();
  72. glNewList(BLUEMAT, GL_COMPILE);
  73. glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse);
  74. glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular);
  75. glMaterialf(GL_FRONT, GL_SHININESS, 45.0);
  76. glEndList();
  77. glLightfv(GL_LIGHT0, GL_POSITION, position_one);
  78. glEnable(GL_LIGHT0);
  79. glEnable(GL_LIGHTING);
  80. glDepthFunc(GL_LESS);
  81. glEnable(GL_DEPTH_TEST);
  82. glClearStencil(0x0);
  83. glEnable(GL_STENCIL_TEST);
  84. }
  85. /* Draw a sphere in a diamond-shaped section in the
  86. * middle of a window with 2 tori.
  87. */
  88. void display(void)
  89. {
  90. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  91. glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
  92. /* draw blue sphere where the stencil is 1 */
  93. glStencilFunc (GL_EQUAL, 0x1, 0x1);
  94. glCallList (BLUEMAT);
  95. glutSolidSphere (0.5, 15, 15);
  96. /* draw the tori where the stencil is not 1 */
  97. glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);
  98. glPushMatrix();
  99. glRotatef (45.0, 0.0, 0.0, 1.0);
  100. glRotatef (45.0, 0.0, 1.0, 0.0);
  101. glCallList (YELLOWMAT);
  102. glutSolidTorus (0.275, 0.85, 15, 15);
  103. glPushMatrix();
  104. glRotatef (90.0, 1.0, 0.0, 0.0);
  105. glutSolidTorus (0.275, 0.85, 15, 15);
  106. glPopMatrix();
  107. glPopMatrix();
  108. glFlush();
  109. glutSwapBuffers();
  110. }
  111. /* Whenever the window is reshaped, redefine the
  112. * coordinate system and redraw the stencil area.
  113. */
  114. void myReshape(int w, int h)
  115. {
  116. glViewport(0, 0, w, h);
  117. glClear(GL_STENCIL_BUFFER_BIT);
  118. /* create a diamond shaped stencil area */
  119. glMatrixMode(GL_PROJECTION);
  120. glLoadIdentity();
  121. glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
  122. glMatrixMode(GL_MODELVIEW);
  123. glLoadIdentity();
  124. glStencilFunc (GL_ALWAYS, 0x1, 0x1);
  125. glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);
  126. glBegin(GL_QUADS);
  127. glVertex3f (-1.0, 0.0, 0.0);
  128. glVertex3f (0.0, 1.0, 0.0);
  129. glVertex3f (1.0, 0.0, 0.0);
  130. glVertex3f (0.0, -1.0, 0.0);
  131. glEnd();
  132. glMatrixMode(GL_PROJECTION);
  133. glLoadIdentity();
  134. gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0);
  135. glMatrixMode(GL_MODELVIEW);
  136. glLoadIdentity();
  137. glTranslatef(0.0, 0.0, -5.0);
  138. }
  139. static void
  140. key(unsigned char k, int x, int y)
  141. {
  142. switch (k) {
  143. case 27: /* Escape */
  144. exit(0);
  145. break;
  146. default:
  147. return;
  148. }
  149. glutPostRedisplay();
  150. }
  151. /* Main Loop
  152. * Open window with initial window size, title bar,
  153. * RGBA display mode, and handle input events.
  154. */
  155. int main(int argc, char** argv)
  156. {
  157. glutInit(&argc, argv);
  158. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
  159. glutInitWindowSize (400, 400);
  160. glutCreateWindow (argv[0]);
  161. myinit ();
  162. glutReshapeFunc (myReshape);
  163. glutDisplayFunc(display);
  164. glutKeyboardFunc(key);
  165. glutMainLoop();
  166. return 0; /* ANSI C requires main to return int. */
  167. }