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