2 /* ugltexcube.c - WindML/Mesa example program */
4 /* Copyright (C) 2001 by Wind River Systems, Inc */
7 * Mesa 3-D graphics library
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:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
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.
32 * Stephane Raimbault <stephane.raimbault@windriver.com>
45 #include <ugl/uglevent.h>
46 #include <ugl/uglinput.h>
47 #include <GL/uglmesa.h>
50 #define IMAGE_FILE "Mesa/windmldemos/wrs_logo.bmp"
52 UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId
;
53 UGL_LOCAL UGL_EVENT_Q_ID qId
;
54 UGL_LOCAL UGL_MESA_CONTEXT umc
;
56 UGL_LOCAL GLfloat xrot
, yrot
, zrot
;
57 UGL_LOCAL GLuint texture
[1];
58 UGL_LOCAL GLuint theTexCube
;
66 UGL_LOCAL
void cleanUp (void);
68 UGL_LOCAL GLboolean
imageLoad(char *filename
, TEX_IMAGE
* texImage
)
73 unsigned short int planes
;
74 unsigned short int bpp
;
77 if ((file
= fopen(filename
, "rb")) == NULL
)
79 printf("File Not Found : %s\n", filename
);
83 fseek(file
, 18, SEEK_CUR
);
85 if ((i
= fread(&texImage
->sizeX
, 4, 1, file
)) != 1)
87 printf("Error reading width from %s.\n", filename
);
91 printf("Width of %s: %lu\n", filename
, texImage
->sizeX
);
93 if ((i
= fread(&texImage
->sizeY
, 4, 1, file
)) != 1)
95 printf("Error reading height from %s.\n", filename
);
99 printf("Height of %s: %lu\n", filename
, texImage
->sizeY
);
100 size
= texImage
->sizeX
* texImage
->sizeY
* 3;
102 if ((fread(&planes
, 2, 1, file
)) != 1)
104 printf("Error reading planes from %s.\n", filename
);
110 printf("Planes from %s is not 1: %u\n", filename
, planes
);
114 if ((i
= fread(&bpp
, 2, 1, file
)) != 1)
116 printf("Error reading bpp from %s.\n", filename
);
122 printf("Bpp from %s is not 24: %u\n", filename
, bpp
);
126 fseek(file
, 24, SEEK_CUR
);
128 texImage
->data
= (char *) malloc(size
);
130 if (texImage
->data
== NULL
)
132 printf("Error allocating memory for color-corrected texImage data");
136 if ((i
= fread(texImage
->data
, size
, 1, file
)) != 1)
138 printf("Error reading texImage data from %s.\n", filename
);
139 free(texImage
->data
);
145 for (i
=0; i
<size
; i
+=3)
147 temp
= texImage
->data
[i
];
148 texImage
->data
[i
] = texImage
->data
[i
+ 2];
149 texImage
->data
[i
+ 2] = temp
;
158 UGL_LOCAL
void loadGLTexture()
160 TEX_IMAGE
* texImage
=NULL
;
162 texImage
= (TEX_IMAGE
*) malloc(sizeof(TEX_IMAGE
));
164 if (texImage
== NULL
)
166 printf("Error allocating space for image");
171 if (!imageLoad(IMAGE_FILE
, texImage
))
173 printf("Error allocating space for image data");
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
);
186 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
187 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
189 free(texImage
->data
);
193 UGL_LOCAL
void initGL(int width
, int height
)
196 /* Load the texture(s) */
199 /* Enable texture mapping */
200 glEnable(GL_TEXTURE_2D
);
202 /* Clear the background color to black */
203 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
205 glEnable(GL_CULL_FACE
);
207 /* Enables smooth color shading */
208 glShadeModel(GL_SMOOTH
);
210 /* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); */
211 /* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); */
213 theTexCube
= glGenLists(1);
214 glNewList(theTexCube
, GL_COMPILE
);
216 /* Choose the texture to use */
217 glBindTexture(GL_TEXTURE_2D
, texture
[0]);
219 /* Begin drawing a cube */
222 /* Front face (note that the texture's corners have to match the
225 /* Bottom left of the texture and quad */
226 glTexCoord2f(0.0f
, 0.0f
);
227 glVertex3f(-1.0f
, -1.0f
, 1.0f
);
229 /* Bottom Right Of The Texture and Quad */
230 glTexCoord2f(1.0f
, 0.0f
);
231 glVertex3f(1.0f
, -1.0f
, 1.0f
);
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
);
242 /* Bottom Right Of The Texture and Quad */
243 glTexCoord2f(1.0f
, 0.0f
);
244 glVertex3f(-1.0f
, -1.0f
, -1.0f
);
246 /* Top Right Of The Texture and Quad */
247 glTexCoord2f(1.0f
, 1.0f
);
248 glVertex3f(-1.0f
, 1.0f
, -1.0f
);
250 /* Top Left Of The Texture and Quad */
251 glTexCoord2f(0.0f
, 1.0f
);
252 glVertex3f(1.0f
, 1.0f
, -1.0f
);
254 /* Bottom Left Of The Texture and Quad */
255 glTexCoord2f(0.0f
, 0.0f
);
256 glVertex3f(1.0f
, -1.0f
, -1.0f
);
261 /* Top Left Of The Texture and Quad */
262 glTexCoord2f(0.0f
, 1.0f
);
263 glVertex3f(-1.0f
, 1.0f
, -1.0f
);
265 /* Bottom Left Of The Texture and Quad */
266 glTexCoord2f(0.0f
, 0.0f
);
267 glVertex3f(-1.0f
, 1.0f
, 1.0f
);
269 /* Bottom Right Of The Texture and Quad */
270 glTexCoord2f(1.0f
, 0.0f
);
271 glVertex3f(1.0f
, 1.0f
, 1.0f
);
273 /* Top Right Of The Texture and Quad */
274 glTexCoord2f(1.0f
, 1.0f
);
275 glVertex3f(1.0f
, 1.0f
, -1.0f
);
279 /* Top Right Of The Texture and Quad */
280 glTexCoord2f(1.0f
, 1.0f
);
281 glVertex3f(-1.0f
, -1.0f
, -1.0f
);
283 /* Top Left Of The Texture and Quad */
284 glTexCoord2f(0.0f
, 1.0f
);
285 glVertex3f(1.0f
, -1.0f
, -1.0f
);
287 /* Bottom Left Of The Texture and Quad */
288 glTexCoord2f(0.0f
, 0.0f
);
289 glVertex3f(1.0f
, -1.0f
, 1.0f
);
291 /* Bottom Right Of The Texture and Quad */
292 glTexCoord2f(1.0f
, 0.0f
);
293 glVertex3f(-1.0f
, -1.0f
, 1.0f
);
297 /* Bottom Right Of The Texture and Quad */
298 glTexCoord2f(1.0f
, 0.0f
);
299 glVertex3f(1.0f
, -1.0f
, -1.0f
);
301 /* Top Right Of The Texture and Quad */
302 glTexCoord2f(1.0f
, 1.0f
);
303 glVertex3f(1.0f
, 1.0f
, -1.0f
);
305 /* Top Left Of The Texture and Quad */
306 glTexCoord2f(0.0f
, 1.0f
);
307 glVertex3f(1.0f
, 1.0f
, 1.0f
);
309 /* Bottom Left Of The Texture and Quad */
310 glTexCoord2f(0.0f
, 0.0f
);
311 glVertex3f(1.0f
, -1.0f
, 1.0f
);
315 /* Bottom Left Of The Texture and Quad */
316 glTexCoord2f(0.0f
, 0.0f
);
317 glVertex3f(-1.0f
, -1.0f
, -1.0f
);
319 /* Bottom Right Of The Texture and Quad */
320 glTexCoord2f(1.0f
, 0.0f
);
321 glVertex3f(-1.0f
, -1.0f
, 1.0f
);
323 /* Top Right Of The Texture and Quad */
324 glTexCoord2f(1.0f
, 1.0f
);
325 glVertex3f(-1.0f
, 1.0f
, 1.0f
);
327 /* Top Left Of The Texture and Quad */
328 glTexCoord2f(0.0f
, 1.0f
);
329 glVertex3f(-1.0f
, 1.0f
, -1.0f
);
331 glEnd(); /* done with the polygon */
334 glMatrixMode(GL_PROJECTION
);
335 /* Reset the projection matrix */
337 /* Calculate the aspect ratio of the window */
338 gluPerspective(45.0f
, (GLfloat
) width
/ (GLfloat
) height
, 0.1f
, 100.0f
);
340 glMatrixMode(GL_MODELVIEW
);
344 UGL_LOCAL
void drawGL()
346 glClear(GL_COLOR_BUFFER_BIT
);
351 /* Move 8 units into the screen */
352 glTranslatef(0.0f
, 0.0f
, -8.0f
);
354 /* Rotate on the X axis */
355 glRotatef(xrot
, 1.0f
, 0.0f
, 0.0f
);
357 /* Rotate on the Y axis */
358 glRotatef(yrot
, 0.0f
, 1.0f
, 0.0f
);
360 /* Rotate On The Z Axis */
361 glRotatef(zrot
, 0.0f
, 0.0f
, 1.0f
);
363 glCallList(theTexCube
);
367 uglMesaSwapBuffers();
376 UGL_LOCAL
int getEvent(void)
382 status
= uglEventGet (qId
, &event
, sizeof (event
), UGL_NO_WAIT
);
384 while (status
!= UGL_STATUS_Q_EMPTY
)
386 UGL_INPUT_EVENT
* pInputEvent
= (UGL_INPUT_EVENT
*)&event
;
388 if (pInputEvent
->modifiers
& UGL_KEYBOARD_KEYDOWN
)
391 status
= uglEventGet (qId
, &event
, sizeof (event
), UGL_NO_WAIT
);
397 UGL_LOCAL
void cleanUp (void)
399 if (eventServiceId
!= UGL_NULL
)
400 uglEventQDestroy (eventServiceId
, qId
);
402 uglMesaDestroyContext();
406 void windMLTexCube (void);
408 void ugltexcube (void)
410 taskSpawn("tTexCube", 210, VX_FP_TASK
, 100000, (FUNCPTR
)windMLTexCube
,
411 0,1,2,3,4,5,6,7,8,9);
415 void windMLTexCube(void)
417 GLuint width
, height
;
418 UGL_INPUT_DEVICE_ID keyboardDevId
;
422 uglDriverFind (UGL_KEYBOARD_TYPE
, 0, (UGL_UINT32
*)&keyboardDevId
);
424 if (uglDriverFind (UGL_EVENT_SERVICE_TYPE
, 0,
425 (UGL_UINT32
*)&eventServiceId
) == UGL_STATUS_OK
)
427 qId
= uglEventQCreate (eventServiceId
, 100);
431 eventServiceId
= UGL_NULL
;
434 umc
= uglMesaCreateNewContext(UGL_MESA_DOUBLE
, NULL
);
442 uglMesaMakeCurrentContext(umc
, 0, 0,
443 UGL_MESA_FULLSCREEN_WIDTH
,
444 UGL_MESA_FULLSCREEN_HEIGHT
);
447 uglMesaGetIntegerv(UGL_MESA_WIDTH
, &width
);
448 uglMesaGetIntegerv(UGL_MESA_HEIGHT
, &height
);
450 initGL(width
, height
);