blob: 239efac6633b700412d007c617ee129d74d77bea [file] [log] [blame]
Brian Paul785774d2003-05-30 15:30:16 +00001/* $Id: multiarb.c,v 1.12 2003/05/30 15:30:17 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.
Brian Paulcbd9a022002-02-13 02:23:33 +000011 * Modified on 12 Feb 2002 for > 2 texture units.
jtgafb833d1999-08-19 00:55:39 +000012 */
13
14
15#include <math.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <GL/glut.h>
20
pescod1ff1f62000-12-24 22:53:54 +000021#include "readtex.c" /* I know, this is a hack. */
jtgafb833d1999-08-19 00:55:39 +000022
23#define TEXTURE_1_FILE "../images/girl.rgb"
24#define TEXTURE_2_FILE "../images/reflect.rgb"
25
26#define TEX0 1
Brian Paulcbd9a022002-02-13 02:23:33 +000027#define TEX7 8
jtgafb833d1999-08-19 00:55:39 +000028#define ANIMATE 10
29#define QUIT 100
30
31static GLboolean Animate = GL_TRUE;
Brian Paulcbd9a022002-02-13 02:23:33 +000032static GLint NumUnits = 1;
33static GLboolean TexEnabled[8];
jtgafb833d1999-08-19 00:55:39 +000034
35static GLfloat Drift = 0.0;
Brian Paul785774d2003-05-30 15:30:16 +000036static GLfloat drift_increment = 0.005;
jtgafb833d1999-08-19 00:55:39 +000037static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
38
39
40
41static void Idle( void )
42{
43 if (Animate) {
Brian Paulcbd9a022002-02-13 02:23:33 +000044 GLint i;
45
Brian Paul785774d2003-05-30 15:30:16 +000046 Drift += drift_increment;
Brian Paulb45c71a2000-02-02 17:31:45 +000047 if (Drift >= 1.0)
Brian Pauld2702f02000-02-02 01:07:21 +000048 Drift = 0.0;
jtgafb833d1999-08-19 00:55:39 +000049
Brian Paulcbd9a022002-02-13 02:23:33 +000050 for (i = 0; i < NumUnits; i++) {
51 glActiveTextureARB(GL_TEXTURE0_ARB + i);
52 glMatrixMode(GL_TEXTURE);
53 glLoadIdentity();
54 if (i == 0) {
55 glTranslatef(Drift, 0.0, 0.0);
56 glScalef(2, 2, 2);
57 }
58 else if (i == 1) {
59 glTranslatef(0.0, Drift, 0.0);
60 }
61 else {
62 glTranslatef(0.5, 0.5, 0.0);
63 glRotatef(180.0 * Drift, 0, 0, 1);
64 glScalef(1.0/i, 1.0/i, 1.0/i);
65 glTranslatef(-0.5, -0.5, 0.0);
66 }
67 }
jtgafb833d1999-08-19 00:55:39 +000068 glMatrixMode(GL_MODELVIEW);
69
70 glutPostRedisplay();
71 }
72}
73
74
75static void DrawObject(void)
76{
Brian Paulcbd9a022002-02-13 02:23:33 +000077 GLint i;
Brian Paul785774d2003-05-30 15:30:16 +000078 GLint j;
79 static const GLfloat tex_coords[] = { 0.0, 0.0, 1.0, 1.0, 0.0 };
80 static const GLfloat vtx_coords[] = { -1.0, -1.0, 1.0, 1.0, -1.0 };
Brian Paulcbd9a022002-02-13 02:23:33 +000081
82 if (!TexEnabled[0] && !TexEnabled[1])
83 glColor3f(0.1, 0.1, 0.1); /* add onto this */
84 else
85 glColor3f(1, 1, 1); /* modulate this */
86
jtgafb833d1999-08-19 00:55:39 +000087 glBegin(GL_QUADS);
88
Brian Paul785774d2003-05-30 15:30:16 +000089 /* Toggle between the vector and scalar entry points. This is done purely
90 * to hit multiple paths in the driver.
91 */
92 if ( Drift > 0.49 ) {
93 for (j = 0; j < 4; j++ ) {
94 for (i = 0; i < NumUnits; i++)
95 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i,
96 tex_coords[j], tex_coords[j+1]);
97 glVertex2f( vtx_coords[j], vtx_coords[j+1] );
98 }
99 }
100 else {
101 for (j = 0; j < 4; j++ ) {
102 for (i = 0; i < NumUnits; i++)
103 glMultiTexCoord2fvARB(GL_TEXTURE0_ARB + i, & tex_coords[j]);
104 glVertex2fv( & vtx_coords[j] );
105 }
106 }
jtgafb833d1999-08-19 00:55:39 +0000107
108 glEnd();
109}
110
111
112
113static void Display( void )
114{
Brian Paul785774d2003-05-30 15:30:16 +0000115 static GLint T0 = 0;
116 static GLint Frames = 0;
117 GLint t;
118
jtgafb833d1999-08-19 00:55:39 +0000119 glClear( GL_COLOR_BUFFER_BIT );
120
121 glPushMatrix();
122 glRotatef(Xrot, 1.0, 0.0, 0.0);
123 glRotatef(Yrot, 0.0, 1.0, 0.0);
124 glRotatef(Zrot, 0.0, 0.0, 1.0);
125 glScalef(5.0, 5.0, 5.0);
126 DrawObject();
127 glPopMatrix();
128
129 glutSwapBuffers();
Brian Paul785774d2003-05-30 15:30:16 +0000130
131 Frames++;
132
133 t = glutGet(GLUT_ELAPSED_TIME);
134 if (t - T0 >= 250) {
135 GLfloat seconds = (t - T0) / 1000.0;
136 drift_increment = 2.2 * seconds / Frames;
137 T0 = t;
138 Frames = 0;
139 }
jtgafb833d1999-08-19 00:55:39 +0000140}
141
142
143static void Reshape( int width, int height )
144{
145 glViewport( 0, 0, width, height );
146 glMatrixMode( GL_PROJECTION );
147 glLoadIdentity();
148 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
149 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
150 glMatrixMode( GL_MODELVIEW );
151 glLoadIdentity();
152 glTranslatef( 0.0, 0.0, -70.0 );
153}
154
155
156static void ModeMenu(int entry)
157{
Brian Paulcbd9a022002-02-13 02:23:33 +0000158 if (entry >= TEX0 && entry <= TEX7) {
159 /* toggle */
160 GLint i = entry - TEX0;
161 TexEnabled[i] = !TexEnabled[i];
162 glActiveTextureARB(GL_TEXTURE0_ARB + i);
163 if (TexEnabled[i])
164 glEnable(GL_TEXTURE_2D);
165 else
166 glDisable(GL_TEXTURE_2D);
167 printf("Enabled: ");
168 for (i = 0; i < NumUnits; i++)
169 printf("%d ", (int) TexEnabled[i]);
170 printf("\n");
jtgafb833d1999-08-19 00:55:39 +0000171 }
172 else if (entry==ANIMATE) {
173 Animate = !Animate;
174 }
175 else if (entry==QUIT) {
176 exit(0);
177 }
178
jtgafb833d1999-08-19 00:55:39 +0000179 glutPostRedisplay();
180}
181
182
183static void Key( unsigned char key, int x, int y )
184{
185 (void) x;
186 (void) y;
187 switch (key) {
188 case 27:
189 exit(0);
190 break;
191 }
192 glutPostRedisplay();
193}
194
195
196static void SpecialKey( int key, int x, int y )
197{
198 float step = 3.0;
199 (void) x;
200 (void) y;
201
202 switch (key) {
203 case GLUT_KEY_UP:
204 Xrot += step;
205 break;
206 case GLUT_KEY_DOWN:
207 Xrot -= step;
208 break;
209 case GLUT_KEY_LEFT:
210 Yrot += step;
211 break;
212 case GLUT_KEY_RIGHT:
213 Yrot -= step;
214 break;
215 }
216 glutPostRedisplay();
217}
218
219
Brian Paulac126091999-10-21 16:39:06 +0000220static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000221{
Brian Paulcbd9a022002-02-13 02:23:33 +0000222 GLuint texObj[8];
223 GLint size, i;
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000224
jtgafb833d1999-08-19 00:55:39 +0000225 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
226 if (!strstr(exten, "GL_ARB_multitexture")) {
227 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
228 exit(1);
229 }
230
Brian Paulcbd9a022002-02-13 02:23:33 +0000231 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
232 printf("%d texture units supported\n", NumUnits);
233 if (NumUnits > 8)
234 NumUnits = 8;
Brian Paul563d26b2000-11-01 16:02:01 +0000235
Brian Paul4d99e5b2001-06-20 19:12:30 +0000236 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
237 printf("%d x %d max texture size\n", size, size);
238
Brian Paul68860192001-06-13 14:33:16 +0000239 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
240
Brian Paulcbd9a022002-02-13 02:23:33 +0000241 for (i = 0; i < NumUnits; i++) {
242 if (i < 2)
243 TexEnabled[i] = GL_TRUE;
244 else
245 TexEnabled[i] = GL_FALSE;
246 }
247
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000248 /* allocate two texture objects */
Brian Paulcbd9a022002-02-13 02:23:33 +0000249 glGenTextures(NumUnits, texObj);
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000250
Brian Paulcbd9a022002-02-13 02:23:33 +0000251 /* setup the texture objects */
252 for (i = 0; i < NumUnits; i++) {
253
254 glActiveTextureARB(GL_TEXTURE0_ARB + i);
255 glBindTexture(GL_TEXTURE_2D, texObj[i]);
256
257 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
258 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
259
260 if (i == 0) {
261 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
262 printf("Error: couldn't load texture image\n");
263 exit(1);
264 }
265 }
266 else if (i == 1) {
267 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
268 printf("Error: couldn't load texture image\n");
269 exit(1);
270 }
271 }
272 else {
273 /* checker */
274 GLubyte image[8][8][3];
275 GLint i, j;
276 for (i = 0; i < 8; i++) {
277 for (j = 0; j < 8; j++) {
278 if ((i + j) & 1) {
279 image[i][j][0] = 50;
280 image[i][j][1] = 50;
281 image[i][j][2] = 50;
282 }
283 else {
284 image[i][j][0] = 25;
285 image[i][j][1] = 25;
286 image[i][j][2] = 25;
287 }
288 }
289 }
290 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
291 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
292 }
293
294 /* Bind texObj[i] to ith texture unit */
295 if (i < 2)
296 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
297 else
298 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
299
300 if (TexEnabled[i])
301 glEnable(GL_TEXTURE_2D);
jtgafb833d1999-08-19 00:55:39 +0000302 }
303
jtgafb833d1999-08-19 00:55:39 +0000304 glShadeModel(GL_FLAT);
305 glClearColor(0.3, 0.3, 0.4, 1.0);
306
Brian Paulac126091999-10-21 16:39:06 +0000307 if (argc > 1 && strcmp(argv[1], "-info")==0) {
308 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
309 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
310 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
311 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
312 }
jtgafb833d1999-08-19 00:55:39 +0000313}
314
315
316int main( int argc, char *argv[] )
317{
Brian Paulcbd9a022002-02-13 02:23:33 +0000318 GLint i;
319
jtgafb833d1999-08-19 00:55:39 +0000320 glutInit( &argc, argv );
321 glutInitWindowSize( 300, 300 );
Brian Paul4d598442000-05-23 23:21:00 +0000322 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000323 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
324 glutCreateWindow(argv[0] );
325
Brian Paulac126091999-10-21 16:39:06 +0000326 Init( argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000327
328 glutReshapeFunc( Reshape );
329 glutKeyboardFunc( Key );
330 glutSpecialFunc( SpecialKey );
331 glutDisplayFunc( Display );
332 glutIdleFunc( Idle );
333
334 glutCreateMenu(ModeMenu);
Brian Paulcbd9a022002-02-13 02:23:33 +0000335
336 for (i = 0; i < NumUnits; i++) {
337 char s[100];
338 sprintf(s, "Toggle Texture %d", i);
339 glutAddMenuEntry(s, TEX0 + i);
340 }
jtgafb833d1999-08-19 00:55:39 +0000341 glutAddMenuEntry("Toggle Animation", ANIMATE);
342 glutAddMenuEntry("Quit", QUIT);
343 glutAttachMenu(GLUT_RIGHT_BUTTON);
344
345 glutMainLoop();
346 return 0;
347}