blob: 6650f28c3e9fe0e2cb13ae9ed2d1f7a34cae0803 [file] [log] [blame]
Brian Paulb45c71a2000-02-02 17:31:45 +00001/* $Id: multiarb.c,v 1.5 2000/02/02 17:31:45 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.
11 */
12
13/*
14 * $Log: multiarb.c,v $
Brian Paulb45c71a2000-02-02 17:31:45 +000015 * Revision 1.5 2000/02/02 17:31:45 brianp
16 * changed > to >=
17 *
Brian Pauld2702f02000-02-02 01:07:21 +000018 * Revision 1.4 2000/02/02 01:07:21 brianp
19 * limit Drift to [0, 1]
20 *
Brian Paulac126091999-10-21 16:39:06 +000021 * Revision 1.3 1999/10/21 16:40:32 brianp
22 * added -info command line option
23 *
Brian Paul1a3b8ff1999-10-13 12:02:13 +000024 * Revision 1.2 1999/10/13 12:02:13 brianp
25 * use texture objects now
26 *
27 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
28 * Imported sources
jtgafb833d1999-08-19 00:55:39 +000029 *
30 * Revision 1.3 1999/03/28 18:20:49 brianp
31 * minor clean-up
32 *
33 * Revision 1.2 1998/11/05 04:34:04 brianp
34 * moved image files to ../images/ directory
35 *
36 * Revision 1.1 1998/11/03 01:36:33 brianp
37 * Initial revision
38 *
39 */
40
41
42#include <math.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <GL/glut.h>
47
48#include "../util/readtex.c" /* I know, this is a hack. */
49
50#define TEXTURE_1_FILE "../images/girl.rgb"
51#define TEXTURE_2_FILE "../images/reflect.rgb"
52
53#define TEX0 1
54#define TEX1 2
55#define TEXBOTH 3
56#define ANIMATE 10
57#define QUIT 100
58
59static GLboolean Animate = GL_TRUE;
60
61static GLfloat Drift = 0.0;
62static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
63
64
65
66static void Idle( void )
67{
68 if (Animate) {
69 Drift += 0.05;
Brian Paulb45c71a2000-02-02 17:31:45 +000070 if (Drift >= 1.0)
Brian Pauld2702f02000-02-02 01:07:21 +000071 Drift = 0.0;
jtgafb833d1999-08-19 00:55:39 +000072
73#ifdef GL_ARB_multitexture
74 glActiveTextureARB(GL_TEXTURE0_ARB);
75#endif
76 glMatrixMode(GL_TEXTURE);
77 glLoadIdentity();
78 glTranslatef(Drift, 0.0, 0.0);
79 glMatrixMode(GL_MODELVIEW);
80
81#ifdef GL_ARB_multitexture
82 glActiveTextureARB(GL_TEXTURE1_ARB);
83#endif
84 glMatrixMode(GL_TEXTURE);
85 glLoadIdentity();
86 glTranslatef(0.0, Drift, 0.0);
87 glMatrixMode(GL_MODELVIEW);
88
89 glutPostRedisplay();
90 }
91}
92
93
94static void DrawObject(void)
95{
96 glBegin(GL_QUADS);
97
98#ifdef GL_ARB_multitexture
99 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
100 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
101 glVertex2f(-1.0, -1.0);
102
103 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 0.0);
104 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
105 glVertex2f(1.0, -1.0);
106
107 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 2.0);
108 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
109 glVertex2f(1.0, 1.0);
110
111 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 2.0);
112 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
113 glVertex2f(-1.0, 1.0);
114#else
115 glTexCoord2f(0.0, 0.0);
116 glVertex2f(-1.0, -1.0);
117
118 glTexCoord2f(1.0, 0.0);
119 glVertex2f(1.0, -1.0);
120
121 glTexCoord2f(1.0, 1.0);
122 glVertex2f(1.0, 1.0);
123
124 glTexCoord2f(0.0, 1.0);
125 glVertex2f(-1.0, 1.0);
126#endif
127
128 glEnd();
129}
130
131
132
133static void Display( void )
134{
135 glClear( GL_COLOR_BUFFER_BIT );
136
137 glPushMatrix();
138 glRotatef(Xrot, 1.0, 0.0, 0.0);
139 glRotatef(Yrot, 0.0, 1.0, 0.0);
140 glRotatef(Zrot, 0.0, 0.0, 1.0);
141 glScalef(5.0, 5.0, 5.0);
142 DrawObject();
143 glPopMatrix();
144
145 glutSwapBuffers();
146}
147
148
149static void Reshape( int width, int height )
150{
151 glViewport( 0, 0, width, height );
152 glMatrixMode( GL_PROJECTION );
153 glLoadIdentity();
154 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
155 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
156 glMatrixMode( GL_MODELVIEW );
157 glLoadIdentity();
158 glTranslatef( 0.0, 0.0, -70.0 );
159}
160
161
162static void ModeMenu(int entry)
163{
164 GLboolean enable0 = GL_FALSE, enable1 = GL_FALSE;
165 if (entry==TEX0) {
166 enable0 = GL_TRUE;
167 }
168 else if (entry==TEX1) {
169 enable1 = GL_TRUE;
170 }
171 else if (entry==TEXBOTH) {
172 enable0 = GL_TRUE;
173 enable1 = GL_TRUE;
174 }
175 else if (entry==ANIMATE) {
176 Animate = !Animate;
177 }
178 else if (entry==QUIT) {
179 exit(0);
180 }
181
182 if (entry != ANIMATE) {
183#ifdef GL_ARB_multitexture
184 glActiveTextureARB(GL_TEXTURE0_ARB);
185#endif
186 if (enable0) {
187 glEnable(GL_TEXTURE_2D);
188 }
189 else
190 glDisable(GL_TEXTURE_2D);
191
192#ifdef GL_ARB_multitexture
193 glActiveTextureARB(GL_TEXTURE1_ARB);
194#endif
195 if (enable1) {
196 glEnable(GL_TEXTURE_2D);
197 }
198 else
199 glDisable(GL_TEXTURE_2D);
200 }
201
202 glutPostRedisplay();
203}
204
205
206static void Key( unsigned char key, int x, int y )
207{
208 (void) x;
209 (void) y;
210 switch (key) {
211 case 27:
212 exit(0);
213 break;
214 }
215 glutPostRedisplay();
216}
217
218
219static void SpecialKey( int key, int x, int y )
220{
221 float step = 3.0;
222 (void) x;
223 (void) y;
224
225 switch (key) {
226 case GLUT_KEY_UP:
227 Xrot += step;
228 break;
229 case GLUT_KEY_DOWN:
230 Xrot -= step;
231 break;
232 case GLUT_KEY_LEFT:
233 Yrot += step;
234 break;
235 case GLUT_KEY_RIGHT:
236 Yrot -= step;
237 break;
238 }
239 glutPostRedisplay();
240}
241
242
Brian Paulac126091999-10-21 16:39:06 +0000243static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000244{
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000245 GLuint texObj[2];
246
jtgafb833d1999-08-19 00:55:39 +0000247 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
248 if (!strstr(exten, "GL_ARB_multitexture")) {
249 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
250 exit(1);
251 }
252
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000253 /* allocate two texture objects */
254 glGenTextures(2, texObj);
255
256 /* setup texture obj 0 */
257 glBindTexture(GL_TEXTURE_2D, texObj[0]);
jtgafb833d1999-08-19 00:55:39 +0000258#ifdef LINEAR_FILTER
259 /* linear filtering looks much nicer but is much slower for Mesa */
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
262#else
263 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
264 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
265#endif
266
267 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
268
269 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
270
271 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
272 printf("Error: couldn't load texture image\n");
273 exit(1);
274 }
275
276
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000277 /* setup texture obj 1 */
278 glBindTexture(GL_TEXTURE_2D, texObj[1]);
jtgafb833d1999-08-19 00:55:39 +0000279#ifdef LINEAR_FILTER
280 /* linear filtering looks much nicer but is much slower for Mesa */
281 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
282 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
283#else
284 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
285 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
286#endif
287
288 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
289
290 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
291 printf("Error: couldn't load texture image\n");
292 exit(1);
293 }
294
Brian Paul1a3b8ff1999-10-13 12:02:13 +0000295
296 /* now bind the texture objects to the respective texture units */
297#ifdef GL_ARB_multitexture
298 glActiveTextureARB(GL_TEXTURE0_ARB);
299 glBindTexture(GL_TEXTURE_2D, texObj[0]);
300 glActiveTextureARB(GL_TEXTURE1_ARB);
301 glBindTexture(GL_TEXTURE_2D, texObj[1]);
302#endif
303
jtgafb833d1999-08-19 00:55:39 +0000304 glShadeModel(GL_FLAT);
305 glClearColor(0.3, 0.3, 0.4, 1.0);
306
307 ModeMenu(TEXBOTH);
Brian Paulac126091999-10-21 16:39:06 +0000308
309 if (argc > 1 && strcmp(argv[1], "-info")==0) {
310 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
311 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
312 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
313 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
314 }
jtgafb833d1999-08-19 00:55:39 +0000315}
316
317
318int main( int argc, char *argv[] )
319{
320 glutInit( &argc, argv );
321 glutInitWindowSize( 300, 300 );
322 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
323 glutCreateWindow(argv[0] );
324
Brian Paulac126091999-10-21 16:39:06 +0000325 Init( argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000326
327 glutReshapeFunc( Reshape );
328 glutKeyboardFunc( Key );
329 glutSpecialFunc( SpecialKey );
330 glutDisplayFunc( Display );
331 glutIdleFunc( Idle );
332
333 glutCreateMenu(ModeMenu);
334 glutAddMenuEntry("Texture 0", TEX0);
335 glutAddMenuEntry("Texture 1", TEX1);
336 glutAddMenuEntry("Multi-texture", TEXBOTH);
337 glutAddMenuEntry("Toggle Animation", ANIMATE);
338 glutAddMenuEntry("Quit", QUIT);
339 glutAttachMenu(GLUT_RIGHT_BUTTON);
340
341 glutMainLoop();
342 return 0;
343}