/opengles/redbook/stencil.c

http://ftk.googlecode.com/ · C · 174 lines · 99 code · 19 blank · 56 comment · 2 complexity · 717178ace59b95fcf568ff7842ec8b36 MD5 · raw file

  1. /*
  2. * License Applicability. Except to the extent portions of this file are
  3. * made subject to an alternative license as permitted in the SGI Free
  4. * Software License B, Version 1.1 (the "License"), the contents of this
  5. * file are subject only to the provisions of the License. You may not use
  6. * this file except in compliance with the License. You may obtain a copy
  7. * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
  8. * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
  9. *
  10. * http://oss.sgi.com/projects/FreeB
  11. *
  12. * Note that, as provided in the License, the Software is distributed on an
  13. * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
  14. * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
  15. * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
  16. * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. *
  18. * Original Code. The Original Code is: OpenGL Sample Implementation,
  19. * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
  20. * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
  21. * Copyright in any portions created by third parties is as indicated
  22. * elsewhere herein. All Rights Reserved.
  23. *
  24. * Additional Notice Provisions: The application programming interfaces
  25. * established by SGI in conjunction with the Original Code are The
  26. * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
  27. * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
  28. * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
  29. * Window System(R) (Version 1.3), released October 19, 1998. This software
  30. * was created using the OpenGL(R) version 1.2.1 Sample Implementation
  31. * published by SGI, but has not been independently verified as being
  32. * compliant with the OpenGL(R) version 1.2.1 Specification.
  33. *
  34. */
  35. /*
  36. * stencil.c
  37. * This program demonstrates use of the stencil buffer for
  38. * masking nonrectangular regions.
  39. * Whenever the window is redrawn, a value of 1 is drawn
  40. * into a diamond-shaped region in the stencil buffer.
  41. * Elsewhere in the stencil buffer, the value is 0.
  42. * Then a blue sphere is drawn where the stencil value is 1,
  43. * and yellow torii are drawn where the stencil value is not 1.
  44. */
  45. #include <GL/glut.h>
  46. #include <stdlib.h>
  47. #define YELLOWMAT 1
  48. #define BLUEMAT 2
  49. void init (void)
  50. {
  51. GLfloat yellow_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
  52. GLfloat yellow_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  53. GLfloat blue_diffuse[] = { 0.1, 0.1, 0.7, 1.0 };
  54. GLfloat blue_specular[] = { 0.1, 1.0, 1.0, 1.0 };
  55. GLfloat position_one[] = { 1.0, 1.0, 1.0, 0.0 };
  56. glNewList(YELLOWMAT, GL_COMPILE);
  57. glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse);
  58. glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular);
  59. glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
  60. glEndList();
  61. glNewList(BLUEMAT, GL_COMPILE);
  62. glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse);
  63. glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular);
  64. glMaterialf(GL_FRONT, GL_SHININESS, 45.0);
  65. glEndList();
  66. glLightfv(GL_LIGHT0, GL_POSITION, position_one);
  67. glEnable(GL_LIGHT0);
  68. glEnable(GL_LIGHTING);
  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 torii.
  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. glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
  82. glCallList (BLUEMAT);
  83. glutSolidSphere (0.5, 15, 15);
  84. /* draw the tori where the stencil is not 1 */
  85. glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);
  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. }
  97. /* Whenever the window is reshaped, redefine the
  98. * coordinate system and redraw the stencil area.
  99. */
  100. void reshape(int w, int h)
  101. {
  102. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  103. /* create a diamond shaped stencil area */
  104. glMatrixMode(GL_PROJECTION);
  105. glLoadIdentity();
  106. if (w <= h)
  107. gluOrtho2D(-3.0, 3.0, -3.0*(GLfloat)h/(GLfloat)w,
  108. 3.0*(GLfloat)h/(GLfloat)w);
  109. else
  110. gluOrtho2D(-3.0*(GLfloat)w/(GLfloat)h,
  111. 3.0*(GLfloat)w/(GLfloat)h, -3.0, 3.0);
  112. glMatrixMode(GL_MODELVIEW);
  113. glLoadIdentity();
  114. glClear(GL_STENCIL_BUFFER_BIT);
  115. glStencilFunc (GL_ALWAYS, 0x1, 0x1);
  116. glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);
  117. glBegin(GL_QUADS);
  118. glVertex2f (-1.0, 0.0);
  119. glVertex2f (0.0, 1.0);
  120. glVertex2f (1.0, 0.0);
  121. glVertex2f (0.0, -1.0);
  122. glEnd();
  123. glMatrixMode(GL_PROJECTION);
  124. glLoadIdentity();
  125. gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0);
  126. glMatrixMode(GL_MODELVIEW);
  127. glLoadIdentity();
  128. glTranslatef(0.0, 0.0, -5.0);
  129. }
  130. void keyboard(unsigned char key, int x, int y)
  131. {
  132. switch (key) {
  133. case 27:
  134. exit(0);
  135. break;
  136. }
  137. }
  138. /* Main Loop
  139. * Be certain to request stencil bits.
  140. */
  141. int main(int argc, char** argv)
  142. {
  143. glutInit(&argc, argv);
  144. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
  145. | GLUT_DEPTH | GLUT_STENCIL);
  146. glutInitWindowSize (400, 400);
  147. glutInitWindowPosition (100, 100);
  148. glutCreateWindow (argv[0]);
  149. init ();
  150. glutReshapeFunc(reshape);
  151. glutDisplayFunc(display);
  152. glutKeyboardFunc(keyboard);
  153. glutMainLoop();
  154. return 0;
  155. }