blob: dbe4d8d172aa2d85a1b0a9fdcc1653638c6e5a38 [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) {
Vinson Leee81fe082009-12-26 01:08:26 -0800135 int result;
136 result = fscanf( f, "%f %f %f %f %f %f",
137 &data[numverts][0], &data[numverts][1], &data[numverts][2],
138 &data[numverts][3], &data[numverts][4], &data[numverts][5] );
139 (void) result;
jtgafb833d1999-08-19 00:55:39 +0000140 numverts++;
141 }
142 numverts--;
143
144 printf("%d vertices, %d triangles\n", numverts, numverts-2);
145 fclose(f);
146}
147
148
149
Keith Whitwell5759f532001-05-11 12:08:15 +0000150static void print_flags( const char *msg, GLuint flags )
151{
152 fprintf(stderr,
Jakob Bornecrantz54e20822009-02-13 17:53:49 +0100153 "%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 +0000154 msg, flags,
155 (flags & GLVERTEX) ? "glVertex, " : "",
156 (flags & DRAW_ARRAYS) ? "glDrawArrays, " : "",
157 (flags & DRAW_ELTS) ? "glDrawElements, " : "",
158 (flags & ARRAY_ELT) ? "glArrayElement, " : "",
159 (flags & LOCKED) ? "locked arrays, " : "",
160 (flags & TRIANGLES) ? "GL_TRIANGLES, " : "",
161 (flags & STRIPS) ? "GL_TRIANGLE_STRIP, " : "",
162 (flags & POINTS) ? "GL_POINTS, " : "",
163 (flags & DISPLAYLIST) ? "as a displaylist, " : "",
164 (flags & LIT) ? "lit, " : "",
165 (flags & UNLIT) ? "unlit, " : "",
166 (flags & REFLECT) ? "reflect, " : "",
167 (flags & SHADE_FLAT) ? "flat-shaded, " : "",
168 (flags & USER_CLIP) ? "user_clip, " : "",
169 (flags & MATERIALS) ? "materials, " : "",
170 (flags & FOG) ? "fog, " : "",
Gareth Hughes735d9202002-01-04 09:47:17 +0000171 (flags & STIPPLE) ? "stipple, " : "",
Jakob Bornecrantz54e20822009-02-13 17:53:49 +0100172 (flags & POLYGON_LINE) ? "polygon mode line, " : "",
173 (flags & POLYGON_POINT) ? "polygon mode point, " : "");
Keith Whitwell5759f532001-05-11 12:08:15 +0000174}
175
176
jtgafb833d1999-08-19 00:55:39 +0000177
178struct data_idx {
179 float *data;
180 int idx;
181 int uniq_idx;
182};
183
184
185#define COMPARE_FUNC( AXIS ) \
Brian Paulac126091999-10-21 16:39:06 +0000186static int compare_axis_##AXIS( const void *a, const void *b ) \
jtgafb833d1999-08-19 00:55:39 +0000187{ \
188 float t = ( (*(struct data_idx *)a).data[AXIS] - \
189 (*(struct data_idx *)b).data[AXIS] ); \
190 \
191 if (t < 0) return -1; \
192 if (t > 0) return 1; \
193 return 0; \
194}
195
196COMPARE_FUNC(0)
197COMPARE_FUNC(1)
198COMPARE_FUNC(2)
199COMPARE_FUNC(3)
200COMPARE_FUNC(4)
201COMPARE_FUNC(5)
202COMPARE_FUNC(6)
203
204int (*(compare[7]))( const void *a, const void *b ) =
205{
206 compare_axis_0,
207 compare_axis_1,
208 compare_axis_2,
209 compare_axis_3,
210 compare_axis_4,
211 compare_axis_5,
212 compare_axis_6,
213};
214
215
216#define VEC_ELT(f, s, i) (float *)(((char *)f) + s * i)
217
Gareth Hughesc8516462001-01-06 20:38:03 +0000218static int sort_axis( int axis,
jtgafb833d1999-08-19 00:55:39 +0000219 int vec_size,
220 int vec_stride,
221 struct data_idx *indices,
222 int start,
223 int finish,
224 float *out,
225 int uniq,
226 const float fudge )
227{
228 int i;
229
Gareth Hughesc8516462001-01-06 20:38:03 +0000230 if (finish-start > 2)
jtgafb833d1999-08-19 00:55:39 +0000231 {
232 qsort( indices+start, finish-start, sizeof(*indices), compare[axis] );
Gareth Hughesc8516462001-01-06 20:38:03 +0000233 }
234 else if (indices[start].data[axis] > indices[start+1].data[axis])
jtgafb833d1999-08-19 00:55:39 +0000235 {
236 struct data_idx tmp = indices[start];
237 indices[start] = indices[start+1];
238 indices[start+1] = tmp;
239 }
Gareth Hughesc8516462001-01-06 20:38:03 +0000240
jtgafb833d1999-08-19 00:55:39 +0000241 if (axis == vec_size-1) {
242 for (i = start ; i < finish ; ) {
243 float max = indices[i].data[axis] + fudge;
244 float *dest = VEC_ELT(out, vec_stride, uniq);
245 int j;
Gareth Hughesc8516462001-01-06 20:38:03 +0000246
jtgafb833d1999-08-19 00:55:39 +0000247 for (j = 0 ; j < vec_size ; j++)
248 dest[j] = indices[i].data[j];
249
Gareth Hughesc8516462001-01-06 20:38:03 +0000250 for ( ; i < finish && max >= indices[i].data[axis]; i++)
jtgafb833d1999-08-19 00:55:39 +0000251 indices[i].uniq_idx = uniq;
252
253 uniq++;
254 }
255 } else {
256 for (i = start ; i < finish ; ) {
257 int j = i + 1;
258 float max = indices[i].data[axis] + fudge;
259 while (j < finish && max >= indices[j].data[axis]) j++;
260 if (j == i+1) {
261 float *dest = VEC_ELT(out, vec_stride, uniq);
262 int k;
263
264 indices[i].uniq_idx = uniq;
Gareth Hughesc8516462001-01-06 20:38:03 +0000265
jtgafb833d1999-08-19 00:55:39 +0000266 for (k = 0 ; k < vec_size ; k++)
267 dest[k] = indices[i].data[k];
268
269 uniq++;
270 } else {
271 uniq = sort_axis( axis+1, vec_size, vec_stride,
272 indices, i, j, out, uniq, fudge );
273 }
274 i = j;
275 }
276 }
277
278 return uniq;
279}
280
281
Gareth Hughesc8516462001-01-06 20:38:03 +0000282static void extract_indices1( const struct data_idx *in, unsigned int *out,
jtgafb833d1999-08-19 00:55:39 +0000283 int n )
284{
285 int i;
286 for ( i = 0 ; i < n ; i++ ) {
287 out[in[i].idx] = in[i].uniq_idx;
288 }
289}
290
291
Brian Paul36ca6bd1999-09-08 22:14:31 +0000292static void compactify_arrays(void)
jtgafb833d1999-08-19 00:55:39 +0000293{
294 int i;
295 struct data_idx *ind;
296
297 ind = (struct data_idx *) malloc( sizeof(struct data_idx) * numverts );
298
299 for (i = 0 ; i < numverts ; i++) {
300 ind[i].idx = i;
301 ind[i].data = data[i];
302 }
303
Gareth Hughesc8516462001-01-06 20:38:03 +0000304 numuniq = sort_axis(0,
305 sizeof(compressed_data[0])/sizeof(float),
jtgafb833d1999-08-19 00:55:39 +0000306 sizeof(compressed_data[0]),
Gareth Hughesc8516462001-01-06 20:38:03 +0000307 ind,
308 0,
309 numverts,
310 (float *)compressed_data,
jtgafb833d1999-08-19 00:55:39 +0000311 0,
312 1e-6);
313
314 printf("Nr unique vertex/normal pairs: %d\n", numuniq);
315
316 extract_indices1( ind, indices, numverts );
317 free( ind );
318}
319
Keith Whitwell5759f532001-05-11 12:08:15 +0000320static void expand_arrays(void)
321{
322 int i;
323 int parity = 0;
324 for (i = 2 ; i < numverts ; i++, parity ^= 1) {
325 int v0 = i-2+parity;
326 int v1 = i-1-parity;
327 int v2 = i;
328 memcpy( expanded_data[(i-2)*3+0], data[v0], sizeof(data[0]) );
329 memcpy( expanded_data[(i-2)*3+1], data[v1], sizeof(data[0]) );
330 memcpy( expanded_data[(i-2)*3+2], data[v2], sizeof(data[0]) );
331 }
332}
333
jtgafb833d1999-08-19 00:55:39 +0000334static float myrand( float max )
335{
336 return max*rand()/(RAND_MAX+1.0);
337}
338
339
340static void make_tri_indices( void )
341{
342 unsigned int *v = tri_indices;
343 unsigned int parity = 0;
Karl Schultz53d30c52002-10-18 17:47:35 +0000344 int i, j;
jtgafb833d1999-08-19 00:55:39 +0000345
346 for (j=2;j<numverts;j++,parity^=1) {
347 if (parity) {
348 *v++ = indices[j-1];
Gareth Hughesc8516462001-01-06 20:38:03 +0000349 *v++ = indices[j-2];
jtgafb833d1999-08-19 00:55:39 +0000350 *v++ = indices[j];
351 } else {
352 *v++ = indices[j-2];
353 *v++ = indices[j-1];
354 *v++ = indices[j];
355 }
356 }
Gareth Hughesc8516462001-01-06 20:38:03 +0000357
jtgafb833d1999-08-19 00:55:39 +0000358 num_tri_verts = v - tri_indices;
359 printf("num_tri_verts: %d\n", num_tri_verts);
360
361 for (i = j = 0 ; i < num_tri_verts ; i += 600, j++) {
362 col[j][3] = 1;
363 col[j][2] = myrand(1);
364 col[j][1] = myrand(1);
365 col[j][0] = myrand(1);
366 }
Keith Whitwellb8f99802001-05-11 15:47:02 +0000367
368 for (i = 0; i < numverts ; i++)
369 strip_indices[i] = i;
jtgafb833d1999-08-19 00:55:39 +0000370}
371
372#define MIN(x,y) (x < y) ? x : y
373
Karl Schultz53d30c52002-10-18 17:47:35 +0000374static void draw_surface( unsigned int with_state )
jtgafb833d1999-08-19 00:55:39 +0000375{
Karl Schultz53d30c52002-10-18 17:47:35 +0000376 GLint i, j;
Keith Whitwell5759f532001-05-11 12:08:15 +0000377
378 if (with_state & DISPLAYLIST) {
379 if ((with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|MATERIAL_MASK)) !=
380 dlist_state) {
381 /*
382 */
383 fprintf(stderr, "rebuilding displaylist\n");
jtgafb833d1999-08-19 00:55:39 +0000384
Keith Whitwell5759f532001-05-11 12:08:15 +0000385 if (dlist_state)
386 glDeleteLists( surf1, 1 );
387
388 dlist_state = with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|
389 MATERIAL_MASK);
390 surf1 = glGenLists(1);
391 glNewList(surf1, GL_COMPILE);
392 draw_surface( dlist_state );
393 glEndList();
394 }
395
396 glCallList( surf1 );
397 return;
398 }
399
400 switch (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK)) {
jtgafb833d1999-08-19 00:55:39 +0000401#ifdef GL_EXT_vertex_array
402
Keith Whitwell5759f532001-05-11 12:08:15 +0000403 case (DRAW_ELTS|TRIANGLES):
Keith Whitwell44c73931999-09-03 14:56:40 +0000404 if (with_state & MATERIALS) {
jtgafb833d1999-08-19 00:55:39 +0000405 for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
406 GLuint nr = MIN(num_tri_verts-i, 600);
407 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
408 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
409 glDrawElements( GL_TRIANGLES, nr, GL_UNSIGNED_INT, tri_indices+i );
410 }
411 } else {
Gareth Hughesc8516462001-01-06 20:38:03 +0000412 glDrawElements( GL_TRIANGLES, num_tri_verts, GL_UNSIGNED_INT,
jtgafb833d1999-08-19 00:55:39 +0000413 tri_indices );
414 }
jtgafb833d1999-08-19 00:55:39 +0000415 break;
416
Keith Whitwell5759f532001-05-11 12:08:15 +0000417 case (DRAW_ARRAYS|TRIANGLES):
418 glDrawArraysEXT( GL_TRIANGLES, 0, (numverts-2)*3 );
419 break;
420
421 case (ARRAY_ELT|TRIANGLES):
Keith Whitwell44c73931999-09-03 14:56:40 +0000422 if (with_state & MATERIALS) {
jtgafb833d1999-08-19 00:55:39 +0000423 for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
424 GLuint nr = MIN(num_tri_verts-i, 600);
425 GLuint k;
426 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
427 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
428 glBegin( GL_TRIANGLES );
429 for (k = 0 ; k < nr ; k++)
430 glArrayElement( tri_indices[i+k] );
431 glEnd();
432 }
433 } else {
434 glBegin( GL_TRIANGLES );
435 for (i = 0 ; i < num_tri_verts ; i++)
436 glArrayElement( tri_indices[i] );
Gareth Hughesc8516462001-01-06 20:38:03 +0000437
jtgafb833d1999-08-19 00:55:39 +0000438 glEnd();
Gareth Hughesc8516462001-01-06 20:38:03 +0000439 }
jtgafb833d1999-08-19 00:55:39 +0000440 break;
441
Gareth Hughesc8516462001-01-06 20:38:03 +0000442
443 /* Uses the original arrays (including duplicate elements):
444 */
Keith Whitwell5759f532001-05-11 12:08:15 +0000445 case (DRAW_ARRAYS|STRIPS):
Gareth Hughesc8516462001-01-06 20:38:03 +0000446 glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
447 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000448 case (DRAW_ELTS|STRIPS):
Keith Whitwellb8f99802001-05-11 15:47:02 +0000449 glDrawElements( GL_TRIANGLE_STRIP, numverts,
450 GL_UNSIGNED_INT, strip_indices );
Gareth Hughesc8516462001-01-06 20:38:03 +0000451 break;
452
453 /* Uses the original arrays (including duplicate elements):
454 */
Keith Whitwell5759f532001-05-11 12:08:15 +0000455 case (ARRAY_ELT|STRIPS):
Gareth Hughesc8516462001-01-06 20:38:03 +0000456 glBegin( GL_TRIANGLE_STRIP );
457 for (i = 0 ; i < numverts ; i++)
458 glArrayElement( i );
459 glEnd();
460 break;
461
Keith Whitwell5759f532001-05-11 12:08:15 +0000462 case (DRAW_ARRAYS|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000463 glDrawArraysEXT( GL_POINTS, 0, numuniq );
Gareth Hughesc8516462001-01-06 20:38:03 +0000464 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000465 case (DRAW_ELTS|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000466 /* can use numuniq with strip_indices as strip_indices[i] == i.
467 */
468 glDrawElements( GL_POINTS, numuniq,
469 GL_UNSIGNED_INT, strip_indices );
Gareth Hughesc8516462001-01-06 20:38:03 +0000470 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000471 case (ARRAY_ELT|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000472 /* just emit each unique element once:
473 */
Gareth Hughesc8516462001-01-06 20:38:03 +0000474 glBegin( GL_POINTS );
Keith Whitwellabd51342001-06-04 15:34:31 +0000475 for (i = 0 ; i < numuniq ; i++)
476 glArrayElement( i );
Gareth Hughesc8516462001-01-06 20:38:03 +0000477 glEnd();
478 break;
479#endif
480
Keith Whitwell5759f532001-05-11 12:08:15 +0000481 case (GLVERTEX|TRIANGLES):
Keith Whitwell44c73931999-09-03 14:56:40 +0000482 if (with_state & MATERIALS) {
jtgafb833d1999-08-19 00:55:39 +0000483 for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
484 GLuint nr = MIN(num_tri_verts-i, 600);
485 GLuint k;
486 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
487 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
488 glBegin( GL_TRIANGLES );
489 for (k = 0 ; k < nr ; k++) {
490 glNormal3fv( &compressed_data[tri_indices[i+k]][3] );
491 glVertex3fv( &compressed_data[tri_indices[i+k]][0] );
492 }
493 glEnd();
494 }
495 } else {
496 glBegin( GL_TRIANGLES );
497 for (i = 0 ; i < num_tri_verts ; i++) {
498 glNormal3fv( &compressed_data[tri_indices[i]][3] );
499 glVertex3fv( &compressed_data[tri_indices[i]][0] );
500 }
501 glEnd();
Gareth Hughesc8516462001-01-06 20:38:03 +0000502 }
jtgafb833d1999-08-19 00:55:39 +0000503 break;
504
Keith Whitwell5759f532001-05-11 12:08:15 +0000505 case (GLVERTEX|POINTS):
Keith Whitwellabd51342001-06-04 15:34:31 +0000506 /* Renders all points, but not in strip order... Shouldn't be a
507 * problem, but people may be confused as to why points are so
508 * much faster in this demo... And why cva doesn't help them...
509 */
Gareth Hughesc8516462001-01-06 20:38:03 +0000510 glBegin( GL_POINTS );
Keith Whitwellabd51342001-06-04 15:34:31 +0000511 for ( i = 0 ; i < numuniq ; i++ ) {
512 glNormal3fv( &compressed_data[i][3] );
513 glVertex3fv( &compressed_data[i][0] );
Gareth Hughesc8516462001-01-06 20:38:03 +0000514 }
515 glEnd();
Keith Whitwell44c73931999-09-03 14:56:40 +0000516 break;
517
Keith Whitwell5759f532001-05-11 12:08:15 +0000518 case (GLVERTEX|STRIPS):
Keith Whitwella9ae89d2009-05-31 19:07:21 -0700519 if (with_state & MATERIALS) {
520 glBegin( GL_TRIANGLE_STRIP );
521 for (i=0;i<numverts;i++) {
522 if (i % 600 == 0 && i != 0) {
523 unsigned j = i / 600;
524 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
525 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
526 }
527 glNormal3fv( &data[i][3] );
528 glVertex3fv( &data[i][0] );
529 }
530 glEnd();
jtgafb833d1999-08-19 00:55:39 +0000531 }
Keith Whitwella9ae89d2009-05-31 19:07:21 -0700532 else {
533 glBegin( GL_TRIANGLE_STRIP );
534 for (i=0;i<numverts;i++) {
535 glNormal3fv( &data[i][3] );
536 glVertex3fv( &data[i][0] );
537 }
538 glEnd();
539 }
Keith Whitwell5759f532001-05-11 12:08:15 +0000540 break;
541
542 default:
543 fprintf(stderr, "unimplemented mode %x...\n",
544 (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK)));
545 break;
jtgafb833d1999-08-19 00:55:39 +0000546 }
547}
548
549
550
551static void Display(void)
552{
553 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
Keith Whitwell44c73931999-09-03 14:56:40 +0000554 draw_surface( state );
jtgafb833d1999-08-19 00:55:39 +0000555 glFlush();
Gareth Hughesc8516462001-01-06 20:38:03 +0000556 if (doubleBuffer) glutSwapBuffers();
jtgafb833d1999-08-19 00:55:39 +0000557}
558
Keith Whitwell18acf6e2001-04-19 13:12:40 +0000559
jtgafb833d1999-08-19 00:55:39 +0000560/* KW: only do this when necessary, so CVA can re-use results.
561 */
562static void set_matrix( void )
563{
564 glMatrixMode(GL_MODELVIEW);
565 glLoadIdentity();
Keith Whitwell44c73931999-09-03 14:56:40 +0000566 glTranslatef( 0.0, 0.0, dist );
jtgafb833d1999-08-19 00:55:39 +0000567 glRotatef( yrot, 0.0, 1.0, 0.0 );
568 glRotatef( xrot, 1.0, 0.0, 0.0 );
569}
570
571static void Benchmark( float xdiff, float ydiff )
572{
573 int startTime, endTime;
574 int draws;
575 double seconds, fps, triPerSecond;
576
577 printf("Benchmarking...\n");
578
579 draws = 0;
580 startTime = glutGet(GLUT_ELAPSED_TIME);
581 xrot = 0.0;
582 do {
583 xrot += xdiff;
584 yrot += ydiff;
585 set_matrix();
586 Display();
587 draws++;
588 endTime = glutGet(GLUT_ELAPSED_TIME);
589 } while (endTime - startTime < 5000); /* 5 seconds */
590
591 /* Results */
592 seconds = (double) (endTime - startTime) / 1000.0;
593 triPerSecond = (numverts - 2) * draws / seconds;
594 fps = draws / seconds;
595 printf("Result: triangles/sec: %g fps: %g\n", triPerSecond, fps);
596}
597
598
599static void InitMaterials(void)
600{
601 static float ambient[] = {0.1, 0.1, 0.1, 1.0};
602 static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
603 static float position0[] = {0.0, 0.0, 20.0, 0.0};
604 static float position1[] = {0.0, 0.0, -20.0, 0.0};
605 static float front_mat_shininess[] = {60.0};
606 static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
607 static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
608 /*
609 static float back_mat_shininess[] = {60.0};
610 static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
611 static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
612 */
613 static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
614 static float lmodel_twoside[] = {GL_FALSE};
615
616 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
617 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
618 glLightfv(GL_LIGHT0, GL_POSITION, position0);
619 glEnable(GL_LIGHT0);
Gareth Hughesc8516462001-01-06 20:38:03 +0000620
jtgafb833d1999-08-19 00:55:39 +0000621 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
622 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
623 glLightfv(GL_LIGHT1, GL_POSITION, position1);
624 glEnable(GL_LIGHT1);
Gareth Hughesc8516462001-01-06 20:38:03 +0000625
jtgafb833d1999-08-19 00:55:39 +0000626 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
627 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
628
629 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
630 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
631 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000632
633 glPolygonStipple (halftone);
jtgafb833d1999-08-19 00:55:39 +0000634}
635
636
637
638#define UPDATE(o,n,mask) (o&=~mask, o|=n&mask)
Keith Whitwell5759f532001-05-11 12:08:15 +0000639#define CHANGED(o,n,mask) ((n&mask) && (n&mask) != (o&mask) )
jtgafb833d1999-08-19 00:55:39 +0000640
641static void ModeMenu(int m)
642{
643 m &= allowed;
644
645 if (!m) return;
646
Gareth Hughesc8516462001-01-06 20:38:03 +0000647 if (m==QUIT)
jtgafb833d1999-08-19 00:55:39 +0000648 exit(0);
649
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000650 if (m==GLINFO) {
651 printf("GL_VERSION: %s\n", (char *) glGetString(GL_VERSION));
652 printf("GL_EXTENSIONS: %s\n", (char *) glGetString(GL_EXTENSIONS));
653 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
654 return;
655 }
656
jtgafb833d1999-08-19 00:55:39 +0000657 if (CHANGED(state, m, FILTER_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000658 UPDATE(state, m, FILTER_MASK);
jtgafb833d1999-08-19 00:55:39 +0000659 if (m & LINEAR_FILTER) {
660 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
661 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
662 } else {
663 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
664 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
665 }
666 }
667
668 if (CHANGED(state, m, LIGHT_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000669 UPDATE(state, m, LIGHT_MASK);
670 if (m & LIT) {
jtgafb833d1999-08-19 00:55:39 +0000671 glEnable(GL_LIGHTING);
Keith Whitwell5759f532001-05-11 12:08:15 +0000672 glDisable(GL_TEXTURE_GEN_S);
673 glDisable(GL_TEXTURE_GEN_T);
674 glDisable(GL_TEXTURE_2D);
675 }
676 else if (m & UNLIT) {
jtgafb833d1999-08-19 00:55:39 +0000677 glDisable(GL_LIGHTING);
Keith Whitwell5759f532001-05-11 12:08:15 +0000678 glDisable(GL_TEXTURE_GEN_S);
679 glDisable(GL_TEXTURE_GEN_T);
680 glDisable(GL_TEXTURE_2D);
681 }
682 else if (m & REFLECT) {
683 glDisable(GL_LIGHTING);
684 glEnable(GL_TEXTURE_GEN_S);
685 glEnable(GL_TEXTURE_GEN_T);
686 glEnable(GL_TEXTURE_2D);
687 }
jtgafb833d1999-08-19 00:55:39 +0000688 }
689
690 if (CHANGED(state, m, SHADE_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000691 UPDATE(state, m, SHADE_MASK);
jtgafb833d1999-08-19 00:55:39 +0000692 if (m & SHADE_SMOOTH)
693 glShadeModel(GL_SMOOTH);
694 else
695 glShadeModel(GL_FLAT);
696 }
697
698
jtgafb833d1999-08-19 00:55:39 +0000699 if (CHANGED(state, m, CLIP_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000700 UPDATE(state, m, CLIP_MASK);
jtgafb833d1999-08-19 00:55:39 +0000701 if (m & USER_CLIP) {
702 glEnable(GL_CLIP_PLANE0);
703 } else {
704 glDisable(GL_CLIP_PLANE0);
705 }
706 }
707
Keith Whitwell44c73931999-09-03 14:56:40 +0000708 if (CHANGED(state, m, FOG_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000709 UPDATE(state, m, FOG_MASK);
Gareth Hughes735d9202002-01-04 09:47:17 +0000710 if (m & FOG) {
Keith Whitwell44c73931999-09-03 14:56:40 +0000711 glEnable(GL_FOG);
Gareth Hughesc8516462001-01-06 20:38:03 +0000712 }
Gareth Hughes735d9202002-01-04 09:47:17 +0000713 else {
Keith Whitwell44c73931999-09-03 14:56:40 +0000714 glDisable(GL_FOG);
Keith Whitwell44c73931999-09-03 14:56:40 +0000715 }
716 }
717
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000718 if (CHANGED(state, m, STIPPLE_MASK)) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000719 UPDATE(state, m, STIPPLE_MASK);
Gareth Hughes735d9202002-01-04 09:47:17 +0000720 if (m & STIPPLE) {
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000721 glEnable(GL_POLYGON_STIPPLE);
Gareth Hughesc8516462001-01-06 20:38:03 +0000722 }
Gareth Hughes735d9202002-01-04 09:47:17 +0000723 else {
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000724 glDisable(GL_POLYGON_STIPPLE);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000725 }
726 }
727
Gareth Hughes735d9202002-01-04 09:47:17 +0000728 if (CHANGED(state, m, POLYGON_MASK)) {
729 UPDATE(state, m, POLYGON_MASK);
730 if (m & POLYGON_FILL) {
731 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
732 }
Jakob Bornecrantz54e20822009-02-13 17:53:49 +0100733 else if (m & POLYGON_LINE) {
Gareth Hughes735d9202002-01-04 09:47:17 +0000734 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
735 }
Jakob Bornecrantz54e20822009-02-13 17:53:49 +0100736 else {
737 glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
738 }
Gareth Hughes735d9202002-01-04 09:47:17 +0000739 }
740
jtgafb833d1999-08-19 00:55:39 +0000741#ifdef GL_EXT_vertex_array
Keith Whitwell5759f532001-05-11 12:08:15 +0000742 if (CHANGED(state, m, (LOCK_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK)))
jtgafb833d1999-08-19 00:55:39 +0000743 {
Keith Whitwell5759f532001-05-11 12:08:15 +0000744 if (m & (PRIMITIVE_MASK)) {
745 UPDATE(state, m, (PRIMITIVE_MASK));
746 }
747
748 if (m & (RENDER_STYLE_MASK)) {
749 UPDATE(state, m, (RENDER_STYLE_MASK));
750 }
751
752 if (m & LOCK_MASK) {
753 UPDATE(state, m, (LOCK_MASK));
754 }
755
756
757 print_flags("primitive", state & PRIMITIVE_MASK);
758 print_flags("render style", state & RENDER_STYLE_MASK);
759
Keith Whitwellabd51342001-06-04 15:34:31 +0000760 if ((state & PRIMITIVE_MASK) != STRIPS &&
761 ((state & RENDER_STYLE_MASK) == DRAW_ELTS ||
762 (state & RENDER_STYLE_MASK) == ARRAY_ELT ||
763 (state & PRIMITIVE_MASK) == POINTS))
jtgafb833d1999-08-19 00:55:39 +0000764 {
Keith Whitwell5759f532001-05-11 12:08:15 +0000765 fprintf(stderr, "enabling small arrays\n");
Keith Whitwellabd51342001-06-04 15:34:31 +0000766 /* Rendering any primitive with draw-element/array-element
767 * --> Can't do strips here as ordering has been lost in
768 * compaction process...
769 */
Gareth Hughesc8516462001-01-06 20:38:03 +0000770 glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numuniq,
jtgafb833d1999-08-19 00:55:39 +0000771 compressed_data );
Gareth Hughesc8516462001-01-06 20:38:03 +0000772 glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numuniq,
jtgafb833d1999-08-19 00:55:39 +0000773 &compressed_data[0][3]);
jtgafb833d1999-08-19 00:55:39 +0000774#ifdef GL_EXT_compiled_vertex_array
Keith Whitwell5759f532001-05-11 12:08:15 +0000775 if (allowed & LOCKED) {
776 if (state & LOCKED) {
777 glLockArraysEXT( 0, numuniq );
778 } else {
779 glUnlockArraysEXT();
780 }
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000781 }
jtgafb833d1999-08-19 00:55:39 +0000782#endif
Keith Whitwell5759f532001-05-11 12:08:15 +0000783 }
Keith Whitwellabd51342001-06-04 15:34:31 +0000784 else if ((state & PRIMITIVE_MASK) == TRIANGLES &&
785 (state & RENDER_STYLE_MASK) == DRAW_ARRAYS) {
Keith Whitwell5759f532001-05-11 12:08:15 +0000786 fprintf(stderr, "enabling big arrays\n");
Keith Whitwellabd51342001-06-04 15:34:31 +0000787 /* Only get here for TRIANGLES and drawarrays
Keith Whitwell5759f532001-05-11 12:08:15 +0000788 */
789 glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
790 expanded_data );
791 glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
792 &expanded_data[0][3]);
793
794#ifdef GL_EXT_compiled_vertex_array
795 if (allowed & LOCKED) {
796 if (state & LOCKED) {
797 glLockArraysEXT( 0, (numverts-2)*3 );
798 } else {
799 glUnlockArraysEXT();
800 }
801 }
802#endif
803 }
Keith Whitwellabd51342001-06-04 15:34:31 +0000804 else {
805 fprintf(stderr, "enabling normal arrays\n");
806 glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numverts, data );
807 glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numverts, &data[0][3]);
808#ifdef GL_EXT_compiled_vertex_array
809 if (allowed & LOCKED) {
810 if (state & LOCKED) {
811 glLockArraysEXT( 0, numverts );
812 } else {
813 glUnlockArraysEXT();
814 }
815 }
816#endif
817 }
Keith Whitwell5759f532001-05-11 12:08:15 +0000818
jtgafb833d1999-08-19 00:55:39 +0000819 }
820#endif
821
Keith Whitwell5759f532001-05-11 12:08:15 +0000822
823 if (m & DLIST_MASK) {
824 UPDATE(state, m, DLIST_MASK);
jtgafb833d1999-08-19 00:55:39 +0000825 }
Gareth Hughesc8516462001-01-06 20:38:03 +0000826
jtgafb833d1999-08-19 00:55:39 +0000827 if (m & MATERIAL_MASK) {
828 UPDATE(state, m, MATERIAL_MASK);
829 }
830
Keith Whitwell5759f532001-05-11 12:08:15 +0000831 print_flags("new flags", state);
832
jtgafb833d1999-08-19 00:55:39 +0000833 glutPostRedisplay();
834}
835
836
837
Brian Paulac126091999-10-21 16:39:06 +0000838static void Init(int argc, char *argv[])
jtgafb833d1999-08-19 00:55:39 +0000839{
Keith Whitwell44c73931999-09-03 14:56:40 +0000840 GLfloat fogColor[4] = {0.5,1.0,0.5,1.0};
841
Keith Whitwell5759f532001-05-11 12:08:15 +0000842 xrot = 0;
843 yrot = 0;
844 dist = -6;
845 plane[0] = 1.0;
846 plane[1] = 0.0;
847 plane[2] = -1.0;
848 plane[3] = 0.0;
849
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000850 glClearColor(0.0, 0.0, 1.0, 0.0);
jtgafb833d1999-08-19 00:55:39 +0000851 glEnable( GL_DEPTH_TEST );
Brian Paul820436f2009-07-08 13:58:30 -0600852 glEnableClientState( GL_VERTEX_ARRAY );
853 glEnableClientState( GL_NORMAL_ARRAY );
jtgafb833d1999-08-19 00:55:39 +0000854
jtgafb833d1999-08-19 00:55:39 +0000855 glMatrixMode(GL_PROJECTION);
856 glLoadIdentity();
857 glFrustum( -1.0, 1.0, -1.0, 1.0, 5, 25 );
858
859 glMatrixMode(GL_MODELVIEW);
860 glLoadIdentity();
Gareth Hughesc8516462001-01-06 20:38:03 +0000861 glClipPlane(GL_CLIP_PLANE0, plane);
jtgafb833d1999-08-19 00:55:39 +0000862
Keith Whitwellb8f99802001-05-11 15:47:02 +0000863 InitMaterials();
864
jtgafb833d1999-08-19 00:55:39 +0000865 set_matrix();
866
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000867 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
jtgafb833d1999-08-19 00:55:39 +0000868 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
869
870 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
871 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
872
jtgafb833d1999-08-19 00:55:39 +0000873
Keith Whitwell44c73931999-09-03 14:56:40 +0000874 /* Green fog is easy to see */
875 glFogi(GL_FOG_MODE,GL_EXP2);
876 glFogfv(GL_FOG_COLOR,fogColor);
877 glFogf(GL_FOG_DENSITY,0.15);
878 glHint(GL_FOG_HINT,GL_DONT_CARE);
879
Keith Whitwell5759f532001-05-11 12:08:15 +0000880 {
881 static int firsttime = 1;
882 if (firsttime) {
883 firsttime = 0;
884 compactify_arrays();
885 expand_arrays();
886 make_tri_indices();
Keith Whitwell44c73931999-09-03 14:56:40 +0000887
Keith Whitwell5759f532001-05-11 12:08:15 +0000888 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
889 printf("Error: couldn't load texture image\n");
890 exit(1);
891 }
892 }
893 }
jtgafb833d1999-08-19 00:55:39 +0000894
895 ModeMenu(SHADE_SMOOTH|
896 LIT|
jtgafb833d1999-08-19 00:55:39 +0000897 POINT_FILTER|
jtgafb833d1999-08-19 00:55:39 +0000898 NO_USER_CLIP|
899 NO_MATERIALS|
Keith Whitwell44c73931999-09-03 14:56:40 +0000900 NO_FOG|
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000901 NO_STIPPLE|
Keith Whitwell5759f532001-05-11 12:08:15 +0000902 IMMEDIATE|
903 STRIPS|
904 UNLOCKED|
Keith Whitwell44c73931999-09-03 14:56:40 +0000905 GLVERTEX);
Brian Paulac126091999-10-21 16:39:06 +0000906
907 if (PrintInfo) {
908 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
909 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
910 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
911 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
912 }
jtgafb833d1999-08-19 00:55:39 +0000913}
914
915
916
917static void Reshape(int width, int height)
918{
919 glViewport(0, 0, (GLint)width, (GLint)height);
920}
921
922
923
924static void Key( unsigned char key, int x, int y )
925{
Brian Paul36ca6bd1999-09-08 22:14:31 +0000926 (void) x;
927 (void) y;
jtgafb833d1999-08-19 00:55:39 +0000928 switch (key) {
Keith Whitwell44c73931999-09-03 14:56:40 +0000929 case 27:
930 exit(0);
931 case 'f':
932 ModeMenu((state ^ FOG_MASK) & FOG_MASK);
933 break;
934 case 's':
935 ModeMenu((state ^ SHADE_MASK) & SHADE_MASK);
936 break;
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000937 case 't':
938 ModeMenu((state ^ STIPPLE_MASK) & STIPPLE_MASK);
939 break;
Keith Whitwell44c73931999-09-03 14:56:40 +0000940 case 'l':
Keith Whitwell5759f532001-05-11 12:08:15 +0000941 ModeMenu((state ^ LIGHT_MASK) & (LIT|UNLIT));
Keith Whitwell44c73931999-09-03 14:56:40 +0000942 break;
943 case 'm':
944 ModeMenu((state ^ MATERIAL_MASK) & MATERIAL_MASK);
945 break;
946 case 'c':
947 ModeMenu((state ^ CLIP_MASK) & CLIP_MASK);
948 break;
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000949 case 'v':
Keith Whitwell5759f532001-05-11 12:08:15 +0000950 ModeMenu((LOCKED|IMMEDIATE|DRAW_ELTS|TRIANGLES) & allowed);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000951 break;
952 case 'V':
Keith Whitwell5759f532001-05-11 12:08:15 +0000953 ModeMenu(UNLOCKED|IMMEDIATE|GLVERTEX|STRIPS);
Keith Whitwell03b7aee2000-03-30 17:58:56 +0000954 break;
Keith Whitwell44c73931999-09-03 14:56:40 +0000955 case 'b':
956 Benchmark(5.0, 0);
957 break;
958 case 'B':
959 Benchmark(0, 5.0);
960 break;
961 case 'i':
962 dist += .25;
963 set_matrix();
964 glutPostRedisplay();
965 break;
966 case 'I':
967 dist -= .25;
968 set_matrix();
969 glutPostRedisplay();
970 break;
971 case '-':
972 case '_':
973 plane[3] += 2.0;
974 glMatrixMode(GL_MODELVIEW);
975 glLoadIdentity();
976 glClipPlane(GL_CLIP_PLANE0, plane);
977 set_matrix();
978 glutPostRedisplay();
979 break;
980 case '+':
981 case '=':
982 plane[3] -= 2.0;
983 glMatrixMode(GL_MODELVIEW);
984 glLoadIdentity();
985 glClipPlane(GL_CLIP_PLANE0, plane);
986 set_matrix();
987 glutPostRedisplay();
988 break;
Keith Whitwell5759f532001-05-11 12:08:15 +0000989 case ' ':
990 Init(0,0);
991 break;
jtgafb833d1999-08-19 00:55:39 +0000992 }
993}
994
995
996static void SpecialKey( int key, int x, int y )
997{
Brian Paul36ca6bd1999-09-08 22:14:31 +0000998 (void) x;
999 (void) y;
jtgafb833d1999-08-19 00:55:39 +00001000 switch (key) {
1001 case GLUT_KEY_LEFT:
1002 yrot -= 15.0;
1003 break;
1004 case GLUT_KEY_RIGHT:
1005 yrot += 15.0;
1006 break;
1007 case GLUT_KEY_UP:
1008 xrot += 15.0;
1009 break;
1010 case GLUT_KEY_DOWN:
1011 xrot -= 15.0;
1012 break;
1013 default:
1014 return;
1015 }
1016 set_matrix();
1017 glutPostRedisplay();
1018}
1019
1020
1021
1022static GLint Args(int argc, char **argv)
1023{
1024 GLint i;
1025 GLint mode = 0;
1026
1027 for (i = 1; i < argc; i++) {
1028 if (strcmp(argv[i], "-sb") == 0) {
1029 doubleBuffer = GL_FALSE;
1030 }
1031 else if (strcmp(argv[i], "-db") == 0) {
1032 doubleBuffer = GL_TRUE;
1033 }
Brian Paulac126091999-10-21 16:39:06 +00001034 else if (strcmp(argv[i], "-info") == 0) {
1035 PrintInfo = GL_TRUE;
1036 }
Keith Whitwell18acf6e2001-04-19 13:12:40 +00001037 else if (strcmp(argv[i], "-10") == 0) {
1038 maxverts = 10;
1039 }
1040 else if (strcmp(argv[i], "-100") == 0) {
1041 maxverts = 100;
1042 }
1043 else if (strcmp(argv[i], "-1000") == 0) {
1044 maxverts = 1000;
1045 }
jtgafb833d1999-08-19 00:55:39 +00001046 else {
1047 printf("%s (Bad option).\n", argv[i]);
1048 return QUIT;
1049 }
1050 }
1051
1052 return mode;
1053}
1054
1055int main(int argc, char **argv)
1056{
1057 GLenum type;
jtgafb833d1999-08-19 00:55:39 +00001058
1059 GLuint arg_mode = Args(argc, argv);
1060
1061 if (arg_mode & QUIT)
1062 exit(0);
1063
1064 read_surface( "isosurf.dat" );
1065
jtgafb833d1999-08-19 00:55:39 +00001066 glutInitWindowSize(400, 400);
Brian Paul263f4322009-12-18 08:12:55 -07001067 glutInit( &argc, argv);
Gareth Hughesc8516462001-01-06 20:38:03 +00001068
jtgafb833d1999-08-19 00:55:39 +00001069 type = GLUT_DEPTH;
1070 type |= GLUT_RGB;
1071 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
1072 glutInitDisplayMode(type);
1073
1074 if (glutCreateWindow("Isosurface") <= 0) {
1075 exit(0);
1076 }
1077
José Fonsecaefdb7792009-01-24 16:47:50 +00001078 glewInit();
jtgafb833d1999-08-19 00:55:39 +00001079
José Fonsecaefdb7792009-01-24 16:47:50 +00001080 /* Make sure server supports the vertex array extension */
1081 if (!GLEW_EXT_vertex_array)
jtgafb833d1999-08-19 00:55:39 +00001082 {
1083 printf("Vertex arrays not supported by this renderer\n");
Keith Whitwell5759f532001-05-11 12:08:15 +00001084 allowed &= ~(LOCKED|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT);
jtgafb833d1999-08-19 00:55:39 +00001085 }
José Fonsecaefdb7792009-01-24 16:47:50 +00001086 else if (!GLEW_EXT_compiled_vertex_array)
jtgafb833d1999-08-19 00:55:39 +00001087 {
1088 printf("Compiled vertex arrays not supported by this renderer\n");
Keith Whitwell5759f532001-05-11 12:08:15 +00001089 allowed &= ~LOCKED;
jtgafb833d1999-08-19 00:55:39 +00001090 }
1091
Brian Paulac126091999-10-21 16:39:06 +00001092 Init(argc, argv);
jtgafb833d1999-08-19 00:55:39 +00001093 ModeMenu(arg_mode);
Gareth Hughesc8516462001-01-06 20:38:03 +00001094
jtgafb833d1999-08-19 00:55:39 +00001095 glutCreateMenu(ModeMenu);
Gareth Hughesc8516462001-01-06 20:38:03 +00001096 glutAddMenuEntry("GL info", GLINFO);
1097 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001098 glutAddMenuEntry("Lit", LIT);
1099 glutAddMenuEntry("Unlit", UNLIT);
1100 glutAddMenuEntry("Reflect", REFLECT);
Gareth Hughesc8516462001-01-06 20:38:03 +00001101 glutAddMenuEntry("", 0);
jtgafb833d1999-08-19 00:55:39 +00001102 glutAddMenuEntry("Smooth", SHADE_SMOOTH);
1103 glutAddMenuEntry("Flat", SHADE_FLAT);
Gareth Hughesc8516462001-01-06 20:38:03 +00001104 glutAddMenuEntry("", 0);
Keith Whitwell44c73931999-09-03 14:56:40 +00001105 glutAddMenuEntry("Fog", FOG);
1106 glutAddMenuEntry("No Fog", NO_FOG);
Gareth Hughesc8516462001-01-06 20:38:03 +00001107 glutAddMenuEntry("", 0);
Keith Whitwell03b7aee2000-03-30 17:58:56 +00001108 glutAddMenuEntry("Stipple", STIPPLE);
1109 glutAddMenuEntry("No Stipple", NO_STIPPLE);
Gareth Hughesc8516462001-01-06 20:38:03 +00001110 glutAddMenuEntry("", 0);
Gareth Hughes735d9202002-01-04 09:47:17 +00001111 glutAddMenuEntry("Polygon Mode Fill", POLYGON_FILL);
1112 glutAddMenuEntry("Polygon Mode Line", POLYGON_LINE);
Jakob Bornecrantz54e20822009-02-13 17:53:49 +01001113 glutAddMenuEntry("Polygon Mode Points", POLYGON_POINT);
Gareth Hughes735d9202002-01-04 09:47:17 +00001114 glutAddMenuEntry("", 0);
jtgafb833d1999-08-19 00:55:39 +00001115 glutAddMenuEntry("Point Filtered", POINT_FILTER);
1116 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
Gareth Hughesc8516462001-01-06 20:38:03 +00001117 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001118 glutAddMenuEntry("GL_TRIANGLES", TRIANGLES);
1119 glutAddMenuEntry("GL_TRIANGLE_STRIPS", STRIPS);
1120 glutAddMenuEntry("GL_POINTS", POINTS);
Gareth Hughesc8516462001-01-06 20:38:03 +00001121 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001122 glutAddMenuEntry("Displaylist", DISPLAYLIST);
1123 glutAddMenuEntry("Immediate", IMMEDIATE);
Gareth Hughesc8516462001-01-06 20:38:03 +00001124 glutAddMenuEntry("", 0);
Keith Whitwell5759f532001-05-11 12:08:15 +00001125 if (allowed & LOCKED) {
1126 glutAddMenuEntry("Locked Arrays (CVA)", LOCKED);
1127 glutAddMenuEntry("Unlocked Arrays", UNLOCKED);
1128 glutAddMenuEntry("", 0);
1129 }
1130 glutAddMenuEntry("glVertex", GLVERTEX);
jtgafb833d1999-08-19 00:55:39 +00001131 if (allowed & DRAW_ARRAYS) {
Keith Whitwell5759f532001-05-11 12:08:15 +00001132 glutAddMenuEntry("glDrawElements", DRAW_ELTS);
1133 glutAddMenuEntry("glDrawArrays", DRAW_ARRAYS);
1134 glutAddMenuEntry("glArrayElement", ARRAY_ELT);
jtgafb833d1999-08-19 00:55:39 +00001135 }
Keith Whitwell5759f532001-05-11 12:08:15 +00001136 glutAddMenuEntry("", 0);
1137 glutAddMenuEntry("Quit", QUIT);
jtgafb833d1999-08-19 00:55:39 +00001138 glutAttachMenu(GLUT_RIGHT_BUTTON);
1139
1140 glutReshapeFunc(Reshape);
1141 glutKeyboardFunc(Key);
1142 glutSpecialFunc(SpecialKey);
1143 glutDisplayFunc(Display);
Keith Whitwell03b7aee2000-03-30 17:58:56 +00001144
jtgafb833d1999-08-19 00:55:39 +00001145 glutMainLoop();
1146 return 0;
1147}