Merge commit 'origin/gallium-master-merge'
[mesa.git] / progs / demos / textures.c
1 /*
2 * Simple test of multiple textures
3 */
4
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <math.h>
9 #include <GL/glut.h>
10 #include "readtex.h"
11
12 #define TEST_CLAMP 0
13 #define TEST_MIPMAPS 0
14
15 #define MAX_TEXTURES 8
16
17
18 static int Win;
19 static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
20 static GLboolean Anim = GL_TRUE;
21 static GLboolean Blend = GL_FALSE;
22 static GLuint Filter = 0;
23 static GLboolean Clamp = GL_FALSE;
24
25 static GLuint NumTextures;
26 static GLuint Textures[MAX_TEXTURES];
27 static float TexRot[MAX_TEXTURES][3];
28 static float TexPos[MAX_TEXTURES][3];
29 static float TexAspect[MAX_TEXTURES];
30
31 static const char *DefaultFiles[] = {
32 "../images/arch.rgb",
33 "../images/reflect.rgb",
34 "../images/tree2.rgba",
35 "../images/tile.rgb"
36 };
37
38
39 #define NUM_FILTERS 5
40 static
41 struct filter {
42 GLenum min, mag;
43 const char *name;
44 } FilterModes[NUM_FILTERS] = {
45 { GL_NEAREST, GL_NEAREST, "Nearest,Nearest" },
46 { GL_LINEAR, GL_LINEAR, "Linear,Linear" },
47 { GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST, "NearestMipmapNearest,Nearest" },
48 { GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR, "LinearMipmapNearest,Linear" },
49 { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, "LinearMipmapLinear,Linear" }
50 };
51
52
53
54
55 static void
56 Idle(void)
57 {
58 Xrot = glutGet(GLUT_ELAPSED_TIME) * 0.02;
59 Yrot = glutGet(GLUT_ELAPSED_TIME) * 0.04;
60 //Zrot += 2.0;
61 glutPostRedisplay();
62 }
63
64
65 static void
66 DrawTextures(void)
67 {
68 GLuint i;
69
70 for (i = 0; i < NumTextures; i++) {
71 GLfloat ar = TexAspect[i];
72
73 glPushMatrix();
74 glTranslatef(TexPos[i][0], TexPos[i][1], TexPos[i][2]);
75 glRotatef(TexRot[i][0], 1, 0, 0);
76 glRotatef(TexRot[i][1], 0, 1, 0);
77 glRotatef(TexRot[i][2], 0, 0, 1);
78
79 glBindTexture(GL_TEXTURE_2D, Textures[i]);
80 glBegin(GL_POLYGON);
81 #if TEST_CLAMP
82 glTexCoord2f( -0.5, -0.5 ); glVertex2f( -ar, -1.0 );
83 glTexCoord2f( 1.5, -0.5 ); glVertex2f( ar, -1.0 );
84 glTexCoord2f( 1.5, 1.5 ); glVertex2f( ar, 1.0 );
85 glTexCoord2f( -0.5, 1.5 ); glVertex2f( -ar, 1.0 );
86 #else
87 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -ar, -1.0 );
88 glTexCoord2f( 1.0, 0.0 ); glVertex2f( ar, -1.0 );
89 glTexCoord2f( 1.0, 1.0 ); glVertex2f( ar, 1.0 );
90 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -ar, 1.0 );
91 #endif
92 glEnd();
93
94 glPopMatrix();
95 }
96 }
97
98 static void
99 Draw(void)
100 {
101 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
102
103 if (Blend) {
104 glEnable(GL_BLEND);
105 glDisable(GL_DEPTH_TEST);
106 }
107 else {
108 glDisable(GL_BLEND);
109 glEnable(GL_DEPTH_TEST);
110 }
111
112 glPushMatrix();
113 glRotatef(Xrot, 1, 0, 0);
114 glRotatef(Yrot, 0, 1, 0);
115 glRotatef(Zrot, 0, 0, 1);
116
117 DrawTextures();
118
119 glPopMatrix();
120
121 glutSwapBuffers();
122 }
123
124
125 static void
126 Reshape(int width, int height)
127 {
128 glViewport(0, 0, width, height);
129 glMatrixMode(GL_PROJECTION);
130 glLoadIdentity();
131 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 50.0);
132 glMatrixMode(GL_MODELVIEW);
133 glLoadIdentity();
134 glTranslatef(0.0, 0.0, -10.0);
135 }
136
137
138 static GLfloat
139 RandFloat(float min, float max)
140 {
141 float x = (float) (rand() % 1000) * 0.001;
142 x = x * (max - min) + min;
143 return x;
144 }
145
146
147 static void
148 Randomize(void)
149 {
150 GLfloat k = 1.0;
151 GLuint i;
152
153 srand(glutGet(GLUT_ELAPSED_TIME));
154
155 for (i = 0; i < NumTextures; i++) {
156 TexRot[i][0] = RandFloat(0.0, 360);
157 TexRot[i][1] = RandFloat(0.0, 360);
158 TexRot[i][2] = RandFloat(0.0, 360);
159 TexPos[i][0] = RandFloat(-k, k);
160 TexPos[i][1] = RandFloat(-k, k);
161 TexPos[i][2] = RandFloat(-k, k);
162 }
163 }
164
165
166 static void
167 SetTexParams(void)
168 {
169 GLuint i;
170 for (i = 0; i < NumTextures; i++) {
171 glBindTexture(GL_TEXTURE_2D, Textures[i]);
172 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
173 FilterModes[Filter].min);
174 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
175 FilterModes[Filter].mag);
176
177 if (Clamp) {
178 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
179 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
180 }
181 else {
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
184 }
185 }
186 }
187
188
189 static void
190 Key(unsigned char key, int x, int y)
191 {
192 const GLfloat step = 3.0;
193 (void) x;
194 (void) y;
195 switch (key) {
196 case 'a':
197 case ' ':
198 Anim = !Anim;
199 if (Anim)
200 glutIdleFunc(Idle);
201 else
202 glutIdleFunc(NULL);
203 break;
204 case 'b':
205 Blend = !Blend;
206 break;
207 case 'f':
208 Filter = (Filter + 1) % NUM_FILTERS;
209 SetTexParams();
210 break;
211 case 'r':
212 Randomize();
213 break;
214 #if TEST_CLAMP
215 case 'c':
216 Clamp = !Clamp;
217 SetTexParams();
218 break;
219 #endif
220 case 'z':
221 Zrot -= step;
222 break;
223 case 'Z':
224 Zrot += step;
225 break;
226 case 27:
227 glutDestroyWindow(Win);
228 exit(0);
229 break;
230 }
231
232 printf("Blend=%s Filter=%s\n",
233 Blend ? "Y" : "n",
234 FilterModes[Filter].name);
235
236 glutPostRedisplay();
237 }
238
239
240 static void
241 SpecialKey(int key, int x, int y)
242 {
243 const GLfloat step = 3.0;
244 (void) x;
245 (void) y;
246 switch (key) {
247 case GLUT_KEY_UP:
248 Xrot -= step;
249 break;
250 case GLUT_KEY_DOWN:
251 Xrot += step;
252 break;
253 case GLUT_KEY_LEFT:
254 Yrot -= step;
255 break;
256 case GLUT_KEY_RIGHT:
257 Yrot += step;
258 break;
259 }
260 glutPostRedisplay();
261 }
262
263
264 static void
265 LoadTextures(GLuint n, const char *files[])
266 {
267 GLuint i;
268
269 NumTextures = n < MAX_TEXTURES ? n : MAX_TEXTURES;
270
271 glGenTextures(n, Textures);
272
273 SetTexParams();
274
275 for (i = 0; i < n; i++) {
276 GLint w, h;
277 glBindTexture(GL_TEXTURE_2D, Textures[i]);
278 #if TEST_MIPMAPS
279 {
280 static const GLubyte color[9][4] = {
281 {255, 0, 0},
282 {0, 255, 0},
283 {0, 0, 255},
284 {0, 255, 255},
285 {255, 0, 255},
286 {255, 255, 0},
287 {255, 128, 255},
288 {128, 128, 128},
289 {64, 64, 64}
290 };
291
292 GLubyte image[256*256*4];
293 int i, level;
294 w = h = 256;
295 for (level = 0; level <= 8; level++) {
296 for (i = 0; i < w * h; i++) {
297 image[i*4+0] = color[level][0];
298 image[i*4+1] = color[level][1];
299 image[i*4+2] = color[level][2];
300 image[i*4+3] = color[level][3];
301 }
302 printf("Load level %d: %d x %d\n", level, w>>level, h>>level);
303 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w>>level, h>>level, 0,
304 GL_RGBA, GL_UNSIGNED_BYTE, image);
305 }
306 }
307 #else
308 if (!LoadRGBMipmaps2(files[i], GL_TEXTURE_2D, GL_RGB, &w, &h)) {
309 printf("Error: couldn't load %s\n", files[i]);
310 exit(1);
311 }
312 #endif
313 TexAspect[i] = (float) w / (float) h;
314 printf("Loaded %s\n", files[i]);
315 }
316 }
317
318
319 static void
320 Init(int argc, const char *argv[])
321 {
322 if (argc == 1)
323 LoadTextures(4, DefaultFiles);
324 else
325 LoadTextures(argc - 1, argv + 1);
326
327 Randomize();
328
329 glEnable(GL_TEXTURE_2D);
330
331 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
332 glColor4f(1, 1, 1, 0.5);
333
334 #if 0
335 /* setup lighting, etc */
336 glEnable(GL_LIGHTING);
337 glEnable(GL_LIGHT0);
338 #endif
339 }
340
341
342 static void
343 Usage(void)
344 {
345 printf("Usage:\n");
346 printf(" textures [file.rgb] ...\n");
347 printf("Keys:\n");
348 printf(" a - toggle animation\n");
349 printf(" b - toggle blending\n");
350 printf(" f - change texture filter mode\n");
351 printf(" r - randomize\n");
352 printf(" ESC - exit\n");
353 }
354
355
356 int
357 main(int argc, char *argv[])
358 {
359 glutInit(&argc, argv);
360 glutInitWindowPosition(0, 0);
361 glutInitWindowSize(700, 700);
362 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
363 Win = glutCreateWindow(argv[0]);
364 glutReshapeFunc(Reshape);
365 glutKeyboardFunc(Key);
366 glutSpecialFunc(SpecialKey);
367 glutDisplayFunc(Draw);
368 if (Anim)
369 glutIdleFunc(Idle);
370 Init(argc, (const char **) argv);
371 Usage();
372 glutMainLoop();
373 return 0;
374 }