jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 3 | * Display an isosurface of 3-D wind speed volume. |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 4 | * |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 5 | * Command line options: |
| 6 | * -info print GL implementation information |
| 7 | * |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 8 | * Brian Paul This file in public domain. |
| 9 | */ |
| 10 | |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 11 | |
| 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 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <stdlib.h> |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 30 | #include <string.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 31 | #include <math.h> |
Karl Schultz | bffae58 | 2001-10-04 19:14:26 +0000 | [diff] [blame] | 32 | #ifdef _WIN32 |
| 33 | #include <windows.h> |
Karl Schultz | 53d30c5 | 2002-10-18 17:47:35 +0000 | [diff] [blame] | 34 | #undef CLIP_MASK |
Karl Schultz | bffae58 | 2001-10-04 19:14:26 +0000 | [diff] [blame] | 35 | #endif |
José Fonseca | 2e61d13 | 2009-01-24 16:39:49 +0000 | [diff] [blame] | 36 | #include <GL/glew.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 37 | #include "GL/glut.h" |
| 38 | |
Brian Paul | 575d24a | 2005-01-09 17:15:41 +0000 | [diff] [blame] | 39 | #include "readtex.h" |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 40 | #define TEXTURE_FILE "../images/reflect.rgb" |
| 41 | |
Gareth Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 42 | #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 Bornecrantz | 54e2082 | 2009-02-13 17:53:49 +0100 | [diff] [blame] | 72 | #define POLYGON_POINT 0x40000000 |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 73 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 74 | #define LIGHT_MASK (LIT|UNLIT|REFLECT) |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 75 | #define FILTER_MASK (POINT_FILTER|LINEAR_FILTER) |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 76 | #define RENDER_STYLE_MASK (GLVERTEX|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT) |
| 77 | #define DLIST_MASK (IMMEDIATE|DISPLAYLIST) |
| 78 | #define LOCK_MASK (LOCKED|UNLOCKED) |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 79 | #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 Bornecrantz | 54e2082 | 2009-02-13 17:53:49 +0100 | [diff] [blame] | 85 | #define POLYGON_MASK (POLYGON_FILL|POLYGON_LINE|POLYGON_POINT) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 86 | |
| 87 | #define MAXVERTS 10000 |
Karl Schultz | 53d30c5 | 2002-10-18 17:47:35 +0000 | [diff] [blame] | 88 | static GLint maxverts = MAXVERTS; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 89 | static float data[MAXVERTS][6]; |
| 90 | static float compressed_data[MAXVERTS][6]; |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 91 | static float expanded_data[MAXVERTS*3][6]; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 92 | static GLuint indices[MAXVERTS]; |
| 93 | static GLuint tri_indices[MAXVERTS*3]; |
Keith Whitwell | b8f9980 | 2001-05-11 15:47:02 +0000 | [diff] [blame] | 94 | static GLuint strip_indices[MAXVERTS]; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 95 | static GLfloat col[100][4]; |
| 96 | static GLint numverts, num_tri_verts, numuniq; |
| 97 | |
| 98 | static GLfloat xrot; |
| 99 | static GLfloat yrot; |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 100 | static GLfloat dist; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 101 | static GLint state, allowed = ~0; |
| 102 | static GLboolean doubleBuffer = GL_TRUE; |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 103 | static GLdouble plane[4]; |
| 104 | static GLuint surf1, dlist_state; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 105 | |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 106 | static GLboolean PrintInfo = GL_FALSE; |
| 107 | |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 108 | |
| 109 | static 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 Paul | 36ca6bd | 1999-09-08 22:14:31 +0000 | [diff] [blame] | 122 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 123 | static 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 Whitwell | 18acf6e | 2001-04-19 13:12:40 +0000 | [diff] [blame] | 134 | while (!feof(f) && numverts<maxverts) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 135 | 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 Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 148 | static void print_flags( const char *msg, GLuint flags ) |
| 149 | { |
| 150 | fprintf(stderr, |
Jakob Bornecrantz | 54e2082 | 2009-02-13 17:53:49 +0100 | [diff] [blame] | 151 | "%s (0x%x): %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 152 | 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 Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 169 | (flags & STIPPLE) ? "stipple, " : "", |
Jakob Bornecrantz | 54e2082 | 2009-02-13 17:53:49 +0100 | [diff] [blame] | 170 | (flags & POLYGON_LINE) ? "polygon mode line, " : "", |
| 171 | (flags & POLYGON_POINT) ? "polygon mode point, " : ""); |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 175 | |
| 176 | struct data_idx { |
| 177 | float *data; |
| 178 | int idx; |
| 179 | int uniq_idx; |
| 180 | }; |
| 181 | |
| 182 | |
| 183 | #define COMPARE_FUNC( AXIS ) \ |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 184 | static int compare_axis_##AXIS( const void *a, const void *b ) \ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 185 | { \ |
| 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 | |
| 194 | COMPARE_FUNC(0) |
| 195 | COMPARE_FUNC(1) |
| 196 | COMPARE_FUNC(2) |
| 197 | COMPARE_FUNC(3) |
| 198 | COMPARE_FUNC(4) |
| 199 | COMPARE_FUNC(5) |
| 200 | COMPARE_FUNC(6) |
| 201 | |
| 202 | int (*(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 Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 216 | static int sort_axis( int axis, |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 217 | 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 Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 228 | if (finish-start > 2) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 229 | { |
| 230 | qsort( indices+start, finish-start, sizeof(*indices), compare[axis] ); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 231 | } |
| 232 | else if (indices[start].data[axis] > indices[start+1].data[axis]) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 233 | { |
| 234 | struct data_idx tmp = indices[start]; |
| 235 | indices[start] = indices[start+1]; |
| 236 | indices[start+1] = tmp; |
| 237 | } |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 238 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 239 | 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 Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 244 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 245 | for (j = 0 ; j < vec_size ; j++) |
| 246 | dest[j] = indices[i].data[j]; |
| 247 | |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 248 | for ( ; i < finish && max >= indices[i].data[axis]; i++) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 249 | 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 Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 263 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 264 | 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 Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 280 | static void extract_indices1( const struct data_idx *in, unsigned int *out, |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 281 | 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 Paul | 36ca6bd | 1999-09-08 22:14:31 +0000 | [diff] [blame] | 290 | static void compactify_arrays(void) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 291 | { |
| 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 Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 302 | numuniq = sort_axis(0, |
| 303 | sizeof(compressed_data[0])/sizeof(float), |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 304 | sizeof(compressed_data[0]), |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 305 | ind, |
| 306 | 0, |
| 307 | numverts, |
| 308 | (float *)compressed_data, |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 309 | 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 Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 318 | static 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 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 332 | static float myrand( float max ) |
| 333 | { |
| 334 | return max*rand()/(RAND_MAX+1.0); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | static void make_tri_indices( void ) |
| 339 | { |
| 340 | unsigned int *v = tri_indices; |
| 341 | unsigned int parity = 0; |
Karl Schultz | 53d30c5 | 2002-10-18 17:47:35 +0000 | [diff] [blame] | 342 | int i, j; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 343 | |
| 344 | for (j=2;j<numverts;j++,parity^=1) { |
| 345 | if (parity) { |
| 346 | *v++ = indices[j-1]; |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 347 | *v++ = indices[j-2]; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 348 | *v++ = indices[j]; |
| 349 | } else { |
| 350 | *v++ = indices[j-2]; |
| 351 | *v++ = indices[j-1]; |
| 352 | *v++ = indices[j]; |
| 353 | } |
| 354 | } |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 355 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 356 | 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 Whitwell | b8f9980 | 2001-05-11 15:47:02 +0000 | [diff] [blame] | 365 | |
| 366 | for (i = 0; i < numverts ; i++) |
| 367 | strip_indices[i] = i; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | #define MIN(x,y) (x < y) ? x : y |
| 371 | |
Karl Schultz | 53d30c5 | 2002-10-18 17:47:35 +0000 | [diff] [blame] | 372 | static void draw_surface( unsigned int with_state ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 373 | { |
Karl Schultz | 53d30c5 | 2002-10-18 17:47:35 +0000 | [diff] [blame] | 374 | GLint i, j; |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 375 | |
| 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"); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 382 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 383 | 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)) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 399 | #ifdef GL_EXT_vertex_array |
| 400 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 401 | case (DRAW_ELTS|TRIANGLES): |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 402 | if (with_state & MATERIALS) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 403 | 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 Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 410 | glDrawElements( GL_TRIANGLES, num_tri_verts, GL_UNSIGNED_INT, |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 411 | tri_indices ); |
| 412 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 413 | break; |
| 414 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 415 | case (DRAW_ARRAYS|TRIANGLES): |
| 416 | glDrawArraysEXT( GL_TRIANGLES, 0, (numverts-2)*3 ); |
| 417 | break; |
| 418 | |
| 419 | case (ARRAY_ELT|TRIANGLES): |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 420 | if (with_state & MATERIALS) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 421 | 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 Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 435 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 436 | glEnd(); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 437 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 438 | break; |
| 439 | |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 440 | |
| 441 | /* Uses the original arrays (including duplicate elements): |
| 442 | */ |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 443 | case (DRAW_ARRAYS|STRIPS): |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 444 | glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts ); |
| 445 | break; |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 446 | case (DRAW_ELTS|STRIPS): |
Keith Whitwell | b8f9980 | 2001-05-11 15:47:02 +0000 | [diff] [blame] | 447 | glDrawElements( GL_TRIANGLE_STRIP, numverts, |
| 448 | GL_UNSIGNED_INT, strip_indices ); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 449 | break; |
| 450 | |
| 451 | /* Uses the original arrays (including duplicate elements): |
| 452 | */ |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 453 | case (ARRAY_ELT|STRIPS): |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 454 | glBegin( GL_TRIANGLE_STRIP ); |
| 455 | for (i = 0 ; i < numverts ; i++) |
| 456 | glArrayElement( i ); |
| 457 | glEnd(); |
| 458 | break; |
| 459 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 460 | case (DRAW_ARRAYS|POINTS): |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 461 | glDrawArraysEXT( GL_POINTS, 0, numuniq ); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 462 | break; |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 463 | case (DRAW_ELTS|POINTS): |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 464 | /* can use numuniq with strip_indices as strip_indices[i] == i. |
| 465 | */ |
| 466 | glDrawElements( GL_POINTS, numuniq, |
| 467 | GL_UNSIGNED_INT, strip_indices ); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 468 | break; |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 469 | case (ARRAY_ELT|POINTS): |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 470 | /* just emit each unique element once: |
| 471 | */ |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 472 | glBegin( GL_POINTS ); |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 473 | for (i = 0 ; i < numuniq ; i++) |
| 474 | glArrayElement( i ); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 475 | glEnd(); |
| 476 | break; |
| 477 | #endif |
| 478 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 479 | case (GLVERTEX|TRIANGLES): |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 480 | if (with_state & MATERIALS) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 481 | 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 Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 500 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 501 | break; |
| 502 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 503 | case (GLVERTEX|POINTS): |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 504 | /* 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 Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 508 | glBegin( GL_POINTS ); |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 509 | for ( i = 0 ; i < numuniq ; i++ ) { |
| 510 | glNormal3fv( &compressed_data[i][3] ); |
| 511 | glVertex3fv( &compressed_data[i][0] ); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 512 | } |
| 513 | glEnd(); |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 514 | break; |
| 515 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 516 | case (GLVERTEX|STRIPS): |
Keith Whitwell | a9ae89d | 2009-05-31 19:07:21 -0700 | [diff] [blame] | 517 | if (with_state & MATERIALS) { |
| 518 | glBegin( GL_TRIANGLE_STRIP ); |
| 519 | for (i=0;i<numverts;i++) { |
| 520 | if (i % 600 == 0 && i != 0) { |
| 521 | unsigned j = i / 600; |
| 522 | glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]); |
| 523 | glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]); |
| 524 | } |
| 525 | glNormal3fv( &data[i][3] ); |
| 526 | glVertex3fv( &data[i][0] ); |
| 527 | } |
| 528 | glEnd(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 529 | } |
Keith Whitwell | a9ae89d | 2009-05-31 19:07:21 -0700 | [diff] [blame] | 530 | else { |
| 531 | glBegin( GL_TRIANGLE_STRIP ); |
| 532 | for (i=0;i<numverts;i++) { |
| 533 | glNormal3fv( &data[i][3] ); |
| 534 | glVertex3fv( &data[i][0] ); |
| 535 | } |
| 536 | glEnd(); |
| 537 | } |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 538 | break; |
| 539 | |
| 540 | default: |
| 541 | fprintf(stderr, "unimplemented mode %x...\n", |
| 542 | (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK))); |
| 543 | break; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | |
| 548 | |
| 549 | static void Display(void) |
| 550 | { |
| 551 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 552 | draw_surface( state ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 553 | glFlush(); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 554 | if (doubleBuffer) glutSwapBuffers(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Keith Whitwell | 18acf6e | 2001-04-19 13:12:40 +0000 | [diff] [blame] | 557 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 558 | /* KW: only do this when necessary, so CVA can re-use results. |
| 559 | */ |
| 560 | static void set_matrix( void ) |
| 561 | { |
| 562 | glMatrixMode(GL_MODELVIEW); |
| 563 | glLoadIdentity(); |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 564 | glTranslatef( 0.0, 0.0, dist ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 565 | glRotatef( yrot, 0.0, 1.0, 0.0 ); |
| 566 | glRotatef( xrot, 1.0, 0.0, 0.0 ); |
| 567 | } |
| 568 | |
| 569 | static void Benchmark( float xdiff, float ydiff ) |
| 570 | { |
| 571 | int startTime, endTime; |
| 572 | int draws; |
| 573 | double seconds, fps, triPerSecond; |
| 574 | |
| 575 | printf("Benchmarking...\n"); |
| 576 | |
| 577 | draws = 0; |
| 578 | startTime = glutGet(GLUT_ELAPSED_TIME); |
| 579 | xrot = 0.0; |
| 580 | do { |
| 581 | xrot += xdiff; |
| 582 | yrot += ydiff; |
| 583 | set_matrix(); |
| 584 | Display(); |
| 585 | draws++; |
| 586 | endTime = glutGet(GLUT_ELAPSED_TIME); |
| 587 | } while (endTime - startTime < 5000); /* 5 seconds */ |
| 588 | |
| 589 | /* Results */ |
| 590 | seconds = (double) (endTime - startTime) / 1000.0; |
| 591 | triPerSecond = (numverts - 2) * draws / seconds; |
| 592 | fps = draws / seconds; |
| 593 | printf("Result: triangles/sec: %g fps: %g\n", triPerSecond, fps); |
| 594 | } |
| 595 | |
| 596 | |
| 597 | static void InitMaterials(void) |
| 598 | { |
| 599 | static float ambient[] = {0.1, 0.1, 0.1, 1.0}; |
| 600 | static float diffuse[] = {0.5, 1.0, 1.0, 1.0}; |
| 601 | static float position0[] = {0.0, 0.0, 20.0, 0.0}; |
| 602 | static float position1[] = {0.0, 0.0, -20.0, 0.0}; |
| 603 | static float front_mat_shininess[] = {60.0}; |
| 604 | static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0}; |
| 605 | static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0}; |
| 606 | /* |
| 607 | static float back_mat_shininess[] = {60.0}; |
| 608 | static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0}; |
| 609 | static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0}; |
| 610 | */ |
| 611 | static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0}; |
| 612 | static float lmodel_twoside[] = {GL_FALSE}; |
| 613 | |
| 614 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); |
| 615 | glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); |
| 616 | glLightfv(GL_LIGHT0, GL_POSITION, position0); |
| 617 | glEnable(GL_LIGHT0); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 618 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 619 | glLightfv(GL_LIGHT1, GL_AMBIENT, ambient); |
| 620 | glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse); |
| 621 | glLightfv(GL_LIGHT1, GL_POSITION, position1); |
| 622 | glEnable(GL_LIGHT1); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 623 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 624 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); |
| 625 | glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside); |
| 626 | |
| 627 | glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess); |
| 628 | glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular); |
| 629 | glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse); |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 630 | |
| 631 | glPolygonStipple (halftone); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | |
| 635 | |
| 636 | #define UPDATE(o,n,mask) (o&=~mask, o|=n&mask) |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 637 | #define CHANGED(o,n,mask) ((n&mask) && (n&mask) != (o&mask) ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 638 | |
| 639 | static void ModeMenu(int m) |
| 640 | { |
| 641 | m &= allowed; |
| 642 | |
| 643 | if (!m) return; |
| 644 | |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 645 | if (m==QUIT) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 646 | exit(0); |
| 647 | |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 648 | if (m==GLINFO) { |
| 649 | printf("GL_VERSION: %s\n", (char *) glGetString(GL_VERSION)); |
| 650 | printf("GL_EXTENSIONS: %s\n", (char *) glGetString(GL_EXTENSIONS)); |
| 651 | printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER)); |
| 652 | return; |
| 653 | } |
| 654 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 655 | if (CHANGED(state, m, FILTER_MASK)) { |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 656 | UPDATE(state, m, FILTER_MASK); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 657 | if (m & LINEAR_FILTER) { |
| 658 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 659 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 660 | } else { |
| 661 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 662 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | if (CHANGED(state, m, LIGHT_MASK)) { |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 667 | UPDATE(state, m, LIGHT_MASK); |
| 668 | if (m & LIT) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 669 | glEnable(GL_LIGHTING); |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 670 | glDisable(GL_TEXTURE_GEN_S); |
| 671 | glDisable(GL_TEXTURE_GEN_T); |
| 672 | glDisable(GL_TEXTURE_2D); |
| 673 | } |
| 674 | else if (m & UNLIT) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 675 | glDisable(GL_LIGHTING); |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 676 | glDisable(GL_TEXTURE_GEN_S); |
| 677 | glDisable(GL_TEXTURE_GEN_T); |
| 678 | glDisable(GL_TEXTURE_2D); |
| 679 | } |
| 680 | else if (m & REFLECT) { |
| 681 | glDisable(GL_LIGHTING); |
| 682 | glEnable(GL_TEXTURE_GEN_S); |
| 683 | glEnable(GL_TEXTURE_GEN_T); |
| 684 | glEnable(GL_TEXTURE_2D); |
| 685 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | if (CHANGED(state, m, SHADE_MASK)) { |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 689 | UPDATE(state, m, SHADE_MASK); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 690 | if (m & SHADE_SMOOTH) |
| 691 | glShadeModel(GL_SMOOTH); |
| 692 | else |
| 693 | glShadeModel(GL_FLAT); |
| 694 | } |
| 695 | |
| 696 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 697 | if (CHANGED(state, m, CLIP_MASK)) { |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 698 | UPDATE(state, m, CLIP_MASK); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 699 | if (m & USER_CLIP) { |
| 700 | glEnable(GL_CLIP_PLANE0); |
| 701 | } else { |
| 702 | glDisable(GL_CLIP_PLANE0); |
| 703 | } |
| 704 | } |
| 705 | |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 706 | if (CHANGED(state, m, FOG_MASK)) { |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 707 | UPDATE(state, m, FOG_MASK); |
Gareth Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 708 | if (m & FOG) { |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 709 | glEnable(GL_FOG); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 710 | } |
Gareth Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 711 | else { |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 712 | glDisable(GL_FOG); |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 716 | if (CHANGED(state, m, STIPPLE_MASK)) { |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 717 | UPDATE(state, m, STIPPLE_MASK); |
Gareth Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 718 | if (m & STIPPLE) { |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 719 | glEnable(GL_POLYGON_STIPPLE); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 720 | } |
Gareth Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 721 | else { |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 722 | glDisable(GL_POLYGON_STIPPLE); |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Gareth Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 726 | if (CHANGED(state, m, POLYGON_MASK)) { |
| 727 | UPDATE(state, m, POLYGON_MASK); |
| 728 | if (m & POLYGON_FILL) { |
| 729 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 730 | } |
Jakob Bornecrantz | 54e2082 | 2009-02-13 17:53:49 +0100 | [diff] [blame] | 731 | else if (m & POLYGON_LINE) { |
Gareth Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 732 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
| 733 | } |
Jakob Bornecrantz | 54e2082 | 2009-02-13 17:53:49 +0100 | [diff] [blame] | 734 | else { |
| 735 | glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); |
| 736 | } |
Gareth Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 737 | } |
| 738 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 739 | #ifdef GL_EXT_vertex_array |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 740 | if (CHANGED(state, m, (LOCK_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK))) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 741 | { |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 742 | if (m & (PRIMITIVE_MASK)) { |
| 743 | UPDATE(state, m, (PRIMITIVE_MASK)); |
| 744 | } |
| 745 | |
| 746 | if (m & (RENDER_STYLE_MASK)) { |
| 747 | UPDATE(state, m, (RENDER_STYLE_MASK)); |
| 748 | } |
| 749 | |
| 750 | if (m & LOCK_MASK) { |
| 751 | UPDATE(state, m, (LOCK_MASK)); |
| 752 | } |
| 753 | |
| 754 | |
| 755 | print_flags("primitive", state & PRIMITIVE_MASK); |
| 756 | print_flags("render style", state & RENDER_STYLE_MASK); |
| 757 | |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 758 | if ((state & PRIMITIVE_MASK) != STRIPS && |
| 759 | ((state & RENDER_STYLE_MASK) == DRAW_ELTS || |
| 760 | (state & RENDER_STYLE_MASK) == ARRAY_ELT || |
| 761 | (state & PRIMITIVE_MASK) == POINTS)) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 762 | { |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 763 | fprintf(stderr, "enabling small arrays\n"); |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 764 | /* Rendering any primitive with draw-element/array-element |
| 765 | * --> Can't do strips here as ordering has been lost in |
| 766 | * compaction process... |
| 767 | */ |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 768 | glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numuniq, |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 769 | compressed_data ); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 770 | glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numuniq, |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 771 | &compressed_data[0][3]); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 772 | #ifdef GL_EXT_compiled_vertex_array |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 773 | if (allowed & LOCKED) { |
| 774 | if (state & LOCKED) { |
| 775 | glLockArraysEXT( 0, numuniq ); |
| 776 | } else { |
| 777 | glUnlockArraysEXT(); |
| 778 | } |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 779 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 780 | #endif |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 781 | } |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 782 | else if ((state & PRIMITIVE_MASK) == TRIANGLES && |
| 783 | (state & RENDER_STYLE_MASK) == DRAW_ARRAYS) { |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 784 | fprintf(stderr, "enabling big arrays\n"); |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 785 | /* Only get here for TRIANGLES and drawarrays |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 786 | */ |
| 787 | glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), (numverts-2) * 3, |
| 788 | expanded_data ); |
| 789 | glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), (numverts-2) * 3, |
| 790 | &expanded_data[0][3]); |
| 791 | |
| 792 | #ifdef GL_EXT_compiled_vertex_array |
| 793 | if (allowed & LOCKED) { |
| 794 | if (state & LOCKED) { |
| 795 | glLockArraysEXT( 0, (numverts-2)*3 ); |
| 796 | } else { |
| 797 | glUnlockArraysEXT(); |
| 798 | } |
| 799 | } |
| 800 | #endif |
| 801 | } |
Keith Whitwell | abd5134 | 2001-06-04 15:34:31 +0000 | [diff] [blame] | 802 | else { |
| 803 | fprintf(stderr, "enabling normal arrays\n"); |
| 804 | glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numverts, data ); |
| 805 | glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numverts, &data[0][3]); |
| 806 | #ifdef GL_EXT_compiled_vertex_array |
| 807 | if (allowed & LOCKED) { |
| 808 | if (state & LOCKED) { |
| 809 | glLockArraysEXT( 0, numverts ); |
| 810 | } else { |
| 811 | glUnlockArraysEXT(); |
| 812 | } |
| 813 | } |
| 814 | #endif |
| 815 | } |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 816 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 817 | } |
| 818 | #endif |
| 819 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 820 | |
| 821 | if (m & DLIST_MASK) { |
| 822 | UPDATE(state, m, DLIST_MASK); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 823 | } |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 824 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 825 | if (m & MATERIAL_MASK) { |
| 826 | UPDATE(state, m, MATERIAL_MASK); |
| 827 | } |
| 828 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 829 | print_flags("new flags", state); |
| 830 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 831 | glutPostRedisplay(); |
| 832 | } |
| 833 | |
| 834 | |
| 835 | |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 836 | static void Init(int argc, char *argv[]) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 837 | { |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 838 | GLfloat fogColor[4] = {0.5,1.0,0.5,1.0}; |
| 839 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 840 | xrot = 0; |
| 841 | yrot = 0; |
| 842 | dist = -6; |
| 843 | plane[0] = 1.0; |
| 844 | plane[1] = 0.0; |
| 845 | plane[2] = -1.0; |
| 846 | plane[3] = 0.0; |
| 847 | |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 848 | glClearColor(0.0, 0.0, 1.0, 0.0); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 849 | glEnable( GL_DEPTH_TEST ); |
Brian Paul | 820436f | 2009-07-08 13:58:30 -0600 | [diff] [blame] | 850 | glEnableClientState( GL_VERTEX_ARRAY ); |
| 851 | glEnableClientState( GL_NORMAL_ARRAY ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 852 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 853 | glMatrixMode(GL_PROJECTION); |
| 854 | glLoadIdentity(); |
| 855 | glFrustum( -1.0, 1.0, -1.0, 1.0, 5, 25 ); |
| 856 | |
| 857 | glMatrixMode(GL_MODELVIEW); |
| 858 | glLoadIdentity(); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 859 | glClipPlane(GL_CLIP_PLANE0, plane); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 860 | |
Keith Whitwell | b8f9980 | 2001-05-11 15:47:02 +0000 | [diff] [blame] | 861 | InitMaterials(); |
| 862 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 863 | set_matrix(); |
| 864 | |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 865 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 866 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); |
| 867 | |
| 868 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
| 869 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
| 870 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 871 | |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 872 | /* Green fog is easy to see */ |
| 873 | glFogi(GL_FOG_MODE,GL_EXP2); |
| 874 | glFogfv(GL_FOG_COLOR,fogColor); |
| 875 | glFogf(GL_FOG_DENSITY,0.15); |
| 876 | glHint(GL_FOG_HINT,GL_DONT_CARE); |
| 877 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 878 | { |
| 879 | static int firsttime = 1; |
| 880 | if (firsttime) { |
| 881 | firsttime = 0; |
| 882 | compactify_arrays(); |
| 883 | expand_arrays(); |
| 884 | make_tri_indices(); |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 885 | |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 886 | if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) { |
| 887 | printf("Error: couldn't load texture image\n"); |
| 888 | exit(1); |
| 889 | } |
| 890 | } |
| 891 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 892 | |
| 893 | ModeMenu(SHADE_SMOOTH| |
| 894 | LIT| |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 895 | POINT_FILTER| |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 896 | NO_USER_CLIP| |
| 897 | NO_MATERIALS| |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 898 | NO_FOG| |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 899 | NO_STIPPLE| |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 900 | IMMEDIATE| |
| 901 | STRIPS| |
| 902 | UNLOCKED| |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 903 | GLVERTEX); |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 904 | |
| 905 | if (PrintInfo) { |
| 906 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 907 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 908 | printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); |
| 909 | printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); |
| 910 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | |
| 914 | |
| 915 | static void Reshape(int width, int height) |
| 916 | { |
| 917 | glViewport(0, 0, (GLint)width, (GLint)height); |
| 918 | } |
| 919 | |
| 920 | |
| 921 | |
| 922 | static void Key( unsigned char key, int x, int y ) |
| 923 | { |
Brian Paul | 36ca6bd | 1999-09-08 22:14:31 +0000 | [diff] [blame] | 924 | (void) x; |
| 925 | (void) y; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 926 | switch (key) { |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 927 | case 27: |
| 928 | exit(0); |
| 929 | case 'f': |
| 930 | ModeMenu((state ^ FOG_MASK) & FOG_MASK); |
| 931 | break; |
| 932 | case 's': |
| 933 | ModeMenu((state ^ SHADE_MASK) & SHADE_MASK); |
| 934 | break; |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 935 | case 't': |
| 936 | ModeMenu((state ^ STIPPLE_MASK) & STIPPLE_MASK); |
| 937 | break; |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 938 | case 'l': |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 939 | ModeMenu((state ^ LIGHT_MASK) & (LIT|UNLIT)); |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 940 | break; |
| 941 | case 'm': |
| 942 | ModeMenu((state ^ MATERIAL_MASK) & MATERIAL_MASK); |
| 943 | break; |
| 944 | case 'c': |
| 945 | ModeMenu((state ^ CLIP_MASK) & CLIP_MASK); |
| 946 | break; |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 947 | case 'v': |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 948 | ModeMenu((LOCKED|IMMEDIATE|DRAW_ELTS|TRIANGLES) & allowed); |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 949 | break; |
| 950 | case 'V': |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 951 | ModeMenu(UNLOCKED|IMMEDIATE|GLVERTEX|STRIPS); |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 952 | break; |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 953 | case 'b': |
| 954 | Benchmark(5.0, 0); |
| 955 | break; |
| 956 | case 'B': |
| 957 | Benchmark(0, 5.0); |
| 958 | break; |
| 959 | case 'i': |
| 960 | dist += .25; |
| 961 | set_matrix(); |
| 962 | glutPostRedisplay(); |
| 963 | break; |
| 964 | case 'I': |
| 965 | dist -= .25; |
| 966 | set_matrix(); |
| 967 | glutPostRedisplay(); |
| 968 | break; |
| 969 | case '-': |
| 970 | case '_': |
| 971 | plane[3] += 2.0; |
| 972 | glMatrixMode(GL_MODELVIEW); |
| 973 | glLoadIdentity(); |
| 974 | glClipPlane(GL_CLIP_PLANE0, plane); |
| 975 | set_matrix(); |
| 976 | glutPostRedisplay(); |
| 977 | break; |
| 978 | case '+': |
| 979 | case '=': |
| 980 | plane[3] -= 2.0; |
| 981 | glMatrixMode(GL_MODELVIEW); |
| 982 | glLoadIdentity(); |
| 983 | glClipPlane(GL_CLIP_PLANE0, plane); |
| 984 | set_matrix(); |
| 985 | glutPostRedisplay(); |
| 986 | break; |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 987 | case ' ': |
| 988 | Init(0,0); |
| 989 | break; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
| 993 | |
| 994 | static void SpecialKey( int key, int x, int y ) |
| 995 | { |
Brian Paul | 36ca6bd | 1999-09-08 22:14:31 +0000 | [diff] [blame] | 996 | (void) x; |
| 997 | (void) y; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 998 | switch (key) { |
| 999 | case GLUT_KEY_LEFT: |
| 1000 | yrot -= 15.0; |
| 1001 | break; |
| 1002 | case GLUT_KEY_RIGHT: |
| 1003 | yrot += 15.0; |
| 1004 | break; |
| 1005 | case GLUT_KEY_UP: |
| 1006 | xrot += 15.0; |
| 1007 | break; |
| 1008 | case GLUT_KEY_DOWN: |
| 1009 | xrot -= 15.0; |
| 1010 | break; |
| 1011 | default: |
| 1012 | return; |
| 1013 | } |
| 1014 | set_matrix(); |
| 1015 | glutPostRedisplay(); |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | static GLint Args(int argc, char **argv) |
| 1021 | { |
| 1022 | GLint i; |
| 1023 | GLint mode = 0; |
| 1024 | |
| 1025 | for (i = 1; i < argc; i++) { |
| 1026 | if (strcmp(argv[i], "-sb") == 0) { |
| 1027 | doubleBuffer = GL_FALSE; |
| 1028 | } |
| 1029 | else if (strcmp(argv[i], "-db") == 0) { |
| 1030 | doubleBuffer = GL_TRUE; |
| 1031 | } |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 1032 | else if (strcmp(argv[i], "-info") == 0) { |
| 1033 | PrintInfo = GL_TRUE; |
| 1034 | } |
Keith Whitwell | 18acf6e | 2001-04-19 13:12:40 +0000 | [diff] [blame] | 1035 | else if (strcmp(argv[i], "-10") == 0) { |
| 1036 | maxverts = 10; |
| 1037 | } |
| 1038 | else if (strcmp(argv[i], "-100") == 0) { |
| 1039 | maxverts = 100; |
| 1040 | } |
| 1041 | else if (strcmp(argv[i], "-1000") == 0) { |
| 1042 | maxverts = 1000; |
| 1043 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1044 | else { |
| 1045 | printf("%s (Bad option).\n", argv[i]); |
| 1046 | return QUIT; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | return mode; |
| 1051 | } |
| 1052 | |
| 1053 | int main(int argc, char **argv) |
| 1054 | { |
| 1055 | GLenum type; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1056 | |
| 1057 | GLuint arg_mode = Args(argc, argv); |
| 1058 | |
| 1059 | if (arg_mode & QUIT) |
| 1060 | exit(0); |
| 1061 | |
| 1062 | read_surface( "isosurf.dat" ); |
| 1063 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1064 | glutInitWindowSize(400, 400); |
Brian Paul | 263f432 | 2009-12-18 08:12:55 -0700 | [diff] [blame] | 1065 | glutInit( &argc, argv); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 1066 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1067 | type = GLUT_DEPTH; |
| 1068 | type |= GLUT_RGB; |
| 1069 | type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; |
| 1070 | glutInitDisplayMode(type); |
| 1071 | |
| 1072 | if (glutCreateWindow("Isosurface") <= 0) { |
| 1073 | exit(0); |
| 1074 | } |
| 1075 | |
José Fonseca | efdb779 | 2009-01-24 16:47:50 +0000 | [diff] [blame] | 1076 | glewInit(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1077 | |
José Fonseca | efdb779 | 2009-01-24 16:47:50 +0000 | [diff] [blame] | 1078 | /* Make sure server supports the vertex array extension */ |
| 1079 | if (!GLEW_EXT_vertex_array) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1080 | { |
| 1081 | printf("Vertex arrays not supported by this renderer\n"); |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 1082 | allowed &= ~(LOCKED|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1083 | } |
José Fonseca | efdb779 | 2009-01-24 16:47:50 +0000 | [diff] [blame] | 1084 | else if (!GLEW_EXT_compiled_vertex_array) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1085 | { |
| 1086 | printf("Compiled vertex arrays not supported by this renderer\n"); |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 1087 | allowed &= ~LOCKED; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 1090 | Init(argc, argv); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1091 | ModeMenu(arg_mode); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 1092 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1093 | glutCreateMenu(ModeMenu); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 1094 | glutAddMenuEntry("GL info", GLINFO); |
| 1095 | glutAddMenuEntry("", 0); |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 1096 | glutAddMenuEntry("Lit", LIT); |
| 1097 | glutAddMenuEntry("Unlit", UNLIT); |
| 1098 | glutAddMenuEntry("Reflect", REFLECT); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 1099 | glutAddMenuEntry("", 0); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1100 | glutAddMenuEntry("Smooth", SHADE_SMOOTH); |
| 1101 | glutAddMenuEntry("Flat", SHADE_FLAT); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 1102 | glutAddMenuEntry("", 0); |
Keith Whitwell | 44c7393 | 1999-09-03 14:56:40 +0000 | [diff] [blame] | 1103 | glutAddMenuEntry("Fog", FOG); |
| 1104 | glutAddMenuEntry("No Fog", NO_FOG); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 1105 | glutAddMenuEntry("", 0); |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 1106 | glutAddMenuEntry("Stipple", STIPPLE); |
| 1107 | glutAddMenuEntry("No Stipple", NO_STIPPLE); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 1108 | glutAddMenuEntry("", 0); |
Gareth Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 1109 | glutAddMenuEntry("Polygon Mode Fill", POLYGON_FILL); |
| 1110 | glutAddMenuEntry("Polygon Mode Line", POLYGON_LINE); |
Jakob Bornecrantz | 54e2082 | 2009-02-13 17:53:49 +0100 | [diff] [blame] | 1111 | glutAddMenuEntry("Polygon Mode Points", POLYGON_POINT); |
Gareth Hughes | 735d920 | 2002-01-04 09:47:17 +0000 | [diff] [blame] | 1112 | glutAddMenuEntry("", 0); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1113 | glutAddMenuEntry("Point Filtered", POINT_FILTER); |
| 1114 | glutAddMenuEntry("Linear Filtered", LINEAR_FILTER); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 1115 | glutAddMenuEntry("", 0); |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 1116 | glutAddMenuEntry("GL_TRIANGLES", TRIANGLES); |
| 1117 | glutAddMenuEntry("GL_TRIANGLE_STRIPS", STRIPS); |
| 1118 | glutAddMenuEntry("GL_POINTS", POINTS); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 1119 | glutAddMenuEntry("", 0); |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 1120 | glutAddMenuEntry("Displaylist", DISPLAYLIST); |
| 1121 | glutAddMenuEntry("Immediate", IMMEDIATE); |
Gareth Hughes | c851646 | 2001-01-06 20:38:03 +0000 | [diff] [blame] | 1122 | glutAddMenuEntry("", 0); |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 1123 | if (allowed & LOCKED) { |
| 1124 | glutAddMenuEntry("Locked Arrays (CVA)", LOCKED); |
| 1125 | glutAddMenuEntry("Unlocked Arrays", UNLOCKED); |
| 1126 | glutAddMenuEntry("", 0); |
| 1127 | } |
| 1128 | glutAddMenuEntry("glVertex", GLVERTEX); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1129 | if (allowed & DRAW_ARRAYS) { |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 1130 | glutAddMenuEntry("glDrawElements", DRAW_ELTS); |
| 1131 | glutAddMenuEntry("glDrawArrays", DRAW_ARRAYS); |
| 1132 | glutAddMenuEntry("glArrayElement", ARRAY_ELT); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1133 | } |
Keith Whitwell | 5759f53 | 2001-05-11 12:08:15 +0000 | [diff] [blame] | 1134 | glutAddMenuEntry("", 0); |
| 1135 | glutAddMenuEntry("Quit", QUIT); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1136 | glutAttachMenu(GLUT_RIGHT_BUTTON); |
| 1137 | |
| 1138 | glutReshapeFunc(Reshape); |
| 1139 | glutKeyboardFunc(Key); |
| 1140 | glutSpecialFunc(SpecialKey); |
| 1141 | glutDisplayFunc(Display); |
Keith Whitwell | 03b7aee | 2000-03-30 17:58:56 +0000 | [diff] [blame] | 1142 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1143 | glutMainLoop(); |
| 1144 | return 0; |
| 1145 | } |