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