blob: d963985c69de0012520a52de4a8eb2fb5e922a37 [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
Brian Paul2d84ed82005-01-09 17:39:06 +000020#include "readtex.h"
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
jtgafb833d1999-08-19 00:55:39 +000039static void Idle( void )
40{
41 if (Animate) {
Brian Paulcbd9a022002-02-13 02:23:33 +000042 GLint i;
43
Brian Paul785774d2003-05-30 15:30:16 +000044 Drift += drift_increment;
Brian Paulb45c71a2000-02-02 17:31:45 +000045 if (Drift >= 1.0)
Brian Pauld2702f02000-02-02 01:07:21 +000046 Drift = 0.0;
jtgafb833d1999-08-19 00:55:39 +000047
Brian Paulcbd9a022002-02-13 02:23:33 +000048 for (i = 0; i < NumUnits; i++) {
49 glActiveTextureARB(GL_TEXTURE0_ARB + i);
50 glMatrixMode(GL_TEXTURE);
51 glLoadIdentity();
52 if (i == 0) {
53 glTranslatef(Drift, 0.0, 0.0);
Keith Whitwell740f7de2004-01-27 16:18:00 +000054 glScalef(2, 2, 1);
Brian Paulcbd9a022002-02-13 02:23:33 +000055 }
56 else if (i == 1) {
57 glTranslatef(0.0, Drift, 0.0);
58 }
59 else {
60 glTranslatef(0.5, 0.5, 0.0);
61 glRotatef(180.0 * Drift, 0, 0, 1);
62 glScalef(1.0/i, 1.0/i, 1.0/i);
63 glTranslatef(-0.5, -0.5, 0.0);
64 }
65 }
jtgafb833d1999-08-19 00:55:39 +000066 glMatrixMode(GL_MODELVIEW);
67
68 glutPostRedisplay();
69 }
70}
71
72
73static void DrawObject(void)
74{
Brian Paulcbd9a022002-02-13 02:23:33 +000075 GLint i;
Brian Paul785774d2003-05-30 15:30:16 +000076 GLint j;
77 static const GLfloat tex_coords[] = { 0.0, 0.0, 1.0, 1.0, 0.0 };
78 static const GLfloat vtx_coords[] = { -1.0, -1.0, 1.0, 1.0, -1.0 };
Brian Paulcbd9a022002-02-13 02:23:33 +000079
80 if (!TexEnabled[0] && !TexEnabled[1])
81 glColor3f(0.1, 0.1, 0.1); /* add onto this */
82 else
83 glColor3f(1, 1, 1); /* modulate this */
84
jtgafb833d1999-08-19 00:55:39 +000085 glBegin(GL_QUADS);
86
Brian Paul785774d2003-05-30 15:30:16 +000087 /* Toggle between the vector and scalar entry points. This is done purely
88 * to hit multiple paths in the driver.
89 */
90 if ( Drift > 0.49 ) {
91 for (j = 0; j < 4; j++ ) {
92 for (i = 0; i < NumUnits; i++)
93 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i,
94 tex_coords[j], tex_coords[j+1]);
95 glVertex2f( vtx_coords[j], vtx_coords[j+1] );
96 }
97 }
98 else {
99 for (j = 0; j < 4; j++ ) {
100 for (i = 0; i < NumUnits; i++)
101 glMultiTexCoord2fvARB(GL_TEXTURE0_ARB + i, & tex_coords[j]);
102 glVertex2fv( & vtx_coords[j] );
103 }
104 }
jtgafb833d1999-08-19 00:55:39 +0000105
106 glEnd();
107}
108
109
110
111static void Display( void )
112{
Brian Paul785774d2003-05-30 15:30:16 +0000113 static GLint T0 = 0;
114 static GLint Frames = 0;
115 GLint t;
116
jtgafb833d1999-08-19 00:55:39 +0000117 glClear( GL_COLOR_BUFFER_BIT );
118
119 glPushMatrix();
120 glRotatef(Xrot, 1.0, 0.0, 0.0);
121 glRotatef(Yrot, 0.0, 1.0, 0.0);
122 glRotatef(Zrot, 0.0, 0.0, 1.0);
123 glScalef(5.0, 5.0, 5.0);
124 DrawObject();
125 glPopMatrix();
126
127 glutSwapBuffers();
Brian Paul785774d2003-05-30 15:30:16 +0000128
129 Frames++;
130
131 t = glutGet(GLUT_ELAPSED_TIME);
132 if (t - T0 >= 250) {
133 GLfloat seconds = (t - T0) / 1000.0;
134 drift_increment = 2.2 * seconds / Frames;
135 T0 = t;
136 Frames = 0;
137 }
jtgafb833d1999-08-19 00:55:39 +0000138}
139
140
141static void Reshape( int width, int height )
142{
143 glViewport( 0, 0, width, height );
144 glMatrixMode( GL_PROJECTION );
145 glLoadIdentity();
146 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
147 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
148 glMatrixMode( GL_MODELVIEW );
149 glLoadIdentity();
150 glTranslatef( 0.0, 0.0, -70.0 );
151}
152
153
154static void ModeMenu(int entry)
155{
Brian Paulcbd9a022002-02-13 02:23:33 +0000156 if (entry >= TEX0 && entry <= TEX7) {
157 /* toggle */
158 GLint i = entry - TEX0;
159 TexEnabled[i] = !TexEnabled[i];
160 glActiveTextureARB(GL_TEXTURE0_ARB + i);
161 if (TexEnabled[i])
162 glEnable(GL_TEXTURE_2D);
163 else
164 glDisable(GL_TEXTURE_2D);
165 printf("Enabled: ");
166 for (i = 0; i < NumUnits; i++)
167 printf("%d ", (int) TexEnabled[i]);
168 printf("\n");
jtgafb833d1999-08-19 00:55:39 +0000169 }
170 else if (entry==ANIMATE) {
171 Animate = !Animate;
172 }
173 else if (entry==QUIT) {
174 exit(0);
175 }
176
jtgafb833d1999-08-19 00:55:39 +0000177 glutPostRedisplay();
178}
179
180
181static void Key( unsigned char key, int x, int y )
182{
183 (void) x;
184 (void) y;
185 switch (key) {
186 case 27:
187 exit(0);
188 break;
189 }
190 glutPostRedisplay();
191}
192
193
194static void SpecialKey( int key, int x, int y )
195{
196 float step = 3.0;
197 (void) x;
198 (void) y;
199
200 switch (key) {
201 case GLUT_KEY_UP:
202 Xrot += step;
203 break;
204 case GLUT_KEY_DOWN:
205 Xrot -= step;
206 break;
207 case GLUT_KEY_LEFT:
208 Yrot += step;
209 break;
210 case GLUT_KEY_RIGHT:
211 Yrot -= step;
212 break;
213 }
214 glutPostRedisplay();
215}
216
217
Brian Paulac126091999-10-21 16:39:06 +0000218static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000219{
Brian Paulcbd9a022002-02-13 02:23:33 +0000220 GLuint texObj[8];
221 GLint size, i;
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000222
jtgafb833d1999-08-19 00:55:39 +0000223 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
224 if (!strstr(exten, "GL_ARB_multitexture")) {
225 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
226 exit(1);
227 }
228
Brian Paulcbd9a022002-02-13 02:23:33 +0000229 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
230 printf("%d texture units supported\n", NumUnits);
231 if (NumUnits > 8)
232 NumUnits = 8;
Brian Paul563d26b2000-11-01 16:02:01 +0000233
Brian Paul4d99e5b2001-06-20 19:12:30 +0000234 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
235 printf("%d x %d max texture size\n", size, size);
236
Brian Paul68860192001-06-13 14:33:16 +0000237 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
238
Brian Paulcbd9a022002-02-13 02:23:33 +0000239 for (i = 0; i < NumUnits; i++) {
240 if (i < 2)
241 TexEnabled[i] = GL_TRUE;
242 else
243 TexEnabled[i] = GL_FALSE;
244 }
245
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000246 /* allocate two texture objects */
Brian Paulcbd9a022002-02-13 02:23:33 +0000247 glGenTextures(NumUnits, texObj);
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000248
Brian Paulcbd9a022002-02-13 02:23:33 +0000249 /* setup the texture objects */
250 for (i = 0; i < NumUnits; i++) {
251
252 glActiveTextureARB(GL_TEXTURE0_ARB + i);
253 glBindTexture(GL_TEXTURE_2D, texObj[i]);
254
255 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
256 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
257
258 if (i == 0) {
259 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
260 printf("Error: couldn't load texture image\n");
261 exit(1);
262 }
263 }
264 else if (i == 1) {
265 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
266 printf("Error: couldn't load texture image\n");
267 exit(1);
268 }
269 }
270 else {
271 /* checker */
272 GLubyte image[8][8][3];
273 GLint i, j;
274 for (i = 0; i < 8; i++) {
275 for (j = 0; j < 8; j++) {
276 if ((i + j) & 1) {
277 image[i][j][0] = 50;
278 image[i][j][1] = 50;
279 image[i][j][2] = 50;
280 }
281 else {
282 image[i][j][0] = 25;
283 image[i][j][1] = 25;
284 image[i][j][2] = 25;
285 }
286 }
287 }
288 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
289 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
290 }
291
292 /* Bind texObj[i] to ith texture unit */
293 if (i < 2)
294 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
295 else
296 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
297
298 if (TexEnabled[i])
299 glEnable(GL_TEXTURE_2D);
jtgafb833d1999-08-19 00:55:39 +0000300 }
301
jtgafb833d1999-08-19 00:55:39 +0000302 glShadeModel(GL_FLAT);
303 glClearColor(0.3, 0.3, 0.4, 1.0);
304
Brian Paulac126091999-10-21 16:39:06 +0000305 if (argc > 1 && strcmp(argv[1], "-info")==0) {
306 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
307 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
308 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
309 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
310 }
jtgafb833d1999-08-19 00:55:39 +0000311}
312
313
314int main( int argc, char *argv[] )
315{
Brian Paulcbd9a022002-02-13 02:23:33 +0000316 GLint i;
317
jtgafb833d1999-08-19 00:55:39 +0000318 glutInit( &argc, argv );
319 glutInitWindowSize( 300, 300 );
Brian Paul4d598442000-05-23 23:21:00 +0000320 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000321 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
322 glutCreateWindow(argv[0] );
323
Brian Paulac126091999-10-21 16:39:06 +0000324 Init( argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000325
326 glutReshapeFunc( Reshape );
327 glutKeyboardFunc( Key );
328 glutSpecialFunc( SpecialKey );
329 glutDisplayFunc( Display );
330 glutIdleFunc( Idle );
331
332 glutCreateMenu(ModeMenu);
Brian Paulcbd9a022002-02-13 02:23:33 +0000333
334 for (i = 0; i < NumUnits; i++) {
335 char s[100];
336 sprintf(s, "Toggle Texture %d", i);
337 glutAddMenuEntry(s, TEX0 + i);
338 }
jtgafb833d1999-08-19 00:55:39 +0000339 glutAddMenuEntry("Toggle Animation", ANIMATE);
340 glutAddMenuEntry("Quit", QUIT);
341 glutAttachMenu(GLUT_RIGHT_BUTTON);
342
343 glutMainLoop();
344 return 0;
345}