20aa29f99662c514d9edfcfb0e53ba8788ef0283
[mesa.git] / progs / demos / multiarb.c
1
2 /*
3 * GL_ARB_multitexture demo
4 *
5 * Command line options:
6 * -info print GL implementation information
7 *
8 *
9 * Brian Paul November 1998 This program is in the public domain.
10 * Modified on 12 Feb 2002 for > 2 texture units.
11 */
12
13
14 #include <math.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <GL/glut.h>
19
20 #include "readtex.c" /* I know, this is a hack. */
21
22 #define TEXTURE_1_FILE "../images/girl.rgb"
23 #define TEXTURE_2_FILE "../images/reflect.rgb"
24
25 #define TEX0 1
26 #define TEX7 8
27 #define ANIMATE 10
28 #define QUIT 100
29
30 static GLboolean Animate = GL_TRUE;
31 static GLint NumUnits = 1;
32 static GLboolean TexEnabled[8];
33
34 static GLfloat Drift = 0.0;
35 static GLfloat drift_increment = 0.005;
36 static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
37
38
39
40 static void Idle( void )
41 {
42 if (Animate) {
43 GLint i;
44
45 Drift += drift_increment;
46 if (Drift >= 1.0)
47 Drift = 0.0;
48
49 for (i = 0; i < NumUnits; i++) {
50 glActiveTextureARB(GL_TEXTURE0_ARB + i);
51 glMatrixMode(GL_TEXTURE);
52 glLoadIdentity();
53 if (i == 0) {
54 glTranslatef(Drift, 0.0, 0.0);
55 glScalef(2, 2, 2);
56 }
57 else if (i == 1) {
58 glTranslatef(0.0, Drift, 0.0);
59 }
60 else {
61 glTranslatef(0.5, 0.5, 0.0);
62 glRotatef(180.0 * Drift, 0, 0, 1);
63 glScalef(1.0/i, 1.0/i, 1.0/i);
64 glTranslatef(-0.5, -0.5, 0.0);
65 }
66 }
67 glMatrixMode(GL_MODELVIEW);
68
69 glutPostRedisplay();
70 }
71 }
72
73
74 static void DrawObject(void)
75 {
76 GLint i;
77 GLint j;
78 static const GLfloat tex_coords[] = { 0.0, 0.0, 1.0, 1.0, 0.0 };
79 static const GLfloat vtx_coords[] = { -1.0, -1.0, 1.0, 1.0, -1.0 };
80
81 if (!TexEnabled[0] && !TexEnabled[1])
82 glColor3f(0.1, 0.1, 0.1); /* add onto this */
83 else
84 glColor3f(1, 1, 1); /* modulate this */
85
86 glBegin(GL_QUADS);
87
88 /* Toggle between the vector and scalar entry points. This is done purely
89 * to hit multiple paths in the driver.
90 */
91 if ( Drift > 0.49 ) {
92 for (j = 0; j < 4; j++ ) {
93 for (i = 0; i < NumUnits; i++)
94 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i,
95 tex_coords[j], tex_coords[j+1]);
96 glVertex2f( vtx_coords[j], vtx_coords[j+1] );
97 }
98 }
99 else {
100 for (j = 0; j < 4; j++ ) {
101 for (i = 0; i < NumUnits; i++)
102 glMultiTexCoord2fvARB(GL_TEXTURE0_ARB + i, & tex_coords[j]);
103 glVertex2fv( & vtx_coords[j] );
104 }
105 }
106
107 glEnd();
108 }
109
110
111
112 static void Display( void )
113 {
114 static GLint T0 = 0;
115 static GLint Frames = 0;
116 GLint t;
117
118 glClear( GL_COLOR_BUFFER_BIT );
119
120 glPushMatrix();
121 glRotatef(Xrot, 1.0, 0.0, 0.0);
122 glRotatef(Yrot, 0.0, 1.0, 0.0);
123 glRotatef(Zrot, 0.0, 0.0, 1.0);
124 glScalef(5.0, 5.0, 5.0);
125 DrawObject();
126 glPopMatrix();
127
128 glutSwapBuffers();
129
130 Frames++;
131
132 t = glutGet(GLUT_ELAPSED_TIME);
133 if (t - T0 >= 250) {
134 GLfloat seconds = (t - T0) / 1000.0;
135 drift_increment = 2.2 * seconds / Frames;
136 T0 = t;
137 Frames = 0;
138 }
139 }
140
141
142 static void Reshape( int width, int height )
143 {
144 glViewport( 0, 0, width, height );
145 glMatrixMode( GL_PROJECTION );
146 glLoadIdentity();
147 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
148 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
149 glMatrixMode( GL_MODELVIEW );
150 glLoadIdentity();
151 glTranslatef( 0.0, 0.0, -70.0 );
152 }
153
154
155 static void ModeMenu(int entry)
156 {
157 if (entry >= TEX0 && entry <= TEX7) {
158 /* toggle */
159 GLint i = entry - TEX0;
160 TexEnabled[i] = !TexEnabled[i];
161 glActiveTextureARB(GL_TEXTURE0_ARB + i);
162 if (TexEnabled[i])
163 glEnable(GL_TEXTURE_2D);
164 else
165 glDisable(GL_TEXTURE_2D);
166 printf("Enabled: ");
167 for (i = 0; i < NumUnits; i++)
168 printf("%d ", (int) TexEnabled[i]);
169 printf("\n");
170 }
171 else if (entry==ANIMATE) {
172 Animate = !Animate;
173 }
174 else if (entry==QUIT) {
175 exit(0);
176 }
177
178 glutPostRedisplay();
179 }
180
181
182 static void Key( unsigned char key, int x, int y )
183 {
184 (void) x;
185 (void) y;
186 switch (key) {
187 case 27:
188 exit(0);
189 break;
190 }
191 glutPostRedisplay();
192 }
193
194
195 static void SpecialKey( int key, int x, int y )
196 {
197 float step = 3.0;
198 (void) x;
199 (void) y;
200
201 switch (key) {
202 case GLUT_KEY_UP:
203 Xrot += step;
204 break;
205 case GLUT_KEY_DOWN:
206 Xrot -= step;
207 break;
208 case GLUT_KEY_LEFT:
209 Yrot += step;
210 break;
211 case GLUT_KEY_RIGHT:
212 Yrot -= step;
213 break;
214 }
215 glutPostRedisplay();
216 }
217
218
219 static void Init( int argc, char *argv[] )
220 {
221 GLuint texObj[8];
222 GLint size, i;
223
224 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
225 if (!strstr(exten, "GL_ARB_multitexture")) {
226 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
227 exit(1);
228 }
229
230 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
231 printf("%d texture units supported\n", NumUnits);
232 if (NumUnits > 8)
233 NumUnits = 8;
234
235 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
236 printf("%d x %d max texture size\n", size, size);
237
238 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
239
240 for (i = 0; i < NumUnits; i++) {
241 if (i < 2)
242 TexEnabled[i] = GL_TRUE;
243 else
244 TexEnabled[i] = GL_FALSE;
245 }
246
247 /* allocate two texture objects */
248 glGenTextures(NumUnits, texObj);
249
250 /* setup the texture objects */
251 for (i = 0; i < NumUnits; i++) {
252
253 glActiveTextureARB(GL_TEXTURE0_ARB + i);
254 glBindTexture(GL_TEXTURE_2D, texObj[i]);
255
256 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
257 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
258
259 if (i == 0) {
260 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
261 printf("Error: couldn't load texture image\n");
262 exit(1);
263 }
264 }
265 else if (i == 1) {
266 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
267 printf("Error: couldn't load texture image\n");
268 exit(1);
269 }
270 }
271 else {
272 /* checker */
273 GLubyte image[8][8][3];
274 GLint i, j;
275 for (i = 0; i < 8; i++) {
276 for (j = 0; j < 8; j++) {
277 if ((i + j) & 1) {
278 image[i][j][0] = 50;
279 image[i][j][1] = 50;
280 image[i][j][2] = 50;
281 }
282 else {
283 image[i][j][0] = 25;
284 image[i][j][1] = 25;
285 image[i][j][2] = 25;
286 }
287 }
288 }
289 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
290 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
291 }
292
293 /* Bind texObj[i] to ith texture unit */
294 if (i < 2)
295 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
296 else
297 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
298
299 if (TexEnabled[i])
300 glEnable(GL_TEXTURE_2D);
301 }
302
303 glShadeModel(GL_FLAT);
304 glClearColor(0.3, 0.3, 0.4, 1.0);
305
306 if (argc > 1 && strcmp(argv[1], "-info")==0) {
307 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
308 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
309 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
310 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
311 }
312 }
313
314
315 int main( int argc, char *argv[] )
316 {
317 GLint i;
318
319 glutInit( &argc, argv );
320 glutInitWindowSize( 300, 300 );
321 glutInitWindowPosition( 0, 0 );
322 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
323 glutCreateWindow(argv[0] );
324
325 Init( argc, argv );
326
327 glutReshapeFunc( Reshape );
328 glutKeyboardFunc( Key );
329 glutSpecialFunc( SpecialKey );
330 glutDisplayFunc( Display );
331 glutIdleFunc( Idle );
332
333 glutCreateMenu(ModeMenu);
334
335 for (i = 0; i < NumUnits; i++) {
336 char s[100];
337 sprintf(s, "Toggle Texture %d", i);
338 glutAddMenuEntry(s, TEX0 + i);
339 }
340 glutAddMenuEntry("Toggle Animation", ANIMATE);
341 glutAddMenuEntry("Quit", QUIT);
342 glutAttachMenu(GLUT_RIGHT_BUTTON);
343
344 glutMainLoop();
345 return 0;
346 }