New visual studio project fiile for building demos.
[mesa.git] / progs / windml / uglicotorus.c
1 /* uglicotorus.c - WindML/Mesa example program */
2
3 /* Copyright (C) 2001 by Wind River Systems, Inc */
4
5 /*
6 * Mesa 3-D graphics library
7 * Version: 3.5
8 *
9 * The MIT License
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 */
28
29 /*
30 modification history
31 --------------------
32 01a,jun01,sra
33 */
34
35 #include <stdio.h>
36 #include <math.h>
37
38 #include <ugl/uglevent.h>
39 #include <ugl/uglinput.h>
40 #include <ugl/uglucode.h>
41
42 #include <GL/uglmesa.h>
43 #include <GL/glu.h>
44
45 /* Need GLUT_SHAPES */
46
47 #include <GL/uglglutshapes.h>
48
49 UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
50 UGL_LOCAL UGL_EVENT_Q_ID qId;
51 UGL_LOCAL UGL_MESA_CONTEXT umc;
52 UGL_LOCAL volatile UGL_BOOL stopWex;
53
54 UGL_LOCAL GLfloat angle;
55 UGL_LOCAL GLboolean chaos_on;
56 UGL_LOCAL GLboolean color_on;
57
58 UGL_LOCAL GLuint theIco, theTorus, theSphere, theCube;
59
60 UGL_LOCAL void initGL
61 (
62 int w,
63 int h
64 )
65 {
66 glViewport(0,0,(GLsizei)w,(GLsizei)h);
67 glMatrixMode(GL_PROJECTION);
68 glLoadIdentity();
69 gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,60.0);
70
71 glMatrixMode(GL_MODELVIEW);
72 glLoadIdentity();
73 gluLookAt(0.0,0.0,25.0,0.0,0.0,0.0,0.0,1.0,0.0);
74
75 glClearColor(0.0,0.0,0.0,0.0);
76
77 glEnable(GL_DEPTH_TEST);
78 glEnable(GL_LIGHTING);
79 glEnable(GL_LIGHT0);
80
81 glEnable(GL_COLOR_MATERIAL);
82
83 theIco = glGenLists(1);
84 glNewList(theIco, GL_COMPILE);
85 glutSolidIcosahedron();
86 glEndList();
87
88 theTorus = glGenLists(1);
89 glNewList(theTorus, GL_COMPILE);
90 glutSolidTorus(0.2,1.0,10,10);
91 glEndList();
92
93 theSphere = glGenLists(1);
94 glNewList(theSphere, GL_COMPILE);
95 glutSolidSphere(2.5,20,20);
96 glEndList();
97
98 theCube = glGenLists(1);
99 glNewList(theCube, GL_COMPILE);
100 glutSolidCube(4.0);
101 glEndList();
102
103 }
104
105 UGL_LOCAL void createIcoToruses
106 (
107 int i
108 )
109 {
110 glPushMatrix();
111 glRotatef(angle,1.0,1.0,1.0);
112 glCallList(theIco);
113
114 switch (i)
115 {
116 case 9 :
117 glColor3f(1.0,0.0,0.0);
118 break;
119 case 0 :
120 glColor3f(1.0,0.1,0.7);
121 break;
122 case 1 :
123 glColor3f(1.0,0.0,1.0);
124 break;
125 case 2 :
126 glColor3f(0.0,0.0,1.0);
127 break;
128 case 3 :
129 glColor3f(0.0,0.5,1.0);
130 break;
131 case 4 :
132 glColor3f(0.0,1.0,0.7);
133 break;
134 case 5 :
135 glColor3f(0.0,1.0,0.0);
136 break;
137 case 6 :
138 glColor3f(0.5,1.0,0.0);
139 break;
140 case 7 :
141 glColor3f(1.0,1.0,0.0);
142 break;
143 case 8 :
144 glColor3f(1.0,0.5,0.0);
145 break;
146 }
147
148 glRotatef(angle,1.0,1.0,1.0);
149 glCallList(theTorus);
150 glRotatef(-2*angle,1.0,1.0,1.0);
151 glCallList(theTorus);
152 glPopMatrix();
153 }
154
155 UGL_LOCAL void drawGL (void)
156 {
157 int i;
158
159 if (color_on)
160 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
161 else
162 glClear(GL_DEPTH_BUFFER_BIT);
163
164 glPushMatrix();
165
166 if (chaos_on)
167 glRotatef(angle,1.0,1.0,1.0);
168
169 glPushMatrix();
170 glRotatef(angle,1.0,1.0,1.0);
171 glColor3f(1.0,0.5,0.0);
172 glCallList(theSphere);
173 glColor3f(1.0,0.0,0.0);
174 glCallList(theCube);
175 glPopMatrix();
176
177 glRotatef(-angle,0.0,0.0,1.0);
178 glPushMatrix();
179 /* draw ten icosahedrons */
180 for (i = 0; i < 10; i++)
181 {
182 glPushMatrix();
183 glRotatef(36*i,0.0,0.0,1.0);
184 glTranslatef(10.0,0.0,0.0);
185 glRotatef(2*angle,0.0,1.0,0.0);
186 glTranslatef(0.0,0.0,2.0);
187
188 createIcoToruses(i);
189 glPopMatrix();
190 }
191 glPopMatrix();
192
193 glPopMatrix();
194
195 uglMesaSwapBuffers();
196
197 angle += 1.0;
198
199 }
200
201 UGL_LOCAL void echoUse(void)
202 {
203 printf("tIcoTorus keys:\n");
204 printf(" c Toggle color buffer clear\n");
205 printf(" SPACE Toggle chaos mode\n");
206 printf(" ESC Exit\n");
207 }
208
209 UGL_LOCAL void readKey (UGL_WCHAR key)
210 {
211
212 switch(key)
213 {
214 case 'c':
215 color_on = !color_on;
216 break;
217 case UGL_UNI_SPACE:
218 chaos_on = !chaos_on;
219 break;
220 case UGL_UNI_ESCAPE:
221 stopWex = UGL_TRUE;
222 break;
223 }
224 }
225
226 UGL_LOCAL void loopEvent(void)
227 {
228 UGL_EVENT event;
229 UGL_INPUT_EVENT * pInputEvent;
230
231 UGL_FOREVER
232 {
233 if (uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT)
234 != UGL_STATUS_Q_EMPTY)
235 {
236 pInputEvent = (UGL_INPUT_EVENT *)&event;
237
238 if (pInputEvent->header.type == UGL_EVENT_TYPE_KEYBOARD &&
239 pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
240 readKey(pInputEvent->type.keyboard.key);
241 }
242
243 drawGL();
244 if (stopWex)
245 break;
246 }
247 }
248
249 void windMLIcoTorus (UGL_BOOL windMLMode);
250
251 void uglicotorus (void)
252 {
253 taskSpawn ("tIcoTorus", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLIcoTorus,
254 UGL_FALSE,1,2,3,4,5,6,7,8,9);
255 }
256
257 void windMLIcoTorus (UGL_BOOL windMLMode)
258 {
259 GLsizei width, height;
260 UGL_INPUT_DEVICE_ID keyboardDevId;
261
262 angle = 0.0;
263 chaos_on = GL_TRUE;
264 color_on = GL_TRUE;
265
266 uglInitialize ();
267
268 uglDriverFind (UGL_KEYBOARD_TYPE, 0,
269 (UGL_UINT32 *)&keyboardDevId);
270
271 if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0,
272 (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK)
273 {
274 qId = uglEventQCreate (eventServiceId, 100);
275 }
276 else
277 {
278 eventServiceId = UGL_NULL;
279 }
280
281 /* Double buffering */
282 if (windMLMode)
283 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE
284 | UGL_MESA_WINDML_EXCLUSIVE, NULL);
285 else
286 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
287
288 if (umc == NULL)
289 {
290 uglDeinitialize ();
291 return;
292 }
293
294 uglMesaMakeCurrentContext (umc, 0, 0, UGL_MESA_FULLSCREEN_WIDTH,
295 UGL_MESA_FULLSCREEN_HEIGHT);
296
297 uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
298 uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
299
300 initGL (width, height);
301
302 echoUse();
303
304 stopWex = UGL_FALSE;
305 loopEvent();
306
307 if (eventServiceId != UGL_NULL)
308 uglEventQDestroy (eventServiceId, qId);
309
310 uglMesaDestroyContext ();
311 uglDeinitialize ();
312
313 return;
314 }
315