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.

uglstencil.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. * Conversion to UGL/Mesa by Stephane Raimbault, 2001
  46. */
  47. #include <stdio.h>
  48. #include <math.h>
  49. #include <ugl/ugl.h>
  50. #include <ugl/uglevent.h>
  51. #include <ugl/uglinput.h>
  52. #include <GL/uglmesa.h>
  53. #include <GL/glu.h>
  54. #include <GL/uglglutshapes.h>
  55. #define YELLOWMAT 1
  56. #define BLUEMAT 2
  57. UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
  58. UGL_LOCAL UGL_EVENT_Q_ID qId;
  59. UGL_LOCAL UGL_MESA_CONTEXT umc;
  60. UGL_LOCAL void initGL (GLsizei w, GLsizei h)
  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. glClear(GL_STENCIL_BUFFER_BIT);
  85. /* create a diamond shaped stencil area */
  86. glMatrixMode(GL_PROJECTION);
  87. glLoadIdentity();
  88. glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
  89. glMatrixMode(GL_MODELVIEW);
  90. glLoadIdentity();
  91. glStencilFunc (GL_ALWAYS, 0x1, 0x1);
  92. glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);
  93. glBegin(GL_QUADS);
  94. glVertex3f (-1.0, 0.0, 0.0);
  95. glVertex3f (0.0, 1.0, 0.0);
  96. glVertex3f (1.0, 0.0, 0.0);
  97. glVertex3f (0.0, -1.0, 0.0);
  98. glEnd();
  99. glMatrixMode(GL_PROJECTION);
  100. glLoadIdentity();
  101. gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0);
  102. glMatrixMode(GL_MODELVIEW);
  103. glLoadIdentity();
  104. glTranslatef(0.0, 0.0, -5.0);
  105. }
  106. /* Draw a sphere in a diamond-shaped section in the
  107. * middle of a window with 2 tori.
  108. */
  109. UGL_LOCAL void drawGL(void)
  110. {
  111. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  112. glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
  113. /* draw blue sphere where the stencil is 1 */
  114. glStencilFunc (GL_EQUAL, 0x1, 0x1);
  115. glCallList (BLUEMAT);
  116. glutSolidSphere (0.5, 15, 15);
  117. /* draw the tori where the stencil is not 1 */
  118. glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);
  119. glPushMatrix();
  120. glRotatef (45.0, 0.0, 0.0, 1.0);
  121. glRotatef (45.0, 0.0, 1.0, 0.0);
  122. glCallList (YELLOWMAT);
  123. glutSolidTorus (0.275, 0.85, 15, 15);
  124. glPushMatrix();
  125. glRotatef (90.0, 1.0, 0.0, 0.0);
  126. glutSolidTorus (0.275, 0.85, 15, 15);
  127. glPopMatrix();
  128. glPopMatrix();
  129. glFlush();
  130. uglMesaSwapBuffers();
  131. }
  132. UGL_LOCAL int getEvent(void)
  133. {
  134. UGL_EVENT event;
  135. UGL_STATUS status;
  136. int retVal = 0;
  137. status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
  138. while (status != UGL_STATUS_Q_EMPTY)
  139. {
  140. UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event;
  141. if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
  142. retVal = 1;
  143. status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
  144. }
  145. return(retVal);
  146. }
  147. void windMLStencil (UGL_BOOL windMLMode);
  148. void uglstencil (void)
  149. {
  150. taskSpawn("tStencil", 210, VX_FP_TASK, 100000,
  151. (FUNCPTR)windMLStencil,UGL_FALSE,1,2,3,4,5,6,7,8,9);
  152. }
  153. void windMLStencil(UGL_BOOL windMLMode)
  154. {
  155. UGL_INPUT_DEVICE_ID keyboardDevId;
  156. GLsizei width, height;
  157. uglInitialize();
  158. uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);
  159. uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0, (UGL_UINT32 *)&eventServiceId);
  160. qId = uglEventQCreate (eventServiceId, 100);
  161. if (windMLMode)
  162. umc = uglMesaCreateNewContextExt(UGL_MESA_SINGLE
  163. | UGL_MESA_WINDML_EXCLUSIVE,
  164. 16,
  165. 8,
  166. 0,0,0,0,
  167. NULL);
  168. else
  169. umc = uglMesaCreateNewContextExt(UGL_MESA_SINGLE,
  170. 16,
  171. 8,
  172. 0,0,0,0,
  173. NULL);
  174. if (umc == NULL)
  175. {
  176. uglDeinitialize();
  177. return;
  178. }
  179. /* Fullscreen */
  180. uglMesaMakeCurrentContext(umc, 0, 0, UGL_MESA_FULLSCREEN_WIDTH,
  181. UGL_MESA_FULLSCREEN_HEIGHT);
  182. uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
  183. uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
  184. initGL(width, height);
  185. drawGL();
  186. while (!getEvent());
  187. uglEventQDestroy (eventServiceId, qId);
  188. uglMesaDestroyContext();
  189. uglDeinitialize();
  190. return;
  191. }