Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / progs / tests / multipal.c
1
2 /*
3 * Test multitexture and paletted textures.
4 */
5
6 #include <assert.h>
7 #include <math.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #ifdef __VMS
12 # include <stddef.h> /* for ptrdiff_t, referenced by GL.h when GL_GLEXT_LEGACY defined */
13 #else
14 # include <malloc.h> /* for ptrdiff_t, referenced by GL.h when GL_GLEXT_LEGACY defined */
15 #endif
16 #ifdef _WIN32
17 #include <windows.h>
18 #endif
19 #define GL_GLEXT_LEGACY
20 #include <GL/glut.h>
21
22 #include "../util/readtex.c" /* I know, this is a hack. */
23
24 #define TEXTURE_1_FILE "../images/tile.rgb"
25 #define TEXTURE_2_FILE "../images/reflect.rgb"
26
27 #define TEX0 1
28 #define TEX1 2
29 #define TEXBOTH 3
30 #define ANIMATE 10
31 #define QUIT 100
32
33 static GLboolean Animate = GL_TRUE;
34
35 static GLfloat Drift = 0.0;
36 static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
37
38
39
40 static void Idle( void )
41 {
42 if (Animate) {
43 Drift += 0.05;
44 if (Drift >= 1.0)
45 Drift = 0.0;
46
47 #ifdef GL_ARB_multitexture
48 glActiveTextureARB(GL_TEXTURE0_ARB);
49 #endif
50 glMatrixMode(GL_TEXTURE);
51 glLoadIdentity();
52 glTranslatef(Drift, 0.0, 0.0);
53 glMatrixMode(GL_MODELVIEW);
54
55 #ifdef GL_ARB_multitexture
56 glActiveTextureARB(GL_TEXTURE1_ARB);
57 #endif
58 glMatrixMode(GL_TEXTURE);
59 glLoadIdentity();
60 glTranslatef(0.0, Drift, 0.0);
61 glMatrixMode(GL_MODELVIEW);
62
63 glutPostRedisplay();
64 }
65 }
66
67
68 static void DrawObject(void)
69 {
70 glBegin(GL_QUADS);
71
72 #ifdef GL_ARB_multitexture
73 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
74 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
75 glVertex2f(-1.0, -1.0);
76
77 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 0.0);
78 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
79 glVertex2f(1.0, -1.0);
80
81 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 2.0);
82 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
83 glVertex2f(1.0, 1.0);
84
85 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 2.0);
86 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
87 glVertex2f(-1.0, 1.0);
88 #else
89 glTexCoord2f(0.0, 0.0);
90 glVertex2f(-1.0, -1.0);
91
92 glTexCoord2f(1.0, 0.0);
93 glVertex2f(1.0, -1.0);
94
95 glTexCoord2f(1.0, 1.0);
96 glVertex2f(1.0, 1.0);
97
98 glTexCoord2f(0.0, 1.0);
99 glVertex2f(-1.0, 1.0);
100 #endif
101
102 glEnd();
103 }
104
105
106
107 static void Display( void )
108 {
109 glClear( GL_COLOR_BUFFER_BIT );
110
111 glPushMatrix();
112 glRotatef(Xrot, 1.0, 0.0, 0.0);
113 glRotatef(Yrot, 0.0, 1.0, 0.0);
114 glRotatef(Zrot, 0.0, 0.0, 1.0);
115 glScalef(5.0, 5.0, 5.0);
116 DrawObject();
117 glPopMatrix();
118
119 glutSwapBuffers();
120 }
121
122
123 static void Reshape( int width, int height )
124 {
125 glViewport( 0, 0, width, height );
126 glMatrixMode( GL_PROJECTION );
127 glLoadIdentity();
128 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
129 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
130 glMatrixMode( GL_MODELVIEW );
131 glLoadIdentity();
132 glTranslatef( 0.0, 0.0, -70.0 );
133 }
134
135
136 static void ModeMenu(int entry)
137 {
138 GLboolean enable0 = GL_FALSE, enable1 = GL_FALSE;
139 if (entry==TEX0) {
140 enable0 = GL_TRUE;
141 }
142 else if (entry==TEX1) {
143 enable1 = GL_TRUE;
144 }
145 else if (entry==TEXBOTH) {
146 enable0 = GL_TRUE;
147 enable1 = GL_TRUE;
148 }
149 else if (entry==ANIMATE) {
150 Animate = !Animate;
151 }
152 else if (entry==QUIT) {
153 exit(0);
154 }
155
156 if (entry != ANIMATE) {
157 #ifdef GL_ARB_multitexture
158 glActiveTextureARB(GL_TEXTURE0_ARB);
159 #endif
160 if (enable0) {
161 glEnable(GL_TEXTURE_2D);
162 }
163 else
164 glDisable(GL_TEXTURE_2D);
165
166 #ifdef GL_ARB_multitexture
167 glActiveTextureARB(GL_TEXTURE1_ARB);
168 #endif
169 if (enable1) {
170 glEnable(GL_TEXTURE_2D);
171 }
172 else
173 glDisable(GL_TEXTURE_2D);
174 }
175
176 glutPostRedisplay();
177 }
178
179
180 static void Key( unsigned char key, int x, int y )
181 {
182 (void) x;
183 (void) y;
184 switch (key) {
185 case 27:
186 exit(0);
187 break;
188 }
189 glutPostRedisplay();
190 }
191
192
193 static void SpecialKey( int key, int x, int y )
194 {
195 float step = 3.0;
196 (void) x;
197 (void) y;
198
199 switch (key) {
200 case GLUT_KEY_UP:
201 Xrot += step;
202 break;
203 case GLUT_KEY_DOWN:
204 Xrot -= step;
205 break;
206 case GLUT_KEY_LEFT:
207 Yrot += step;
208 break;
209 case GLUT_KEY_RIGHT:
210 Yrot -= step;
211 break;
212 }
213 glutPostRedisplay();
214 }
215
216
217 static void load_tex(const char *fname, int channel)
218 {
219 GLubyte *image;
220 GLenum format;
221 GLint w, h;
222 GLubyte *grayImage;
223 int i;
224 GLubyte table[256][4];
225
226 image = LoadRGBImage(fname, &w, &h, &format);
227 if (!image)
228 exit(1);
229
230 printf("%s %d x %d\n", fname, w, h);
231 grayImage = malloc(w * h * 1);
232 assert(grayImage);
233 for (i = 0; i < w * h; i++) {
234 int g = (image[i*3+0] + image[i*3+1] + image[i*3+2]) / 3;
235 assert(g < 256);
236 grayImage[i] = g;
237 }
238
239 glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX, w, h, 0, GL_COLOR_INDEX,
240 GL_UNSIGNED_BYTE, grayImage);
241
242 for (i = 0; i < 256; i++) {
243 table[i][0] = channel ? i : 0;
244 table[i][1] = i;
245 table[i][2] = channel ? 0 : i;
246 table[i][3] = 255;
247 }
248
249 glColorTableEXT(GL_TEXTURE_2D, /* target */
250 GL_RGBA, /* internal format */
251 256, /* table size */
252 GL_RGBA, /* table format */
253 GL_UNSIGNED_BYTE, /* table type */
254 table); /* the color table */
255
256 free(grayImage);
257 free(image);
258 }
259
260
261
262 static void Init( int argc, char *argv[] )
263 {
264 GLuint texObj[2];
265 GLint units;
266
267 if (!glutExtensionSupported("GL_ARB_multitexture")) {
268 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
269 exit(1);
270 }
271 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
272 printf("Sorry, GL_EXT_paletted_texture not supported by this renderer.\n");
273 exit(1);
274 }
275
276 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &units);
277 printf("%d texture units supported\n", units);
278
279 /* allocate two texture objects */
280 glGenTextures(2, texObj);
281
282 /* setup texture obj 0 */
283 glBindTexture(GL_TEXTURE_2D, texObj[0]);
284 #ifdef LINEAR_FILTER
285 /* linear filtering looks much nicer but is much slower for Mesa */
286 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
287 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
288 foo
289 #else
290 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
292 #endif
293
294 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
295
296 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
297
298 load_tex(TEXTURE_1_FILE, 0);
299 #if 0
300 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
301 printf("Error: couldn't load texture image\n");
302 exit(1);
303 }
304 #endif
305
306 /* setup texture obj 1 */
307 glBindTexture(GL_TEXTURE_2D, texObj[1]);
308 #ifdef LINEAR_FILTER
309 /* linear filtering looks much nicer but is much slower for Mesa */
310 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
311 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
312 foo
313 #else
314 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
315 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
316 #endif
317
318 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
319
320 load_tex(TEXTURE_2_FILE, 1);
321 #if 0
322 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
323 printf("Error: couldn't load texture image\n");
324 exit(1);
325 }
326 #endif
327
328 /* now bind the texture objects to the respective texture units */
329 #ifdef GL_ARB_multitexture
330 glActiveTextureARB(GL_TEXTURE0_ARB);
331 glBindTexture(GL_TEXTURE_2D, texObj[0]);
332 glActiveTextureARB(GL_TEXTURE1_ARB);
333 glBindTexture(GL_TEXTURE_2D, texObj[1]);
334 #endif
335
336 glShadeModel(GL_FLAT);
337 glClearColor(0.3, 0.3, 0.4, 1.0);
338
339 ModeMenu(TEXBOTH);
340
341 if (argc > 1 && strcmp(argv[1], "-info")==0) {
342 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
343 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
344 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
345 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
346 }
347 }
348
349
350 int main( int argc, char *argv[] )
351 {
352 glutInit( &argc, argv );
353 glutInitWindowSize( 300, 300 );
354 glutInitWindowPosition( 0, 0 );
355 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
356 glutCreateWindow(argv[0] );
357
358 Init( argc, argv );
359
360 glutReshapeFunc( Reshape );
361 glutKeyboardFunc( Key );
362 glutSpecialFunc( SpecialKey );
363 glutDisplayFunc( Display );
364 glutIdleFunc( Idle );
365
366 glutCreateMenu(ModeMenu);
367 glutAddMenuEntry("Texture 0", TEX0);
368 glutAddMenuEntry("Texture 1", TEX1);
369 glutAddMenuEntry("Multi-texture", TEXBOTH);
370 glutAddMenuEntry("Toggle Animation", ANIMATE);
371 glutAddMenuEntry("Quit", QUIT);
372 glutAttachMenu(GLUT_RIGHT_BUTTON);
373
374 glutMainLoop();
375 return 0;
376 }