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