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