WIP - port to Mali EGL
[mesa-demos/mali.git] / src / samples / texture.c
blobb7c04eec3a59e25e8dae7642bb704cfcf41d6b92
1 /*
2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that (i) the above copyright notices and this permission notice appear in
7 * all copies of the software and related documentation, and (ii) the name of
8 * Silicon Graphics may not be used in any advertising or
9 * publicity relating to the software without the specific, prior written
10 * permission of Silicon Graphics.
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
13 * ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
25 #include <stdio.h>
26 #include <string.h>
27 #include <math.h>
28 #include <stdlib.h>
29 #include "glut_wrap.h"
32 #include "loadppm.c"
34 GLenum doubleBuffer;
36 char *texFileName = 0;
37 PPMImage *image;
39 float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
40 float decal[] = {GL_DECAL};
41 float modulate[] = {GL_MODULATE};
42 float repeat[] = {GL_REPEAT};
43 float clamp[] = {GL_CLAMP};
44 float nr[] = {GL_NEAREST};
45 float ln[] = {GL_LINEAR};
46 float nr_mipmap_nr[] = {GL_NEAREST_MIPMAP_NEAREST};
47 float nr_mipmap_ln[] = {GL_NEAREST_MIPMAP_LINEAR};
48 float ln_mipmap_nr[] = {GL_LINEAR_MIPMAP_NEAREST};
49 float ln_mipmap_ln[] = {GL_LINEAR_MIPMAP_LINEAR};
50 GLint sphereMap[] = {GL_SPHERE_MAP};
52 GLenum doSphere = GL_FALSE;
53 float xRotation = 0.0, yRotation = 0.0, zTranslate = -3.125;
55 GLint cube;
56 float c[6][4][3] = {
59 1.0, 1.0, -1.0
62 -1.0, 1.0, -1.0
65 -1.0, -1.0, -1.0
68 1.0, -1.0, -1.0
73 1.0, 1.0, 1.0
76 1.0, 1.0, -1.0
79 1.0, -1.0, -1.0
82 1.0, -1.0, 1.0
87 -1.0, 1.0, 1.0
90 1.0, 1.0, 1.0
93 1.0, -1.0, 1.0
96 -1.0, -1.0, 1.0
101 -1.0, 1.0, -1.0
104 -1.0, 1.0, 1.0
107 -1.0, -1.0, 1.0
110 -1.0, -1.0, -1.0
115 -1.0, 1.0, 1.0
118 -1.0, 1.0, -1.0
121 1.0, 1.0, -1.0
124 1.0, 1.0, 1.0
129 -1.0, -1.0, -1.0
132 -1.0, -1.0, 1.0
135 1.0, -1.0, 1.0
138 1.0, -1.0, -1.0
142 static float n[6][3] = {
144 0.0, 0.0, -1.0
147 1.0, 0.0, 0.0
150 0.0, 0.0, 1.0
153 -1.0, 0.0, 0.0
156 0.0, 1.0, 0.0
159 0.0, -1.0, 0.0
162 static float t[6][4][2] = {
165 1.1, 1.1
168 -0.1, 1.1
171 -0.1, -0.1
174 1.1, -0.1
179 1.1, 1.1
182 -0.1, 1.1
185 -0.1, -0.1
188 1.1, -0.1
193 -0.1, 1.1
196 1.1, 1.1
199 1.1, -0.1
202 -0.1, -0.1
207 1.1, 1.1
210 -0.1, 1.1
213 -0.1, -0.1
216 1.1, -0.1
221 1.1, 1.1
224 -0.1, 1.1
227 -0.1, -0.1
230 1.1, -0.1
235 1.1, 1.1
238 -0.1, 1.1
241 -0.1, -0.1
244 1.1, -0.1
249 static void BuildCube(void)
251 GLint i;
253 glNewList(cube, GL_COMPILE);
254 for (i = 0; i < 6; i++) {
255 glBegin(GL_POLYGON);
256 glNormal3fv(n[i]); glTexCoord2fv(t[i][0]); glVertex3fv(c[i][0]);
257 glNormal3fv(n[i]); glTexCoord2fv(t[i][1]); glVertex3fv(c[i][1]);
258 glNormal3fv(n[i]); glTexCoord2fv(t[i][2]); glVertex3fv(c[i][2]);
259 glNormal3fv(n[i]); glTexCoord2fv(t[i][3]); glVertex3fv(c[i][3]);
260 glEnd();
262 glEndList();
265 static void BuildLists(void)
268 cube = glGenLists(1);
269 BuildCube();
272 static void Init(void)
275 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
276 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY,
277 GL_RGB, GL_UNSIGNED_BYTE, image->data);
278 glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
279 glEnable(GL_TEXTURE_2D);
281 glFrontFace(GL_CCW);
282 glCullFace(GL_FRONT);
283 glEnable(GL_CULL_FACE);
285 BuildLists();
287 glClearColor(0.0, 0.0, 0.0, 0.0);
289 magFilter = nr;
290 minFilter = nr;
291 sWrapMode = repeat;
292 tWrapMode = repeat;
295 static void Reshape(int width, int height)
298 glViewport(0, 0, (GLint)width, (GLint)height);
300 glMatrixMode(GL_PROJECTION);
301 glLoadIdentity();
302 gluPerspective(145.0, 1.0, 0.01, 1000);
303 glMatrixMode(GL_MODELVIEW);
306 static void Key2(int key, int x, int y)
309 switch (key) {
310 case GLUT_KEY_LEFT:
311 yRotation -= 0.5;
312 break;
313 case GLUT_KEY_RIGHT:
314 yRotation += 0.5;
315 break;
316 case GLUT_KEY_UP:
317 xRotation -= 0.5;
318 break;
319 case GLUT_KEY_DOWN:
320 xRotation += 0.5;
321 break;
322 default:
323 return;
326 glutPostRedisplay();
329 static void Key(unsigned char key, int x, int y)
332 switch (key) {
333 case 27:
334 exit(1);
336 case 'T':
337 zTranslate += 0.25;
338 break;
339 case 't':
340 zTranslate -= 0.25;
341 break;
343 case 's':
344 doSphere = !doSphere;
345 if (doSphere) {
346 glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
347 glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
348 glEnable(GL_TEXTURE_GEN_S);
349 glEnable(GL_TEXTURE_GEN_T);
350 } else {
351 glDisable(GL_TEXTURE_GEN_S);
352 glDisable(GL_TEXTURE_GEN_T);
354 break;
356 case '0':
357 magFilter = nr;
358 break;
359 case '1':
360 magFilter = ln;
361 break;
362 case '2':
363 minFilter = nr;
364 break;
365 case '3':
366 minFilter = ln;
367 break;
368 case '4':
369 minFilter = nr_mipmap_nr;
370 break;
371 case '5':
372 minFilter = nr_mipmap_ln;
373 break;
374 case '6':
375 minFilter = ln_mipmap_nr;
376 break;
377 case '7':
378 minFilter = ln_mipmap_ln;
379 break;
380 default:
381 return;
384 glutPostRedisplay();
387 static void Draw(void)
390 glClear(GL_COLOR_BUFFER_BIT);
392 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
393 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
394 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
395 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
397 glPushMatrix();
399 glTranslatef(0.0, 0.0, zTranslate);
400 glRotatef(xRotation, 1, 0, 0);
401 glRotatef(yRotation, 0, 1, 0);
402 glCallList(cube);
404 glPopMatrix();
406 glFlush();
408 if (doubleBuffer) {
409 glutSwapBuffers();
413 static GLenum Args(int argc, char **argv)
415 GLint i;
417 doubleBuffer = GL_FALSE;
419 for (i = 1; i < argc; i++) {
420 if (strcmp(argv[i], "-sb") == 0) {
421 doubleBuffer = GL_FALSE;
422 } else if (strcmp(argv[i], "-db") == 0) {
423 doubleBuffer = GL_TRUE;
424 } else if (strcmp(argv[i], "-f") == 0) {
425 if (i+1 >= argc || argv[i+1][0] == '-') {
426 printf("-f (No file name).\n");
427 return GL_FALSE;
428 } else {
429 texFileName = argv[++i];
431 } else {
432 printf("%s (Bad option).\n", argv[i]);
433 return GL_FALSE;
436 return GL_TRUE;
439 int main(int argc, char **argv)
441 GLenum type;
443 glutInit(&argc, argv);
445 if (Args(argc, argv) == GL_FALSE) {
446 exit(1);
449 if (texFileName == 0) {
450 printf("No image file.\n");
451 exit(1);
454 image = LoadPPM(texFileName);
456 glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
458 type = GLUT_RGB;
459 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
460 glutInitDisplayMode(type);
462 if (glutCreateWindow("Texture Test") == GL_FALSE) {
463 exit(1);
466 Init();
468 glutReshapeFunc(Reshape);
469 glutKeyboardFunc(Key);
470 glutSpecialFunc(Key2);
471 glutDisplayFunc(Draw);
472 glutMainLoop();
473 return 0;