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