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