blob: 6fe35e7fdd21284c741bb26c25c665e53dc646d9 [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001
2/*
3 * GL_ARB_multitexture demo
Brian Paulac126091999-10-21 16:39:06 +00004 *
5 * Command line options:
6 * -info print GL implementation information
7 *
8 *
jtgafb833d1999-08-19 00:55:39 +00009 * Brian Paul November 1998 This program is in the public domain.
Brian Paulcbd9a022002-02-13 02:23:33 +000010 * Modified on 12 Feb 2002 for > 2 texture units.
jtgafb833d1999-08-19 00:55:39 +000011 */
12
13
14#include <math.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <GL/glut.h>
19
pescod1ff1f62000-12-24 22:53:54 +000020#include "readtex.c" /* I know, this is a hack. */
jtgafb833d1999-08-19 00:55:39 +000021
22#define TEXTURE_1_FILE "../images/girl.rgb"
23#define TEXTURE_2_FILE "../images/reflect.rgb"
24
25#define TEX0 1
Brian Paulcbd9a022002-02-13 02:23:33 +000026#define TEX7 8
jtgafb833d1999-08-19 00:55:39 +000027#define ANIMATE 10
28#define QUIT 100
29
30static GLboolean Animate = GL_TRUE;
Brian Paulcbd9a022002-02-13 02:23:33 +000031static GLint NumUnits = 1;
32static GLboolean TexEnabled[8];
jtgafb833d1999-08-19 00:55:39 +000033
34static GLfloat Drift = 0.0;
Brian Paul785774d2003-05-30 15:30:16 +000035static GLfloat drift_increment = 0.005;
jtgafb833d1999-08-19 00:55:39 +000036static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
37
38
39
40static void Idle( void )
41{
42 if (Animate) {
Brian Paulcbd9a022002-02-13 02:23:33 +000043 GLint i;
44
Brian Paul785774d2003-05-30 15:30:16 +000045 Drift += drift_increment;
Brian Paulb45c71a2000-02-02 17:31:45 +000046 if (Drift >= 1.0)
Brian Pauld2702f02000-02-02 01:07:21 +000047 Drift = 0.0;
jtgafb833d1999-08-19 00:55:39 +000048
Brian Paulcbd9a022002-02-13 02:23:33 +000049 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);
Keith Whitwell740f7de2004-01-27 16:18:00 +000055 glScalef(2, 2, 1);
Brian Paulcbd9a022002-02-13 02:23:33 +000056 }
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 }
jtgafb833d1999-08-19 00:55:39 +000067 glMatrixMode(GL_MODELVIEW);
68
69 glutPostRedisplay();
70 }
71}
72
73
74static void DrawObject(void)
75{
Brian Paulcbd9a022002-02-13 02:23:33 +000076 GLint i;
Brian Paul785774d2003-05-30 15:30:16 +000077 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 };
Brian Paulcbd9a022002-02-13 02:23:33 +000080
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
jtgafb833d1999-08-19 00:55:39 +000086 glBegin(GL_QUADS);
87
Brian Paul785774d2003-05-30 15:30:16 +000088 /* 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 }
jtgafb833d1999-08-19 00:55:39 +0000106
107 glEnd();
108}
109
110
111
112static void Display( void )
113{
Brian Paul785774d2003-05-30 15:30:16 +0000114 static GLint T0 = 0;
115 static GLint Frames = 0;
116 GLint t;
117
jtgafb833d1999-08-19 00:55:39 +0000118 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();
Brian Paul785774d2003-05-30 15:30:16 +0000129
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 }
jtgafb833d1999-08-19 00:55:39 +0000139}
140
141
142static 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
155static void ModeMenu(int entry)
156{
Brian Paulcbd9a022002-02-13 02:23:33 +0000157 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");
jtgafb833d1999-08-19 00:55:39 +0000170 }
171 else if (entry==ANIMATE) {
172 Animate = !Animate;
173 }
174 else if (entry==QUIT) {
175 exit(0);
176 }
177
jtgafb833d1999-08-19 00:55:39 +0000178 glutPostRedisplay();
179}
180
181
182static 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
195static 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
Brian Paulac126091999-10-21 16:39:06 +0000219static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000220{
Brian Paulcbd9a022002-02-13 02:23:33 +0000221 GLuint texObj[8];
222 GLint size, i;
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000223
jtgafb833d1999-08-19 00:55:39 +0000224 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
Brian Paulcbd9a022002-02-13 02:23:33 +0000230 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
231 printf("%d texture units supported\n", NumUnits);
232 if (NumUnits > 8)
233 NumUnits = 8;
Brian Paul563d26b2000-11-01 16:02:01 +0000234
Brian Paul4d99e5b2001-06-20 19:12:30 +0000235 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
236 printf("%d x %d max texture size\n", size, size);
237
Brian Paul68860192001-06-13 14:33:16 +0000238 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
239
Brian Paulcbd9a022002-02-13 02:23:33 +0000240 for (i = 0; i < NumUnits; i++) {
241 if (i < 2)
242 TexEnabled[i] = GL_TRUE;
243 else
244 TexEnabled[i] = GL_FALSE;
245 }
246
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000247 /* allocate two texture objects */
Brian Paulcbd9a022002-02-13 02:23:33 +0000248 glGenTextures(NumUnits, texObj);
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000249
Brian Paulcbd9a022002-02-13 02:23:33 +0000250 /* 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);
jtgafb833d1999-08-19 00:55:39 +0000301 }
302
jtgafb833d1999-08-19 00:55:39 +0000303 glShadeModel(GL_FLAT);
304 glClearColor(0.3, 0.3, 0.4, 1.0);
305
Brian Paulac126091999-10-21 16:39:06 +0000306 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 }
jtgafb833d1999-08-19 00:55:39 +0000312}
313
314
315int main( int argc, char *argv[] )
316{
Brian Paulcbd9a022002-02-13 02:23:33 +0000317 GLint i;
318
jtgafb833d1999-08-19 00:55:39 +0000319 glutInit( &argc, argv );
320 glutInitWindowSize( 300, 300 );
Brian Paul4d598442000-05-23 23:21:00 +0000321 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000322 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
323 glutCreateWindow(argv[0] );
324
Brian Paulac126091999-10-21 16:39:06 +0000325 Init( argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000326
327 glutReshapeFunc( Reshape );
328 glutKeyboardFunc( Key );
329 glutSpecialFunc( SpecialKey );
330 glutDisplayFunc( Display );
331 glutIdleFunc( Idle );
332
333 glutCreateMenu(ModeMenu);
Brian Paulcbd9a022002-02-13 02:23:33 +0000334
335 for (i = 0; i < NumUnits; i++) {
336 char s[100];
337 sprintf(s, "Toggle Texture %d", i);
338 glutAddMenuEntry(s, TEX0 + i);
339 }
jtgafb833d1999-08-19 00:55:39 +0000340 glutAddMenuEntry("Toggle Animation", ANIMATE);
341 glutAddMenuEntry("Quit", QUIT);
342 glutAttachMenu(GLUT_RIGHT_BUTTON);
343
344 glutMainLoop();
345 return 0;
346}