blob: aa0beb9bc7514e4c03c9527b6029bbc7f5d2e80a [file] [log] [blame]
Brian Paul563d26b2000-11-01 16:02:01 +00001/* $Id: multiarb.c,v 1.7 2000/11/01 16:02:01 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 Paul563d26b2000-11-01 16:02:01 +000015 * Revision 1.7 2000/11/01 16:02:01 brianp
16 * print number of texture units
17 *
Brian Paul4d598442000-05-23 23:21:00 +000018 * Revision 1.6 2000/05/23 23:21:00 brianp
19 * set default window pos
20 *
Brian Paulb45c71a2000-02-02 17:31:45 +000021 * Revision 1.5 2000/02/02 17:31:45 brianp
22 * changed > to >=
23 *
Brian Pauld2702f02000-02-02 01:07:21 +000024 * Revision 1.4 2000/02/02 01:07:21 brianp
25 * limit Drift to [0, 1]
26 *
Brian Paulac126091999-10-21 16:39:06 +000027 * Revision 1.3 1999/10/21 16:40:32 brianp
28 * added -info command line option
29 *
Brian Paul1a3b8ff1999-10-13 12:02:13 +000030 * Revision 1.2 1999/10/13 12:02:13 brianp
31 * use texture objects now
32 *
33 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
34 * Imported sources
jtgafb833d1999-08-19 00:55:39 +000035 *
36 * Revision 1.3 1999/03/28 18:20:49 brianp
37 * minor clean-up
38 *
39 * Revision 1.2 1998/11/05 04:34:04 brianp
40 * moved image files to ../images/ directory
41 *
42 * Revision 1.1 1998/11/03 01:36:33 brianp
43 * Initial revision
44 *
45 */
46
47
48#include <math.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <GL/glut.h>
53
54#include "../util/readtex.c" /* I know, this is a hack. */
55
56#define TEXTURE_1_FILE "../images/girl.rgb"
57#define TEXTURE_2_FILE "../images/reflect.rgb"
58
59#define TEX0 1
60#define TEX1 2
61#define TEXBOTH 3
62#define ANIMATE 10
63#define QUIT 100
64
65static GLboolean Animate = GL_TRUE;
66
67static GLfloat Drift = 0.0;
68static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
69
70
71
72static void Idle( void )
73{
74 if (Animate) {
75 Drift += 0.05;
Brian Paulb45c71a2000-02-02 17:31:45 +000076 if (Drift >= 1.0)
Brian Pauld2702f02000-02-02 01:07:21 +000077 Drift = 0.0;
jtgafb833d1999-08-19 00:55:39 +000078
79#ifdef GL_ARB_multitexture
80 glActiveTextureARB(GL_TEXTURE0_ARB);
81#endif
82 glMatrixMode(GL_TEXTURE);
83 glLoadIdentity();
84 glTranslatef(Drift, 0.0, 0.0);
85 glMatrixMode(GL_MODELVIEW);
86
87#ifdef GL_ARB_multitexture
88 glActiveTextureARB(GL_TEXTURE1_ARB);
89#endif
90 glMatrixMode(GL_TEXTURE);
91 glLoadIdentity();
92 glTranslatef(0.0, Drift, 0.0);
93 glMatrixMode(GL_MODELVIEW);
94
95 glutPostRedisplay();
96 }
97}
98
99
100static void DrawObject(void)
101{
102 glBegin(GL_QUADS);
103
104#ifdef GL_ARB_multitexture
105 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
106 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
107 glVertex2f(-1.0, -1.0);
108
109 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 0.0);
110 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
111 glVertex2f(1.0, -1.0);
112
113 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 2.0);
114 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
115 glVertex2f(1.0, 1.0);
116
117 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 2.0);
118 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
119 glVertex2f(-1.0, 1.0);
120#else
121 glTexCoord2f(0.0, 0.0);
122 glVertex2f(-1.0, -1.0);
123
124 glTexCoord2f(1.0, 0.0);
125 glVertex2f(1.0, -1.0);
126
127 glTexCoord2f(1.0, 1.0);
128 glVertex2f(1.0, 1.0);
129
130 glTexCoord2f(0.0, 1.0);
131 glVertex2f(-1.0, 1.0);
132#endif
133
134 glEnd();
135}
136
137
138
139static void Display( void )
140{
141 glClear( GL_COLOR_BUFFER_BIT );
142
143 glPushMatrix();
144 glRotatef(Xrot, 1.0, 0.0, 0.0);
145 glRotatef(Yrot, 0.0, 1.0, 0.0);
146 glRotatef(Zrot, 0.0, 0.0, 1.0);
147 glScalef(5.0, 5.0, 5.0);
148 DrawObject();
149 glPopMatrix();
150
151 glutSwapBuffers();
152}
153
154
155static void Reshape( int width, int height )
156{
157 glViewport( 0, 0, width, height );
158 glMatrixMode( GL_PROJECTION );
159 glLoadIdentity();
160 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
161 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
162 glMatrixMode( GL_MODELVIEW );
163 glLoadIdentity();
164 glTranslatef( 0.0, 0.0, -70.0 );
165}
166
167
168static void ModeMenu(int entry)
169{
170 GLboolean enable0 = GL_FALSE, enable1 = GL_FALSE;
171 if (entry==TEX0) {
172 enable0 = GL_TRUE;
173 }
174 else if (entry==TEX1) {
175 enable1 = GL_TRUE;
176 }
177 else if (entry==TEXBOTH) {
178 enable0 = GL_TRUE;
179 enable1 = GL_TRUE;
180 }
181 else if (entry==ANIMATE) {
182 Animate = !Animate;
183 }
184 else if (entry==QUIT) {
185 exit(0);
186 }
187
188 if (entry != ANIMATE) {
189#ifdef GL_ARB_multitexture
190 glActiveTextureARB(GL_TEXTURE0_ARB);
191#endif
192 if (enable0) {
193 glEnable(GL_TEXTURE_2D);
194 }
195 else
196 glDisable(GL_TEXTURE_2D);
197
198#ifdef GL_ARB_multitexture
199 glActiveTextureARB(GL_TEXTURE1_ARB);
200#endif
201 if (enable1) {
202 glEnable(GL_TEXTURE_2D);
203 }
204 else
205 glDisable(GL_TEXTURE_2D);
206 }
207
208 glutPostRedisplay();
209}
210
211
212static void Key( unsigned char key, int x, int y )
213{
214 (void) x;
215 (void) y;
216 switch (key) {
217 case 27:
218 exit(0);
219 break;
220 }
221 glutPostRedisplay();
222}
223
224
225static void SpecialKey( int key, int x, int y )
226{
227 float step = 3.0;
228 (void) x;
229 (void) y;
230
231 switch (key) {
232 case GLUT_KEY_UP:
233 Xrot += step;
234 break;
235 case GLUT_KEY_DOWN:
236 Xrot -= step;
237 break;
238 case GLUT_KEY_LEFT:
239 Yrot += step;
240 break;
241 case GLUT_KEY_RIGHT:
242 Yrot -= step;
243 break;
244 }
245 glutPostRedisplay();
246}
247
248
Brian Paulac126091999-10-21 16:39:06 +0000249static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000250{
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000251 GLuint texObj[2];
Brian Paul563d26b2000-11-01 16:02:01 +0000252 GLint units;
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000253
jtgafb833d1999-08-19 00:55:39 +0000254 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
255 if (!strstr(exten, "GL_ARB_multitexture")) {
256 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
257 exit(1);
258 }
259
Brian Paul563d26b2000-11-01 16:02:01 +0000260 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &units);
261 printf("%d texture units supported\n", units);
262
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000263 /* allocate two texture objects */
264 glGenTextures(2, texObj);
265
266 /* setup texture obj 0 */
267 glBindTexture(GL_TEXTURE_2D, texObj[0]);
jtgafb833d1999-08-19 00:55:39 +0000268#ifdef LINEAR_FILTER
269 /* linear filtering looks much nicer but is much slower for Mesa */
270 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
271 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
272#else
273 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
274 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
275#endif
276
277 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
278
279 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
280
281 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
282 printf("Error: couldn't load texture image\n");
283 exit(1);
284 }
285
286
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000287 /* setup texture obj 1 */
288 glBindTexture(GL_TEXTURE_2D, texObj[1]);
jtgafb833d1999-08-19 00:55:39 +0000289#ifdef LINEAR_FILTER
290 /* linear filtering looks much nicer but is much slower for Mesa */
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
293#else
294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
295 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
296#endif
297
298 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
299
300 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
301 printf("Error: couldn't load texture image\n");
302 exit(1);
303 }
304
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000305
306 /* now bind the texture objects to the respective texture units */
307#ifdef GL_ARB_multitexture
308 glActiveTextureARB(GL_TEXTURE0_ARB);
309 glBindTexture(GL_TEXTURE_2D, texObj[0]);
310 glActiveTextureARB(GL_TEXTURE1_ARB);
311 glBindTexture(GL_TEXTURE_2D, texObj[1]);
312#endif
313
jtgafb833d1999-08-19 00:55:39 +0000314 glShadeModel(GL_FLAT);
315 glClearColor(0.3, 0.3, 0.4, 1.0);
316
317 ModeMenu(TEXBOTH);
Brian Paulac126091999-10-21 16:39:06 +0000318
319 if (argc > 1 && strcmp(argv[1], "-info")==0) {
320 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
321 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
322 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
323 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
324 }
jtgafb833d1999-08-19 00:55:39 +0000325}
326
327
328int main( int argc, char *argv[] )
329{
330 glutInit( &argc, argv );
331 glutInitWindowSize( 300, 300 );
Brian Paul4d598442000-05-23 23:21:00 +0000332 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000333 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
334 glutCreateWindow(argv[0] );
335
Brian Paulac126091999-10-21 16:39:06 +0000336 Init( argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000337
338 glutReshapeFunc( Reshape );
339 glutKeyboardFunc( Key );
340 glutSpecialFunc( SpecialKey );
341 glutDisplayFunc( Display );
342 glutIdleFunc( Idle );
343
344 glutCreateMenu(ModeMenu);
345 glutAddMenuEntry("Texture 0", TEX0);
346 glutAddMenuEntry("Texture 1", TEX1);
347 glutAddMenuEntry("Multi-texture", TEXBOTH);
348 glutAddMenuEntry("Toggle Animation", ANIMATE);
349 glutAddMenuEntry("Quit", QUIT);
350 glutAttachMenu(GLUT_RIGHT_BUTTON);
351
352 glutMainLoop();
353 return 0;
354}