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