blob: 451fd11efe12c68fc973b9eef4d42603639f04d3 [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001/*
2 * GL_ARB_multitexture demo
Brian Paulac126091999-10-21 16:39:06 +00003 *
4 * Command line options:
5 * -info print GL implementation information
6 *
7 *
jtgafb833d1999-08-19 00:55:39 +00008 * Brian Paul November 1998 This program is in the public domain.
Brian Paulcbd9a022002-02-13 02:23:33 +00009 * Modified on 12 Feb 2002 for > 2 texture units.
jtgafb833d1999-08-19 00:55:39 +000010 */
11
12
13#include <math.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <GL/glut.h>
18
Brian Paul2d84ed82005-01-09 17:39:06 +000019#include "readtex.h"
jtgafb833d1999-08-19 00:55:39 +000020
21#define TEXTURE_1_FILE "../images/girl.rgb"
22#define TEXTURE_2_FILE "../images/reflect.rgb"
23
24#define TEX0 1
Brian Paulcbd9a022002-02-13 02:23:33 +000025#define TEX7 8
jtgafb833d1999-08-19 00:55:39 +000026#define ANIMATE 10
27#define QUIT 100
28
29static GLboolean Animate = GL_TRUE;
Brian Paulcbd9a022002-02-13 02:23:33 +000030static GLint NumUnits = 1;
31static GLboolean TexEnabled[8];
jtgafb833d1999-08-19 00:55:39 +000032
33static GLfloat Drift = 0.0;
34static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
35
36
jtgafb833d1999-08-19 00:55:39 +000037static void Idle( void )
38{
39 if (Animate) {
Brian Paulcbd9a022002-02-13 02:23:33 +000040 GLint i;
41
Brianfb69fe52007-11-16 15:17:37 -070042 Drift = glutGet(GLUT_ELAPSED_TIME) * 0.001;
jtgafb833d1999-08-19 00:55:39 +000043
Brian Paulcbd9a022002-02-13 02:23:33 +000044 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);
Keith Whitwell740f7de2004-01-27 16:18:00 +000050 glScalef(2, 2, 1);
Brian Paulcbd9a022002-02-13 02:23:33 +000051 }
52 else if (i == 1) {
53 glTranslatef(0.0, Drift, 0.0);
54 }
55 else {
Brianfb69fe52007-11-16 15:17:37 -070056 float tx = 0.5, ty = 0.5;
57 glTranslatef(tx, ty, 0.0);
Brian Paulcbd9a022002-02-13 02:23:33 +000058 glRotatef(180.0 * Drift, 0, 0, 1);
59 glScalef(1.0/i, 1.0/i, 1.0/i);
Brianfb69fe52007-11-16 15:17:37 -070060 glTranslatef(-tx, -ty + i * 0.1, 0.0);
Brian Paulcbd9a022002-02-13 02:23:33 +000061 }
62 }
jtgafb833d1999-08-19 00:55:39 +000063 glMatrixMode(GL_MODELVIEW);
64
65 glutPostRedisplay();
66 }
67}
68
69
70static void DrawObject(void)
71{
Brianfb69fe52007-11-16 15:17:37 -070072 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;
Brian Paulcbd9a022002-02-13 02:23:33 +000075
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
jtgafb833d1999-08-19 00:55:39 +000081 glBegin(GL_QUADS);
Brianfb69fe52007-11-16 15:17:37 -070082 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]);
Brian Paul785774d2003-05-30 15:30:16 +000087 }
Brianfb69fe52007-11-16 15:17:37 -070088 glVertex2f( vtx_coords[j], vtx_coords[j+1] );
Brian Paul785774d2003-05-30 15:30:16 +000089 }
jtgafb833d1999-08-19 00:55:39 +000090 glEnd();
91}
92
93
jtgafb833d1999-08-19 00:55:39 +000094static 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
110static 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
Brianfb69fe52007-11-16 15:17:37 -0700123static 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
jtgafb833d1999-08-19 00:55:39 +0000138static void ModeMenu(int entry)
139{
Brian Paulcbd9a022002-02-13 02:23:33 +0000140 if (entry >= TEX0 && entry <= TEX7) {
141 /* toggle */
142 GLint i = entry - TEX0;
Brianfb69fe52007-11-16 15:17:37 -0700143 ToggleUnit(i);
jtgafb833d1999-08-19 00:55:39 +0000144 }
145 else if (entry==ANIMATE) {
146 Animate = !Animate;
Brianfb69fe52007-11-16 15:17:37 -0700147 if (Animate)
148 glutIdleFunc(Idle);
149 else
150 glutIdleFunc(NULL);
jtgafb833d1999-08-19 00:55:39 +0000151 }
152 else if (entry==QUIT) {
153 exit(0);
154 }
155
jtgafb833d1999-08-19 00:55:39 +0000156 glutPostRedisplay();
157}
158
159
160static void Key( unsigned char key, int x, int y )
161{
162 (void) x;
163 (void) y;
164 switch (key) {
Brianfb69fe52007-11-16 15:17:37 -0700165 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;
jtgafb833d1999-08-19 00:55:39 +0000195 }
196 glutPostRedisplay();
197}
198
199
200static 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
Brian Paulac126091999-10-21 16:39:06 +0000224static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000225{
Brian Paulcbd9a022002-02-13 02:23:33 +0000226 GLuint texObj[8];
227 GLint size, i;
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000228
jtgafb833d1999-08-19 00:55:39 +0000229 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
Brian Paulcbd9a022002-02-13 02:23:33 +0000235 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
236 printf("%d texture units supported\n", NumUnits);
237 if (NumUnits > 8)
238 NumUnits = 8;
Brian Paul563d26b2000-11-01 16:02:01 +0000239
Brian Paul4d99e5b2001-06-20 19:12:30 +0000240 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
241 printf("%d x %d max texture size\n", size, size);
242
Brian Paul68860192001-06-13 14:33:16 +0000243 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
244
Brian Paulcbd9a022002-02-13 02:23:33 +0000245 for (i = 0; i < NumUnits; i++) {
246 if (i < 2)
247 TexEnabled[i] = GL_TRUE;
248 else
249 TexEnabled[i] = GL_FALSE;
250 }
251
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000252 /* allocate two texture objects */
Brian Paulcbd9a022002-02-13 02:23:33 +0000253 glGenTextures(NumUnits, texObj);
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000254
Brian Paulcbd9a022002-02-13 02:23:33 +0000255 /* 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);
jtgafb833d1999-08-19 00:55:39 +0000306 }
307
jtgafb833d1999-08-19 00:55:39 +0000308 glShadeModel(GL_FLAT);
309 glClearColor(0.3, 0.3, 0.4, 1.0);
310
Brian Paulac126091999-10-21 16:39:06 +0000311 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 }
jtgafb833d1999-08-19 00:55:39 +0000317}
318
319
320int main( int argc, char *argv[] )
321{
Brian Paulcbd9a022002-02-13 02:23:33 +0000322 GLint i;
323
jtgafb833d1999-08-19 00:55:39 +0000324 glutInit( &argc, argv );
325 glutInitWindowSize( 300, 300 );
Brian Paul4d598442000-05-23 23:21:00 +0000326 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000327 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
328 glutCreateWindow(argv[0] );
329
Brian Paulac126091999-10-21 16:39:06 +0000330 Init( argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000331
332 glutReshapeFunc( Reshape );
333 glutKeyboardFunc( Key );
334 glutSpecialFunc( SpecialKey );
335 glutDisplayFunc( Display );
Brianfb69fe52007-11-16 15:17:37 -0700336 if (Animate)
337 glutIdleFunc(Idle);
jtgafb833d1999-08-19 00:55:39 +0000338
339 glutCreateMenu(ModeMenu);
Brian Paulcbd9a022002-02-13 02:23:33 +0000340
341 for (i = 0; i < NumUnits; i++) {
342 char s[100];
343 sprintf(s, "Toggle Texture %d", i);
344 glutAddMenuEntry(s, TEX0 + i);
345 }
jtgafb833d1999-08-19 00:55:39 +0000346 glutAddMenuEntry("Toggle Animation", ANIMATE);
347 glutAddMenuEntry("Quit", QUIT);
348 glutAttachMenu(GLUT_RIGHT_BUTTON);
349
350 glutMainLoop();
351 return 0;
352}