added missing GL_MAX_TEXTURE_LOD_BIAS_EXT query
[mesa.git] / progs / demos / multiarb.c
1 /* $Id: multiarb.c,v 1.10 2001/06/20 19:12:30 brianp Exp $ */
2
3 /*
4 * GL_ARB_multitexture demo
5 *
6 * Command line options:
7 * -info print GL implementation information
8 *
9 *
10 * Brian Paul November 1998 This program is in the public domain.
11 */
12
13 /*
14 * $Log: multiarb.c,v $
15 * Revision 1.10 2001/06/20 19:12:30 brianp
16 * also print GL_MAX_TEXTURE_SIZE
17 *
18 * Revision 1.9 2001/06/13 14:33:16 brianp
19 * moved glTexEnvi calls to better logical locations
20 *
21 * Revision 1.8 2000/12/24 22:53:54 pesco
22 * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
23 * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
24 * Essentially the same.
25 * Program files updated to include "readtex.c", not "../util/readtex.c".
26 * * demos/reflect.c: Likewise for "showbuffer.c".
27 *
28 *
29 * * Makefile.am (EXTRA_DIST): Added top-level regular files.
30 *
31 * * include/GL/Makefile.am (INC_X11): Added glxext.h.
32 *
33 *
34 * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
35 * Mesa GGI headers in dist even if HAVE_GGI is not given.
36 *
37 * * configure.in: Look for GLUT and demo source dirs in $srcdir.
38 *
39 * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
40 * More source list updates in various Makefile.am's.
41 *
42 * * Makefile.am (dist-hook): Remove CVS directory from distribution.
43 * (DIST_SUBDIRS): List all possible subdirs here.
44 * (SUBDIRS): Only list subdirs selected for build again.
45 * The above two applied to all subdir Makefile.am's also.
46 *
47 * Revision 1.7 2000/11/01 16:02:01 brianp
48 * print number of texture units
49 *
50 * Revision 1.6 2000/05/23 23:21:00 brianp
51 * set default window pos
52 *
53 * Revision 1.5 2000/02/02 17:31:45 brianp
54 * changed > to >=
55 *
56 * Revision 1.4 2000/02/02 01:07:21 brianp
57 * limit Drift to [0, 1]
58 *
59 * Revision 1.3 1999/10/21 16:40:32 brianp
60 * added -info command line option
61 *
62 * Revision 1.2 1999/10/13 12:02:13 brianp
63 * use texture objects now
64 *
65 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
66 * Imported sources
67 *
68 * Revision 1.3 1999/03/28 18:20:49 brianp
69 * minor clean-up
70 *
71 * Revision 1.2 1998/11/05 04:34:04 brianp
72 * moved image files to ../images/ directory
73 *
74 * Revision 1.1 1998/11/03 01:36:33 brianp
75 * Initial revision
76 *
77 */
78
79
80 #include <math.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <GL/glut.h>
85
86 #include "readtex.c" /* I know, this is a hack. */
87
88 #define TEXTURE_1_FILE "../images/girl.rgb"
89 #define TEXTURE_2_FILE "../images/reflect.rgb"
90
91 #define TEX0 1
92 #define TEX1 2
93 #define TEXBOTH 3
94 #define ANIMATE 10
95 #define QUIT 100
96
97 static GLboolean Animate = GL_TRUE;
98
99 static GLfloat Drift = 0.0;
100 static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
101
102
103
104 static void Idle( void )
105 {
106 if (Animate) {
107 Drift += 0.05;
108 if (Drift >= 1.0)
109 Drift = 0.0;
110
111 #ifdef GL_ARB_multitexture
112 glActiveTextureARB(GL_TEXTURE0_ARB);
113 #endif
114 glMatrixMode(GL_TEXTURE);
115 glLoadIdentity();
116 glTranslatef(Drift, 0.0, 0.0);
117 glMatrixMode(GL_MODELVIEW);
118
119 #ifdef GL_ARB_multitexture
120 glActiveTextureARB(GL_TEXTURE1_ARB);
121 #endif
122 glMatrixMode(GL_TEXTURE);
123 glLoadIdentity();
124 glTranslatef(0.0, Drift, 0.0);
125 glMatrixMode(GL_MODELVIEW);
126
127 glutPostRedisplay();
128 }
129 }
130
131
132 static void DrawObject(void)
133 {
134 glBegin(GL_QUADS);
135
136 #ifdef GL_ARB_multitexture
137 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
138 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
139 glVertex2f(-1.0, -1.0);
140
141 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 0.0);
142 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
143 glVertex2f(1.0, -1.0);
144
145 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 2.0);
146 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
147 glVertex2f(1.0, 1.0);
148
149 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 2.0);
150 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
151 glVertex2f(-1.0, 1.0);
152 #else
153 glTexCoord2f(0.0, 0.0);
154 glVertex2f(-1.0, -1.0);
155
156 glTexCoord2f(1.0, 0.0);
157 glVertex2f(1.0, -1.0);
158
159 glTexCoord2f(1.0, 1.0);
160 glVertex2f(1.0, 1.0);
161
162 glTexCoord2f(0.0, 1.0);
163 glVertex2f(-1.0, 1.0);
164 #endif
165
166 glEnd();
167 }
168
169
170
171 static void Display( void )
172 {
173 glClear( GL_COLOR_BUFFER_BIT );
174
175 glPushMatrix();
176 glRotatef(Xrot, 1.0, 0.0, 0.0);
177 glRotatef(Yrot, 0.0, 1.0, 0.0);
178 glRotatef(Zrot, 0.0, 0.0, 1.0);
179 glScalef(5.0, 5.0, 5.0);
180 DrawObject();
181 glPopMatrix();
182
183 glutSwapBuffers();
184 }
185
186
187 static void Reshape( int width, int height )
188 {
189 glViewport( 0, 0, width, height );
190 glMatrixMode( GL_PROJECTION );
191 glLoadIdentity();
192 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
193 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
194 glMatrixMode( GL_MODELVIEW );
195 glLoadIdentity();
196 glTranslatef( 0.0, 0.0, -70.0 );
197 }
198
199
200 static void ModeMenu(int entry)
201 {
202 GLboolean enable0 = GL_FALSE, enable1 = GL_FALSE;
203 if (entry==TEX0) {
204 enable0 = GL_TRUE;
205 }
206 else if (entry==TEX1) {
207 enable1 = GL_TRUE;
208 }
209 else if (entry==TEXBOTH) {
210 enable0 = GL_TRUE;
211 enable1 = GL_TRUE;
212 }
213 else if (entry==ANIMATE) {
214 Animate = !Animate;
215 }
216 else if (entry==QUIT) {
217 exit(0);
218 }
219
220 if (entry != ANIMATE) {
221 #ifdef GL_ARB_multitexture
222 glActiveTextureARB(GL_TEXTURE0_ARB);
223 #endif
224 if (enable0) {
225 glEnable(GL_TEXTURE_2D);
226 }
227 else
228 glDisable(GL_TEXTURE_2D);
229
230 #ifdef GL_ARB_multitexture
231 glActiveTextureARB(GL_TEXTURE1_ARB);
232 #endif
233 if (enable1) {
234 glEnable(GL_TEXTURE_2D);
235 }
236 else
237 glDisable(GL_TEXTURE_2D);
238 }
239
240 glutPostRedisplay();
241 }
242
243
244 static void Key( unsigned char key, int x, int y )
245 {
246 (void) x;
247 (void) y;
248 switch (key) {
249 case 27:
250 exit(0);
251 break;
252 }
253 glutPostRedisplay();
254 }
255
256
257 static void SpecialKey( int key, int x, int y )
258 {
259 float step = 3.0;
260 (void) x;
261 (void) y;
262
263 switch (key) {
264 case GLUT_KEY_UP:
265 Xrot += step;
266 break;
267 case GLUT_KEY_DOWN:
268 Xrot -= step;
269 break;
270 case GLUT_KEY_LEFT:
271 Yrot += step;
272 break;
273 case GLUT_KEY_RIGHT:
274 Yrot -= step;
275 break;
276 }
277 glutPostRedisplay();
278 }
279
280
281 static void Init( int argc, char *argv[] )
282 {
283 GLuint texObj[2];
284 GLint units, size;
285
286 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
287 if (!strstr(exten, "GL_ARB_multitexture")) {
288 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
289 exit(1);
290 }
291
292 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &units);
293 printf("%d texture units supported\n", units);
294
295 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
296 printf("%d x %d max texture size\n", size, size);
297
298 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
299
300 /* allocate two texture objects */
301 glGenTextures(2, texObj);
302
303 /* setup texture obj 0 */
304 glBindTexture(GL_TEXTURE_2D, texObj[0]);
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 #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 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
314 printf("Error: couldn't load texture image\n");
315 exit(1);
316 }
317
318
319 /* setup texture obj 1 */
320 glBindTexture(GL_TEXTURE_2D, texObj[1]);
321 #ifdef LINEAR_FILTER
322 /* linear filtering looks much nicer but is much slower for Mesa */
323 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
324 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
325 #else
326 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
328 #endif
329 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
330 printf("Error: couldn't load texture image\n");
331 exit(1);
332 }
333
334
335 /* now bind the texture objects to the respective texture units */
336 #ifdef GL_ARB_multitexture
337 glActiveTextureARB(GL_TEXTURE0_ARB);
338 glBindTexture(GL_TEXTURE_2D, texObj[0]);
339 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
340 glActiveTextureARB(GL_TEXTURE1_ARB);
341 glBindTexture(GL_TEXTURE_2D, texObj[1]);
342 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
343 #endif
344
345 glShadeModel(GL_FLAT);
346 glClearColor(0.3, 0.3, 0.4, 1.0);
347
348 ModeMenu(TEXBOTH);
349
350 if (argc > 1 && strcmp(argv[1], "-info")==0) {
351 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
352 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
353 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
354 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
355 }
356 }
357
358
359 int main( int argc, char *argv[] )
360 {
361 glutInit( &argc, argv );
362 glutInitWindowSize( 300, 300 );
363 glutInitWindowPosition( 0, 0 );
364 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
365 glutCreateWindow(argv[0] );
366
367 Init( argc, argv );
368
369 glutReshapeFunc( Reshape );
370 glutKeyboardFunc( Key );
371 glutSpecialFunc( SpecialKey );
372 glutDisplayFunc( Display );
373 glutIdleFunc( Idle );
374
375 glutCreateMenu(ModeMenu);
376 glutAddMenuEntry("Texture 0", TEX0);
377 glutAddMenuEntry("Texture 1", TEX1);
378 glutAddMenuEntry("Multi-texture", TEXBOTH);
379 glutAddMenuEntry("Toggle Animation", ANIMATE);
380 glutAddMenuEntry("Quit", QUIT);
381 glutAttachMenu(GLUT_RIGHT_BUTTON);
382
383 glutMainLoop();
384 return 0;
385 }