docs: detect when too many varying vars are used
[mesa/nouveau-pmpeg.git] / progs / demos / multiarb.c
blob85c4e3a266c7f9cd2d9152060d3fa0da580ac05f
1 /*
2 * GL_ARB_multitexture demo
4 * Command line options:
5 * -info print GL implementation information
8 * Brian Paul November 1998 This program is in the public domain.
9 * Modified on 12 Feb 2002 for > 2 texture units.
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>
20 #include "readtex.h"
22 #define TEXTURE_1_FILE "../images/girl.rgb"
23 #define TEXTURE_2_FILE "../images/reflect.rgb"
25 #define TEX0 1
26 #define TEX7 8
27 #define ANIMATE 10
28 #define QUIT 100
30 static GLboolean Animate = GL_TRUE;
31 static GLint NumUnits = 1;
32 static GLboolean TexEnabled[8];
34 static GLfloat Drift = 0.0;
35 static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
38 static void Idle( void )
40 if (Animate) {
41 GLint i;
43 Drift = glutGet(GLUT_ELAPSED_TIME) * 0.001;
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);
53 else if (i == 1) {
54 glTranslatef(0.0, Drift, 0.0);
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);
64 glMatrixMode(GL_MODELVIEW);
66 glutPostRedisplay();
71 static void DrawObject(void)
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;
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 */
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]);
89 glVertex2f( vtx_coords[j], vtx_coords[j+1] );
91 glEnd();
95 static void Display( void )
97 glClear( GL_COLOR_BUFFER_BIT );
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();
107 glutSwapBuffers();
111 static void Reshape( int width, int height )
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 );
124 static void ToggleUnit(int unit)
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");
139 static void ModeMenu(int entry)
141 if (entry >= TEX0 && entry <= TEX7) {
142 /* toggle */
143 GLint i = entry - TEX0;
144 ToggleUnit(i);
146 else if (entry==ANIMATE) {
147 Animate = !Animate;
148 if (Animate)
149 glutIdleFunc(Idle);
150 else
151 glutIdleFunc(NULL);
153 else if (entry==QUIT) {
154 exit(0);
157 glutPostRedisplay();
161 static void Key( unsigned char key, int x, int y )
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;
197 glutPostRedisplay();
201 static void SpecialKey( int key, int x, int y )
203 float step = 3.0;
204 (void) x;
205 (void) y;
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;
221 glutPostRedisplay();
225 static void Init( int argc, char *argv[] )
227 GLuint texObj[8];
228 GLint size, i;
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);
236 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
237 printf("%d texture units supported\n", NumUnits);
238 if (NumUnits > 8)
239 NumUnits = 8;
241 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
242 printf("%d x %d max texture size\n", size, size);
244 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
246 for (i = 0; i < NumUnits; i++) {
247 if (i < 2)
248 TexEnabled[i] = GL_TRUE;
249 else
250 TexEnabled[i] = GL_FALSE;
253 /* allocate two texture objects */
254 glGenTextures(NumUnits, texObj);
256 /* setup the texture objects */
257 for (i = 0; i < NumUnits; i++) {
259 glActiveTextureARB(GL_TEXTURE0_ARB + i);
260 glBindTexture(GL_TEXTURE_2D, texObj[i]);
262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
263 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
265 if (i == 0) {
266 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
267 printf("Error: couldn't load texture image\n");
268 exit(1);
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);
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;
288 else {
289 image[i][j][0] = 25;
290 image[i][j][1] = 25;
291 image[i][j][2] = 25;
295 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
296 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
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);
305 if (TexEnabled[i])
306 glEnable(GL_TEXTURE_2D);
309 glShadeModel(GL_FLAT);
310 glClearColor(0.3, 0.3, 0.4, 1.0);
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));
321 int main( int argc, char *argv[] )
323 GLint i;
325 glutInit( &argc, argv );
326 glutInitWindowSize( 300, 300 );
327 glutInitWindowPosition( 0, 0 );
328 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
329 glutCreateWindow(argv[0] );
330 glewInit();
332 Init( argc, argv );
334 glutReshapeFunc( Reshape );
335 glutKeyboardFunc( Key );
336 glutSpecialFunc( SpecialKey );
337 glutDisplayFunc( Display );
338 if (Animate)
339 glutIdleFunc(Idle);
341 glutCreateMenu(ModeMenu);
343 for (i = 0; i < NumUnits; i++) {
344 char s[100];
345 sprintf(s, "Toggle Texture %d", i);
346 glutAddMenuEntry(s, TEX0 + i);
348 glutAddMenuEntry("Toggle Animation", ANIMATE);
349 glutAddMenuEntry("Quit", QUIT);
350 glutAttachMenu(GLUT_RIGHT_BUTTON);
352 glutMainLoop();
353 return 0;