WindML updates (Stephane Raimbault)
[mesa.git] / progs / windml / ugltexcube.c
1
2 /* ugltexcube.c - WindML/Mesa example program */
3
4 /* Copyright (C) 2001 by Wind River Systems, Inc */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 3.5
9 *
10 * The MIT License
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 */
29
30 /*
31 * Authors:
32 * Stephane Raimbault <stephane.raimbault@windriver.com>
33 */
34
35 /*
36 DESCRIPTION
37 Draw a textured cube
38 */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <math.h>
43
44 #include <ugl/ugl.h>
45 #include <ugl/uglevent.h>
46 #include <ugl/uglinput.h>
47 #include <GL/uglmesa.h>
48 #include <GL/glu.h>
49
50 #include "../util/readtex.h"
51
52 #define IMAGE_FILE "Mesa/images/wrs_logo.rgb"
53
54 UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
55 UGL_LOCAL UGL_EVENT_Q_ID qId;
56 UGL_LOCAL UGL_MESA_CONTEXT umc;
57
58 UGL_LOCAL GLfloat xrot, yrot, zrot;
59 UGL_LOCAL GLuint texture[1];
60 UGL_LOCAL GLuint theTexCube;
61
62 typedef struct {
63 GLubyte *data;
64 int width, height;
65 GLenum format;
66 } TEX_IMAGE;
67
68 UGL_LOCAL void cleanUp (void);
69
70 UGL_LOCAL void loadGLTexture()
71 {
72 TEX_IMAGE * texImage=NULL;
73
74 texImage = (TEX_IMAGE *) malloc(sizeof(TEX_IMAGE));
75
76 if (texImage == NULL)
77 {
78 printf("Error allocating space for image");
79 cleanUp();
80 exit(1);
81 }
82
83 texImage->data = LoadRGBImage(IMAGE_FILE, &texImage->width,
84 &texImage->height, &texImage->format);
85 if (!texImage->data)
86 {
87 printf("Couldn't read %s\n", IMAGE_FILE);
88 free(texImage);
89 cleanUp();
90 exit(1);
91 }
92
93 /* Create Texture */
94 glGenTextures(1, &texture[0]);
95 glBindTexture(GL_TEXTURE_2D, texture[0]);
96 glTexImage2D(GL_TEXTURE_2D, 0, 3,
97 texImage->width, texImage->height,
98 0, GL_RGB, GL_UNSIGNED_BYTE, texImage->data);
99
100 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
102
103 free(texImage->data);
104 free(texImage);
105 }
106
107 UGL_LOCAL void initGL(int width, int height)
108 {
109
110 /* Load the texture(s) */
111 loadGLTexture();
112
113 /* Enable texture mapping */
114 glEnable(GL_TEXTURE_2D);
115
116 /* Clear the background color to black */
117 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
118
119 glEnable(GL_CULL_FACE);
120
121 /* Enables smooth color shading */
122 glShadeModel(GL_SMOOTH);
123
124 /* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); */
125 /* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); */
126
127 theTexCube = glGenLists(1);
128 glNewList(theTexCube, GL_COMPILE);
129
130 /* Choose the texture to use */
131 glBindTexture(GL_TEXTURE_2D, texture[0]);
132
133 /* Begin drawing a cube */
134 glBegin(GL_QUADS);
135
136 /* Front face (note that the texture's corners have to match the
137 quad's corners) */
138
139 /* Bottom left of the texture and quad */
140 glTexCoord2f(0.0f, 0.0f);
141 glVertex3f(-1.0f, -1.0f, 1.0f);
142
143 /* Bottom Right Of The Texture and Quad */
144 glTexCoord2f(1.0f, 0.0f);
145 glVertex3f(1.0f, -1.0f, 1.0f);
146
147 /* Top Right Of The Texture and Quad */
148 glTexCoord2f(1.0f, 1.0f);
149 glVertex3f(1.0f, 1.0f, 1.0f);
150 /* Top Left Of The Texture and Quad */
151 glTexCoord2f(0.0f, 1.0f);
152 glVertex3f(-1.0f, 1.0f, 1.0f);
153
154 /* Back Face */
155
156 /* Bottom Right Of The Texture and Quad */
157 glTexCoord2f(1.0f, 0.0f);
158 glVertex3f(-1.0f, -1.0f, -1.0f);
159
160 /* Top Right Of The Texture and Quad */
161 glTexCoord2f(1.0f, 1.0f);
162 glVertex3f(-1.0f, 1.0f, -1.0f);
163
164 /* Top Left Of The Texture and Quad */
165 glTexCoord2f(0.0f, 1.0f);
166 glVertex3f(1.0f, 1.0f, -1.0f);
167
168 /* Bottom Left Of The Texture and Quad */
169 glTexCoord2f(0.0f, 0.0f);
170 glVertex3f(1.0f, -1.0f, -1.0f);
171
172
173 /* Top Face */
174
175 /* Top Left Of The Texture and Quad */
176 glTexCoord2f(0.0f, 1.0f);
177 glVertex3f(-1.0f, 1.0f, -1.0f);
178
179 /* Bottom Left Of The Texture and Quad */
180 glTexCoord2f(0.0f, 0.0f);
181 glVertex3f(-1.0f, 1.0f, 1.0f);
182
183 /* Bottom Right Of The Texture and Quad */
184 glTexCoord2f(1.0f, 0.0f);
185 glVertex3f(1.0f, 1.0f, 1.0f);
186
187 /* Top Right Of The Texture and Quad */
188 glTexCoord2f(1.0f, 1.0f);
189 glVertex3f(1.0f, 1.0f, -1.0f);
190
191 /* Bottom Face */
192
193 /* Top Right Of The Texture and Quad */
194 glTexCoord2f(1.0f, 1.0f);
195 glVertex3f(-1.0f, -1.0f, -1.0f);
196
197 /* Top Left Of The Texture and Quad */
198 glTexCoord2f(0.0f, 1.0f);
199 glVertex3f(1.0f, -1.0f, -1.0f);
200
201 /* Bottom Left Of The Texture and Quad */
202 glTexCoord2f(0.0f, 0.0f);
203 glVertex3f(1.0f, -1.0f, 1.0f);
204
205 /* Bottom Right Of The Texture and Quad */
206 glTexCoord2f(1.0f, 0.0f);
207 glVertex3f(-1.0f, -1.0f, 1.0f);
208
209
210 /* Right face */
211 /* Bottom Right Of The Texture and Quad */
212 glTexCoord2f(1.0f, 0.0f);
213 glVertex3f(1.0f, -1.0f, -1.0f);
214
215 /* Top Right Of The Texture and Quad */
216 glTexCoord2f(1.0f, 1.0f);
217 glVertex3f(1.0f, 1.0f, -1.0f);
218
219 /* Top Left Of The Texture and Quad */
220 glTexCoord2f(0.0f, 1.0f);
221 glVertex3f(1.0f, 1.0f, 1.0f);
222
223 /* Bottom Left Of The Texture and Quad */
224 glTexCoord2f(0.0f, 0.0f);
225 glVertex3f(1.0f, -1.0f, 1.0f);
226
227
228 /* Left Face */
229 /* Bottom Left Of The Texture and Quad */
230 glTexCoord2f(0.0f, 0.0f);
231 glVertex3f(-1.0f, -1.0f, -1.0f);
232
233 /* Bottom Right Of The Texture and Quad */
234 glTexCoord2f(1.0f, 0.0f);
235 glVertex3f(-1.0f, -1.0f, 1.0f);
236
237 /* Top Right Of The Texture and Quad */
238 glTexCoord2f(1.0f, 1.0f);
239 glVertex3f(-1.0f, 1.0f, 1.0f);
240
241 /* Top Left Of The Texture and Quad */
242 glTexCoord2f(0.0f, 1.0f);
243 glVertex3f(-1.0f, 1.0f, -1.0f);
244
245 glEnd(); /* done with the polygon */
246 glEndList();
247
248 glDisable(GL_DITHER);
249 glMatrixMode(GL_PROJECTION);
250 /* Reset the projection matrix */
251 glLoadIdentity();
252 /* Calculate the aspect ratio of the window */
253 gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);
254
255 glMatrixMode(GL_MODELVIEW);
256 glLoadIdentity();
257 }
258
259 UGL_LOCAL void drawGL()
260 {
261 glClear(GL_COLOR_BUFFER_BIT);
262
263 /* Reset The View */
264 glPushMatrix();
265
266 /* Move 8 units into the screen */
267 glTranslatef(0.0f, 0.0f, -8.0f);
268
269 /* Rotate on the X axis */
270 glRotatef(xrot, 1.0f, 0.0f, 0.0f);
271
272 /* Rotate on the Y axis */
273 glRotatef(yrot, 0.0f, 1.0f, 0.0f);
274
275 /* Rotate On The Z Axis */
276 glRotatef(zrot, 0.0f, 0.0f, 1.0f);
277
278 glCallList(theTexCube);
279
280 glFlush();
281
282 uglMesaSwapBuffers();
283
284 glPopMatrix();
285
286 xrot += 1.6f;
287 yrot += 1.6f;
288 zrot += 1.6f;
289 }
290
291 UGL_LOCAL int getEvent(void)
292 {
293 UGL_EVENT event;
294 UGL_STATUS status;
295 int retVal = 0;
296
297 status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
298
299 while (status != UGL_STATUS_Q_EMPTY)
300 {
301 UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event;
302
303 if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
304 retVal = 1;
305
306 status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
307 }
308
309 return(retVal);
310 }
311
312 UGL_LOCAL void cleanUp (void)
313 {
314 if (eventServiceId != UGL_NULL)
315 uglEventQDestroy (eventServiceId, qId);
316
317 uglMesaDestroyContext();
318 uglDeinitialize();
319 }
320
321 void windMLTexCube (UGL_BOOL windMLMode);
322
323 void ugltexcube (void)
324 {
325 taskSpawn("tTexCube", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLTexCube,
326 UGL_FALSE,1,2,3,4,5,6,7,8,9);
327 }
328
329
330 void windMLTexCube(UGL_BOOL windMLMode)
331 {
332 GLuint width, height;
333 UGL_INPUT_DEVICE_ID keyboardDevId;
334
335 uglInitialize();
336
337 uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);
338
339 if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0,
340 (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK)
341 {
342 qId = uglEventQCreate (eventServiceId, 100);
343 }
344 else
345 {
346 eventServiceId = UGL_NULL;
347 }
348
349 if (windMLMode)
350 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE
351 | UGL_MESA_WINDML_EXCLUSIVE, NULL);
352 else
353 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
354
355 if (umc == NULL)
356 {
357 uglDeinitialize();
358 return;
359 }
360
361 uglMesaMakeCurrentContext(umc, 0, 0,
362 UGL_MESA_FULLSCREEN_WIDTH,
363 UGL_MESA_FULLSCREEN_HEIGHT);
364
365
366 uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
367 uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
368
369 initGL(width, height);
370
371 while(!getEvent())
372 drawGL();
373
374 cleanUp();
375
376 return;
377 }
378
379