summaryrefslogtreecommitdiffstats
path: root/progs/windml/ugltexcube.c
blob: 1ce6666b65af89abd577dc2036a3798f0d6b34b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460

/* ugltexcube.c - WindML/Mesa example program */

/* Copyright (C) 2001 by Wind River Systems, Inc */

/*
 * Mesa 3-D graphics library
 * Version:  3.5
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/*
 * Authors:
 * Stephane Raimbault <stephane.raimbault@windriver.com> 
 */

/*
DESCRIPTION
Draw a textured cube
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <ugl/ugl.h>
#include <ugl/uglevent.h>
#include <ugl/uglinput.h>
#include <GL/uglmesa.h>
#include <GL/glu.h>

#define IMAGE_FILE "Mesa/windmldemos/wrs_logo.bmp"

UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
UGL_LOCAL UGL_EVENT_Q_ID qId;
UGL_LOCAL UGL_MESA_CONTEXT umc;

UGL_LOCAL GLfloat xrot, yrot, zrot;
UGL_LOCAL GLuint texture[1];
UGL_LOCAL GLuint theTexCube;

typedef struct {
    unsigned long sizeX;
    unsigned long sizeY;
    char *data;
    } TEX_IMAGE;

UGL_LOCAL void cleanUp (void);

UGL_LOCAL GLboolean imageLoad(char *filename, TEX_IMAGE * texImage)
    {
    FILE * file = NULL;
    unsigned long size;
    unsigned long i;
    unsigned short int planes;
    unsigned short int bpp;
    char temp;		
    
    if ((file = fopen(filename, "rb")) == NULL)
	{
	printf("File Not Found : %s\n", filename);
	return GL_FALSE;
	}
    
    fseek(file, 18, SEEK_CUR);
    
    if ((i = fread(&texImage->sizeX, 4, 1, file)) != 1)
	{
	printf("Error reading width from %s.\n", filename);
	return GL_FALSE;
	}
    
    printf("Width of %s: %lu\n", filename, texImage->sizeX);
		    
    if ((i = fread(&texImage->sizeY, 4, 1, file)) != 1)
	{
	printf("Error reading height from %s.\n", filename);
	return GL_FALSE;
	}

    printf("Height of %s: %lu\n", filename, texImage->sizeY);
    size = texImage->sizeX * texImage->sizeY * 3;
    
    if ((fread(&planes, 2, 1, file)) != 1)
	{
	printf("Error reading planes from %s.\n", filename);
	return GL_FALSE;
	}
    
    if (planes != 1)
	{
	printf("Planes from %s is not 1: %u\n", filename, planes);
	return GL_FALSE;
	}
	
    if ((i = fread(&bpp, 2, 1, file)) != 1)
	{
	printf("Error reading bpp from %s.\n", filename);
	return GL_FALSE;
	}
    
    if (bpp != 24)
	{
	printf("Bpp from %s is not 24: %u\n", filename, bpp);
	return GL_FALSE;
	}
	
    fseek(file, 24, SEEK_CUR);
    
    texImage->data = (char *) malloc(size);

    if (texImage->data == NULL)
	{
	printf("Error allocating memory for color-corrected texImage data");
	return GL_FALSE;
	}
    
    if ((i = fread(texImage->data, size, 1, file)) != 1)
	{
	printf("Error reading texImage data from %s.\n", filename);
	free(texImage->data);
	return GL_FALSE;
	}
    
    /* bgr -> rgb */
    
    for (i=0; i<size; i+=3)
	{
	temp = texImage->data[i];
	texImage->data[i] = texImage->data[i + 2];
	texImage->data[i + 2] = temp;
	}

    fclose(file);
    
    return GL_TRUE;
    }


UGL_LOCAL void loadGLTexture()
    {
    TEX_IMAGE * texImage=NULL;

    texImage = (TEX_IMAGE *) malloc(sizeof(TEX_IMAGE));

    if (texImage == NULL)
	{
	printf("Error allocating space for image");
	cleanUp();
	exit(1);
	}
	
    if (!imageLoad(IMAGE_FILE, texImage))
	{
	printf("Error allocating space for image data");
	free(texImage);
	cleanUp();
	exit(1);
	}
	
    /* Create Texture */
    glGenTextures(1, &texture[0]);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, 3,
		 texImage->sizeX, texImage->sizeY,
		 0, GL_RGB, GL_UNSIGNED_BYTE, texImage->data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    free(texImage->data);
    free(texImage);
    }

