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