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