Clone of mesa.
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.

stencil.c 5.4KB

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