blob: 85c4e3a266c7f9cd2d9152060d3fa0da580ac05f [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>
José Fonseca9aa73cf2009-02-01 12:00:07 +000017#include <GL/glew.h>
jtgafb833d1999-08-19 00:55:39 +000018#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;
35static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
36
37
jtgafb833d1999-08-19 00:55:39 +000038static void Idle( void )
39{
40 if (Animate) {
Brian Paulcbd9a022002-02-13 02:23:33 +000041 GLint i;
42
Brianfb69fe52007-11-16 15:17:37 -070043 Drift = glutGet(GLUT_ELAPSED_TIME) * 0.001;
jtgafb833d1999-08-19 00:55:39 +000044
Brian Paulcbd9a022002-02-13 02:23:33 +000045 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);
Keith Whitwell740f7de2004-01-27 16:18:00 +000051 glScalef(2, 2, 1);
Brian Paulcbd9a022002-02-13 02:23:33 +000052 }
53 else if (i == 1) {
54 glTranslatef(0.0, Drift, 0.0);
55 }
56 else {
Brianfb69fe52007-11-16 15:17:37 -070057 float tx = 0.5, ty = 0.5;
58 glTranslatef(tx, ty, 0.0);
Brian Paulcbd9a022002-02-13 02:23:33 +000059 glRotatef(180.0 * Drift, 0, 0, 1);
60 glScalef(1.0/i, 1.0/i, 1.0/i);
Brianfb69fe52007-11-16 15:17:37 -070061 glTranslatef(-tx, -ty + i * 0.1, 0.0);
Brian Paulcbd9a022002-02-13 02:23:33 +000062 }
63 }
jtgafb833d1999-08-19 00:55:39 +000064 glMatrixMode(GL_MODELVIEW);
65
66 glutPostRedisplay();
67 }
68}
69
70
71static void DrawObject(void)
72{
Brianfb69fe52007-11-16 15:17:37 -070073 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;
Brian Paulcbd9a022002-02-13 02:23:33 +000076
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 */
81
jtgafb833d1999-08-19 00:55:39 +000082 glBegin(GL_QUADS);
Brianfb69fe52007-11-16 15:17:37 -070083 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]);
Brian Paul785774d2003-05-30 15:30:16 +000088 }
Brianfb69fe52007-11-16 15:17:37 -070089 glVertex2f( vtx_coords[j], vtx_coords[j+1] );
Brian Paul785774d2003-05-30 15:30:16 +000090 }
jtgafb833d1999-08-19 00:55:39 +000091 glEnd();
92}
93
94
jtgafb833d1999-08-19 00:55:39 +000095static void Display( void )
96{
97 glClear( GL_COLOR_BUFFER_BIT );
98
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();
106
107 glutSwapBuffers();
108}
109
110
111static void Reshape( int width, int height )
112{
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 );
121}
122
123
Brianfb69fe52007-11-16 15:17:37 -0700124static void ToggleUnit(int unit)
125{
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");
136}
137
138
jtgafb833d1999-08-19 00:55:39 +0000139static void ModeMenu(int entry)
140{
Brian Paulcbd9a022002-02-13 02:23:33 +0000141 if (entry >= TEX0 && entry <= TEX7) {
142 /* toggle */
143 GLint i = entry - TEX0;
Brianfb69fe52007-11-16 15:17:37 -0700144 ToggleUnit(i);
jtgafb833d1999-08-19 00:55:39 +0000145 }
146 else if (entry==ANIMATE) {
147 Animate = !Animate;
Brianfb69fe52007-11-16 15:17:37 -0700148 if (Animate)
149 glutIdleFunc(Idle);
150 else
151 glutIdleFunc(NULL);
jtgafb833d1999-08-19 00:55:39 +0000152 }
153 else if (entry==QUIT) {
154 exit(0);
155 }
156
jtgafb833d1999-08-19 00:55:39 +0000157 glutPostRedisplay();
158}
159
160
161static void Key( unsigned char key, int x, int y )
162{
163 (void) x;
164 (void) y;
165 switch (key) {
Brianfb69fe52007-11-16 15:17:37 -0700166 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;
jtgafb833d1999-08-19 00:55:39 +0000196 }
197 glutPostRedisplay();
198}
199
200
201static void SpecialKey( int key, int x, int y )
202{
203 float step = 3.0;
204 (void) x;
205 (void) y;
206
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;
220 }
221 glutPostRedisplay();
222}
223
224
Brian Paulac126091999-10-21 16:39:06 +0000225static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000226{
Brian Paulcbd9a022002-02-13 02:23:33 +0000227 GLuint texObj[8];
228 GLint size, i;
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000229
jtgafb833d1999-08-19 00:55:39 +0000230 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);
234 }
235
Brian Paulcbd9a022002-02-13 02:23:33 +0000236 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
237 printf("%d texture units supported\n", NumUnits);
238 if (NumUnits > 8)
239 NumUnits = 8;
Brian Paul563d26b2000-11-01 16:02:01 +0000240
Brian Paul4d99e5b2001-06-20 19:12:30 +0000241 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
242 printf("%d x %d max texture size\n", size, size);
243
Brian Paul68860192001-06-13 14:33:16 +0000244 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
245
Brian Paulcbd9a022002-02-13 02:23:33 +0000246 for (i = 0; i < NumUnits; i++) {
247 if (i < 2)
248 TexEnabled[i] = GL_TRUE;
249 else
250 TexEnabled[i] = GL_FALSE;
251 }
252
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000253 /* allocate two texture objects */
Brian Paulcbd9a022002-02-13 02:23:33 +0000254 glGenTextures(NumUnits, texObj);
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000255
Brian Paulcbd9a022002-02-13 02:23:33 +0000256 /* setup the texture objects */
257 for (i = 0; i < NumUnits; i++) {
258
259 glActiveTextureARB(GL_TEXTURE0_ARB + i);
260 glBindTexture(GL_TEXTURE_2D, texObj[i]);
261
262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
263 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
264
265 if (i == 0) {
266 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
267 printf("Error: couldn't load texture image\n");
268 exit(1);
269 }
270 }
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);
275 }
276 }
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;
287 }
288 else {
289 image[i][j][0] = 25;
290 image[i][j][1] = 25;
291 image[i][j][2] = 25;
292 }
293 }
294 }
295 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
296 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
297 }
298
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);
304
305 if (TexEnabled[i])
306 glEnable(GL_TEXTURE_2D);
jtgafb833d1999-08-19 00:55:39 +0000307 }
308
jtgafb833d1999-08-19 00:55:39 +0000309 glShadeModel(GL_FLAT);
310 glClearColor(0.3, 0.3, 0.4, 1.0);
311
Brian Paulac126091999-10-21 16:39:06 +0000312 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));
317 }
jtgafb833d1999-08-19 00:55:39 +0000318}
319
320
321int main( int argc, char *argv[] )
322{
Brian Paulcbd9a022002-02-13 02:23:33 +0000323 GLint i;
324
jtgafb833d1999-08-19 00:55:39 +0000325 glutInit( &argc, argv );
326 glutInitWindowSize( 300, 300 );
Brian Paul4d598442000-05-23 23:21:00 +0000327 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000328 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
329 glutCreateWindow(argv[0] );
José Fonseca9aa73cf2009-02-01 12:00:07 +0000330 glewInit();
jtgafb833d1999-08-19 00:55:39 +0000331
Brian Paulac126091999-10-21 16:39:06 +0000332 Init( argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000333
334 glutReshapeFunc( Reshape );
335 glutKeyboardFunc( Key );
336 glutSpecialFunc( SpecialKey );
337 glutDisplayFunc( Display );
Brianfb69fe52007-11-16 15:17:37 -0700338 if (Animate)
339 glutIdleFunc(Idle);
jtgafb833d1999-08-19 00:55:39 +0000340
341 glutCreateMenu(ModeMenu);
Brian Paulcbd9a022002-02-13 02:23:33 +0000342
343 for (i = 0; i < NumUnits; i++) {
344 char s[100];
345 sprintf(s, "Toggle Texture %d", i);
346 glutAddMenuEntry(s, TEX0 + i);
347 }
jtgafb833d1999-08-19 00:55:39 +0000348 glutAddMenuEntry("Toggle Animation", ANIMATE);
349 glutAddMenuEntry("Quit", QUIT);
350 glutAttachMenu(GLUT_RIGHT_BUTTON);
351
352 glutMainLoop();
353 return 0;
354}