blob: c4671b516c47209477cce467164ce319e539b983 [file] [log] [blame]
Brian Paulcbd9a022002-02-13 02:23:33 +00001/* $Id: multiarb.c,v 1.11 2002/02/13 02:23:33 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
4 * GL_ARB_multitexture demo
Brian Paulac126091999-10-21 16:39:06 +00005 *
6 * Command line options:
7 * -info print GL implementation information
8 *
9 *
jtgafb833d1999-08-19 00:55:39 +000010 * Brian Paul November 1998 This program is in the public domain.
Brian Paulcbd9a022002-02-13 02:23:33 +000011 * Modified on 12 Feb 2002 for > 2 texture units.
jtgafb833d1999-08-19 00:55:39 +000012 */
13
14
15#include <math.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <GL/glut.h>
20
pescod1ff1f62000-12-24 22:53:54 +000021#include "readtex.c" /* I know, this is a hack. */
jtgafb833d1999-08-19 00:55:39 +000022
23#define TEXTURE_1_FILE "../images/girl.rgb"
24#define TEXTURE_2_FILE "../images/reflect.rgb"
25
26#define TEX0 1
Brian Paulcbd9a022002-02-13 02:23:33 +000027#define TEX7 8
jtgafb833d1999-08-19 00:55:39 +000028#define ANIMATE 10
29#define QUIT 100
30
31static GLboolean Animate = GL_TRUE;
Brian Paulcbd9a022002-02-13 02:23:33 +000032static GLint NumUnits = 1;
33static GLboolean TexEnabled[8];
jtgafb833d1999-08-19 00:55:39 +000034
35static GLfloat Drift = 0.0;
36static 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
jtgafb833d1999-08-19 00:55:39 +000045 Drift += 0.05;
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);
55 glScalef(2, 2, 2);
56 }
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;
77
78 if (!TexEnabled[0] && !TexEnabled[1])
79 glColor3f(0.1, 0.1, 0.1); /* add onto this */
80 else
81 glColor3f(1, 1, 1); /* modulate this */
82
jtgafb833d1999-08-19 00:55:39 +000083 glBegin(GL_QUADS);
84
Brian Paulcbd9a022002-02-13 02:23:33 +000085 for (i = 0; i < NumUnits; i++)
86 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, 0.0, 0.0);
jtgafb833d1999-08-19 00:55:39 +000087 glVertex2f(-1.0, -1.0);
88
Brian Paulcbd9a022002-02-13 02:23:33 +000089 for (i = 0; i < NumUnits; i++)
90 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, 1.0, 0.0);
jtgafb833d1999-08-19 00:55:39 +000091 glVertex2f(1.0, -1.0);
92
Brian Paulcbd9a022002-02-13 02:23:33 +000093 for (i = 0; i < NumUnits; i++)
94 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, 1.0, 1.0);
jtgafb833d1999-08-19 00:55:39 +000095 glVertex2f(1.0, 1.0);
96
Brian Paulcbd9a022002-02-13 02:23:33 +000097 for (i = 0; i < NumUnits; i++)
98 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, 0.0, 1.0);
jtgafb833d1999-08-19 00:55:39 +000099 glVertex2f(-1.0, 1.0);
jtgafb833d1999-08-19 00:55:39 +0000100
101 glEnd();
102}
103
104
105
106static void Display( void )
107{
108 glClear( GL_COLOR_BUFFER_BIT );
109
110 glPushMatrix();
111 glRotatef(Xrot, 1.0, 0.0, 0.0);
112 glRotatef(Yrot, 0.0, 1.0, 0.0);
113 glRotatef(Zrot, 0.0, 0.0, 1.0);
114 glScalef(5.0, 5.0, 5.0);
115 DrawObject();
116 glPopMatrix();
117
118 glutSwapBuffers();
119}
120
121
122static void Reshape( int width, int height )
123{
124 glViewport( 0, 0, width, height );
125 glMatrixMode( GL_PROJECTION );
126 glLoadIdentity();
127 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
128 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
129 glMatrixMode( GL_MODELVIEW );
130 glLoadIdentity();
131 glTranslatef( 0.0, 0.0, -70.0 );
132}
133
134
135static void ModeMenu(int entry)
136{
Brian Paulcbd9a022002-02-13 02:23:33 +0000137 if (entry >= TEX0 && entry <= TEX7) {
138 /* toggle */
139 GLint i = entry - TEX0;
140 TexEnabled[i] = !TexEnabled[i];
141 glActiveTextureARB(GL_TEXTURE0_ARB + i);
142 if (TexEnabled[i])
143 glEnable(GL_TEXTURE_2D);
144 else
145 glDisable(GL_TEXTURE_2D);
146 printf("Enabled: ");
147 for (i = 0; i < NumUnits; i++)
148 printf("%d ", (int) TexEnabled[i]);
149 printf("\n");
jtgafb833d1999-08-19 00:55:39 +0000150 }
151 else if (entry==ANIMATE) {
152 Animate = !Animate;
153 }
154 else if (entry==QUIT) {
155 exit(0);
156 }
157
jtgafb833d1999-08-19 00:55:39 +0000158 glutPostRedisplay();
159}
160
161
162static void Key( unsigned char key, int x, int y )
163{
164 (void) x;
165 (void) y;
166 switch (key) {
167 case 27:
168 exit(0);
169 break;
170 }
171 glutPostRedisplay();
172}
173
174
175static void SpecialKey( int key, int x, int y )
176{
177 float step = 3.0;
178 (void) x;
179 (void) y;
180
181 switch (key) {
182 case GLUT_KEY_UP:
183 Xrot += step;
184 break;
185 case GLUT_KEY_DOWN:
186 Xrot -= step;
187 break;
188 case GLUT_KEY_LEFT:
189 Yrot += step;
190 break;
191 case GLUT_KEY_RIGHT:
192 Yrot -= step;
193 break;
194 }
195 glutPostRedisplay();
196}
197
198
Brian Paulac126091999-10-21 16:39:06 +0000199static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000200{
Brian Paulcbd9a022002-02-13 02:23:33 +0000201 GLuint texObj[8];
202 GLint size, i;
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000203
jtgafb833d1999-08-19 00:55:39 +0000204 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
205 if (!strstr(exten, "GL_ARB_multitexture")) {
206 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
207 exit(1);
208 }
209
Brian Paulcbd9a022002-02-13 02:23:33 +0000210 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
211 printf("%d texture units supported\n", NumUnits);
212 if (NumUnits > 8)
213 NumUnits = 8;
Brian Paul563d26b2000-11-01 16:02:01 +0000214
Brian Paul4d99e5b2001-06-20 19:12:30 +0000215 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
216 printf("%d x %d max texture size\n", size, size);
217
Brian Paul68860192001-06-13 14:33:16 +0000218 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
219
Brian Paulcbd9a022002-02-13 02:23:33 +0000220 for (i = 0; i < NumUnits; i++) {
221 if (i < 2)
222 TexEnabled[i] = GL_TRUE;
223 else
224 TexEnabled[i] = GL_FALSE;
225 }
226
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000227 /* allocate two texture objects */
Brian Paulcbd9a022002-02-13 02:23:33 +0000228 glGenTextures(NumUnits, texObj);
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000229
Brian Paulcbd9a022002-02-13 02:23:33 +0000230 /* setup the texture objects */
231 for (i = 0; i < NumUnits; i++) {
232
233 glActiveTextureARB(GL_TEXTURE0_ARB + i);
234 glBindTexture(GL_TEXTURE_2D, texObj[i]);
235
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
238
239 if (i == 0) {
240 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
241 printf("Error: couldn't load texture image\n");
242 exit(1);
243 }
244 }
245 else if (i == 1) {
246 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
247 printf("Error: couldn't load texture image\n");
248 exit(1);
249 }
250 }
251 else {
252 /* checker */
253 GLubyte image[8][8][3];
254 GLint i, j;
255 for (i = 0; i < 8; i++) {
256 for (j = 0; j < 8; j++) {
257 if ((i + j) & 1) {
258 image[i][j][0] = 50;
259 image[i][j][1] = 50;
260 image[i][j][2] = 50;
261 }
262 else {
263 image[i][j][0] = 25;
264 image[i][j][1] = 25;
265 image[i][j][2] = 25;
266 }
267 }
268 }
269 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
270 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
271 }
272
273 /* Bind texObj[i] to ith texture unit */
274 if (i < 2)
275 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
276 else
277 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
278
279 if (TexEnabled[i])
280 glEnable(GL_TEXTURE_2D);
jtgafb833d1999-08-19 00:55:39 +0000281 }
282
jtgafb833d1999-08-19 00:55:39 +0000283 glShadeModel(GL_FLAT);
284 glClearColor(0.3, 0.3, 0.4, 1.0);
285
Brian Paulac126091999-10-21 16:39:06 +0000286 if (argc > 1 && strcmp(argv[1], "-info")==0) {
287 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
288 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
289 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
290 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
291 }
jtgafb833d1999-08-19 00:55:39 +0000292}
293
294
295int main( int argc, char *argv[] )
296{
Brian Paulcbd9a022002-02-13 02:23:33 +0000297 GLint i;
298
jtgafb833d1999-08-19 00:55:39 +0000299 glutInit( &argc, argv );
300 glutInitWindowSize( 300, 300 );
Brian Paul4d598442000-05-23 23:21:00 +0000301 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000302 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
303 glutCreateWindow(argv[0] );
304
Brian Paulac126091999-10-21 16:39:06 +0000305 Init( argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000306
307 glutReshapeFunc( Reshape );
308 glutKeyboardFunc( Key );
309 glutSpecialFunc( SpecialKey );
310 glutDisplayFunc( Display );
311 glutIdleFunc( Idle );
312
313 glutCreateMenu(ModeMenu);
Brian Paulcbd9a022002-02-13 02:23:33 +0000314
315 for (i = 0; i < NumUnits; i++) {
316 char s[100];
317 sprintf(s, "Toggle Texture %d", i);
318 glutAddMenuEntry(s, TEX0 + i);
319 }
jtgafb833d1999-08-19 00:55:39 +0000320 glutAddMenuEntry("Toggle Animation", ANIMATE);
321 glutAddMenuEntry("Quit", QUIT);
322 glutAttachMenu(GLUT_RIGHT_BUTTON);
323
324 glutMainLoop();
325 return 0;
326}