blob: d83c20b2f38d2d8a2ef439e0e695ffa74c1df046 [file] [log] [blame]
Brian Paul976c26c2001-08-20 16:07:10 +00001/*
2 * Textured cylinder demo: lighting, texturing, reflection mapping.
3 *
4 * Brian Paul May 1997 This program is in the public domain.
5 *
6 * Conversion to UGL/Mesa by Stephane Raimbault
7 */
8
9/*
10 * $Log: ugltexcyl.c,v $
11 * Revision 1.1 2001/08/20 16:07:11 brianp
12 * WindML driver (Stephane Raimbault)
13 *
14 * Revision 1.5 2001/03/27 17:35:26 brianp
15 * set initial window pos
16 *
17 * Revision 1.4 2000/12/24 22:53:54 pesco
18 * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
19 * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
20 * Essentially the same.
21 * Program files updated to include "readtex.c", not "../util/readtex.c".
22 * * demos/reflect.c: Likewise for "showbuffer.c".
23 *
24 *
25 * * Makefile.am (EXTRA_DIST): Added top-level regular files.
26 *
27 * * include/GL/Makefile.am (INC_X11): Added glxext.h.
28 *
29 *
30 * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
31 * Mesa GGI headers in dist even if HAVE_GGI is not given.
32 *
33 * * configure.in: Look for GLUT and demo source dirs in $srcdir.
34 *
35 * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
36 * More source list updates in various Makefile.am's.
37 *
38 * * Makefile.am (dist-hook): Remove CVS directory from distribution.
39 * (DIST_SUBDIRS): List all possible subdirs here.
40 * (SUBDIRS): Only list subdirs selected for build again.
41 * The above two applied to all subdir Makefile.am's also.
42 *
43 * Revision 1.3 2000/09/29 23:09:39 brianp
44 * added fps output
45 *
46 * Revision 1.2 1999/10/21 16:39:06 brianp
47 * added -info command line option
48 *
49 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
50 * Imported sources
51 *
52 * Revision 3.3 1999/03/28 18:24:37 brianp
53 * minor clean-up
54 *
55 * Revision 3.2 1998/11/05 04:34:04 brianp
56 * moved image files to ../images/ directory
57 *
58 * Revision 3.1 1998/06/23 03:16:51 brianp
59 * added Point/Linear sampling menu items
60 *
61 * Revision 3.0 1998/02/14 18:42:29 brianp
62 * initial rev
63 *
64 */
65
66
67#include <stdio.h>
68#include <stdlib.h>
69#include <math.h>
70#include <tickLib.h>
71
72#include <ugl/ugl.h>
73#include <ugl/uglucode.h>
74#include <ugl/uglevent.h>
75#include <ugl/uglinput.h>
76
77#include <GL/uglmesa.h>
78#include <GL/glu.h>
79
80#include "../util/readtex.h"
81
82#define TEXTURE_FILE "Mesa/images/reflect.rgb"
83
84#define LIT 1
85#define TEXTURED 2
86#define REFLECT 3
87#define ANIMATE 10
88#define POINT_FILTER 20
89#define LINEAR_FILTER 21
90#define QUIT 100
91#define COUNT_FRAMES
92
93UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
94UGL_LOCAL UGL_EVENT_Q_ID qId;
95UGL_LOCAL volatile UGL_BOOL stopWex;
96UGL_LOCAL UGL_MESA_CONTEXT umc;
97
98UGL_LOCAL GLuint CylinderObj;
99UGL_LOCAL GLboolean Animate;
100UGL_LOCAL GLboolean linearFilter;
101
102UGL_LOCAL GLfloat Xrot, Yrot, Zrot;
103UGL_LOCAL GLfloat DXrot, DYrot;
104
105UGL_LOCAL GLuint limit;
106UGL_LOCAL GLuint count;
107UGL_LOCAL GLuint tickStart, tickStop, tickBySec;
108
109UGL_LOCAL void cleanUp (void);
110
111UGL_LOCAL void drawGL(void)
112 {
113#ifdef COUNT_FRAMES
114 int time;
115#endif
116
117 glClear( GL_COLOR_BUFFER_BIT );
118
119 glPushMatrix();
120 glRotatef(Xrot, 1.0, 0.0, 0.0);
121 glRotatef(Yrot, 0.0, 1.0, 0.0);
122 glRotatef(Zrot, 0.0, 0.0, 1.0);
123 glScalef(5.0, 5.0, 5.0);
124 glCallList(CylinderObj);
125
126 glPopMatrix();
127
128 uglMesaSwapBuffers();
129
130 if (Animate)
131 {
132 Xrot += DXrot;
133 Yrot += DYrot;
134 }
135
136#ifdef COUNT_FRAMES
137 if (count > limit)
138 {
139 tickStop = tickGet ();
140 time = (tickStop-tickStart)/tickBySec;
141 printf (" %i fps\n", count/time);
142 tickStart = tickStop;
143 count = 0;
144 }
145 else
146 count++;
147#endif
148
149 }
150
151UGL_LOCAL void echoUse(void)
152 {
153 printf("Keys:\n");
154 printf(" Up/Down Rotate on Y\n");
155 printf(" Left/Right Rotate on X\n");
156 printf(" a Toggle animation\n");
157 printf(" f Toggle point/linear filtered\n");
158 printf(" l Lit\n");
159 printf(" t Textured\n");
160 printf(" r Reflect\n");
161 printf(" ESC Exit\n");
162 }
163
164UGL_LOCAL void readKey(UGL_WCHAR key)
165 {
166 float step = 3.0;
167 switch (key)
168 {
169 case 'a':
170 Animate = !Animate;
171 break;
172 case 'f':
173 if(linearFilter)
174 {
175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
176 GL_NEAREST);
177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
178 GL_NEAREST);
179 }
180 else
181 {
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
183 GL_LINEAR);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
185 GL_LINEAR);
186 }
187 linearFilter = !linearFilter;
188 break;
189 case 'l':
190 glEnable(GL_LIGHTING);
191 glDisable(GL_TEXTURE_2D);
192 glDisable(GL_TEXTURE_GEN_S);
193 glDisable(GL_TEXTURE_GEN_T);
194 break;
195 case 't':
196 glDisable(GL_LIGHTING);
197 glEnable(GL_TEXTURE_2D);
198 glDisable(GL_TEXTURE_GEN_S);
199 glDisable(GL_TEXTURE_GEN_T);
200 break;
201 case 'r':
202 glDisable(GL_LIGHTING);
203 glEnable(GL_TEXTURE_2D);
204 glEnable(GL_TEXTURE_GEN_S);
205 glEnable(GL_TEXTURE_GEN_T);
206 break;
207 case UGL_UNI_UP_ARROW:
208 Xrot += step;
209 break;
210 case UGL_UNI_DOWN_ARROW:
211 Xrot -= step;
212 break;
213 case UGL_UNI_LEFT_ARROW:
214 Yrot += step;
215 break;
216 case UGL_UNI_RIGHT_ARROW:
217 Yrot -= step;
218 break;
219 case UGL_UNI_ESCAPE:
220 stopWex = UGL_TRUE;
221 break;
222 }
223 }
224
225UGL_LOCAL void loopEvent(void)
226 {
227 UGL_EVENT event;
228 UGL_INPUT_EVENT * pInputEvent;
229
230 UGL_FOREVER
231 {
232 if (uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT)
233 != UGL_STATUS_Q_EMPTY)
234 {
235 pInputEvent = (UGL_INPUT_EVENT *)&event;
236
237 if (pInputEvent->header.type == UGL_EVENT_TYPE_KEYBOARD &&
238 pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
239 readKey(pInputEvent->type.keyboard.key);
240 }
241
242 drawGL();
243 if (stopWex)
244 break;
245 }
246 }
247
248UGL_LOCAL void initGL(void)
249 {
250 GLUquadricObj *q = gluNewQuadric();
251 CylinderObj = glGenLists(1);
252 glNewList(CylinderObj, GL_COMPILE);
253
254 glTranslatef(0.0, 0.0, -1.0);
255
256 /* cylinder */
257 gluQuadricNormals(q, GL_SMOOTH);
258 gluQuadricTexture(q, GL_TRUE);
259 gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
260
261 /* end cap */
262 glTranslatef(0.0, 0.0, 2.0);
263 gluDisk(q, 0.0, 0.6, 24, 1);
264
265 /* other end cap */
266 glTranslatef(0.0, 0.0, -2.0);
267 gluQuadricOrientation(q, GLU_INSIDE);
268 gluDisk(q, 0.0, 0.6, 24, 1);
269
270 glEndList();
271 gluDeleteQuadric(q);
272
273 /* lighting */
274 glEnable(GL_LIGHTING);
275 {
276 GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
277 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
278 GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
279 glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
280 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
281 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
282 glEnable(GL_LIGHT0);
283 }
284
285 /* fitering = nearest, initially */
286 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
287 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
288
289 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
290 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
291
292 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
293 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
294
295 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB))
296 {
297 printf("Error: couldn't load texture image\n");
298 cleanUp();
299 exit(1);
300 }
301
302 glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
303
304 glEnable(GL_LIGHTING);
305
306 glMatrixMode( GL_PROJECTION );
307 glLoadIdentity();
308 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
309 glMatrixMode( GL_MODELVIEW );
310 glLoadIdentity();
311 glTranslatef( 0.0, 0.0, -70.0 );
312
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#ifdef COUNT_FRAMES
319 tickStart = tickGet ();
320 tickBySec = sysClkRateGet ();
321#endif
322
323 }
324
325UGL_LOCAL void cleanUp (void)
326 {
327 uglEventQDestroy (eventServiceId, qId);
328
329 uglMesaDestroyContext();
330 uglDeinitialize ();
331 }
332
333void windMLTexCyl (void);
334
335void ugltexcyl (void)
336 {
337 taskSpawn ("tTexCyl", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLTexCyl,
338 0,1,2,3,4,5,6,7,8,9);
339 }
340
341void windMLTexCyl (void)
342 {
343 UGL_INPUT_DEVICE_ID keyboardDevId;
344 GLsizei displayWidth, displayHeight;
345 GLsizei x, y, w, h;
346
347 CylinderObj = 0;
348 Animate = GL_TRUE;
349 linearFilter = GL_FALSE;
350 Xrot = 0.0;
351 Yrot = 0.0;
352 Zrot = 0.0;
353 DXrot = 1.0;
354 DYrot = 2.5;
355 limit = 100;
356 count = 1;
357
358 uglInitialize ();
359
360 uglDriverFind (UGL_KEYBOARD_TYPE, 0,
361 (UGL_UINT32 *)&keyboardDevId);
362
363 uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0, (UGL_UINT32 *)&eventServiceId);
364
365 qId = uglEventQCreate (eventServiceId, 100);
366
367 /* Double buffering */
368 umc = uglMesaCreateNewContext (UGL_MESA_DOUBLE, NULL);
369 if (umc == NULL)
370 {
371 uglDeinitialize ();
372 return;
373 }
374
375 uglMesaMakeCurrentContext (umc, 0, 0, 1, 1);
376
377 uglMesaGetIntegerv(UGL_MESA_DISPLAY_WIDTH, &displayWidth);
378 uglMesaGetIntegerv(UGL_MESA_DISPLAY_HEIGHT, &displayHeight);
379
380 h = (displayHeight*3)/4;
381 w = h;
382 x = (displayWidth-w)/2;
383 y = (displayHeight-h)/2;
384
385 uglMesaMoveToWindow(x, y);
386 uglMesaResizeToWindow(w, h);
387
388 initGL ();
389
390 echoUse();
391
392 stopWex = UGL_FALSE;
393 loopEvent();
394
395 cleanUp();
396
397 return;
398 }
399