UGL_LOCAL void initGL(int width, int height)
    {

    /* Load the texture(s) */
    loadGLTexture();

    /* Enable texture mapping */
    glEnable(GL_TEXTURE_2D);

    /* Clear the background color to black */
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glEnable(GL_CULL_FACE);
    
    /* Enables smooth color shading */
    glShadeModel(GL_SMOOTH);

/* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); */	 
/* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); */
 
    theTexCube = glGenLists(1);
    glNewList(theTexCube, GL_COMPILE);

    /* Choose the texture to use */
    glBindTexture(GL_TEXTURE_2D, texture[0]);

    /* Begin drawing a cube */
    glBegin(GL_QUADS);
        
    /* Front face (note that the texture's corners have to match the
       quad's corners) */

    /* Bottom left of the texture and quad */
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);

    /* Bottom Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);

    /* Top Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);
    /* Top Left Of The Texture and Quad	*/
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    
    /* Back Face */

    /* Bottom Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);

    /* Top Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);

    /* Top Left Of The Texture and Quad */
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);

    /* Bottom Left Of The Texture and Quad */
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    
    /* Top Face */

    /* Top Left Of The Texture and Quad */
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);

    /* Bottom Left Of The Texture and Quad */
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);

    /* Bottom Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    /* Top Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);
    
    /* Bottom Face */

    /* Top Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);

    /* Top Left Of The Texture and Quad */
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    /* Bottom Left Of The Texture and Quad */
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);

    /* Bottom Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
	
	
    /* Right face */
    /* Bottom Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    /* Top Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);

    /* Top Left Of The Texture and Quad */
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    /* Bottom Left Of The Texture and Quad */
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);
    
	
    /* Left Face */
    /* Bottom Left Of The Texture and Quad */
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);

    /* Bottom Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);

    /* Top Right Of The Texture and Quad */
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);

    /* Top Left Of The Texture and Quad */
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
						   
    glEnd();		/* done with the polygon */
    glEndList();
    
    glMatrixMode(GL_PROJECTION);
    /* Reset the projection matrix */
    glLoadIdentity();
    /* Calculate the aspect ratio of the window */
    gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

UGL_LOCAL void drawGL() 
    {
    glClear(GL_COLOR_BUFFER_BIT);

    /* Reset The View */
    glPushMatrix();
    
    /* Move 8 units into the screen */
    glTranslatef(0.0f, 0.0f, -8.0f);

    /* Rotate on the X axis */
    glRotatef(xrot, 1.0f, 0.0f, 0.0f);
    
    /* Rotate on the Y axis */
    glRotatef(yrot, 0.0f, 1.0f, 0.0f);

    /* Rotate On The Z Axis */
    glRotatef(zrot, 0.0f, 0.0f, 1.0f);

    glCallList(theTexCube);
    
    glFlush();
        
    uglMesaSwapBuffers();

    glPopMatrix();

    xrot += 1.6f;
    yrot += 1.6f;
    zrot += 1.6f;
} 

UGL_LOCAL int getEvent(void)
    {
    UGL_EVENT event;
    UGL_STATUS status;
    int retVal = 0;

    status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);

    while (status != UGL_STATUS_Q_EMPTY)
        {
	UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event;
	
	if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
	    retVal = 1;

	status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
        }
 
    return(retVal);
    }

UGL_LOCAL void cleanUp (void)
    {
    if (eventServiceId != UGL_NULL)
	uglEventQDestroy (eventServiceId, qId);

    uglMesaDestroyContext();
    uglDeinitialize();
    }

void windMLTexCube (void);

void ugltexcube (void)
    {
    taskSpawn("tTexCube", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLTexCube,
	      0,1,2,3,4,5,6,7,8,9);
    }


void windMLTexCube(void)
    {
    GLuint width, height;
    UGL_INPUT_DEVICE_ID keyboardDevId;
    
    uglInitialize();

    uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);

    if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0,
                       (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK)
        {
        qId = uglEventQCreate (eventServiceId, 100);
        }
    else 
        {
        eventServiceId = UGL_NULL;
        }

    umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);

    if (umc == NULL)
        {
	uglDeinitialize();
	return;
        }

    uglMesaMakeCurrentContext(umc, 0, 0,
			      UGL_MESA_FULLSCREEN_WIDTH,
			      UGL_MESA_FULLSCREEN_HEIGHT);
			      

    uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
    uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);

    initGL(width, height);

    while(!getEvent())
	drawGL();
        
    cleanUp();

    return;
    }