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