WindML driver (Stephane Raimbault)
[mesa.git] / progs / windml / uglstencil.c
1
2 /* Copyright (c) Mark J. Kilgard, 1994. */
3
4 /*
5 * (c) Copyright 1993, Silicon Graphics, Inc.
6 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
8 * any purpose and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both the copyright notice
10 * and this permission notice appear in supporting documentation, and that
11 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
13 * written prior permission.
14 *
15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * US Government Users Restricted Rights
29 * Use, duplication, or disclosure by the Government is subject to
30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32 * clause at DFARS 252.227-7013 and/or in similar or successor
33 * clauses in the FAR or the DOD or NASA FAR Supplement.
34 * Unpublished-- rights reserved under the copyright laws of the
35 * United States. Contractor/manufacturer is Silicon Graphics,
36 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
37 *
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39 */
40 /* stencil.c
41 * This program draws two rotated tori in a window.
42 * A diamond in the center of the window masks out part
43 * of the scene. Within this mask, a different model
44 * (a sphere) is drawn in a different color.
45 */
46
47 /*
48 * Conversion to UGL/Mesa by Stephane Raimbault, 2001
49 */
50
51 #include <stdio.h>
52 #include <math.h>
53
54 #include <ugl/ugl.h>
55 #include <ugl/uglevent.h>
56 #include <ugl/uglinput.h>
57
58 #include <GL/uglmesa.h>
59 #include <GL/glu.h>
60 #include <GL/uglglutshapes.h>
61
62 #define YELLOWMAT 1
63 #define BLUEMAT 2
64
65 UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
66 UGL_LOCAL UGL_EVENT_Q_ID qId;
67 UGL_LOCAL UGL_MESA_CONTEXT umc;
68
69 UGL_LOCAL void initGL (GLsizei w, GLsizei h)
70 {
71 GLfloat yellow_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
72 GLfloat yellow_specular[] = { 1.0, 1.0, 1.0, 1.0 };
73
74 GLfloat blue_diffuse[] = { 0.1, 0.1, 0.7, 1.0 };
75 GLfloat blue_specular[] = { 0.1, 1.0, 1.0, 1.0 };
76
77 GLfloat position_one[] = { 1.0, 1.0, 1.0, 0.0 };
78
79 glNewList(YELLOWMAT, GL_COMPILE);
80 glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse);
81 glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular);
82 glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
83 glEndList();
84
85 glNewList(BLUEMAT, GL_COMPILE);
86 glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse);
87 glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular);
88 glMaterialf(GL_FRONT, GL_SHININESS, 45.0);
89 glEndList();
90
91 glLightfv(GL_LIGHT0, GL_POSITION, position_one);
92
93 glEnable(GL_LIGHT0);
94 glEnable(GL_LIGHTING);
95 glDepthFunc(GL_LESS);
96 glEnable(GL_DEPTH_TEST);
97
98 glClearStencil(0x0);
99 glEnable(GL_STENCIL_TEST);
100
101 glClear(GL_STENCIL_BUFFER_BIT);
102
103 /* create a diamond shaped stencil area */
104 glMatrixMode(GL_PROJECTION);
105 glLoadIdentity();
106 glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
107 glMatrixMode(GL_MODELVIEW);
108 glLoadIdentity();
109
110 glStencilFunc (GL_ALWAYS, 0x1, 0x1);
111 glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);
112 glBegin(GL_QUADS);
113 glVertex3f (-1.0, 0.0, 0.0);
114 glVertex3f (0.0, 1.0, 0.0);
115 glVertex3f (1.0, 0.0, 0.0);
116 glVertex3f (0.0, -1.0, 0.0);
117 glEnd();
118
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
127 /* Draw a sphere in a diamond-shaped section in the
128 * middle of a window with 2 tori.
129 */
130 UGL_LOCAL void drawGL(void)
131 {
132 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
133
134 glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
135
136 /* draw blue sphere where the stencil is 1 */
137 glStencilFunc (GL_EQUAL, 0x1, 0x1);
138 glCallList (BLUEMAT);
139 glutSolidSphere (0.5, 15, 15);
140
141 /* draw the tori where the stencil is not 1 */
142 glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);
143 glPushMatrix();
144 glRotatef (45.0, 0.0, 0.0, 1.0);
145 glRotatef (45.0, 0.0, 1.0, 0.0);
146 glCallList (YELLOWMAT);
147 glutSolidTorus (0.275, 0.85, 15, 15);
148 glPushMatrix();
149 glRotatef (90.0, 1.0, 0.0, 0.0);
150 glutSolidTorus (0.275, 0.85, 15, 15);
151 glPopMatrix();
152 glPopMatrix();
153
154 glFlush();
155
156 uglMesaSwapBuffers();
157 }
158
159 UGL_LOCAL int getEvent(void)
160 {
161 UGL_EVENT event;
162 UGL_STATUS status;
163 int retVal = 0;
164
165 status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
166
167 while (status != UGL_STATUS_Q_EMPTY)
168 {
169 UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event;
170
171 if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
172 retVal = 1;
173
174 status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
175 }
176
177 return(retVal);
178 }
179
180 void windMLStencil (void);
181
182 void uglstencil (void)
183 {
184 taskSpawn("tStencil", 210, VX_FP_TASK, 100000,
185 (FUNCPTR)windMLStencil, 0,1,2,3,4,5,6,7,8,9);
186 }
187
188 void windMLStencil(void)
189 {
190 UGL_INPUT_DEVICE_ID keyboardDevId;
191 GLsizei width, height;
192
193 uglInitialize();
194
195 uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);
196
197 uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0, (UGL_UINT32 *)&eventServiceId);
198
199 qId = uglEventQCreate (eventServiceId, 100);
200
201 umc = uglMesaCreateNewContextExt(GL_FALSE,
202 16,
203 8,
204 0,0,0,0,
205 NULL);
206 if (umc == NULL)
207 {
208 uglDeinitialize();
209 return;
210 }
211
212 /* Fullscreen */
213
214 uglMesaMakeCurrentContext(umc, 0, 0, UGL_MESA_FULLSCREEN_WIDTH,
215 UGL_MESA_FULLSCREEN_HEIGHT);
216
217 uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
218 uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
219
220 initGL(width, height);
221
222 drawGL();
223
224 while (!getEvent());
225
226 uglEventQDestroy (eventServiceId, qId);
227
228 uglMesaDestroyContext();
229 uglDeinitialize();
230
231 return;
232 }
233