WindML driver (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 #define IMAGE_FILE "Mesa/windmldemos/wrs_logo.bmp"
51
52 UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
53 UGL_LOCAL UGL_EVENT_Q_ID qId;
54 UGL_LOCAL UGL_MESA_CONTEXT umc;
55
56 UGL_LOCAL GLfloat xrot, yrot, zrot;
57 UGL_LOCAL GLuint texture[1];
58 UGL_LOCAL GLuint theTexCube;
59
60 typedef struct {
61 unsigned long sizeX;
62 unsigned long sizeY;
63 char *data;
64 } TEX_IMAGE;
65
66 UGL_LOCAL void cleanUp (void);
67
68 UGL_LOCAL GLboolean imageLoad(char *filename, TEX_IMAGE * texImage)
69 {
70 FILE * file = NULL;
71 unsigned long size;
72 unsigned long i;
73 unsigned short int planes;
74 unsigned short int bpp;
75 char temp;
76
77 if ((file = fopen(filename, "rb")) == NULL)
78 {
79 printf("File Not Found : %s\n", filename);
80 return GL_FALSE;
81 }
82
83 fseek(file, 18, SEEK_CUR);
84
85 if ((i = fread(&texImage->sizeX, 4, 1, file)) != 1)
86 {
87 printf("Error reading width from %s.\n", filename);
88 return GL_FALSE;
89 }
90
91 printf("Width of %s: %lu\n", filename, texImage->sizeX);
92
93 if ((i = fread(&texImage->sizeY, 4, 1, file)) != 1)
94 {
95 printf("Error reading height from %s.\n", filename);
96 return GL_FALSE;
97 }
98
99 printf("Height of %s: %lu\n", filename, texImage->sizeY);
100 size = texImage->sizeX * texImage->sizeY * 3;
101
102 if ((fread(&planes, 2, 1, file)) != 1)
103 {
104 printf("Error reading planes from %s.\n", filename);
105 return GL_FALSE;
106 }
107
108 if (planes != 1)
109 {
110 printf("Planes from %s is not 1: %u\n", filename, planes);
111 return GL_FALSE;
112 }
113
114 if ((i = fread(&bpp, 2, 1, file)) != 1)
115 {
116 printf("Error reading bpp from %s.\n", filename);
117 return GL_FALSE;
118 }
119
120 if (bpp != 24)
121 {
122 printf("Bpp from %s is not 24: %u\n", filename, bpp);
123 return GL_FALSE;
124 }
125
126 fseek(file, 24, SEEK_CUR);
127
128 texImage->data = (char *) malloc(size);
129
130 if (texImage->data == NULL)
131 {
132 printf("Error allocating memory for color-corrected texImage data");
133 return GL_FALSE;
134 }
135
136 if ((i = fread(texImage->data, size, 1, file)) != 1)
137 {
138 printf("Error reading texImage data from %s.\n", filename);
139 free(texImage->data);
140 return GL_FALSE;
141 }
142
143 /* bgr -> rgb */
144
145 for (i=0; i<size; i+=3)
146 {
147 temp = texImage->data[i];
148 texImage->data[i] = texImage->data[i + 2];
149 texImage->data[i + 2] = temp;
150 }
151
152 fclose(file);
153
154 return GL_TRUE;
155 }
156
157
158 UGL_LOCAL void loadGLTexture()
159 {
160 TEX_IMAGE * texImage=NULL;
161
162 texImage = (TEX_IMAGE *) malloc(sizeof(TEX_IMAGE));
163
164 if (texImage == NULL)
165 {
166 printf("Error allocating space for image");
167 cleanUp();
168 exit(1);
169 }
170
171 if (!imageLoad(IMAGE_FILE, texImage))
172 {
173 printf("Error allocating space for image data");
174 free(texImage);
175 cleanUp();
176 exit(1);
177 }
178
179 /* Create Texture */
180 glGenTextures(1, &texture[0]);
181 glBindTexture(GL_TEXTURE_2D, texture[0]);
182 glTexImage2D(GL_TEXTURE_2D, 0, 3,
183 texImage->sizeX, texImage->sizeY,
184 0, GL_RGB, GL_UNSIGNED_BYTE, texImage->data);
185
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
188
189 free(texImage->data);
190 free(texImage);
191 }
192
193 UGL_LOCAL void initGL(int width, int height)
194 {
195
196 /* Load the texture(s) */
197 loadGLTexture();
198
199 /* Enable texture mapping */
200 glEnable(GL_TEXTURE_2D);
201
202 /* Clear the background color to black */
203 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
204
205 glEnable(GL_CULL_FACE);
206
207 /* Enables smooth color shading */
208 glShadeModel(GL_SMOOTH);
209
210 /* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); */
211 /* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); */
212
213 theTexCube = glGenLists(1);
214 glNewList(theTexCube, GL_COMPILE);
215
216 /* Choose the texture to use */
217 glBindTexture(GL_TEXTURE_2D, texture[0]);
218
219 /* Begin drawing a cube */
220 glBegin(GL_QUADS);
221
222 /* Front face (note that the texture's corners have to match the
223 quad's corners) */
224
225 /* Bottom left of the texture and quad */
226 glTexCoord2f(0.0f, 0.0f);
227 glVertex3f(-1.0f, -1.0f, 1.0f);
228
229 /* Bottom Right Of The Texture and Quad */
230 glTexCoord2f(1.0f, 0.0f);
231 glVertex3f(1.0f, -1.0f, 1.0f);
232
233 /* Top Right Of The Texture and Quad */
234 glTexCoord2f(1.0f, 1.0f);
235 glVertex3f(1.0f, 1.0f, 1.0f);
236 /* Top Left Of The Texture and Quad */
237 glTexCoord2f(0.0f, 1.0f);
238 glVertex3f(-1.0f, 1.0f, 1.0f);
239
240 /* Back Face */
241
242 /* Bottom Right Of The Texture and Quad */
243 glTexCoord2f(1.0f, 0.0f);
244 glVertex3f(-1.0f, -1.0f, -1.0f);
245
246 /* Top Right Of The Texture and Quad */
247 glTexCoord2f(1.0f, 1.0f);
248 glVertex3f(-1.0f, 1.0f, -1.0f);
249
250 /* Top Left Of The Texture and Quad */
251 glTexCoord2f(0.0f, 1.0f);
252 glVertex3f(1.0f, 1.0f, -1.0f);
253
254 /* Bottom Left Of The Texture and Quad */
255 glTexCoord2f(0.0f, 0.0f);
256 glVertex3f(1.0f, -1.0f, -1.0f);
257
258
259 /* Top Face */
260
261 /* Top Left Of The Texture and Quad */
262 glTexCoord2f(0.0f, 1.0f);
263 glVertex3f(-1.0f, 1.0f, -1.0f);
264
265 /* Bottom Left Of The Texture and Quad */
266 glTexCoord2f(0.0f, 0.0f);
267 glVertex3f(-1.0f, 1.0f, 1.0f);
268
269 /* Bottom Right Of The Texture and Quad */
270 glTexCoord2f(1.0f, 0.0f);
271 glVertex3f(1.0f, 1.0f, 1.0f);
272
273 /* Top Right Of The Texture and Quad */
274 glTexCoord2f(1.0f, 1.0f);
275 glVertex3f(1.0f, 1.0f, -1.0f);
276
277 /* Bottom Face */
278
279 /* Top Right Of The Texture and Quad */
280 glTexCoord2f(1.0f, 1.0f);
281 glVertex3f(-1.0f, -1.0f, -1.0f);
282
283 /* Top Left Of The Texture and Quad */
284 glTexCoord2f(0.0f, 1.0f);
285 glVertex3f(1.0f, -1.0f, -1.0f);
286
287 /* Bottom Left Of The Texture and Quad */
288 glTexCoord2f(0.0f, 0.0f);
289 glVertex3f(1.0f, -1.0f, 1.0f);
290
291 /* Bottom Right Of The Texture and Quad */
292 glTexCoord2f(1.0f, 0.0f);
293 glVertex3f(-1.0f, -1.0f, 1.0f);
294
295
296 /* Right face */
297 /* Bottom Right Of The Texture and Quad */
298 glTexCoord2f(1.0f, 0.0f);
299 glVertex3f(1.0f, -1.0f, -1.0f);
300
301 /* Top Right Of The Texture and Quad */
302 glTexCoord2f(1.0f, 1.0f);
303 glVertex3f(1.0f, 1.0f, -1.0f);
304
305 /* Top Left Of The Texture and Quad */
306 glTexCoord2f(0.0f, 1.0f);
307 glVertex3f(1.0f, 1.0f, 1.0f);
308
309 /* Bottom Left Of The Texture and Quad */
310 glTexCoord2f(0.0f, 0.0f);
311 glVertex3f(1.0f, -1.0f, 1.0f);
312
313
314 /* Left Face */
315 /* Bottom Left Of The Texture and Quad */
316 glTexCoord2f(0.0f, 0.0f);
317 glVertex3f(-1.0f, -1.0f, -1.0f);
318
319 /* Bottom Right Of The Texture and Quad */
320 glTexCoord2f(1.0f, 0.0f);
321 glVertex3f(-1.0f, -1.0f, 1.0f);
322
323 /* Top Right Of The Texture and Quad */
324 glTexCoord2f(1.0f, 1.0f);
325 glVertex3f(-1.0f, 1.0f, 1.0f);
326
327 /* Top Left Of The Texture and Quad */
328 glTexCoord2f(0.0f, 1.0f);
329 glVertex3f(-1.0f, 1.0f, -1.0f);
330
331 glEnd(); /* done with the polygon */
332 glEndList();
333
334 glMatrixMode(GL_PROJECTION);
335 /* Reset the projection matrix */
336 glLoadIdentity();
337 /* Calculate the aspect ratio of the window */
338 gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);
339
340 glMatrixMode(GL_MODELVIEW);
341 glLoadIdentity();
342 }
343
344 UGL_LOCAL void drawGL()
345 {
346 glClear(GL_COLOR_BUFFER_BIT);
347
348 /* Reset The View */
349 glPushMatrix();
350
351 /* Move 8 units into the screen */
352 glTranslatef(0.0f, 0.0f, -8.0f);
353
354 /* Rotate on the X axis */
355 glRotatef(xrot, 1.0f, 0.0f, 0.0f);
356
357 /* Rotate on the Y axis */
358 glRotatef(yrot, 0.0f, 1.0f, 0.0f);
359
360 /* Rotate On The Z Axis */
361 glRotatef(zrot, 0.0f, 0.0f, 1.0f);
362
363 glCallList(theTexCube);
364
365 glFlush();
366
367 uglMesaSwapBuffers();
368
369 glPopMatrix();
370
371 xrot += 1.6f;
372 yrot += 1.6f;
373 zrot += 1.6f;
374 }
375
376 UGL_LOCAL int getEvent(void)
377 {
378 UGL_EVENT event;
379 UGL_STATUS status;
380 int retVal = 0;
381
382 status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
383
384 while (status != UGL_STATUS_Q_EMPTY)
385 {
386 UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event;
387
388 if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
389 retVal = 1;
390
391 status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
392 }
393
394 return(retVal);
395 }
396
397 UGL_LOCAL void cleanUp (void)
398 {
399 if (eventServiceId != UGL_NULL)
400 uglEventQDestroy (eventServiceId, qId);
401
402 uglMesaDestroyContext();
403 uglDeinitialize();
404 }
405
406 void windMLTexCube (void);
407
408 void ugltexcube (void)
409 {
410 taskSpawn("tTexCube", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLTexCube,
411 0,1,2,3,4,5,6,7,8,9);
412 }
413
414
415 void windMLTexCube(void)
416 {
417 GLuint width, height;
418 UGL_INPUT_DEVICE_ID keyboardDevId;
419
420 uglInitialize();
421
422 uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);
423
424 if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0,
425 (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK)
426 {
427 qId = uglEventQCreate (eventServiceId, 100);
428 }
429 else
430 {
431 eventServiceId = UGL_NULL;
432 }
433
434 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
435
436 if (umc == NULL)
437 {
438 uglDeinitialize();
439 return;
440 }
441
442 uglMesaMakeCurrentContext(umc, 0, 0,
443 UGL_MESA_FULLSCREEN_WIDTH,
444 UGL_MESA_FULLSCREEN_HEIGHT);
445
446
447 uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
448 uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
449
450 initGL(width, height);
451
452 while(!getEvent())
453 drawGL();
454
455 cleanUp();
456
457 return;
458 }
459
460