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