blob: e280d8f507c553d2d4201cf9176b6ef4d8325950 [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
José Fonseca2e61d132009-01-24 16:39:49 +000036#include <GL/glew.h>
jtgafb833d1999-08-19 00:55:39 +000037#include "GL/glut.h"
38
Brian Paul575d24a2005-01-09 17:15:41 +000039#include "readtex.h"
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
Jakob Bornecrantz54e20822009-02-13 17:53:49 +010072#define POLYGON_POINT 0x40000000
jtgafb833d1999-08-19 00:55:39 +000073
Keith Whitwell5759f532001-05-11 12:08:15 +000074#define LIGHT_MASK (LIT|UNLIT|REFLECT)
Gareth Hughesc8516462001-01-06 20:38:03 +000075#define FILTER_MASK (POINT_FILTER|LINEAR_FILTER)
Keith Whitwell5759f532001-05-11 12:08:15 +000076#define RENDER_STYLE_MASK (GLVERTEX|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT)
77#define DLIST_MASK (IMMEDIATE|DISPLAYLIST)
78#define LOCK_MASK (LOCKED|UNLOCKED)
Gareth Hughesc8516462001-01-06 20:38:03 +000079#define MATERIAL_MASK (MATERIALS|NO_MATERIALS)
80#define PRIMITIVE_MASK (TRIANGLES|STRIPS|POINTS)
81#define CLIP_MASK (USER_CLIP|NO_USER_CLIP)
82#define SHADE_MASK (SHADE_SMOOTH|SHADE_FLAT)
83#define FOG_MASK (FOG|NO_FOG)
84#define STIPPLE_MASK (STIPPLE|NO_STIPPLE)
Jakob Bornecrantz54e20822009-02-13 17:53:49 +010085#define POLYGON_MASK (POLYGON_FILL|POLYGON_LINE|POLYGON_POINT)
jtgafb833d1999-08-19 00:55:39 +000086
87#define MAXVERTS 10000
Karl Schultz53d30c52002-10-18 17:47:35 +000088static GLint maxverts = MAXVERTS;
jtgafb833d1999-08-19 00:55:39 +000089static float data[MAXVERTS][6];
90static float compressed_data[MAXVERTS][6];
Keith Whitwell5759f532001-05-11 12:08:15 +000091static float expanded_data[MAXVERTS*3][6];
jtgafb833d1999-08-19 00:55:39 +000092static GLuint indices[MAXVERTS];
93static GLuint tri_indices[MAXVERTS*3];
Keith Whitwellb8f99802001-05-11 15:47:02 +000094static GLuint strip_indices[MAXVERTS];
jtgafb833d1999-08-19 00:55:39 +000095static GLfloat col[100][4];
96static GLint numverts, num_tri_verts, numuniq;
97
98static GLfloat xrot;
99static GLfloat yrot;
Keith Whitwell5759f532001-05-11 12:08:15 +0000100static GLfloat dist;
jtgafb833d1999-08-19 00:55:39 +0000101static GLint state, allowed = ~0;
102static GLboolean doubleBuffer = GL_TRUE;
Keith Whitwell5759f532001-05-11 12:08:15 +0000103static GLdouble plane[4];
104static GLuint surf1, dlist_state;
jtgafb833d1999-08-19 00:55:39 +0000105
Brian Paulac126091999-10-21 16:39:06 +0000106static GLboolean PrintInfo = GL_FALSE;
107
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000108
109static GLubyte halftone[] = {
110 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
111 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
112 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
113 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
114 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
115 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
116 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
117 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
118 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
119 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
120 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};
121
Brian Paul36ca6bd1999-09-08 22:14:31 +0000122
jtgafb833d1999-08-19 00:55:39 +0000123static void read_surface( char *filename )
124{
125 FILE *f;
126
127 f = fopen(filename,"r");
128 if (!f) {
129 printf("couldn't read %s\n", filename);
130 exit(1);
131 }
132
133 numverts = 0;
Keith Whitwell18acf6e2001-04-19 13:12:40 +0000134 while (!feof(f) && numverts<maxverts) {
jtgafb833d1999-08-19 00:55:39 +0000135 fscanf( f, "%f %f %f %f %f %f",
136 &data[numverts][0], &data[numverts][1], &data[numverts][2],
137 &data[numverts][3], &data[numverts][4], &data[numverts][5] );
138 numverts++;
139 }
140 numverts--;
141
142 printf("%d vertices, %d triangles\n", numverts, numverts-2);
143 fclose(f);
144}
145
146
147
Keith Whitwell5759f532001-05-11 12:08:15 +0000148static void print_flags( const char *msg, GLuint flags )
149{
150 fprintf(stderr,
Jakob Bornecrantz54e20822009-02-13 17:53:49 +0100151 "%s (0x%x): %s%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 +0000152 msg, flags,
153 (flags & GLVERTEX) ? "glVertex, " : "",
154 (flags & DRAW_ARRAYS) ? "glDrawArrays, " : "",
155 (flags & DRAW_ELTS) ? "glDrawElements, " : "",
156 (flags & ARRAY_ELT) ? "glArrayElement, " : "",
157 (flags & LOCKED) ? "locked arrays, " : "",
158 (flags & TRIANGLES) ? "GL_TRIANGLES, " : "",
159 (flags & STRIPS) ? "GL_TRIANGLE_STRIP, " : "",
160 (flags & POINTS) ? "GL_POINTS, " : "",
161 (flags & DISPLAYLIST) ? "as a displaylist, " : "",
162 (flags & LIT) ? "lit, " : "",
163 (flags & UNLIT) ? "unlit, " : "",
164 (flags & REFLECT) ? "reflect, " : "",
165 (flags & SHADE_FLAT) ? "flat-shaded, " : "",
166 (flags & USER_CLIP) ? "user_clip, " : "",
167 (flags & MATERIALS) ? "materials, " : "",
168 (flags & FOG) ? "fog, " : "",
Gareth Hughes735d9202002-01-04 09:47:17 +0000169 (flags & STIPPLE) ? "stipple, " : "",
Jakob Bornecrantz54e20822009-02-13 17:53:49 +0100170 (flags & POLYGON_LINE) ? "polygon mode line, " : "",
171 (flags & POLYGON_POINT) ? "polygon mode point, " : "");
Keith Whitwell5759f532001-05-11 12:08:15 +0000172}
173
174
jtgafb833d1999-08-19 00:55:39 +0000175
176struct data_idx {
177 float *data;
178 int idx;
179 int uniq_idx;
180};
181
182
183#define COMPARE_FUNC( AXIS ) \
Brian Paulac126091999-10-21 16:39:06 +0000184static int compare_axis_##AXIS( const void *a, const void *b ) \
jtgafb833d1999-08-19 00:55:39 +0000185{ \
186 float t = ( (*(struct data_idx *)a).data[AXIS] - \
187 (*(struct data_idx *)b).data[AXIS] ); \
188 \
189 if (t < 0) return -1; \
190 if (t > 0) return 1; \
191 return 0; \
192}
193
194COMPARE_FUNC(0)
195COMPARE_FUNC(1)
196COMPARE_FUNC(2)
197COMPARE_FUNC(3)
198COMPARE_FUNC(4)
199COMPARE_FUNC(5)
200COMPARE_FUNC(6)
201
202int (*(compare[7]))( const void *a, const void *b ) =
203{
204 compare_axis_0,
205 compare_axis_1,
206 compare_axis_2,
207 compare_axis_3,
208 compare_axis_4,
209 compare_axis_5,
210 compare_axis_6,
211};
212
213
214#define VEC_ELT(f, s, i) (float *)(((char *)f) + s * i)
215
Gareth Hughesc8516462001-01-06 20:38:03 +0000216static int sort_axis( int axis,
jtgafb833d1999-08-19 00:55:39 +0000217 int vec_size,
218 int vec_stride,
219 struct data_idx *indices,
220 int start,
221 int finish,
222 float *out,
223 int uniq,
224 const float fudge )
225{
226 int i;
227
Gareth Hughesc8516462001-01-06 20:38:03 +0000228 if (finish-start > 2)
jtgafb833d1999-08-19 00:55:39 +0000229 {
230 qsort( indices+start, finish-start, sizeof(*indices), compare[axis] );
Gareth Hughesc8516462001-01-06 20:38:03 +0000231 }
232 else if (indices[start].data[axis] > indices[start+1].data[axis])
jtgafb833d1999-08-19 00:55:39 +0000233 {
234 struct data_idx tmp = indices[start];
235 indices[start] = indices[start+1];
236 indices[start+1] = tmp;
237 }
Gareth Hughesc8516462001-01-06 20:38:03 +0000238
jtgafb833d1999-08-19 00:55:39 +0000239 if (axis == vec_size-1) {
240 for (i = start ; i < finish ; ) {
241 float max = indices[i].data[axis] + fudge;
242 float *dest = VEC_ELT(out, vec_stride, uniq);
243 int j;
Gareth Hughesc8516462001-01-06 20:38:03 +0000244
jtgafb833d1999-08-19 00:55:39 +0000245 for (j = 0 ; j < vec_size ; j++)
246 dest[j] = indices[i].data[j];
247
Gareth Hughesc8516462001-01-06 20:38:03 +0000248 for ( ; i < finish && max >= indices[i].data[axis]; i++)
jtgafb833d1999-08-19 00:55:39 +0000249 indices[i].uniq_idx = uniq;
250
251 uniq++;
252 }
253 } else {
254 for (i = start ; i < finish ; ) {
255 int j = i + 1;
256 float max = indices[i].data[axis] + fudge;
257 while (j < finish && max >= indices[j].data[axis]) j++;
258 if (j == i+1) {
259 float *dest = VEC_ELT(out, vec_stride, uniq);
260 int k;
261
262 indices[i].uniq_idx = uniq;
Gareth Hughesc8516462001-01-06 20:38:03 +0000263
jtgafb833d1999-08-19 00:55:39 +0000264 for (k = 0 ; k < vec_size ; k++)
265 dest[k] = indices[i].data[k];
266
267 uniq++;
268 } else {
269 uniq = sort_axis( axis+1, vec_size, vec_stride,
270 indices, i, j, out, uniq, fudge );
271 }
272 i = j;
273 }
274 }
275
276 return uniq;
277}
278
279
Gareth Hughesc8516462001-01-06 20:38:03 +0000280static void extract_indices1( const struct data_idx *in, unsigned int *out,
jtgafb833d1999-08-19 00:55:39 +0000281 int n )
282{
283 int i;
284 for ( i = 0 ; i < n ; i++ ) {
285 out[in[i].idx] = in[i].uniq_idx;
286 }
287}
288
289
Brian Paul36ca6bd1999-09-08 22:14:31 +0000290static void compactify_arrays(void)
jtgafb833d1999-08-19 00:55:39 +0000291{
292 int i;
293 struct data_idx *ind;
294
295 ind = (struct data_idx *) malloc( sizeof(struct data_idx) * numverts );
296
297 for (i = 0 ; i < numverts ; i++) {
298 ind[i].idx = i;
299 ind[i].data = data[i];
300 }
301
Gareth Hughesc8516462001-01-06 20:38:03 +0000302 numuniq = sort_axis(0,
303 sizeof(compressed_data[0])/sizeof(float),
jtgafb833d1999-08-19 00:55:39 +0000304 sizeof(compressed_data[0]),
Gareth Hughesc8516462001-01-06 20:38:03 +0000305 ind,
306 0,
307 numverts,
308 (float *)compressed_data,
jtgafb833d1999-08-19 00:55:39 +0000309 0,
310 1e-6);
311
312 printf("Nr unique vertex/normal pairs: %d\n", numuniq);
313
314 extract_indices1( ind, indices, numverts );
315 free( ind );
316}
317
Keith Whitwell5759f532001-05-11 12:08:15 +0000318static void expand_arrays(void)
319{
320 int i;
321 int parity = 0;
322 for (i = 2 ; i < numverts ; i++, parity ^= 1) {
323 int v0 = i-2+parity;
324 int v1 = i-1-parity;
325 int v2 = i;
326 memcpy( expanded_data[(i-2)*3+0], data[v0], sizeof(data[0]) );
327 memcpy( expanded_data[(i-2)*3+1], data[v1], sizeof(data[0]) );
328 memcpy( expanded_data[(i-2)*3+2], data[v2], sizeof(data[0]) );
329 }
330}
331
jtgafb833d1999-08-19 00:55:39 +0000332static float myrand( float max )
333{
334 return max*rand()/(RAND_MAX+1.0);
335}
336
337
338static void make_tri_indices( void )
339{
340 unsigned int *v = tri_indices;
341 unsigned int parity = 0;
Karl Schultz53d30c52002-10-18 17:47:35 +0000342 int i, j;
jtgafb833d1999-08-19 00:55:39 +0000343
344 for (j=2;j<numverts;j++,parity^=1) {
345 if (parity) {
346 *v++ = indices[j-1];
Gareth Hughesc8516462001-01-06 20:38:03 +0000347 *v++ = indices[j-2];
jtgafb833d1999-08-19 00:55:39 +0000348 *v++ = indices[j];
349 } else {
350 *v++ = indices[j-2];
351 *v++ = indices[j-1];
352 *v++ = indices[j];
353 }
354 }
Gareth Hughesc8516462001-01-06 20:38:03 +0000355
jtgafb833d1999-08-19 00:55:39 +0000356 num_tri_verts = v - tri_indices;
357 printf("num_tri_verts: %d\n", num_tri_verts);
358
359 for (i = j = 0 ; i < num_tri_verts ; i += 600, j++) {
360 col[j][3] = 1;
361 col[j][2] = myrand(1);
362 col[j][1] = myrand(1);
363 col[j][0] = myrand(1);
364 }
Keith Whitwellb8f99802001-05-11 15:47:02 +0000365
366 for (i = 0; i < numverts ; i++)
367 strip_indices[i] = i;
jtgafb833d1999-08-19 00:55:39 +0000368}
369
370#define MIN(x,y) (x < y) ? x : y
371
Karl Schultz53d30c52002-10-18 17:47:35 +0000372static void draw_surface( unsigned int with_state )
jtgafb833d1999-08-19 00:55:39 +0000373{
Karl Schultz53d30c52002-10-18 17:47:35 +0000374 GLint i, j;
Keith Whitwell5759f532001-05-11 12:08:15 +0000375
376 if (with_state & DISPLAYLIST) {
377 if ((with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|MATERIAL_MASK)) !=
378 dlist_state) {
379 /*
380 */
381 fprintf(stderr, "rebuilding displaylist\n");
jtgafb833d1999-08-19 00:55:39 +0000382
Keith Whitwell5759f532001-05-11 12:08:15 +0000383 if (dlist_state)
384 glDeleteLists( surf1, 1 );
385
386 dlist_state = with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|
387 MATERIAL_MASK);
388 surf1 = glGenLists(1);
389 glNewList(surf1, GL_COMPILE);
390 draw_surface( dlist_state );
391 glEndList();
392 }
393
394 glCallList( surf1 );
395 return;
396 }
397
398 switch (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK)) {
jtgafb833d1999-08-19 00:55:39 +0000399#ifdef GL_EXT_vertex_array
400
Keith Whitwell5759f532001-05-11 12:08:15 +0000401 case (DRAW_ELTS|TRIANGLES):
Keith Whitwell44c73931999-09-03 14:56:40 +0000402 if (with_state & MATERIALS) {
jtgafb833d1999-08-19 00:55:39 +0000403 for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
404 GLuint nr = MIN(num_tri_verts-i, 600);
405 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
406 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
407 glDrawElements( GL_TRIANGLES, nr, GL_UNSIGNED_INT, tri_indices+i );
408 }
409 } else {
Gareth Hughesc8516462001-01-06 20:38:03 +0000410 glDrawElements( GL_TRIANGLES, num_tri_verts, GL_UNSIGNED_INT,
jtgafb833d1999-08-19 00:55:39 +0000411 tri_indices );
412 }
jtgafb833d1999-08-19 00:55:39 +0000413 break;
414
Keith Whitwell5759f532001-05-11 12:08:15 +0000415 case (DRAW_ARRAYS|TRIANGLES):
416 glDrawArraysEXT( GL_TRIANGLES, 0, (numverts-2)*3 );
417 break;
418
419 case (ARRAY_ELT|TRIANGLES):
Keith Whitwell44c73931999-09-03 14:56:40 +0000420 if (with_state & MATERIALS) {
jtgafb833d1999-08-19 00:55:39 +0000421 for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
422 GLuint nr = MIN(num_tri_verts-i, 600);
423 GLuint k;
424 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
425 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
426 glBegin( GL_TRIANGLES );
427 for (k = 0 ; k < nr ; k++)
428 glArrayElement( tri_indices[i+k] );
429 glEnd();
430 }
431 } else {
432 glBegin( GL_TRIANGLES );
433 for (i = 0 ; i < num_tri_verts ; i++)
434 glArrayElement( tri_indices[i] );
Gareth Hughesc8516462001-01-06 20:38:03 +0000435
jtgafb833d1999-08-19 00:55:39 +0000436 glEnd();
Gareth Hughesc8516462001-01-06 20:38:03 +0000437 }
jtgafb833d1999-08-19 00:55:39 +0000438 break;
439
Gareth Hughesc8516462001-01-06 20:38:03 +0000440
441 /* Uses the original arrays (including duplicate elements):
442 */
Keith Whitwell5759f532001-05-11 12:08:15 +0000443 case (DRAW_ARRAYS|STRIPS):
Gareth Hughesc8516462001-01-06 20:38:03 +0000444 glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
445 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000446 case (DRAW_ELTS|STRIPS):
Keith Whitwellb8f99802001-05-11 15:47:02 +0000447 glDrawElements( GL_TRIANGLE_STRIP, numverts,
448 GL_UNSIGNED_INT, strip_indices );
Gareth Hughesc8516462001-01-06 20:38:03 +0000449 break;
450
451 /* Uses the original arrays (including duplicate elements):
452 */
Keith Whitwell5759f532001-05-11 12:08:15 +0000453 case (ARRAY_ELT|STRIPS):
Gareth Hughesc8516462001-01-06 20:38:03 +0000454 glBegin( GL_TRIANGLE_STRIP );
455 for (i = 0 ; i < numverts ; i++)
456 glArrayElement( i );
457 glEnd();
458 break;
459
Keith Whitwell5759f532001-05-11 12:08:15 +0000460 case (DRAW_ARRAYS|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000461 glDrawArraysEXT( GL_POINTS, 0, numuniq );
Gareth Hughesc8516462001-01-06 20:38:03 +0000462 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000463 case (DRAW_ELTS|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000464 /* can use numuniq with strip_indices as strip_indices[i] == i.
465 */
466 glDrawElements( GL_POINTS, numuniq,
467 GL_UNSIGNED_INT, strip_indices );
Gareth Hughesc8516462001-01-06 20:38:03 +0000468 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000469 case (ARRAY_ELT|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000470 /* just emit each unique element once:
471 */
Gareth Hughesc8516462001-01-06 20:38:03 +0000472 glBegin( GL_POINTS );
Keith Whitwellabd51342001-06-04 15:34:31 +0000473 for (i = 0 ; i < numuniq ; i++)
474 glArrayElement( i );
Gareth Hughesc8516462001-01-06 20:38:03 +0000475 glEnd();
476 break;
477#endif
478
Keith Whitwell5759f532001-05-11 12:08:15 +0000479 case (GLVERTEX|TRIANGLES):
Keith Whitwell44c73931999-09-03 14:56:40 +0000480 if (with_state & MATERIALS) {
jtgafb833d1999-08-19 00:55:39 +0000481 for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
482 GLuint nr = MIN(num_tri_verts-i, 600);
483 GLuint k;
484 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
485 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
486 glBegin( GL_TRIANGLES );
487 for (k = 0 ; k < nr ; k++) {
488 glNormal3fv( &compressed_data[tri_indices[i+k]][3] );
489 glVertex3fv( &compressed_data[tri_indices[i+k]][0] );
490 }
491 glEnd();
492 }
493 } else {
494 glBegin( GL_TRIANGLES );
495 for (i = 0 ; i < num_tri_verts ; i++) {
496 glNormal3fv( &compressed_data[tri_indices[i]][3] );
497 glVertex3fv( &compressed_data[tri_indices[i]][0] );
498 }
499 glEnd();
Gareth Hughesc8516462001-01-06 20:38:03 +0000500 }
jtgafb833d1999-08-19 00:55:39 +0000501 break;
502
Keith Whitwell5759f532001-05-11 12:08:15 +0000503 case (GLVERTEX|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000504 /* Renders all points, but not in strip order... Shouldn't be a
505 * problem, but people may be confused as to why points are so
506 * much faster in this demo... And why cva doesn't help them...
507 */
Gareth Hughesc8516462001-01-06 20:38:03 +0000508 glBegin( GL_POINTS );
Keith Whitwellabd51342001-06-04 15:34:31 +0000509 for ( i = 0 ; i < numuniq ; i++ ) {
510 glNormal3fv( &compressed_data[i][3] );
511 glVertex3fv( &compressed_data[i][0] );
Gareth Hughesc8516462001-01-06 20:38:03 +0000512 }
513 glEnd();
Keith Whitwell44c73931999-09-03 14:56:40 +0000514 break;
515
Keith Whitwell5759f532001-05-11 12:08:15 +0000516 case (GLVERTEX|STRIPS):
jtgafb833d1999-08-19 00:55:39 +0000517 glBegin( GL_TRIANGLE_STRIP );
518 for (i=0;i<numverts;i++) {
519 glNormal3fv( &data[i][3] );
520 glVertex3fv( &data[i][0] );
521 }
522 glEnd();
Keith Whitwell5759f532001-05-11 12:08:15 +0000523 break;
524
525 default:
526 fprintf(stderr, "unimplemented mode %x...\n",
527 (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK)));
528 break;
jtgafb833d1999-08-19 00:55:39 +0000529 }
530}
531
532
533
534static void Display(void)
535{
536 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
Keith Whitwell44c73931999-09-03 14:56:40 +0000537 draw_surface( state );
jtgafb833d1999-08-19 00:55:39 +0000538 glFlush();
Gareth Hughesc8516462001-01-06 20:38:03 +0000539 if (doubleBuffer) glutSwapBuffers();
jtgafb833d1999-08-19 00:55:39 +0000540}
541
Keith Whitwell18acf6e2001-04-19 13:12:40 +0000542
jtgafb833d1999-08-19 00:55:39 +0000543/* KW: only do this when necessary, so CVA can re-use results.
544 */
545static void set_matrix( void )
546{
547 glMatrixMode(GL_MODELVIEW);
548 glLoadIdentity();
Keith Whitwell44c73931999-09-03 14:56:40 +0000549 glTranslatef( 0.0, 0.0, dist );
jtgafb833d1999-08-19 00:55:39 +0000550 glRotatef( yrot, 0.0, 1.0, 0.0 );
551 glRotatef( xrot, 1.0, 0.0, 0.0 );
552}
553
554static void Benchmark( float xdiff, float ydiff )
555{
556 int startTime, endTime;
557 int draws;
558 double seconds, fps, triPerSecond;
559
560 printf("Benchmarking...\n");
561
562 draws = 0;
563 startTime = glutGet(GLUT_ELAPSED_TIME);
564 xrot = 0.0;
565 do {
566 xrot += xdiff;
567 yrot += ydiff;
568 set_matrix();
569 Display();
570 draws++;
571 endTime = glutGet(GLUT_ELAPSED_TIME);
572 } while (endTime - startTime < 5000); /* 5 seconds */
573
574 /* Results */
575 seconds = (double) (endTime - startTime) / 1000.0;
576 triPerSecond = (numverts - 2) * draws / seconds;
577 fps = draws / seconds;
578 printf("Result: triangles/sec: %g fps: %g\n", triPerSecond, fps);
579}
580
581
582static void InitMaterials(void)
583{
584 static float ambient[] = {0.1, 0.1, 0.1, 1.0};
585 static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
586 static float position0[] = {0.0, 0.0, 20.0, 0.0};
587 static float position1[] = {0.0, 0.0, -20.0, 0.0};
588 static float front_mat_shininess[] = {60.0};
589 static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
590 static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
591 /*
592 static float back_mat_shininess[] = {60.0};
593 static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
594 static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
595 */
596 static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
597 static float lmodel_twoside[] = {GL_FALSE};
598
599 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
600 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
601 glLightfv(GL_LIGHT0, GL_POSITION, position0);
602 glEnable(GL_LIGHT0);
Gareth Hughesc8516462001-01-06 20:38:03 +0000603
jtgafb833d1999-08-19 00:55:39 +0000604 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
605 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
606 glLightfv(GL_LIGHT1, GL_POSITION, position1);
607 glEnable(GL_LIGHT1);
Gareth Hughesc8516462001-01-06 20:38:03 +0000608
jtgafb833d1999-08-19 00:55:39 +0000609 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
610 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
611
612 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
613 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
614 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000615
616 glPolygonStipple (halftone);
jtgafb833d1999-08-19 00:55:39 +0000617}
618
619
620
621#define UPDATE(o,n,mask) (o&=~mask, o|=n&mask)
Keith Whitwell5759f532001-05-11 12:08:15 +0000622#define CHANGED(o,n,mask) ((n&mask) && (n&mask) != (o&mask) )
jtgafb833d1999-08-19 00:55:39 +0000623
624static void ModeMenu(int m)
625{
626 m &= allowed;
627
628 if (!m) return;
629
Gareth Hughesc8516462001-01-06 20:38:03 +0000630 if (m==QUIT)
jtgafb833d1999-08-19 00:55:39 +0000631 exit(0);
632
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000633 if (m==GLINFO) {
634 printf("GL_VERSION: %s\n", (char *) glGetString(GL_VERSION));
635 printf("GL_EXTENSIONS: %s\n", (char *) glGetString(GL_EXTENSIONS));
636 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
637 return;
638 }
639
jtgafb833d1999-08-19 00:55:39 +0000640 if (CHANGED(state, m, FILTER_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000641 UPDATE(state, m, FILTER_MASK);
jtgafb833d1999-08-19 00:55:39 +0000642 if (m & LINEAR_FILTER) {
643 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
644 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
645 } else {
646 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
647 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
648 }
649 }
650
651 if (CHANGED(state, m, LIGHT_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000652 UPDATE(state, m, LIGHT_MASK);
653 if (m & LIT) {
jtgafb833d1999-08-19 00:55:39 +0000654 glEnable(GL_LIGHTING);
Keith Whitwell5759f532001-05-11 12:08:15 +0000655 glDisable(GL_TEXTURE_GEN_S);
656 glDisable(GL_TEXTURE_GEN_T);
657 glDisable(GL_TEXTURE_2D);
658 }
659 else if (m & UNLIT) {
jtgafb833d1999-08-19 00:55:39 +0000660 glDisable(GL_LIGHTING);
Keith Whitwell5759f532001-05-11 12:08:15 +0000661 glDisable(GL_TEXTURE_GEN_S);
662 glDisable(GL_TEXTURE_GEN_T);
663 glDisable(GL_TEXTURE_2D);
664 }
665 else if (m & REFLECT) {
666 glDisable(GL_LIGHTING);
667 glEnable(GL_TEXTURE_GEN_S);
668 glEnable(GL_TEXTURE_GEN_T);
669 glEnable(GL_TEXTURE_2D);
670 }
jtgafb833d1999-08-19 00:55:39 +0000671 }
672
673 if (CHANGED(state, m, SHADE_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000674 UPDATE(state, m, SHADE_MASK);
jtgafb833d1999-08-19 00:55:39 +0000675 if (m & SHADE_SMOOTH)
676 glShadeModel(GL_SMOOTH);
677 else
678 glShadeModel(GL_FLAT);
679 }
680
681
jtgafb833d1999-08-19 00:55:39 +0000682 if (CHANGED(state, m, CLIP_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000683 UPDATE(state, m, CLIP_MASK);
jtgafb833d1999-08-19 00:55:39 +0000684 if (m & USER_CLIP) {
685 glEnable(GL_CLIP_PLANE0);
686 } else {
687 glDisable(GL_CLIP_PLANE0);
688 }
689 }
690
Keith Whitwell44c73931999-09-03 14:56:40 +0000691 if (CHANGED(state, m, FOG_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000692 UPDATE(state, m, FOG_MASK);
Gareth Hughes735d9202002-01-04 09:47:17 +0000693 if (m & FOG) {
Keith Whitwell44c73931999-09-03 14:56:40 +0000694 glEnable(GL_FOG);
Gareth Hughesc8516462001-01-06 20:38:03 +0000695 }
Gareth Hughes735d9202002-01-04 09:47:17 +0000696 else {
Keith Whitwell44c73931999-09-03 14:56:40 +0000697 glDisable(GL_FOG);
Keith Whitwell44c73931999-09-03 14:56:40 +0000698 }
699 }
700
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000701 if (CHANGED(state, m, STIPPLE_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000702 UPDATE(state, m, STIPPLE_MASK);
Gareth Hughes735d9202002-01-04 09:47:17 +0000703 if (m & STIPPLE) {
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000704 glEnable(GL_POLYGON_STIPPLE);
Gareth Hughesc8516462001-01-06 20:38:03 +0000705 }
Gareth Hughes735d9202002-01-04 09:47:17 +0000706 else {
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000707 glDisable(GL_POLYGON_STIPPLE);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000708 }
709 }
710
Gareth Hughes735d9202002-01-04 09:47:17 +0000711 if (CHANGED(state, m, POLYGON_MASK)) {
712 UPDATE(state, m, POLYGON_MASK);
713 if (m & POLYGON_FILL) {
714 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
715 }
Jakob Bornecrantz54e20822009-02-13 17:53:49 +0100716 else if (m & POLYGON_LINE) {
Gareth Hughes735d9202002-01-04 09:47:17 +0000717 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
718 }
Jakob Bornecrantz54e20822009-02-13 17:53:49 +0100719 else {
720 glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
721 }
Gareth Hughes735d9202002-01-04 09:47:17 +0000722 }
723
jtgafb833d1999-08-19 00:55:39 +0000724#ifdef GL_EXT_vertex_array
Keith Whitwell5759f532001-05-11 12:08:15 +0000725 if (CHANGED(state, m, (LOCK_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK)))
jtgafb833d1999-08-19 00:55:39 +0000726 {
Keith Whitwell5759f532001-05-11 12:08:15 +0000727 if (m & (PRIMITIVE_MASK)) {
728 UPDATE(state, m, (PRIMITIVE_MASK));
729 }
730
731 if (m & (RENDER_STYLE_MASK)) {
732 UPDATE(state, m, (RENDER_STYLE_MASK));
733 }
734
735 if (m & LOCK_MASK) {
736 UPDATE(state, m, (LOCK_MASK));
737 }
738
739
740 print_flags("primitive", state & PRIMITIVE_MASK);
741 print_flags("render style", state & RENDER_STYLE_MASK);
742
Keith Whitwellabd51342001-06-04 15:34:31 +0000743 if ((state & PRIMITIVE_MASK) != STRIPS &&
744 ((state & RENDER_STYLE_MASK) == DRAW_ELTS ||
745 (state & RENDER_STYLE_MASK) == ARRAY_ELT ||
746 (state & PRIMITIVE_MASK) == POINTS))
jtgafb833d1999-08-19 00:55:39 +0000747 {
Keith Whitwell5759f532001-05-11 12:08:15 +0000748 fprintf(stderr, "enabling small arrays\n");
Keith Whitwellabd51342001-06-04 15:34:31 +0000749 /* Rendering any primitive with draw-element/array-element
750 * --> Can't do strips here as ordering has been lost in
751 * compaction process...
752 */
Gareth Hughesc8516462001-01-06 20:38:03 +0000753 glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numuniq,
jtgafb833d1999-08-19 00:55:39 +0000754 compressed_data );
Gareth Hughesc8516462001-01-06 20:38:03 +0000755 glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numuniq,
jtgafb833d1999-08-19 00:55:39 +0000756 &compressed_data[0][3]);
jtgafb833d1999-08-19 00:55:39 +0000757#ifdef GL_EXT_compiled_vertex_array
Keith Whitwell5759f532001-05-11 12:08:15 +0000758 if (allowed & LOCKED) {
759 if (state & LOCKED) {
760 glLockArraysEXT( 0, numuniq );
761 } else {
762 glUnlockArraysEXT();
763 }
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000764 }
jtgafb833d1999-08-19 00:55:39 +0000765#endif
Keith Whitwell5759f532001-05-11 12:08:15 +0000766 }
Keith Whitwellabd51342001-06-04 15:34:31 +0000767 else if ((state & PRIMITIVE_MASK) == TRIANGLES &&
768 (state & RENDER_STYLE_MASK) == DRAW_ARRAYS) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000769 fprintf(stderr, "enabling big arrays\n");
Keith Whitwellabd51342001-06-04 15:34:31 +0000770 /* Only get here for TRIANGLES and drawarrays
Keith Whitwell5759f532001-05-11 12:08:15 +0000771 */
772 glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
773 expanded_data );
774 glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
775 &expanded_data[0][3]);
776
777#ifdef GL_EXT_compiled_vertex_array
778 if (allowed & LOCKED) {
779 if (state & LOCKED) {
780 glLockArraysEXT( 0, (numverts-2)*3 );
781 } else {
782 glUnlockArraysEXT();
783 }
784 }
785#endif
786 }
Keith Whitwellabd51342001-06-04 15:34:31 +0000787 else {
788 fprintf(stderr, "enabling normal arrays\n");
789 glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numverts, data );
790 glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numverts, &data[0][3]);
791#ifdef GL_EXT_compiled_vertex_array
792 if (allowed & LOCKED) {
793 if (state & LOCKED) {
794 glLockArraysEXT( 0, numverts );
795 } else {
796 glUnlockArraysEXT();
797 }
798 }
799#endif
800 }
Keith Whitwell5759f532001-05-11 12:08:15 +0000801
jtgafb833d1999-08-19 00:55:39 +0000802 }
803#endif
804
Keith Whitwell5759f532001-05-11 12:08:15 +0000805
806 if (m & DLIST_MASK) {
807 UPDATE(state, m, DLIST_MASK);
jtgafb833d1999-08-19 00:55:39 +0000808 }
Gareth Hughesc8516462001-01-06 20:38:03 +0000809
jtgafb833d1999-08-19 00:55:39 +0000810 if (m & MATERIAL_MASK) {
811 UPDATE(state, m, MATERIAL_MASK);
812 }
813
Keith Whitwell5759f532001-05-11 12:08:15 +0000814 print_flags("new flags", state);
815
jtgafb833d1999-08-19 00:55:39 +0000816 glutPostRedisplay();
817}
818
819
820
Brian Paulac126091999-10-21 16:39:06 +0000821static void Init(int argc, char *argv[])
jtgafb833d1999-08-19 00:55:39 +0000822{
Keith Whitwell44c73931999-09-03 14:56:40 +0000823 GLfloat fogColor[4] = {0.5,1.0,0.5,1.0};
824
Keith Whitwell5759f532001-05-11 12:08:15 +0000825 xrot = 0;
826 yrot = 0;
827 dist = -6;
828 plane[0] = 1.0;
829 plane[1] = 0.0;
830 plane[2] = -1.0;
831 plane[3] = 0.0;
832
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000833 glClearColor(0.0, 0.0, 1.0, 0.0);
jtgafb833d1999-08-19 00:55:39 +0000834 glEnable( GL_DEPTH_TEST );
835 glEnable( GL_VERTEX_ARRAY_EXT );
836 glEnable( GL_NORMAL_ARRAY_EXT );
837
jtgafb833d1999-08-19 00:55:39 +0000838 glMatrixMode(GL_PROJECTION);
839 glLoadIdentity();
840 glFrustum( -1.0, 1.0, -1.0, 1.0, 5, 25 );
841
842 glMatrixMode(GL_MODELVIEW);
843 glLoadIdentity();
Gareth Hughesc8516462001-01-06 20:38:03 +0000844 glClipPlane(GL_CLIP_PLANE0, plane);
jtgafb833d1999-08-19 00:55:39 +0000845
Keith Whitwellb8f99802001-05-11 15:47:02 +0000846 InitMaterials();
847
jtgafb833d1999-08-19 00:55:39 +0000848 set_matrix();
849
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000850 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
jtgafb833d1999-08-19 00:55:39 +0000851 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
852
853 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
854 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
855
jtgafb833d1999-08-19 00:55:39 +0000856
Keith Whitwell44c73931999-09-03 14:56:40 +0000857 /* Green fog is easy to see */
858 glFogi(GL_FOG_MODE,GL_EXP2);
859 glFogfv(GL_FOG_COLOR,fogColor);
860 glFogf(GL_FOG_DENSITY,0.15);
861 glHint(GL_FOG_HINT,GL_DONT_CARE);
862
Keith Whitwell5759f532001-05-11 12:08:15 +0000863 {
864 static int firsttime = 1;
865 if (firsttime) {
866 firsttime = 0;
867 compactify_arrays();
868 expand_arrays();
869 make_tri_indices();
Keith Whitwell44c73931999-09-03 14:56:40 +0000870
Keith Whitwell5759f532001-05-11 12:08:15 +0000871 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
872 printf("Error: couldn't load texture image\n");
873 exit(1);
874 }
875 }
876 }
jtgafb833d1999-08-19 00:55:39 +0000877
878 ModeMenu(SHADE_SMOOTH|
879 LIT|
jtgafb833d1999-08-19 00:55:39 +0000880 POINT_FILTER|
jtgafb833d1999-08-19 00:55:39 +0000881 NO_USER_CLIP|
882 NO_MATERIALS|
Keith Whitwell44c73931999-09-03 14:56:40 +0000883 NO_FOG|
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000884 NO_STIPPLE|
Keith Whitwell5759f532001-05-11 12:08:15 +0000885 IMMEDIATE|
886 STRIPS|
887 UNLOCKED|
Keith Whitwell44c73931999-09-03 14:56:40 +0000888 GLVERTEX);
Brian Paulac126091999-10-21 16:39:06 +0000889
890 if (PrintInfo) {
891 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
892 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
893 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
894 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
895 }
jtgafb833d1999-08-19 00:55:39 +0000896}
897
898
899
900static void Reshape(int width, int height)
901{
902 glViewport(0, 0, (GLint)width, (GLint)height);
903}
904
905
906
907static void Key( unsigned char key, int x, int y )
908{
Brian Paul36ca6bd1999-09-08 22:14:31 +0000909 (void) x;
910 (void) y;
jtgafb833d1999-08-19 00:55:39 +0000911 switch (key) {
Keith Whitwell44c73931999-09-03 14:56:40 +0000912 case 27:
913 exit(0);
914 case 'f':
915 ModeMenu((state ^ FOG_MASK) & FOG_MASK);
916 break;
917 case 's':
918 ModeMenu((state ^ SHADE_MASK) & SHADE_MASK);
919 break;
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000920 case 't':
921 ModeMenu((state ^ STIPPLE_MASK) & STIPPLE_MASK);
922 break;
Keith Whitwell44c73931999-09-03 14:56:40 +0000923 case 'l':
Keith Whitwell5759f532001-05-11 12:08:15 +0000924 ModeMenu((state ^ LIGHT_MASK) & (LIT|UNLIT));
Keith Whitwell44c73931999-09-03 14:56:40 +0000925 break;
926 case 'm':
927 ModeMenu((state ^ MATERIAL_MASK) & MATERIAL_MASK);
928 break;
929 case 'c':
930 ModeMenu((state ^ CLIP_MASK) & CLIP_MASK);
931 break;
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000932 case 'v':
Keith Whitwell5759f532001-05-11 12:08:15 +0000933 ModeMenu((LOCKED|IMMEDIATE|DRAW_ELTS|TRIANGLES) & allowed);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000934 break;
935 case 'V':
Keith Whitwell5759f532001-05-11 12:08:15 +0000936 ModeMenu(UNLOCKED|IMMEDIATE|GLVERTEX|STRIPS);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000937 break;
Keith Whitwell44c73931999-09-03 14:56:40 +0000938 case 'b':
939 Benchmark(5.0, 0);
940 break;
941 case 'B':
942 Benchmark(0, 5.0);
943 break;
944 case 'i':
945 dist += .25;
946 set_matrix();
947 glutPostRedisplay();
948 break;
949 case 'I':
950 dist -= .25;
951 set_matrix();
952 glutPostRedisplay();
953 break;
954 case '-':
955 case '_':
956 plane[3] += 2.0;
957 glMatrixMode(GL_MODELVIEW);
958 glLoadIdentity();
959 glClipPlane(GL_CLIP_PLANE0, plane);
960 set_matrix();
961 glutPostRedisplay();
962 break;
963 case '+':
964 case '=':
965 plane[3] -= 2.0;
966 glMatrixMode(GL_MODELVIEW);
967 glLoadIdentity();
968 glClipPlane(GL_CLIP_PLANE0, plane);
969 set_matrix();
970 glutPostRedisplay();
971 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000972 case ' ':
973 Init(0,0);
974 break;
jtgafb833d1999-08-19 00:55:39 +0000975 }
976}
977
978
979static void SpecialKey( int key, int x, int y )
980{
Brian Paul36ca6bd1999-09-08 22:14:31 +0000981 (void) x;
982 (void) y;
jtgafb833d1999-08-19 00:55:39 +0000983 switch (key) {
984 case GLUT_KEY_LEFT:
985 yrot -= 15.0;
986 break;
987 case GLUT_KEY_RIGHT:
988 yrot += 15.0;
989 break;
990 case GLUT_KEY_UP:
991 xrot += 15.0;
992 break;
993 case GLUT_KEY_DOWN:
994 xrot -= 15.0;
995 break;
996 default:
997 return;
998 }
999 set_matrix();
1000 glutPostRedisplay();
1001}
1002
1003
1004
1005static GLint Args(int argc, char **argv)
1006{
1007 GLint i;
1008 GLint mode = 0;
1009
1010 for (i = 1; i < argc; i++) {
1011 if (strcmp(argv[i], "-sb") == 0) {
1012 doubleBuffer = GL_FALSE;
1013 }
1014 else if (strcmp(argv[i], "-db") == 0) {
1015 doubleBuffer = GL_TRUE;
1016 }
Brian Paulac126091999-10-21 16:39:06 +00001017 else if (strcmp(argv[i], "-info") == 0) {
1018 PrintInfo = GL_TRUE;
1019 }
Keith Whitwell18acf6e2001-04-19 13:12:40 +00001020 else if (strcmp(argv[i], "-10") == 0) {
1021 maxverts = 10;
1022 }
1023 else if (strcmp(argv[i], "-100") == 0) {
1024 maxverts = 100;
1025 }
1026 else if (strcmp(argv[i], "-1000") == 0) {
1027 maxverts = 1000;
1028 }
jtgafb833d1999-08-19 00:55:39 +00001029 else {
1030 printf("%s (Bad option).\n", argv[i]);
1031 return QUIT;
1032 }
1033 }
1034
1035 return mode;
1036}
1037
1038int main(int argc, char **argv)
1039{
1040 GLenum type;
jtgafb833d1999-08-19 00:55:39 +00001041
1042 GLuint arg_mode = Args(argc, argv);
1043
1044 if (arg_mode & QUIT)
1045 exit(0);
1046
1047 read_surface( "isosurf.dat" );
1048
Xavier Bachelotf98bdfc2007-07-06 12:56:21 -06001049 glutInit( &argc, argv);
jtgafb833d1999-08-19 00:55:39 +00001050 glutInitWindowPosition(0, 0);
1051 glutInitWindowSize(400, 400);
Gareth Hughesc8516462001-01-06 20:38:03 +00001052
jtgafb833d1999-08-19 00:55:39 +00001053 type = GLUT_DEPTH;
1054 type |= GLUT_RGB;
1055 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
1056 glutInitDisplayMode(type);
1057
1058 if (glutCreateWindow("Isosurface") <= 0) {
1059 exit(0);
1060 }
1061
José Fonsecaefdb7792009-01-24 16:47:50 +00001062 glewInit();
jtgafb833d1999-08-19 00:55:39 +00001063
José Fonsecaefdb7792009-01-24 16:47:50 +00001064 /* Make sure server supports the vertex array extension */
1065 if (!GLEW_EXT_vertex_array)
jtgafb833d1999-08-19 00:55:39 +00001066 {
1067 printf("Vertex arrays not supported by this renderer\n");
Keith Whitwell5759f532001-05-11 12:08:15 +00001068 allowed &= ~(LOCKED|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT);
jtgafb833d1999-08-19 00:55:39 +00001069 }
José Fonsecaefdb7792009-01-24 16:47:50 +00001070 else if (!GLEW_EXT_compiled_vertex_array)
jtgafb833d1999-08-19 00:55:39 +00001071 {
1072 printf("Compiled vertex arrays not supported by this renderer\n");
Keith Whitwell5759f532001-05-11 12:08:15 +00001073 allowed &= ~LOCKED;
jtgafb833d1999-08-19 00:55:39 +00001074 }
1075
Brian Paulac126091999-10-21 16:39:06 +00001076 Init(argc, argv);
jtgafb833d1999-08-19 00:55:39 +00001077 ModeMenu(arg_mode);
Gareth Hughesc8516462001-01-06 20:38:03 +00001078
jtgafb833d1999-08-19 00:55:39 +00001079 glutCreateMenu(ModeMenu);
Gareth Hughesc8516462001-01-06 20:38:03 +00001080 glutAddMenuEntry("GL info", GLINFO);
1081 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001082 glutAddMenuEntry("Lit", LIT);
1083 glutAddMenuEntry("Unlit", UNLIT);
1084 glutAddMenuEntry("Reflect", REFLECT);
Gareth Hughesc8516462001-01-06 20:38:03 +00001085 glutAddMenuEntry("", 0);
jtgafb833d1999-08-19 00:55:39 +00001086 glutAddMenuEntry("Smooth", SHADE_SMOOTH);
1087 glutAddMenuEntry("Flat", SHADE_FLAT);
Gareth Hughesc8516462001-01-06 20:38:03 +00001088 glutAddMenuEntry("", 0);
Keith Whitwell44c73931999-09-03 14:56:40 +00001089 glutAddMenuEntry("Fog", FOG);
1090 glutAddMenuEntry("No Fog", NO_FOG);
Gareth Hughesc8516462001-01-06 20:38:03 +00001091 glutAddMenuEntry("", 0);
Keith Whitwell03b7aee2000-03-30 17:58:56 +00001092 glutAddMenuEntry("Stipple", STIPPLE);
1093 glutAddMenuEntry("No Stipple", NO_STIPPLE);
Gareth Hughesc8516462001-01-06 20:38:03 +00001094 glutAddMenuEntry("", 0);
Gareth Hughes735d9202002-01-04 09:47:17 +00001095 glutAddMenuEntry("Polygon Mode Fill", POLYGON_FILL);
1096 glutAddMenuEntry("Polygon Mode Line", POLYGON_LINE);
Jakob Bornecrantz54e20822009-02-13 17:53:49 +01001097 glutAddMenuEntry("Polygon Mode Points", POLYGON_POINT);
Gareth Hughes735d9202002-01-04 09:47:17 +00001098 glutAddMenuEntry("", 0);
jtgafb833d1999-08-19 00:55:39 +00001099 glutAddMenuEntry("Point Filtered", POINT_FILTER);
1100 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
Gareth Hughesc8516462001-01-06 20:38:03 +00001101 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001102 glutAddMenuEntry("GL_TRIANGLES", TRIANGLES);
1103 glutAddMenuEntry("GL_TRIANGLE_STRIPS", STRIPS);
1104 glutAddMenuEntry("GL_POINTS", POINTS);
Gareth Hughesc8516462001-01-06 20:38:03 +00001105 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001106 glutAddMenuEntry("Displaylist", DISPLAYLIST);
1107 glutAddMenuEntry("Immediate", IMMEDIATE);
Gareth Hughesc8516462001-01-06 20:38:03 +00001108 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001109 if (allowed & LOCKED) {
1110 glutAddMenuEntry("Locked Arrays (CVA)", LOCKED);
1111 glutAddMenuEntry("Unlocked Arrays", UNLOCKED);
1112 glutAddMenuEntry("", 0);
1113 }
1114 glutAddMenuEntry("glVertex", GLVERTEX);
jtgafb833d1999-08-19 00:55:39 +00001115 if (allowed & DRAW_ARRAYS) {
Keith Whitwell5759f532001-05-11 12:08:15 +00001116 glutAddMenuEntry("glDrawElements", DRAW_ELTS);
1117 glutAddMenuEntry("glDrawArrays", DRAW_ARRAYS);
1118 glutAddMenuEntry("glArrayElement", ARRAY_ELT);
jtgafb833d1999-08-19 00:55:39 +00001119 }
Keith Whitwell5759f532001-05-11 12:08:15 +00001120 glutAddMenuEntry("", 0);
1121 glutAddMenuEntry("Quit", QUIT);
jtgafb833d1999-08-19 00:55:39 +00001122 glutAttachMenu(GLUT_RIGHT_BUTTON);
1123
1124 glutReshapeFunc(Reshape);
1125 glutKeyboardFunc(Key);
1126 glutSpecialFunc(SpecialKey);
1127 glutDisplayFunc(Display);
Keith Whitwell03b7aee2000-03-30 17:58:56 +00001128
jtgafb833d1999-08-19 00:55:39 +00001129 glutMainLoop();
1130 return 0;
1131}