blob: 555757cc42c315b30853a53a652df882275cb573 [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001
2/*
Gareth Hughesc8516462001-01-06 20:38:03 +00003 * Display an isosurface of 3-D wind speed volume.
Keith Whitwell44c73931999-09-03 14:56:40 +00004 *
Brian Paulac126091999-10-21 16:39:06 +00005 * Command line options:
6 * -info print GL implementation information
7 *
jtgafb833d1999-08-19 00:55:39 +00008 * Brian Paul This file in public domain.
9 */
10
Keith Whitwell44c73931999-09-03 14:56:40 +000011
12/* Keys:
13 * =====
14 *
15 * - Arrow keys to rotate
16 * - 's' toggles smooth shading
17 * - 'l' toggles lighting
18 * - 'f' toggles fog
19 * - 'I' and 'i' zoom in and out
20 * - 'c' toggles a user clip plane
21 * - 'm' toggles colorful materials in GL_TRIANGLES modes.
22 * - '+' and '-' move the user clip plane
23 *
24 * Other options are available via the popup menu.
25 */
26
jtgafb833d1999-08-19 00:55:39 +000027#include <stdio.h>
28#include <string.h>
29#include <stdlib.h>
Brian Paulac126091999-10-21 16:39:06 +000030#include <string.h>
jtgafb833d1999-08-19 00:55:39 +000031#include <math.h>
Karl Schultzbffae582001-10-04 19:14:26 +000032#ifdef _WIN32
33#include <windows.h>
Karl Schultz53d30c52002-10-18 17:47:35 +000034#undef CLIP_MASK
Karl Schultzbffae582001-10-04 19:14:26 +000035#endif
Brian Paul445ecdc2003-09-08 14:56:41 +000036#define GL_GLEXT_PROTOTYPES
jtgafb833d1999-08-19 00:55:39 +000037#include "GL/glut.h"
38
pescod1ff1f62000-12-24 22:53:54 +000039#include "readtex.c" /* I know, this is a hack. KW: me too. */
jtgafb833d1999-08-19 00:55:39 +000040#define TEXTURE_FILE "../images/reflect.rgb"
41
Gareth Hughes735d9202002-01-04 09:47:17 +000042#define LIT 0x00000001
43#define UNLIT 0x00000002
44#define REFLECT 0x00000004
45#define POINT_FILTER 0x00000008
46#define LINEAR_FILTER 0x00000010
47#define GLVERTEX 0x00000020
48#define DRAW_ELTS 0x00000040
49#define DRAW_ARRAYS 0x00000080
50#define ARRAY_ELT 0x00000100
51#define LOCKED 0x00000200
52#define UNLOCKED 0x00000400
53#define IMMEDIATE 0x00000800
54#define DISPLAYLIST 0x00001000
55#define SHADE_SMOOTH 0x00002000
56#define SHADE_FLAT 0x00004000
57#define TRIANGLES 0x00008000
58#define STRIPS 0x00010000
59#define POINTS 0x00020000
60#define USER_CLIP 0x00040000
61#define NO_USER_CLIP 0x00080000
62#define MATERIALS 0x00100000
63#define NO_MATERIALS 0x00200000
64#define FOG 0x00400000
65#define NO_FOG 0x00800000
66#define QUIT 0x01000000
67#define GLINFO 0x02000000
68#define STIPPLE 0x04000000
69#define NO_STIPPLE 0x08000000
70#define POLYGON_FILL 0x10000000
71#define POLYGON_LINE 0x20000000
jtgafb833d1999-08-19 00:55:39 +000072
Keith Whitwell5759f532001-05-11 12:08:15 +000073#define LIGHT_MASK (LIT|UNLIT|REFLECT)
Gareth Hughesc8516462001-01-06 20:38:03 +000074#define FILTER_MASK (POINT_FILTER|LINEAR_FILTER)
Keith Whitwell5759f532001-05-11 12:08:15 +000075#define RENDER_STYLE_MASK (GLVERTEX|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT)
76#define DLIST_MASK (IMMEDIATE|DISPLAYLIST)
77#define LOCK_MASK (LOCKED|UNLOCKED)
Gareth Hughesc8516462001-01-06 20:38:03 +000078#define MATERIAL_MASK (MATERIALS|NO_MATERIALS)
79#define PRIMITIVE_MASK (TRIANGLES|STRIPS|POINTS)
80#define CLIP_MASK (USER_CLIP|NO_USER_CLIP)
81#define SHADE_MASK (SHADE_SMOOTH|SHADE_FLAT)
82#define FOG_MASK (FOG|NO_FOG)
83#define STIPPLE_MASK (STIPPLE|NO_STIPPLE)
Gareth Hughes735d9202002-01-04 09:47:17 +000084#define POLYGON_MASK (POLYGON_FILL|POLYGON_LINE)
jtgafb833d1999-08-19 00:55:39 +000085
86#define MAXVERTS 10000
Karl Schultz53d30c52002-10-18 17:47:35 +000087static GLint maxverts = MAXVERTS;
jtgafb833d1999-08-19 00:55:39 +000088static float data[MAXVERTS][6];
89static float compressed_data[MAXVERTS][6];
Keith Whitwell5759f532001-05-11 12:08:15 +000090static float expanded_data[MAXVERTS*3][6];
jtgafb833d1999-08-19 00:55:39 +000091static GLuint indices[MAXVERTS];
92static GLuint tri_indices[MAXVERTS*3];
Keith Whitwellb8f99802001-05-11 15:47:02 +000093static GLuint strip_indices[MAXVERTS];
jtgafb833d1999-08-19 00:55:39 +000094static GLfloat col[100][4];
95static GLint numverts, num_tri_verts, numuniq;
96
97static GLfloat xrot;
98static GLfloat yrot;
Keith Whitwell5759f532001-05-11 12:08:15 +000099static GLfloat dist;
jtgafb833d1999-08-19 00:55:39 +0000100static GLint state, allowed = ~0;
101static GLboolean doubleBuffer = GL_TRUE;
Keith Whitwell5759f532001-05-11 12:08:15 +0000102static GLdouble plane[4];
103static GLuint surf1, dlist_state;
jtgafb833d1999-08-19 00:55:39 +0000104
Brian Paulac126091999-10-21 16:39:06 +0000105static GLboolean PrintInfo = GL_FALSE;
106
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000107
108static GLubyte halftone[] = {
109 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
110 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
111 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
112 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
113 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
114 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
115 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
116 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
117 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
118 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
119 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};
120
Brian Paul36ca6bd1999-09-08 22:14:31 +0000121
jtgafb833d1999-08-19 00:55:39 +0000122static void read_surface( char *filename )
123{
124 FILE *f;
125
126 f = fopen(filename,"r");
127 if (!f) {
128 printf("couldn't read %s\n", filename);
129 exit(1);
130 }
131
132 numverts = 0;
Keith Whitwell18acf6e2001-04-19 13:12:40 +0000133 while (!feof(f) && numverts<maxverts) {
jtgafb833d1999-08-19 00:55:39 +0000134 fscanf( f, "%f %f %f %f %f %f",
135 &data[numverts][0], &data[numverts][1], &data[numverts][2],
136 &data[numverts][3], &data[numverts][4], &data[numverts][5] );
137 numverts++;
138 }
139 numverts--;
140
141 printf("%d vertices, %d triangles\n", numverts, numverts-2);
142 fclose(f);
143}
144
145
146
Keith Whitwell5759f532001-05-11 12:08:15 +0000147static void print_flags( const char *msg, GLuint flags )
148{
149 fprintf(stderr,
Gareth Hughes735d9202002-01-04 09:47:17 +0000150 "%s (0x%x): %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
Keith Whitwell5759f532001-05-11 12:08:15 +0000151 msg, flags,
152 (flags & GLVERTEX) ? "glVertex, " : "",
153 (flags & DRAW_ARRAYS) ? "glDrawArrays, " : "",
154 (flags & DRAW_ELTS) ? "glDrawElements, " : "",
155 (flags & ARRAY_ELT) ? "glArrayElement, " : "",
156 (flags & LOCKED) ? "locked arrays, " : "",
157 (flags & TRIANGLES) ? "GL_TRIANGLES, " : "",
158 (flags & STRIPS) ? "GL_TRIANGLE_STRIP, " : "",
159 (flags & POINTS) ? "GL_POINTS, " : "",
160 (flags & DISPLAYLIST) ? "as a displaylist, " : "",
161 (flags & LIT) ? "lit, " : "",
162 (flags & UNLIT) ? "unlit, " : "",
163 (flags & REFLECT) ? "reflect, " : "",
164 (flags & SHADE_FLAT) ? "flat-shaded, " : "",
165 (flags & USER_CLIP) ? "user_clip, " : "",
166 (flags & MATERIALS) ? "materials, " : "",
167 (flags & FOG) ? "fog, " : "",
Gareth Hughes735d9202002-01-04 09:47:17 +0000168 (flags & STIPPLE) ? "stipple, " : "",
169 (flags & POLYGON_LINE) ? "polygon mode line, " : "");
Keith Whitwell5759f532001-05-11 12:08:15 +0000170}
171
172
jtgafb833d1999-08-19 00:55:39 +0000173
174struct data_idx {
175 float *data;
176 int idx;
177 int uniq_idx;
178};
179
180
181#define COMPARE_FUNC( AXIS ) \
Brian Paulac126091999-10-21 16:39:06 +0000182static int compare_axis_##AXIS( const void *a, const void *b ) \
jtgafb833d1999-08-19 00:55:39 +0000183{ \
184 float t = ( (*(struct data_idx *)a).data[AXIS] - \
185 (*(struct data_idx *)b).data[AXIS] ); \
186 \
187 if (t < 0) return -1; \
188 if (t > 0) return 1; \
189 return 0; \
190}
191
192COMPARE_FUNC(0)
193COMPARE_FUNC(1)
194COMPARE_FUNC(2)
195COMPARE_FUNC(3)
196COMPARE_FUNC(4)
197COMPARE_FUNC(5)
198COMPARE_FUNC(6)
199
200int (*(compare[7]))( const void *a, const void *b ) =
201{
202 compare_axis_0,
203 compare_axis_1,
204 compare_axis_2,
205 compare_axis_3,
206 compare_axis_4,
207 compare_axis_5,
208 compare_axis_6,
209};
210
211
212#define VEC_ELT(f, s, i) (float *)(((char *)f) + s * i)
213
Gareth Hughesc8516462001-01-06 20:38:03 +0000214static int sort_axis( int axis,
jtgafb833d1999-08-19 00:55:39 +0000215 int vec_size,
216 int vec_stride,
217 struct data_idx *indices,
218 int start,
219 int finish,
220 float *out,
221 int uniq,
222 const float fudge )
223{
224 int i;
225
Gareth Hughesc8516462001-01-06 20:38:03 +0000226 if (finish-start > 2)
jtgafb833d1999-08-19 00:55:39 +0000227 {
228 qsort( indices+start, finish-start, sizeof(*indices), compare[axis] );
Gareth Hughesc8516462001-01-06 20:38:03 +0000229 }
230 else if (indices[start].data[axis] > indices[start+1].data[axis])
jtgafb833d1999-08-19 00:55:39 +0000231 {
232 struct data_idx tmp = indices[start];
233 indices[start] = indices[start+1];
234 indices[start+1] = tmp;
235 }
Gareth Hughesc8516462001-01-06 20:38:03 +0000236
jtgafb833d1999-08-19 00:55:39 +0000237 if (axis == vec_size-1) {
238 for (i = start ; i < finish ; ) {
239 float max = indices[i].data[axis] + fudge;
240 float *dest = VEC_ELT(out, vec_stride, uniq);
241 int j;
Gareth Hughesc8516462001-01-06 20:38:03 +0000242
jtgafb833d1999-08-19 00:55:39 +0000243 for (j = 0 ; j < vec_size ; j++)
244 dest[j] = indices[i].data[j];
245
Gareth Hughesc8516462001-01-06 20:38:03 +0000246 for ( ; i < finish && max >= indices[i].data[axis]; i++)
jtgafb833d1999-08-19 00:55:39 +0000247 indices[i].uniq_idx = uniq;
248
249 uniq++;
250 }
251 } else {
252 for (i = start ; i < finish ; ) {
253 int j = i + 1;
254 float max = indices[i].data[axis] + fudge;
255 while (j < finish && max >= indices[j].data[axis]) j++;
256 if (j == i+1) {
257 float *dest = VEC_ELT(out, vec_stride, uniq);
258 int k;
259
260 indices[i].uniq_idx = uniq;
Gareth Hughesc8516462001-01-06 20:38:03 +0000261
jtgafb833d1999-08-19 00:55:39 +0000262 for (k = 0 ; k < vec_size ; k++)
263 dest[k] = indices[i].data[k];
264
265 uniq++;
266 } else {
267 uniq = sort_axis( axis+1, vec_size, vec_stride,
268 indices, i, j, out, uniq, fudge );
269 }
270 i = j;
271 }
272 }
273
274 return uniq;
275}
276
277
Gareth Hughesc8516462001-01-06 20:38:03 +0000278static void extract_indices1( const struct data_idx *in, unsigned int *out,
jtgafb833d1999-08-19 00:55:39 +0000279 int n )
280{
281 int i;
282 for ( i = 0 ; i < n ; i++ ) {
283 out[in[i].idx] = in[i].uniq_idx;
284 }
285}
286
287
Brian Paul36ca6bd1999-09-08 22:14:31 +0000288static void compactify_arrays(void)
jtgafb833d1999-08-19 00:55:39 +0000289{
290 int i;
291 struct data_idx *ind;
292
293 ind = (struct data_idx *) malloc( sizeof(struct data_idx) * numverts );
294
295 for (i = 0 ; i < numverts ; i++) {
296 ind[i].idx = i;
297 ind[i].data = data[i];
298 }
299
Gareth Hughesc8516462001-01-06 20:38:03 +0000300 numuniq = sort_axis(0,
301 sizeof(compressed_data[0])/sizeof(float),
jtgafb833d1999-08-19 00:55:39 +0000302 sizeof(compressed_data[0]),
Gareth Hughesc8516462001-01-06 20:38:03 +0000303 ind,
304 0,
305 numverts,
306 (float *)compressed_data,
jtgafb833d1999-08-19 00:55:39 +0000307 0,
308 1e-6);
309
310 printf("Nr unique vertex/normal pairs: %d\n", numuniq);
311
312 extract_indices1( ind, indices, numverts );
313 free( ind );
314}
315
Keith Whitwell5759f532001-05-11 12:08:15 +0000316static void expand_arrays(void)
317{
318 int i;
319 int parity = 0;
320 for (i = 2 ; i < numverts ; i++, parity ^= 1) {
321 int v0 = i-2+parity;
322 int v1 = i-1-parity;
323 int v2 = i;
324 memcpy( expanded_data[(i-2)*3+0], data[v0], sizeof(data[0]) );
325 memcpy( expanded_data[(i-2)*3+1], data[v1], sizeof(data[0]) );
326 memcpy( expanded_data[(i-2)*3+2], data[v2], sizeof(data[0]) );
327 }
328}
329
jtgafb833d1999-08-19 00:55:39 +0000330static float myrand( float max )
331{
332 return max*rand()/(RAND_MAX+1.0);
333}
334
335
336static void make_tri_indices( void )
337{
338 unsigned int *v = tri_indices;
339 unsigned int parity = 0;
Karl Schultz53d30c52002-10-18 17:47:35 +0000340 int i, j;
jtgafb833d1999-08-19 00:55:39 +0000341
342 for (j=2;j<numverts;j++,parity^=1) {
343 if (parity) {
344 *v++ = indices[j-1];
Gareth Hughesc8516462001-01-06 20:38:03 +0000345 *v++ = indices[j-2];
jtgafb833d1999-08-19 00:55:39 +0000346 *v++ = indices[j];
347 } else {
348 *v++ = indices[j-2];
349 *v++ = indices[j-1];
350 *v++ = indices[j];
351 }
352 }
Gareth Hughesc8516462001-01-06 20:38:03 +0000353
jtgafb833d1999-08-19 00:55:39 +0000354 num_tri_verts = v - tri_indices;
355 printf("num_tri_verts: %d\n", num_tri_verts);
356
357 for (i = j = 0 ; i < num_tri_verts ; i += 600, j++) {
358 col[j][3] = 1;
359 col[j][2] = myrand(1);
360 col[j][1] = myrand(1);
361 col[j][0] = myrand(1);
362 }
Keith Whitwellb8f99802001-05-11 15:47:02 +0000363
364 for (i = 0; i < numverts ; i++)
365 strip_indices[i] = i;
jtgafb833d1999-08-19 00:55:39 +0000366}
367
368#define MIN(x,y) (x < y) ? x : y
369
Karl Schultz53d30c52002-10-18 17:47:35 +0000370static void draw_surface( unsigned int with_state )
jtgafb833d1999-08-19 00:55:39 +0000371{
Karl Schultz53d30c52002-10-18 17:47:35 +0000372 GLint i, j;
Keith Whitwell5759f532001-05-11 12:08:15 +0000373
374 if (with_state & DISPLAYLIST) {
375 if ((with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|MATERIAL_MASK)) !=
376 dlist_state) {
377 /*
378 */
379 fprintf(stderr, "rebuilding displaylist\n");
jtgafb833d1999-08-19 00:55:39 +0000380
Keith Whitwell5759f532001-05-11 12:08:15 +0000381 if (dlist_state)
382 glDeleteLists( surf1, 1 );
383
384 dlist_state = with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|
385 MATERIAL_MASK);
386 surf1 = glGenLists(1);
387 glNewList(surf1, GL_COMPILE);
388 draw_surface( dlist_state );
389 glEndList();
390 }
391
392 glCallList( surf1 );
393 return;
394 }
395
396 switch (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK)) {
jtgafb833d1999-08-19 00:55:39 +0000397#ifdef GL_EXT_vertex_array
398
Keith Whitwell5759f532001-05-11 12:08:15 +0000399 case (DRAW_ELTS|TRIANGLES):
Keith Whitwell44c73931999-09-03 14:56:40 +0000400 if (with_state & MATERIALS) {
jtgafb833d1999-08-19 00:55:39 +0000401 for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
402 GLuint nr = MIN(num_tri_verts-i, 600);
403 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
404 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
405 glDrawElements( GL_TRIANGLES, nr, GL_UNSIGNED_INT, tri_indices+i );
406 }
407 } else {
Gareth Hughesc8516462001-01-06 20:38:03 +0000408 glDrawElements( GL_TRIANGLES, num_tri_verts, GL_UNSIGNED_INT,
jtgafb833d1999-08-19 00:55:39 +0000409 tri_indices );
410 }
jtgafb833d1999-08-19 00:55:39 +0000411 break;
412
Keith Whitwell5759f532001-05-11 12:08:15 +0000413 case (DRAW_ARRAYS|TRIANGLES):
414 glDrawArraysEXT( GL_TRIANGLES, 0, (numverts-2)*3 );
415 break;
416
417 case (ARRAY_ELT|TRIANGLES):
Keith Whitwell44c73931999-09-03 14:56:40 +0000418 if (with_state & MATERIALS) {
jtgafb833d1999-08-19 00:55:39 +0000419 for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
420 GLuint nr = MIN(num_tri_verts-i, 600);
421 GLuint k;
422 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
423 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
424 glBegin( GL_TRIANGLES );
425 for (k = 0 ; k < nr ; k++)
426 glArrayElement( tri_indices[i+k] );
427 glEnd();
428 }
429 } else {
430 glBegin( GL_TRIANGLES );
431 for (i = 0 ; i < num_tri_verts ; i++)
432 glArrayElement( tri_indices[i] );
Gareth Hughesc8516462001-01-06 20:38:03 +0000433
jtgafb833d1999-08-19 00:55:39 +0000434 glEnd();
Gareth Hughesc8516462001-01-06 20:38:03 +0000435 }
jtgafb833d1999-08-19 00:55:39 +0000436 break;
437
Gareth Hughesc8516462001-01-06 20:38:03 +0000438
439 /* Uses the original arrays (including duplicate elements):
440 */
Keith Whitwell5759f532001-05-11 12:08:15 +0000441 case (DRAW_ARRAYS|STRIPS):
Gareth Hughesc8516462001-01-06 20:38:03 +0000442 glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
443 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000444 case (DRAW_ELTS|STRIPS):
Keith Whitwellb8f99802001-05-11 15:47:02 +0000445 glDrawElements( GL_TRIANGLE_STRIP, numverts,
446 GL_UNSIGNED_INT, strip_indices );
Gareth Hughesc8516462001-01-06 20:38:03 +0000447 break;
448
449 /* Uses the original arrays (including duplicate elements):
450 */
Keith Whitwell5759f532001-05-11 12:08:15 +0000451 case (ARRAY_ELT|STRIPS):
Gareth Hughesc8516462001-01-06 20:38:03 +0000452 glBegin( GL_TRIANGLE_STRIP );
453 for (i = 0 ; i < numverts ; i++)
454 glArrayElement( i );
455 glEnd();
456 break;
457
Keith Whitwell5759f532001-05-11 12:08:15 +0000458 case (DRAW_ARRAYS|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000459 glDrawArraysEXT( GL_POINTS, 0, numuniq );
Gareth Hughesc8516462001-01-06 20:38:03 +0000460 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000461 case (DRAW_ELTS|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000462 /* can use numuniq with strip_indices as strip_indices[i] == i.
463 */
464 glDrawElements( GL_POINTS, numuniq,
465 GL_UNSIGNED_INT, strip_indices );
Gareth Hughesc8516462001-01-06 20:38:03 +0000466 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000467 case (ARRAY_ELT|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000468 /* just emit each unique element once:
469 */
Gareth Hughesc8516462001-01-06 20:38:03 +0000470 glBegin( GL_POINTS );
Keith Whitwellabd51342001-06-04 15:34:31 +0000471 for (i = 0 ; i < numuniq ; i++)
472 glArrayElement( i );
Gareth Hughesc8516462001-01-06 20:38:03 +0000473 glEnd();
474 break;
475#endif
476
Keith Whitwell5759f532001-05-11 12:08:15 +0000477 case (GLVERTEX|TRIANGLES):
Keith Whitwell44c73931999-09-03 14:56:40 +0000478 if (with_state & MATERIALS) {
jtgafb833d1999-08-19 00:55:39 +0000479 for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
480 GLuint nr = MIN(num_tri_verts-i, 600);
481 GLuint k;
482 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
483 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
484 glBegin( GL_TRIANGLES );
485 for (k = 0 ; k < nr ; k++) {
486 glNormal3fv( &compressed_data[tri_indices[i+k]][3] );
487 glVertex3fv( &compressed_data[tri_indices[i+k]][0] );
488 }
489 glEnd();
490 }
491 } else {
492 glBegin( GL_TRIANGLES );
493 for (i = 0 ; i < num_tri_verts ; i++) {
494 glNormal3fv( &compressed_data[tri_indices[i]][3] );
495 glVertex3fv( &compressed_data[tri_indices[i]][0] );
496 }
497 glEnd();
Gareth Hughesc8516462001-01-06 20:38:03 +0000498 }
jtgafb833d1999-08-19 00:55:39 +0000499 break;
500
Keith Whitwell5759f532001-05-11 12:08:15 +0000501 case (GLVERTEX|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000502 /* Renders all points, but not in strip order... Shouldn't be a
503 * problem, but people may be confused as to why points are so
504 * much faster in this demo... And why cva doesn't help them...
505 */
Gareth Hughesc8516462001-01-06 20:38:03 +0000506 glBegin( GL_POINTS );
Keith Whitwellabd51342001-06-04 15:34:31 +0000507 for ( i = 0 ; i < numuniq ; i++ ) {
508 glNormal3fv( &compressed_data[i][3] );
509 glVertex3fv( &compressed_data[i][0] );
Gareth Hughesc8516462001-01-06 20:38:03 +0000510 }
511 glEnd();
Keith Whitwell44c73931999-09-03 14:56:40 +0000512 break;
513
Keith Whitwell5759f532001-05-11 12:08:15 +0000514 case (GLVERTEX|STRIPS):
jtgafb833d1999-08-19 00:55:39 +0000515 glBegin( GL_TRIANGLE_STRIP );
516 for (i=0;i<numverts;i++) {
517 glNormal3fv( &data[i][3] );
518 glVertex3fv( &data[i][0] );
519 }
520 glEnd();
Keith Whitwell5759f532001-05-11 12:08:15 +0000521 break;
522
523 default:
524 fprintf(stderr, "unimplemented mode %x...\n",
525 (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK)));
526 break;
jtgafb833d1999-08-19 00:55:39 +0000527 }
528}
529
530
531
532static void Display(void)
533{
534 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
Keith Whitwell44c73931999-09-03 14:56:40 +0000535 draw_surface( state );
jtgafb833d1999-08-19 00:55:39 +0000536 glFlush();
Gareth Hughesc8516462001-01-06 20:38:03 +0000537 if (doubleBuffer) glutSwapBuffers();
jtgafb833d1999-08-19 00:55:39 +0000538}
539
Keith Whitwell18acf6e2001-04-19 13:12:40 +0000540
jtgafb833d1999-08-19 00:55:39 +0000541/* KW: only do this when necessary, so CVA can re-use results.
542 */
543static void set_matrix( void )
544{
545 glMatrixMode(GL_MODELVIEW);
546 glLoadIdentity();
Keith Whitwell44c73931999-09-03 14:56:40 +0000547 glTranslatef( 0.0, 0.0, dist );
jtgafb833d1999-08-19 00:55:39 +0000548 glRotatef( yrot, 0.0, 1.0, 0.0 );
549 glRotatef( xrot, 1.0, 0.0, 0.0 );
550}
551
552static void Benchmark( float xdiff, float ydiff )
553{
554 int startTime, endTime;
555 int draws;
556 double seconds, fps, triPerSecond;
557
558 printf("Benchmarking...\n");
559
560 draws = 0;
561 startTime = glutGet(GLUT_ELAPSED_TIME);
562 xrot = 0.0;
563 do {
564 xrot += xdiff;
565 yrot += ydiff;
566 set_matrix();
567 Display();
568 draws++;
569 endTime = glutGet(GLUT_ELAPSED_TIME);
570 } while (endTime - startTime < 5000); /* 5 seconds */
571
572 /* Results */
573 seconds = (double) (endTime - startTime) / 1000.0;
574 triPerSecond = (numverts - 2) * draws / seconds;
575 fps = draws / seconds;
576 printf("Result: triangles/sec: %g fps: %g\n", triPerSecond, fps);
577}
578
579
580static void InitMaterials(void)
581{
582 static float ambient[] = {0.1, 0.1, 0.1, 1.0};
583 static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
584 static float position0[] = {0.0, 0.0, 20.0, 0.0};
585 static float position1[] = {0.0, 0.0, -20.0, 0.0};
586 static float front_mat_shininess[] = {60.0};
587 static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
588 static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
589 /*
590 static float back_mat_shininess[] = {60.0};
591 static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
592 static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
593 */
594 static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
595 static float lmodel_twoside[] = {GL_FALSE};
596
597 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
598 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
599 glLightfv(GL_LIGHT0, GL_POSITION, position0);
600 glEnable(GL_LIGHT0);
Gareth Hughesc8516462001-01-06 20:38:03 +0000601
jtgafb833d1999-08-19 00:55:39 +0000602 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
603 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
604 glLightfv(GL_LIGHT1, GL_POSITION, position1);
605 glEnable(GL_LIGHT1);
Gareth Hughesc8516462001-01-06 20:38:03 +0000606
jtgafb833d1999-08-19 00:55:39 +0000607 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
608 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
609
610 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
611 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
612 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000613
614 glPolygonStipple (halftone);
jtgafb833d1999-08-19 00:55:39 +0000615}
616
617
618
619#define UPDATE(o,n,mask) (o&=~mask, o|=n&mask)
Keith Whitwell5759f532001-05-11 12:08:15 +0000620#define CHANGED(o,n,mask) ((n&mask) && (n&mask) != (o&mask) )
jtgafb833d1999-08-19 00:55:39 +0000621
622static void ModeMenu(int m)
623{
624 m &= allowed;
625
626 if (!m) return;
627
Gareth Hughesc8516462001-01-06 20:38:03 +0000628 if (m==QUIT)
jtgafb833d1999-08-19 00:55:39 +0000629 exit(0);
630
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000631 if (m==GLINFO) {
632 printf("GL_VERSION: %s\n", (char *) glGetString(GL_VERSION));
633 printf("GL_EXTENSIONS: %s\n", (char *) glGetString(GL_EXTENSIONS));
634 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
635 return;
636 }
637
jtgafb833d1999-08-19 00:55:39 +0000638 if (CHANGED(state, m, FILTER_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000639 UPDATE(state, m, FILTER_MASK);
jtgafb833d1999-08-19 00:55:39 +0000640 if (m & LINEAR_FILTER) {
641 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
642 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
643 } else {
644 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
645 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
646 }
647 }
648
649 if (CHANGED(state, m, LIGHT_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000650 UPDATE(state, m, LIGHT_MASK);
651 if (m & LIT) {
jtgafb833d1999-08-19 00:55:39 +0000652 glEnable(GL_LIGHTING);
Keith Whitwell5759f532001-05-11 12:08:15 +0000653 glDisable(GL_TEXTURE_GEN_S);
654 glDisable(GL_TEXTURE_GEN_T);
655 glDisable(GL_TEXTURE_2D);
656 }
657 else if (m & UNLIT) {
jtgafb833d1999-08-19 00:55:39 +0000658 glDisable(GL_LIGHTING);
Keith Whitwell5759f532001-05-11 12:08:15 +0000659 glDisable(GL_TEXTURE_GEN_S);
660 glDisable(GL_TEXTURE_GEN_T);
661 glDisable(GL_TEXTURE_2D);
662 }
663 else if (m & REFLECT) {
664 glDisable(GL_LIGHTING);
665 glEnable(GL_TEXTURE_GEN_S);
666 glEnable(GL_TEXTURE_GEN_T);
667 glEnable(GL_TEXTURE_2D);
668 }
jtgafb833d1999-08-19 00:55:39 +0000669 }
670
671 if (CHANGED(state, m, SHADE_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000672 UPDATE(state, m, SHADE_MASK);
jtgafb833d1999-08-19 00:55:39 +0000673 if (m & SHADE_SMOOTH)
674 glShadeModel(GL_SMOOTH);
675 else
676 glShadeModel(GL_FLAT);
677 }
678
679
jtgafb833d1999-08-19 00:55:39 +0000680 if (CHANGED(state, m, CLIP_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000681 UPDATE(state, m, CLIP_MASK);
jtgafb833d1999-08-19 00:55:39 +0000682 if (m & USER_CLIP) {
683 glEnable(GL_CLIP_PLANE0);
684 } else {
685 glDisable(GL_CLIP_PLANE0);
686 }
687 }
688
Keith Whitwell44c73931999-09-03 14:56:40 +0000689 if (CHANGED(state, m, FOG_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000690 UPDATE(state, m, FOG_MASK);
Gareth Hughes735d9202002-01-04 09:47:17 +0000691 if (m & FOG) {
Keith Whitwell44c73931999-09-03 14:56:40 +0000692 glEnable(GL_FOG);
Gareth Hughesc8516462001-01-06 20:38:03 +0000693 }
Gareth Hughes735d9202002-01-04 09:47:17 +0000694 else {
Keith Whitwell44c73931999-09-03 14:56:40 +0000695 glDisable(GL_FOG);
Keith Whitwell44c73931999-09-03 14:56:40 +0000696 }
697 }
698
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000699 if (CHANGED(state, m, STIPPLE_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000700 UPDATE(state, m, STIPPLE_MASK);
Gareth Hughes735d9202002-01-04 09:47:17 +0000701 if (m & STIPPLE) {
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000702 glEnable(GL_POLYGON_STIPPLE);
Gareth Hughesc8516462001-01-06 20:38:03 +0000703 }
Gareth Hughes735d9202002-01-04 09:47:17 +0000704 else {
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000705 glDisable(GL_POLYGON_STIPPLE);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000706 }
707 }
708
Gareth Hughes735d9202002-01-04 09:47:17 +0000709 if (CHANGED(state, m, POLYGON_MASK)) {
710 UPDATE(state, m, POLYGON_MASK);
711 if (m & POLYGON_FILL) {
712 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
713 }
714 else {
715 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
716 }
717 }
718
jtgafb833d1999-08-19 00:55:39 +0000719#ifdef GL_EXT_vertex_array
Keith Whitwell5759f532001-05-11 12:08:15 +0000720 if (CHANGED(state, m, (LOCK_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK)))
jtgafb833d1999-08-19 00:55:39 +0000721 {
Keith Whitwell5759f532001-05-11 12:08:15 +0000722 if (m & (PRIMITIVE_MASK)) {
723 UPDATE(state, m, (PRIMITIVE_MASK));
724 }
725
726 if (m & (RENDER_STYLE_MASK)) {
727 UPDATE(state, m, (RENDER_STYLE_MASK));
728 }
729
730 if (m & LOCK_MASK) {
731 UPDATE(state, m, (LOCK_MASK));
732 }
733
734
735 print_flags("primitive", state & PRIMITIVE_MASK);
736 print_flags("render style", state & RENDER_STYLE_MASK);
737
Keith Whitwellabd51342001-06-04 15:34:31 +0000738 if ((state & PRIMITIVE_MASK) != STRIPS &&
739 ((state & RENDER_STYLE_MASK) == DRAW_ELTS ||
740 (state & RENDER_STYLE_MASK) == ARRAY_ELT ||
741 (state & PRIMITIVE_MASK) == POINTS))
jtgafb833d1999-08-19 00:55:39 +0000742 {
Keith Whitwell5759f532001-05-11 12:08:15 +0000743 fprintf(stderr, "enabling small arrays\n");
Keith Whitwellabd51342001-06-04 15:34:31 +0000744 /* Rendering any primitive with draw-element/array-element
745 * --> Can't do strips here as ordering has been lost in
746 * compaction process...
747 */
Gareth Hughesc8516462001-01-06 20:38:03 +0000748 glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numuniq,
jtgafb833d1999-08-19 00:55:39 +0000749 compressed_data );
Gareth Hughesc8516462001-01-06 20:38:03 +0000750 glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numuniq,
jtgafb833d1999-08-19 00:55:39 +0000751 &compressed_data[0][3]);
jtgafb833d1999-08-19 00:55:39 +0000752#ifdef GL_EXT_compiled_vertex_array
Keith Whitwell5759f532001-05-11 12:08:15 +0000753 if (allowed & LOCKED) {
754 if (state & LOCKED) {
755 glLockArraysEXT( 0, numuniq );
756 } else {
757 glUnlockArraysEXT();
758 }
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000759 }
jtgafb833d1999-08-19 00:55:39 +0000760#endif
Keith Whitwell5759f532001-05-11 12:08:15 +0000761 }
Keith Whitwellabd51342001-06-04 15:34:31 +0000762 else if ((state & PRIMITIVE_MASK) == TRIANGLES &&
763 (state & RENDER_STYLE_MASK) == DRAW_ARRAYS) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000764 fprintf(stderr, "enabling big arrays\n");
Keith Whitwellabd51342001-06-04 15:34:31 +0000765 /* Only get here for TRIANGLES and drawarrays
Keith Whitwell5759f532001-05-11 12:08:15 +0000766 */
767 glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
768 expanded_data );
769 glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
770 &expanded_data[0][3]);
771
772#ifdef GL_EXT_compiled_vertex_array
773 if (allowed & LOCKED) {
774 if (state & LOCKED) {
775 glLockArraysEXT( 0, (numverts-2)*3 );
776 } else {
777 glUnlockArraysEXT();
778 }
779 }
780#endif
781 }
Keith Whitwellabd51342001-06-04 15:34:31 +0000782 else {
783 fprintf(stderr, "enabling normal arrays\n");
784 glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numverts, data );
785 glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numverts, &data[0][3]);
786#ifdef GL_EXT_compiled_vertex_array
787 if (allowed & LOCKED) {
788 if (state & LOCKED) {
789 glLockArraysEXT( 0, numverts );
790 } else {
791 glUnlockArraysEXT();
792 }
793 }
794#endif
795 }
Keith Whitwell5759f532001-05-11 12:08:15 +0000796
jtgafb833d1999-08-19 00:55:39 +0000797 }
798#endif
799
Keith Whitwell5759f532001-05-11 12:08:15 +0000800
801 if (m & DLIST_MASK) {
802 UPDATE(state, m, DLIST_MASK);
jtgafb833d1999-08-19 00:55:39 +0000803 }
Gareth Hughesc8516462001-01-06 20:38:03 +0000804
jtgafb833d1999-08-19 00:55:39 +0000805 if (m & MATERIAL_MASK) {
806 UPDATE(state, m, MATERIAL_MASK);
807 }
808
Keith Whitwell5759f532001-05-11 12:08:15 +0000809 print_flags("new flags", state);
810
jtgafb833d1999-08-19 00:55:39 +0000811 glutPostRedisplay();
812}
813
814
815
Brian Paulac126091999-10-21 16:39:06 +0000816static void Init(int argc, char *argv[])
jtgafb833d1999-08-19 00:55:39 +0000817{
Keith Whitwell44c73931999-09-03 14:56:40 +0000818 GLfloat fogColor[4] = {0.5,1.0,0.5,1.0};
819
Keith Whitwell5759f532001-05-11 12:08:15 +0000820 xrot = 0;
821 yrot = 0;
822 dist = -6;
823 plane[0] = 1.0;
824 plane[1] = 0.0;
825 plane[2] = -1.0;
826 plane[3] = 0.0;
827
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000828 glClearColor(0.0, 0.0, 1.0, 0.0);
jtgafb833d1999-08-19 00:55:39 +0000829 glEnable( GL_DEPTH_TEST );
830 glEnable( GL_VERTEX_ARRAY_EXT );
831 glEnable( GL_NORMAL_ARRAY_EXT );
832
jtgafb833d1999-08-19 00:55:39 +0000833 glMatrixMode(GL_PROJECTION);
834 glLoadIdentity();
835 glFrustum( -1.0, 1.0, -1.0, 1.0, 5, 25 );
836
837 glMatrixMode(GL_MODELVIEW);
838 glLoadIdentity();
Gareth Hughesc8516462001-01-06 20:38:03 +0000839 glClipPlane(GL_CLIP_PLANE0, plane);
jtgafb833d1999-08-19 00:55:39 +0000840
Keith Whitwellb8f99802001-05-11 15:47:02 +0000841 InitMaterials();
842
jtgafb833d1999-08-19 00:55:39 +0000843 set_matrix();
844
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000845 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
jtgafb833d1999-08-19 00:55:39 +0000846 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
847
848 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
849 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
850
jtgafb833d1999-08-19 00:55:39 +0000851
Keith Whitwell44c73931999-09-03 14:56:40 +0000852 /* Green fog is easy to see */
853 glFogi(GL_FOG_MODE,GL_EXP2);
854 glFogfv(GL_FOG_COLOR,fogColor);
855 glFogf(GL_FOG_DENSITY,0.15);
856 glHint(GL_FOG_HINT,GL_DONT_CARE);
857
Keith Whitwell5759f532001-05-11 12:08:15 +0000858 {
859 static int firsttime = 1;
860 if (firsttime) {
861 firsttime = 0;
862 compactify_arrays();
863 expand_arrays();
864 make_tri_indices();
Keith Whitwell44c73931999-09-03 14:56:40 +0000865
Keith Whitwell5759f532001-05-11 12:08:15 +0000866 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
867 printf("Error: couldn't load texture image\n");
868 exit(1);
869 }
870 }
871 }
jtgafb833d1999-08-19 00:55:39 +0000872
873 ModeMenu(SHADE_SMOOTH|
874 LIT|
jtgafb833d1999-08-19 00:55:39 +0000875 POINT_FILTER|
jtgafb833d1999-08-19 00:55:39 +0000876 NO_USER_CLIP|
877 NO_MATERIALS|
Keith Whitwell44c73931999-09-03 14:56:40 +0000878 NO_FOG|
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000879 NO_STIPPLE|
Keith Whitwell5759f532001-05-11 12:08:15 +0000880 IMMEDIATE|
881 STRIPS|
882 UNLOCKED|
Keith Whitwell44c73931999-09-03 14:56:40 +0000883 GLVERTEX);
Brian Paulac126091999-10-21 16:39:06 +0000884
885 if (PrintInfo) {
886 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
887 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
888 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
889 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
890 }
jtgafb833d1999-08-19 00:55:39 +0000891}
892
893
894
895static void Reshape(int width, int height)
896{
897 glViewport(0, 0, (GLint)width, (GLint)height);
898}
899
900
901
902static void Key( unsigned char key, int x, int y )
903{
Brian Paul36ca6bd1999-09-08 22:14:31 +0000904 (void) x;
905 (void) y;
jtgafb833d1999-08-19 00:55:39 +0000906 switch (key) {
Keith Whitwell44c73931999-09-03 14:56:40 +0000907 case 27:
908 exit(0);
909 case 'f':
910 ModeMenu((state ^ FOG_MASK) & FOG_MASK);
911 break;
912 case 's':
913 ModeMenu((state ^ SHADE_MASK) & SHADE_MASK);
914 break;
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000915 case 't':
916 ModeMenu((state ^ STIPPLE_MASK) & STIPPLE_MASK);
917 break;
Keith Whitwell44c73931999-09-03 14:56:40 +0000918 case 'l':
Keith Whitwell5759f532001-05-11 12:08:15 +0000919 ModeMenu((state ^ LIGHT_MASK) & (LIT|UNLIT));
Keith Whitwell44c73931999-09-03 14:56:40 +0000920 break;
921 case 'm':
922 ModeMenu((state ^ MATERIAL_MASK) & MATERIAL_MASK);
923 break;
924 case 'c':
925 ModeMenu((state ^ CLIP_MASK) & CLIP_MASK);
926 break;
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000927 case 'v':
Keith Whitwell5759f532001-05-11 12:08:15 +0000928 ModeMenu((LOCKED|IMMEDIATE|DRAW_ELTS|TRIANGLES) & allowed);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000929 break;
930 case 'V':
Keith Whitwell5759f532001-05-11 12:08:15 +0000931 ModeMenu(UNLOCKED|IMMEDIATE|GLVERTEX|STRIPS);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000932 break;
Keith Whitwell44c73931999-09-03 14:56:40 +0000933 case 'b':
934 Benchmark(5.0, 0);
935 break;
936 case 'B':
937 Benchmark(0, 5.0);
938 break;
939 case 'i':
940 dist += .25;
941 set_matrix();
942 glutPostRedisplay();
943 break;
944 case 'I':
945 dist -= .25;
946 set_matrix();
947 glutPostRedisplay();
948 break;
949 case '-':
950 case '_':
951 plane[3] += 2.0;
952 glMatrixMode(GL_MODELVIEW);
953 glLoadIdentity();
954 glClipPlane(GL_CLIP_PLANE0, plane);
955 set_matrix();
956 glutPostRedisplay();
957 break;
958 case '+':
959 case '=':
960 plane[3] -= 2.0;
961 glMatrixMode(GL_MODELVIEW);
962 glLoadIdentity();
963 glClipPlane(GL_CLIP_PLANE0, plane);
964 set_matrix();
965 glutPostRedisplay();
966 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000967 case ' ':
968 Init(0,0);
969 break;
jtgafb833d1999-08-19 00:55:39 +0000970 }
971}
972
973
974static void SpecialKey( int key, int x, int y )
975{
Brian Paul36ca6bd1999-09-08 22:14:31 +0000976 (void) x;
977 (void) y;
jtgafb833d1999-08-19 00:55:39 +0000978 switch (key) {
979 case GLUT_KEY_LEFT:
980 yrot -= 15.0;
981 break;
982 case GLUT_KEY_RIGHT:
983 yrot += 15.0;
984 break;
985 case GLUT_KEY_UP:
986 xrot += 15.0;
987 break;
988 case GLUT_KEY_DOWN:
989 xrot -= 15.0;
990 break;
991 default:
992 return;
993 }
994 set_matrix();
995 glutPostRedisplay();
996}
997
998
999
1000static GLint Args(int argc, char **argv)
1001{
1002 GLint i;
1003 GLint mode = 0;
1004
1005 for (i = 1; i < argc; i++) {
1006 if (strcmp(argv[i], "-sb") == 0) {
1007 doubleBuffer = GL_FALSE;
1008 }
1009 else if (strcmp(argv[i], "-db") == 0) {
1010 doubleBuffer = GL_TRUE;
1011 }
Brian Paulac126091999-10-21 16:39:06 +00001012 else if (strcmp(argv[i], "-info") == 0) {
1013 PrintInfo = GL_TRUE;
1014 }
Keith Whitwell18acf6e2001-04-19 13:12:40 +00001015 else if (strcmp(argv[i], "-10") == 0) {
1016 maxverts = 10;
1017 }
1018 else if (strcmp(argv[i], "-100") == 0) {
1019 maxverts = 100;
1020 }
1021 else if (strcmp(argv[i], "-1000") == 0) {
1022 maxverts = 1000;
1023 }
jtgafb833d1999-08-19 00:55:39 +00001024 else {
1025 printf("%s (Bad option).\n", argv[i]);
1026 return QUIT;
1027 }
1028 }
1029
1030 return mode;
1031}
1032
1033int main(int argc, char **argv)
1034{
1035 GLenum type;
1036 char *extensions;
1037
1038 GLuint arg_mode = Args(argc, argv);
1039
1040 if (arg_mode & QUIT)
1041 exit(0);
1042
1043 read_surface( "isosurf.dat" );
1044
1045 glutInitWindowPosition(0, 0);
1046 glutInitWindowSize(400, 400);
Gareth Hughesc8516462001-01-06 20:38:03 +00001047
jtgafb833d1999-08-19 00:55:39 +00001048 type = GLUT_DEPTH;
1049 type |= GLUT_RGB;
1050 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
1051 glutInitDisplayMode(type);
1052
1053 if (glutCreateWindow("Isosurface") <= 0) {
1054 exit(0);
1055 }
1056
1057 /* Make sure server supports the vertex array extension */
1058 extensions = (char *) glGetString( GL_EXTENSIONS );
1059
Gareth Hughesc8516462001-01-06 20:38:03 +00001060 if (!strstr( extensions, "GL_EXT_vertex_array" ))
jtgafb833d1999-08-19 00:55:39 +00001061 {
1062 printf("Vertex arrays not supported by this renderer\n");
Keith Whitwell5759f532001-05-11 12:08:15 +00001063 allowed &= ~(LOCKED|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT);
jtgafb833d1999-08-19 00:55:39 +00001064 }
Gareth Hughesc8516462001-01-06 20:38:03 +00001065 else if (!strstr( extensions, "GL_EXT_compiled_vertex_array" ))
jtgafb833d1999-08-19 00:55:39 +00001066 {
1067 printf("Compiled vertex arrays not supported by this renderer\n");
Keith Whitwell5759f532001-05-11 12:08:15 +00001068 allowed &= ~LOCKED;
jtgafb833d1999-08-19 00:55:39 +00001069 }
1070
Brian Paulac126091999-10-21 16:39:06 +00001071 Init(argc, argv);
jtgafb833d1999-08-19 00:55:39 +00001072 ModeMenu(arg_mode);
Gareth Hughesc8516462001-01-06 20:38:03 +00001073
jtgafb833d1999-08-19 00:55:39 +00001074 glutCreateMenu(ModeMenu);
Gareth Hughesc8516462001-01-06 20:38:03 +00001075 glutAddMenuEntry("GL info", GLINFO);
1076 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001077 glutAddMenuEntry("Lit", LIT);
1078 glutAddMenuEntry("Unlit", UNLIT);
1079 glutAddMenuEntry("Reflect", REFLECT);
Gareth Hughesc8516462001-01-06 20:38:03 +00001080 glutAddMenuEntry("", 0);
jtgafb833d1999-08-19 00:55:39 +00001081 glutAddMenuEntry("Smooth", SHADE_SMOOTH);
1082 glutAddMenuEntry("Flat", SHADE_FLAT);
Gareth Hughesc8516462001-01-06 20:38:03 +00001083 glutAddMenuEntry("", 0);
Keith Whitwell44c73931999-09-03 14:56:40 +00001084 glutAddMenuEntry("Fog", FOG);
1085 glutAddMenuEntry("No Fog", NO_FOG);
Gareth Hughesc8516462001-01-06 20:38:03 +00001086 glutAddMenuEntry("", 0);
Keith Whitwell03b7aee2000-03-30 17:58:56 +00001087 glutAddMenuEntry("Stipple", STIPPLE);
1088 glutAddMenuEntry("No Stipple", NO_STIPPLE);
Gareth Hughesc8516462001-01-06 20:38:03 +00001089 glutAddMenuEntry("", 0);
Gareth Hughes735d9202002-01-04 09:47:17 +00001090 glutAddMenuEntry("Polygon Mode Fill", POLYGON_FILL);
1091 glutAddMenuEntry("Polygon Mode Line", POLYGON_LINE);
1092 glutAddMenuEntry("", 0);
jtgafb833d1999-08-19 00:55:39 +00001093 glutAddMenuEntry("Point Filtered", POINT_FILTER);
1094 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
Gareth Hughesc8516462001-01-06 20:38:03 +00001095 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001096 glutAddMenuEntry("GL_TRIANGLES", TRIANGLES);
1097 glutAddMenuEntry("GL_TRIANGLE_STRIPS", STRIPS);
1098 glutAddMenuEntry("GL_POINTS", POINTS);
Gareth Hughesc8516462001-01-06 20:38:03 +00001099 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001100 glutAddMenuEntry("Displaylist", DISPLAYLIST);
1101 glutAddMenuEntry("Immediate", IMMEDIATE);
Gareth Hughesc8516462001-01-06 20:38:03 +00001102 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001103 if (allowed & LOCKED) {
1104 glutAddMenuEntry("Locked Arrays (CVA)", LOCKED);
1105 glutAddMenuEntry("Unlocked Arrays", UNLOCKED);
1106 glutAddMenuEntry("", 0);
1107 }
1108 glutAddMenuEntry("glVertex", GLVERTEX);
jtgafb833d1999-08-19 00:55:39 +00001109 if (allowed & DRAW_ARRAYS) {
Keith Whitwell5759f532001-05-11 12:08:15 +00001110 glutAddMenuEntry("glDrawElements", DRAW_ELTS);
1111 glutAddMenuEntry("glDrawArrays", DRAW_ARRAYS);
1112 glutAddMenuEntry("glArrayElement", ARRAY_ELT);
jtgafb833d1999-08-19 00:55:39 +00001113 }
Keith Whitwell5759f532001-05-11 12:08:15 +00001114 glutAddMenuEntry("", 0);
1115 glutAddMenuEntry("Quit", QUIT);
jtgafb833d1999-08-19 00:55:39 +00001116 glutAttachMenu(GLUT_RIGHT_BUTTON);
1117
1118 glutReshapeFunc(Reshape);
1119 glutKeyboardFunc(Key);
1120 glutSpecialFunc(SpecialKey);
1121 glutDisplayFunc(Display);
Keith Whitwell03b7aee2000-03-30 17:58:56 +00001122
jtgafb833d1999-08-19 00:55:39 +00001123 glutMainLoop();
1124 return 0;
1125}