blob: 82796a0c3f4f4341c96b2ebac2d5cfe5575886d8 [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
Keith Whitwell19403932009-09-26 08:39:57 +010030static GLint T0 = 0;
31static GLint Frames = 0;
jtgafb833d1999-08-19 00:55:39 +000032static GLboolean Animate = GL_TRUE;
Brian Paulcbd9a022002-02-13 02:23:33 +000033static GLint NumUnits = 1;
34static GLboolean TexEnabled[8];
jtgafb833d1999-08-19 00:55:39 +000035
36static GLfloat Drift = 0.0;
37static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
38
39
jtgafb833d1999-08-19 00:55:39 +000040static void Idle( void )
41{
42 if (Animate) {
Brian Paulcbd9a022002-02-13 02:23:33 +000043 GLint i;
44
Brianfb69fe52007-11-16 15:17:37 -070045 Drift = glutGet(GLUT_ELAPSED_TIME) * 0.001;
jtgafb833d1999-08-19 00:55:39 +000046
Brian Paulcbd9a022002-02-13 02:23:33 +000047 for (i = 0; i < NumUnits; i++) {
48 glActiveTextureARB(GL_TEXTURE0_ARB + i);
49 glMatrixMode(GL_TEXTURE);
50 glLoadIdentity();
51 if (i == 0) {
52 glTranslatef(Drift, 0.0, 0.0);
Keith Whitwell740f7de2004-01-27 16:18:00 +000053 glScalef(2, 2, 1);
Brian Paulcbd9a022002-02-13 02:23:33 +000054 }
55 else if (i == 1) {
56 glTranslatef(0.0, Drift, 0.0);
57 }
58 else {
Brianfb69fe52007-11-16 15:17:37 -070059 float tx = 0.5, ty = 0.5;
60 glTranslatef(tx, ty, 0.0);
Brian Paulcbd9a022002-02-13 02:23:33 +000061 glRotatef(180.0 * Drift, 0, 0, 1);
62 glScalef(1.0/i, 1.0/i, 1.0/i);
Brianfb69fe52007-11-16 15:17:37 -070063 glTranslatef(-tx, -ty + i * 0.1, 0.0);
Brian Paulcbd9a022002-02-13 02:23:33 +000064 }
65 }
jtgafb833d1999-08-19 00:55:39 +000066 glMatrixMode(GL_MODELVIEW);
67
68 glutPostRedisplay();
69 }
70}
71
72
73static void DrawObject(void)
74{
Brianfb69fe52007-11-16 15:17:37 -070075 static const GLfloat tex_coords[] = { 0.0, 0.0, 1.0, 1.0, 0.0 };
76 static const GLfloat vtx_coords[] = { -1.0, -1.0, 1.0, 1.0, -1.0 };
77 GLint i, j;
Brian Paulcbd9a022002-02-13 02:23:33 +000078
79 if (!TexEnabled[0] && !TexEnabled[1])
80 glColor3f(0.1, 0.1, 0.1); /* add onto this */
81 else
82 glColor3f(1, 1, 1); /* modulate this */
83
jtgafb833d1999-08-19 00:55:39 +000084 glBegin(GL_QUADS);
Brianfb69fe52007-11-16 15:17:37 -070085 for (j = 0; j < 4; j++ ) {
86 for (i = 0; i < NumUnits; i++) {
87 if (TexEnabled[i])
88 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i,
89 tex_coords[j], tex_coords[j+1]);
Brian Paul785774d2003-05-30 15:30:16 +000090 }
Brianfb69fe52007-11-16 15:17:37 -070091 glVertex2f( vtx_coords[j], vtx_coords[j+1] );
Brian Paul785774d2003-05-30 15:30:16 +000092 }
jtgafb833d1999-08-19 00:55:39 +000093 glEnd();
94}
95
96
jtgafb833d1999-08-19 00:55:39 +000097static void Display( void )
98{
99 glClear( GL_COLOR_BUFFER_BIT );
100
101 glPushMatrix();
102 glRotatef(Xrot, 1.0, 0.0, 0.0);
103 glRotatef(Yrot, 0.0, 1.0, 0.0);
104 glRotatef(Zrot, 0.0, 0.0, 1.0);
105 glScalef(5.0, 5.0, 5.0);
106 DrawObject();
107 glPopMatrix();
108
109 glutSwapBuffers();
Keith Whitwell19403932009-09-26 08:39:57 +0100110
111 Frames++;
112
113 {
114 GLint t = glutGet(GLUT_ELAPSED_TIME);
115 if (t - T0 >= 5000) {
116 GLfloat seconds = (t - T0) / 1000.0;
117 GLfloat fps = Frames / seconds;
118 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
119 fflush(stdout);
120 T0 = t;
121 Frames = 0;
122 }
123 }
jtgafb833d1999-08-19 00:55:39 +0000124}
125
126
127static void Reshape( int width, int height )
128{
129 glViewport( 0, 0, width, height );
130 glMatrixMode( GL_PROJECTION );
131 glLoadIdentity();
132 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
133 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
134 glMatrixMode( GL_MODELVIEW );
135 glLoadIdentity();
136 glTranslatef( 0.0, 0.0, -70.0 );
137}
138
139
Brianfb69fe52007-11-16 15:17:37 -0700140static void ToggleUnit(int unit)
141{
142 TexEnabled[unit] = !TexEnabled[unit];
143 glActiveTextureARB(GL_TEXTURE0_ARB + unit);
144 if (TexEnabled[unit])
145 glEnable(GL_TEXTURE_2D);
146 else
147 glDisable(GL_TEXTURE_2D);
148 printf("Enabled: ");
149 for (unit = 0; unit < NumUnits; unit++)
150 printf("%d ", (int) TexEnabled[unit]);
151 printf("\n");
152}
153
154
jtgafb833d1999-08-19 00:55:39 +0000155static 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;
Brianfb69fe52007-11-16 15:17:37 -0700160 ToggleUnit(i);
jtgafb833d1999-08-19 00:55:39 +0000161 }
162 else if (entry==ANIMATE) {
163 Animate = !Animate;
Brianfb69fe52007-11-16 15:17:37 -0700164 if (Animate)
165 glutIdleFunc(Idle);
166 else
167 glutIdleFunc(NULL);
jtgafb833d1999-08-19 00:55:39 +0000168 }
169 else if (entry==QUIT) {
170 exit(0);
171 }
172
jtgafb833d1999-08-19 00:55:39 +0000173 glutPostRedisplay();
174}
175
176
177static void Key( unsigned char key, int x, int y )
178{
179 (void) x;
180 (void) y;
181 switch (key) {
Brianfb69fe52007-11-16 15:17:37 -0700182 case 'a':
183 Animate = !Animate;
184 break;
185 case '0':
186 ToggleUnit(0);
187 break;
188 case '1':
189 ToggleUnit(1);
190 break;
191 case '2':
192 ToggleUnit(2);
193 break;
194 case '3':
195 ToggleUnit(3);
196 break;
197 case '4':
198 ToggleUnit(4);
199 break;
200 case '5':
201 ToggleUnit(5);
202 break;
203 case '6':
204 ToggleUnit(6);
205 break;
206 case '7':
207 ToggleUnit(7);
208 break;
209 case 27:
210 exit(0);
211 break;
jtgafb833d1999-08-19 00:55:39 +0000212 }
213 glutPostRedisplay();
214}
215
216
217static void SpecialKey( int key, int x, int y )
218{
219 float step = 3.0;
220 (void) x;
221 (void) y;
222
223 switch (key) {
224 case GLUT_KEY_UP:
225 Xrot += step;
226 break;
227 case GLUT_KEY_DOWN:
228 Xrot -= step;
229 break;
230 case GLUT_KEY_LEFT:
231 Yrot += step;
232 break;
233 case GLUT_KEY_RIGHT:
234 Yrot -= step;
235 break;
236 }
237 glutPostRedisplay();
238}
239
240
Brian Paulac126091999-10-21 16:39:06 +0000241static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000242{
Brian Paulcbd9a022002-02-13 02:23:33 +0000243 GLuint texObj[8];
244 GLint size, i;
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000245
jtgafb833d1999-08-19 00:55:39 +0000246 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
247 if (!strstr(exten, "GL_ARB_multitexture")) {
248 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
249 exit(1);
250 }
251
Brian Paulcbd9a022002-02-13 02:23:33 +0000252 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
253 printf("%d texture units supported\n", NumUnits);
254 if (NumUnits > 8)
255 NumUnits = 8;
Brian Paul563d26b2000-11-01 16:02:01 +0000256
Brian Paul4d99e5b2001-06-20 19:12:30 +0000257 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
258 printf("%d x %d max texture size\n", size, size);
259
Brian Paul68860192001-06-13 14:33:16 +0000260 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
261
Brian Paulcbd9a022002-02-13 02:23:33 +0000262 for (i = 0; i < NumUnits; i++) {
263 if (i < 2)
264 TexEnabled[i] = GL_TRUE;
265 else
266 TexEnabled[i] = GL_FALSE;
267 }
268
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000269 /* allocate two texture objects */
Brian Paulcbd9a022002-02-13 02:23:33 +0000270 glGenTextures(NumUnits, texObj);
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000271
Brian Paulcbd9a022002-02-13 02:23:33 +0000272 /* setup the texture objects */
273 for (i = 0; i < NumUnits; i++) {
274
275 glActiveTextureARB(GL_TEXTURE0_ARB + i);
276 glBindTexture(GL_TEXTURE_2D, texObj[i]);
277
278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
279 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
280
281 if (i == 0) {
282 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
283 printf("Error: couldn't load texture image\n");
284 exit(1);
285 }
286 }
287 else if (i == 1) {
288 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
289 printf("Error: couldn't load texture image\n");
290 exit(1);
291 }
292 }
293 else {
294 /* checker */
295 GLubyte image[8][8][3];
296 GLint i, j;
297 for (i = 0; i < 8; i++) {
298 for (j = 0; j < 8; j++) {
299 if ((i + j) & 1) {
300 image[i][j][0] = 50;
301 image[i][j][1] = 50;
302 image[i][j][2] = 50;
303 }
304 else {
305 image[i][j][0] = 25;
306 image[i][j][1] = 25;
307 image[i][j][2] = 25;
308 }
309 }
310 }
311 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
312 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
313 }
314
315 /* Bind texObj[i] to ith texture unit */
316 if (i < 2)
317 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
318 else
319 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
320
321 if (TexEnabled[i])
322 glEnable(GL_TEXTURE_2D);
jtgafb833d1999-08-19 00:55:39 +0000323 }
324
jtgafb833d1999-08-19 00:55:39 +0000325 glShadeModel(GL_FLAT);
326 glClearColor(0.3, 0.3, 0.4, 1.0);
327
Brian Paulac126091999-10-21 16:39:06 +0000328 if (argc > 1 && strcmp(argv[1], "-info")==0) {
329 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
330 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
331 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
332 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
333 }
jtgafb833d1999-08-19 00:55:39 +0000334}
335
336
337int main( int argc, char *argv[] )
338{
Brian Paulcbd9a022002-02-13 02:23:33 +0000339 GLint i;
340
jtgafb833d1999-08-19 00:55:39 +0000341 glutInitWindowSize( 300, 300 );
Brian Paul263f4322009-12-18 08:12:55 -0700342 glutInit( &argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000343 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
344 glutCreateWindow(argv[0] );
José Fonseca9aa73cf2009-02-01 12:00:07 +0000345 glewInit();
jtgafb833d1999-08-19 00:55:39 +0000346
Brian Paulac126091999-10-21 16:39:06 +0000347 Init( argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000348
349 glutReshapeFunc( Reshape );
350 glutKeyboardFunc( Key );
351 glutSpecialFunc( SpecialKey );
352 glutDisplayFunc( Display );
Brianfb69fe52007-11-16 15:17:37 -0700353 if (Animate)
354 glutIdleFunc(Idle);
jtgafb833d1999-08-19 00:55:39 +0000355
356 glutCreateMenu(ModeMenu);
Brian Paulcbd9a022002-02-13 02:23:33 +0000357
358 for (i = 0; i < NumUnits; i++) {
359 char s[100];
360 sprintf(s, "Toggle Texture %d", i);
361 glutAddMenuEntry(s, TEX0 + i);
362 }
jtgafb833d1999-08-19 00:55:39 +0000363 glutAddMenuEntry("Toggle Animation", ANIMATE);
364 glutAddMenuEntry("Quit", QUIT);
365 glutAttachMenu(GLUT_RIGHT_BUTTON);
366
367 glutMainLoop();
368 return 0;
369}