Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

isosurf.c 28KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /* $Id: isosurf.c,v 1.15 2002/10/18 17:47:35 kschultz Exp $ */
  2. /*
  3. * Display an isosurface of 3-D wind speed volume.
  4. *
  5. * Command line options:
  6. * -info print GL implementation information
  7. *
  8. * Brian Paul This file in public domain.
  9. */
  10. /* Keys:
  11. * =====
  12. *
  13. * - Arrow keys to rotate
  14. * - 's' toggles smooth shading
  15. * - 'l' toggles lighting
  16. * - 'f' toggles fog
  17. * - 'I' and 'i' zoom in and out
  18. * - 'c' toggles a user clip plane
  19. * - 'm' toggles colorful materials in GL_TRIANGLES modes.
  20. * - '+' and '-' move the user clip plane
  21. *
  22. * Other options are available via the popup menu.
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <math.h>
  29. #ifdef _WIN32
  30. #include <windows.h>
  31. #undef CLIP_MASK
  32. #endif
  33. #define GL_GLEXT_LEGACY
  34. #include "GL/glut.h"
  35. #include "readtex.c" /* I know, this is a hack. KW: me too. */
  36. #define TEXTURE_FILE "../images/reflect.rgb"
  37. #define LIT 0x00000001
  38. #define UNLIT 0x00000002
  39. #define REFLECT 0x00000004
  40. #define POINT_FILTER 0x00000008
  41. #define LINEAR_FILTER 0x00000010
  42. #define GLVERTEX 0x00000020
  43. #define DRAW_ELTS 0x00000040
  44. #define DRAW_ARRAYS 0x00000080
  45. #define ARRAY_ELT 0x00000100
  46. #define LOCKED 0x00000200
  47. #define UNLOCKED 0x00000400
  48. #define IMMEDIATE 0x00000800
  49. #define DISPLAYLIST 0x00001000
  50. #define SHADE_SMOOTH 0x00002000
  51. #define SHADE_FLAT 0x00004000
  52. #define TRIANGLES 0x00008000
  53. #define STRIPS 0x00010000
  54. #define POINTS 0x00020000
  55. #define USER_CLIP 0x00040000
  56. #define NO_USER_CLIP 0x00080000
  57. #define MATERIALS 0x00100000
  58. #define NO_MATERIALS 0x00200000
  59. #define FOG 0x00400000
  60. #define NO_FOG 0x00800000
  61. #define QUIT 0x01000000
  62. #define GLINFO 0x02000000
  63. #define STIPPLE 0x04000000
  64. #define NO_STIPPLE 0x08000000
  65. #define POLYGON_FILL 0x10000000
  66. #define POLYGON_LINE 0x20000000
  67. #define LIGHT_MASK (LIT|UNLIT|REFLECT)
  68. #define FILTER_MASK (POINT_FILTER|LINEAR_FILTER)
  69. #define RENDER_STYLE_MASK (GLVERTEX|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT)
  70. #define DLIST_MASK (IMMEDIATE|DISPLAYLIST)
  71. #define LOCK_MASK (LOCKED|UNLOCKED)
  72. #define MATERIAL_MASK (MATERIALS|NO_MATERIALS)
  73. #define PRIMITIVE_MASK (TRIANGLES|STRIPS|POINTS)
  74. #define CLIP_MASK (USER_CLIP|NO_USER_CLIP)
  75. #define SHADE_MASK (SHADE_SMOOTH|SHADE_FLAT)
  76. #define FOG_MASK (FOG|NO_FOG)
  77. #define STIPPLE_MASK (STIPPLE|NO_STIPPLE)
  78. #define POLYGON_MASK (POLYGON_FILL|POLYGON_LINE)
  79. #define MAXVERTS 10000
  80. static GLint maxverts = MAXVERTS;
  81. static float data[MAXVERTS][6];
  82. static float compressed_data[MAXVERTS][6];
  83. static float expanded_data[MAXVERTS*3][6];
  84. static GLuint indices[MAXVERTS];
  85. static GLuint tri_indices[MAXVERTS*3];
  86. static GLuint strip_indices[MAXVERTS];
  87. static GLfloat col[100][4];
  88. static GLint numverts, num_tri_verts, numuniq;
  89. static GLfloat xrot;
  90. static GLfloat yrot;
  91. static GLfloat dist;
  92. static GLint state, allowed = ~0;
  93. static GLboolean doubleBuffer = GL_TRUE;
  94. static GLdouble plane[4];
  95. static GLuint surf1, dlist_state;
  96. static GLboolean PrintInfo = GL_FALSE;
  97. static GLubyte halftone[] = {
  98. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
  99. 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  100. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
  101. 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  102. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
  103. 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  104. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
  105. 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  106. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA,
  107. 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  108. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};
  109. static void read_surface( char *filename )
  110. {
  111. FILE *f;
  112. f = fopen(filename,"r");
  113. if (!f) {
  114. printf("couldn't read %s\n", filename);
  115. exit(1);
  116. }
  117. numverts = 0;
  118. while (!feof(f) && numverts<maxverts) {
  119. fscanf( f, "%f %f %f %f %f %f",
  120. &data[numverts][0], &data[numverts][1], &data[numverts][2],
  121. &data[numverts][3], &data[numverts][4], &data[numverts][5] );
  122. numverts++;
  123. }
  124. numverts--;
  125. printf("%d vertices, %d triangles\n", numverts, numverts-2);
  126. fclose(f);
  127. }
  128. static void print_flags( const char *msg, GLuint flags )
  129. {
  130. fprintf(stderr,
  131. "%s (0x%x): %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
  132. msg, flags,
  133. (flags & GLVERTEX) ? "glVertex, " : "",
  134. (flags & DRAW_ARRAYS) ? "glDrawArrays, " : "",
  135. (flags & DRAW_ELTS) ? "glDrawElements, " : "",
  136. (flags & ARRAY_ELT) ? "glArrayElement, " : "",
  137. (flags & LOCKED) ? "locked arrays, " : "",
  138. (flags & TRIANGLES) ? "GL_TRIANGLES, " : "",
  139. (flags & STRIPS) ? "GL_TRIANGLE_STRIP, " : "",
  140. (flags & POINTS) ? "GL_POINTS, " : "",
  141. (flags & DISPLAYLIST) ? "as a displaylist, " : "",
  142. (flags & LIT) ? "lit, " : "",
  143. (flags & UNLIT) ? "unlit, " : "",
  144. (flags & REFLECT) ? "reflect, " : "",
  145. (flags & SHADE_FLAT) ? "flat-shaded, " : "",
  146. (flags & USER_CLIP) ? "user_clip, " : "",
  147. (flags & MATERIALS) ? "materials, " : "",
  148. (flags & FOG) ? "fog, " : "",
  149. (flags & STIPPLE) ? "stipple, " : "",
  150. (flags & POLYGON_LINE) ? "polygon mode line, " : "");
  151. }
  152. struct data_idx {
  153. float *data;
  154. int idx;
  155. int uniq_idx;
  156. };
  157. #define COMPARE_FUNC( AXIS ) \
  158. static int compare_axis_##AXIS( const void *a, const void *b ) \
  159. { \
  160. float t = ( (*(struct data_idx *)a).data[AXIS] - \
  161. (*(struct data_idx *)b).data[AXIS] ); \
  162. \
  163. if (t < 0) return -1; \
  164. if (t > 0) return 1; \
  165. return 0; \
  166. }
  167. COMPARE_FUNC(0)
  168. COMPARE_FUNC(1)
  169. COMPARE_FUNC(2)
  170. COMPARE_FUNC(3)
  171. COMPARE_FUNC(4)
  172. COMPARE_FUNC(5)
  173. COMPARE_FUNC(6)
  174. int (*(compare[7]))( const void *a, const void *b ) =
  175. {
  176. compare_axis_0,
  177. compare_axis_1,
  178. compare_axis_2,
  179. compare_axis_3,
  180. compare_axis_4,
  181. compare_axis_5,
  182. compare_axis_6,
  183. };
  184. #define VEC_ELT(f, s, i) (float *)(((char *)f) + s * i)
  185. static int sort_axis( int axis,
  186. int vec_size,
  187. int vec_stride,
  188. struct data_idx *indices,
  189. int start,
  190. int finish,
  191. float *out,
  192. int uniq,
  193. const float fudge )
  194. {
  195. int i;
  196. if (finish-start > 2)
  197. {
  198. qsort( indices+start, finish-start, sizeof(*indices), compare[axis] );
  199. }
  200. else if (indices[start].data[axis] > indices[start+1].data[axis])
  201. {
  202. struct data_idx tmp = indices[start];
  203. indices[start] = indices[start+1];
  204. indices[start+1] = tmp;
  205. }
  206. if (axis == vec_size-1) {
  207. for (i = start ; i < finish ; ) {
  208. float max = indices[i].data[axis] + fudge;
  209. float *dest = VEC_ELT(out, vec_stride, uniq);
  210. int j;
  211. for (j = 0 ; j < vec_size ; j++)
  212. dest[j] = indices[i].data[j];
  213. for ( ; i < finish && max >= indices[i].data[axis]; i++)
  214. indices[i].uniq_idx = uniq;
  215. uniq++;
  216. }
  217. } else {
  218. for (i = start ; i < finish ; ) {
  219. int j = i + 1;
  220. float max = indices[i].data[axis] + fudge;
  221. while (j < finish && max >= indices[j].data[axis]) j++;
  222. if (j == i+1) {
  223. float *dest = VEC_ELT(out, vec_stride, uniq);
  224. int k;
  225. indices[i].uniq_idx = uniq;
  226. for (k = 0 ; k < vec_size ; k++)
  227. dest[k] = indices[i].data[k];
  228. uniq++;
  229. } else {
  230. uniq = sort_axis( axis+1, vec_size, vec_stride,
  231. indices, i, j, out, uniq, fudge );
  232. }
  233. i = j;
  234. }
  235. }
  236. return uniq;
  237. }
  238. static void extract_indices1( const struct data_idx *in, unsigned int *out,
  239. int n )
  240. {
  241. int i;
  242. for ( i = 0 ; i < n ; i++ ) {
  243. out[in[i].idx] = in[i].uniq_idx;
  244. }
  245. }
  246. static void compactify_arrays(void)
  247. {
  248. int i;
  249. struct data_idx *ind;
  250. ind = (struct data_idx *) malloc( sizeof(struct data_idx) * numverts );
  251. for (i = 0 ; i < numverts ; i++) {
  252. ind[i].idx = i;
  253. ind[i].data = data[i];
  254. }
  255. numuniq = sort_axis(0,
  256. sizeof(compressed_data[0])/sizeof(float),
  257. sizeof(compressed_data[0]),
  258. ind,
  259. 0,
  260. numverts,
  261. (float *)compressed_data,
  262. 0,
  263. 1e-6);
  264. printf("Nr unique vertex/normal pairs: %d\n", numuniq);
  265. extract_indices1( ind, indices, numverts );
  266. free( ind );
  267. }
  268. static void expand_arrays(void)
  269. {
  270. int i;
  271. int parity = 0;
  272. for (i = 2 ; i < numverts ; i++, parity ^= 1) {
  273. int v0 = i-2+parity;
  274. int v1 = i-1-parity;
  275. int v2 = i;
  276. memcpy( expanded_data[(i-2)*3+0], data[v0], sizeof(data[0]) );
  277. memcpy( expanded_data[(i-2)*3+1], data[v1], sizeof(data[0]) );
  278. memcpy( expanded_data[(i-2)*3+2], data[v2], sizeof(data[0]) );
  279. }
  280. }
  281. static float myrand( float max )
  282. {
  283. return max*rand()/(RAND_MAX+1.0);
  284. }
  285. static void make_tri_indices( void )
  286. {
  287. unsigned int *v = tri_indices;
  288. unsigned int parity = 0;
  289. int i, j;
  290. for (j=2;j<numverts;j++,parity^=1) {
  291. if (parity) {
  292. *v++ = indices[j-1];
  293. *v++ = indices[j-2];
  294. *v++ = indices[j];
  295. } else {
  296. *v++ = indices[j-2];
  297. *v++ = indices[j-1];
  298. *v++ = indices[j];
  299. }
  300. }
  301. num_tri_verts = v - tri_indices;
  302. printf("num_tri_verts: %d\n", num_tri_verts);
  303. for (i = j = 0 ; i < num_tri_verts ; i += 600, j++) {
  304. col[j][3] = 1;
  305. col[j][2] = myrand(1);
  306. col[j][1] = myrand(1);
  307. col[j][0] = myrand(1);
  308. }
  309. for (i = 0; i < numverts ; i++)
  310. strip_indices[i] = i;
  311. }
  312. #define MIN(x,y) (x < y) ? x : y
  313. static void draw_surface( unsigned int with_state )
  314. {
  315. GLint i, j;
  316. if (with_state & DISPLAYLIST) {
  317. if ((with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|MATERIAL_MASK)) !=
  318. dlist_state) {
  319. /*
  320. */
  321. fprintf(stderr, "rebuilding displaylist\n");
  322. if (dlist_state)
  323. glDeleteLists( surf1, 1 );
  324. dlist_state = with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK|
  325. MATERIAL_MASK);
  326. surf1 = glGenLists(1);
  327. glNewList(surf1, GL_COMPILE);
  328. draw_surface( dlist_state );
  329. glEndList();
  330. }
  331. glCallList( surf1 );
  332. return;
  333. }
  334. switch (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK)) {
  335. #ifdef GL_EXT_vertex_array
  336. case (DRAW_ELTS|TRIANGLES):
  337. if (with_state & MATERIALS) {
  338. for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
  339. GLuint nr = MIN(num_tri_verts-i, 600);
  340. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
  341. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
  342. glDrawElements( GL_TRIANGLES, nr, GL_UNSIGNED_INT, tri_indices+i );
  343. }
  344. } else {
  345. glDrawElements( GL_TRIANGLES, num_tri_verts, GL_UNSIGNED_INT,
  346. tri_indices );
  347. }
  348. break;
  349. case (DRAW_ARRAYS|TRIANGLES):
  350. glDrawArraysEXT( GL_TRIANGLES, 0, (numverts-2)*3 );
  351. break;
  352. case (ARRAY_ELT|TRIANGLES):
  353. if (with_state & MATERIALS) {
  354. for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
  355. GLuint nr = MIN(num_tri_verts-i, 600);
  356. GLuint k;
  357. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
  358. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
  359. glBegin( GL_TRIANGLES );
  360. for (k = 0 ; k < nr ; k++)
  361. glArrayElement( tri_indices[i+k] );
  362. glEnd();
  363. }
  364. } else {
  365. glBegin( GL_TRIANGLES );
  366. for (i = 0 ; i < num_tri_verts ; i++)
  367. glArrayElement( tri_indices[i] );
  368. glEnd();
  369. }
  370. break;
  371. /* Uses the original arrays (including duplicate elements):
  372. */
  373. case (DRAW_ARRAYS|STRIPS):
  374. glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
  375. break;
  376. case (DRAW_ELTS|STRIPS):
  377. glDrawElements( GL_TRIANGLE_STRIP, numverts,
  378. GL_UNSIGNED_INT, strip_indices );
  379. break;
  380. /* Uses the original arrays (including duplicate elements):
  381. */
  382. case (ARRAY_ELT|STRIPS):
  383. glBegin( GL_TRIANGLE_STRIP );
  384. for (i = 0 ; i < numverts ; i++)
  385. glArrayElement( i );
  386. glEnd();
  387. break;
  388. case (DRAW_ARRAYS|POINTS):
  389. glDrawArraysEXT( GL_POINTS, 0, numuniq );
  390. break;
  391. case (DRAW_ELTS|POINTS):
  392. /* can use numuniq with strip_indices as strip_indices[i] == i.
  393. */
  394. glDrawElements( GL_POINTS, numuniq,
  395. GL_UNSIGNED_INT, strip_indices );
  396. break;
  397. case (ARRAY_ELT|POINTS):
  398. /* just emit each unique element once:
  399. */
  400. glBegin( GL_POINTS );
  401. for (i = 0 ; i < numuniq ; i++)
  402. glArrayElement( i );
  403. glEnd();
  404. break;
  405. #endif
  406. case (GLVERTEX|TRIANGLES):
  407. if (with_state & MATERIALS) {
  408. for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
  409. GLuint nr = MIN(num_tri_verts-i, 600);
  410. GLuint k;
  411. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
  412. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
  413. glBegin( GL_TRIANGLES );
  414. for (k = 0 ; k < nr ; k++) {
  415. glNormal3fv( &compressed_data[tri_indices[i+k]][3] );
  416. glVertex3fv( &compressed_data[tri_indices[i+k]][0] );
  417. }
  418. glEnd();
  419. }
  420. } else {
  421. glBegin( GL_TRIANGLES );
  422. for (i = 0 ; i < num_tri_verts ; i++) {
  423. glNormal3fv( &compressed_data[tri_indices[i]][3] );
  424. glVertex3fv( &compressed_data[tri_indices[i]][0] );
  425. }
  426. glEnd();
  427. }
  428. break;
  429. case (GLVERTEX|POINTS):
  430. /* Renders all points, but not in strip order... Shouldn't be a
  431. * problem, but people may be confused as to why points are so
  432. * much faster in this demo... And why cva doesn't help them...
  433. */
  434. glBegin( GL_POINTS );
  435. for ( i = 0 ; i < numuniq ; i++ ) {
  436. glNormal3fv( &compressed_data[i][3] );
  437. glVertex3fv( &compressed_data[i][0] );
  438. }
  439. glEnd();
  440. break;
  441. case (GLVERTEX|STRIPS):
  442. glBegin( GL_TRIANGLE_STRIP );
  443. for (i=0;i<numverts;i++) {
  444. glNormal3fv( &data[i][3] );
  445. glVertex3fv( &data[i][0] );
  446. }
  447. glEnd();
  448. break;
  449. default:
  450. fprintf(stderr, "unimplemented mode %x...\n",
  451. (with_state & (RENDER_STYLE_MASK|PRIMITIVE_MASK)));
  452. break;
  453. }
  454. }
  455. static void Display(void)
  456. {
  457. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  458. draw_surface( state );
  459. glFlush();
  460. if (doubleBuffer) glutSwapBuffers();
  461. }
  462. /* KW: only do this when necessary, so CVA can re-use results.
  463. */
  464. static void set_matrix( void )
  465. {
  466. glMatrixMode(GL_MODELVIEW);
  467. glLoadIdentity();
  468. glTranslatef( 0.0, 0.0, dist );
  469. glRotatef( yrot, 0.0, 1.0, 0.0 );
  470. glRotatef( xrot, 1.0, 0.0, 0.0 );
  471. }
  472. static void Benchmark( float xdiff, float ydiff )
  473. {
  474. int startTime, endTime;
  475. int draws;
  476. double seconds, fps, triPerSecond;
  477. printf("Benchmarking...\n");
  478. draws = 0;
  479. startTime = glutGet(GLUT_ELAPSED_TIME);
  480. xrot = 0.0;
  481. do {
  482. xrot += xdiff;
  483. yrot += ydiff;
  484. set_matrix();
  485. Display();
  486. draws++;
  487. endTime = glutGet(GLUT_ELAPSED_TIME);
  488. } while (endTime - startTime < 5000); /* 5 seconds */
  489. /* Results */
  490. seconds = (double) (endTime - startTime) / 1000.0;
  491. triPerSecond = (numverts - 2) * draws / seconds;
  492. fps = draws / seconds;
  493. printf("Result: triangles/sec: %g fps: %g\n", triPerSecond, fps);
  494. }
  495. static void InitMaterials(void)
  496. {
  497. static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  498. static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
  499. static float position0[] = {0.0, 0.0, 20.0, 0.0};
  500. static float position1[] = {0.0, 0.0, -20.0, 0.0};
  501. static float front_mat_shininess[] = {60.0};
  502. static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
  503. static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
  504. /*
  505. static float back_mat_shininess[] = {60.0};
  506. static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
  507. static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  508. */
  509. static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  510. static float lmodel_twoside[] = {GL_FALSE};
  511. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  512. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  513. glLightfv(GL_LIGHT0, GL_POSITION, position0);
  514. glEnable(GL_LIGHT0);
  515. glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
  516. glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
  517. glLightfv(GL_LIGHT1, GL_POSITION, position1);
  518. glEnable(GL_LIGHT1);
  519. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  520. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  521. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
  522. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
  523. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
  524. glPolygonStipple (halftone);
  525. }
  526. #define UPDATE(o,n,mask) (o&=~mask, o|=n&mask)
  527. #define CHANGED(o,n,mask) ((n&mask) && (n&mask) != (o&mask) )
  528. static void ModeMenu(int m)
  529. {
  530. m &= allowed;
  531. if (!m) return;
  532. if (m==QUIT)
  533. exit(0);
  534. if (m==GLINFO) {
  535. printf("GL_VERSION: %s\n", (char *) glGetString(GL_VERSION));
  536. printf("GL_EXTENSIONS: %s\n", (char *) glGetString(GL_EXTENSIONS));
  537. printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
  538. return;
  539. }
  540. if (CHANGED(state, m, FILTER_MASK)) {
  541. UPDATE(state, m, FILTER_MASK);
  542. if (m & LINEAR_FILTER) {
  543. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  544. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  545. } else {
  546. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  547. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  548. }
  549. }
  550. if (CHANGED(state, m, LIGHT_MASK)) {
  551. UPDATE(state, m, LIGHT_MASK);
  552. if (m & LIT) {
  553. glEnable(GL_LIGHTING);
  554. glDisable(GL_TEXTURE_GEN_S);
  555. glDisable(GL_TEXTURE_GEN_T);
  556. glDisable(GL_TEXTURE_2D);
  557. }
  558. else if (m & UNLIT) {
  559. glDisable(GL_LIGHTING);
  560. glDisable(GL_TEXTURE_GEN_S);
  561. glDisable(GL_TEXTURE_GEN_T);
  562. glDisable(GL_TEXTURE_2D);
  563. }
  564. else if (m & REFLECT) {
  565. glDisable(GL_LIGHTING);
  566. glEnable(GL_TEXTURE_GEN_S);
  567. glEnable(GL_TEXTURE_GEN_T);
  568. glEnable(GL_TEXTURE_2D);
  569. }
  570. }
  571. if (CHANGED(state, m, SHADE_MASK)) {
  572. UPDATE(state, m, SHADE_MASK);
  573. if (m & SHADE_SMOOTH)
  574. glShadeModel(GL_SMOOTH);
  575. else
  576. glShadeModel(GL_FLAT);
  577. }
  578. if (CHANGED(state, m, CLIP_MASK)) {
  579. UPDATE(state, m, CLIP_MASK);
  580. if (m & USER_CLIP) {
  581. glEnable(GL_CLIP_PLANE0);
  582. } else {
  583. glDisable(GL_CLIP_PLANE0);
  584. }
  585. }
  586. if (CHANGED(state, m, FOG_MASK)) {
  587. UPDATE(state, m, FOG_MASK);
  588. if (m & FOG) {
  589. glEnable(GL_FOG);
  590. }
  591. else {
  592. glDisable(GL_FOG);
  593. }
  594. }
  595. if (CHANGED(state, m, STIPPLE_MASK)) {
  596. UPDATE(state, m, STIPPLE_MASK);
  597. if (m & STIPPLE) {
  598. glEnable(GL_POLYGON_STIPPLE);
  599. }
  600. else {
  601. glDisable(GL_POLYGON_STIPPLE);
  602. }
  603. }
  604. if (CHANGED(state, m, POLYGON_MASK)) {
  605. UPDATE(state, m, POLYGON_MASK);
  606. if (m & POLYGON_FILL) {
  607. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  608. }
  609. else {
  610. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  611. }
  612. }
  613. #ifdef GL_EXT_vertex_array
  614. if (CHANGED(state, m, (LOCK_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK)))
  615. {
  616. if (m & (PRIMITIVE_MASK)) {
  617. UPDATE(state, m, (PRIMITIVE_MASK));
  618. }
  619. if (m & (RENDER_STYLE_MASK)) {
  620. UPDATE(state, m, (RENDER_STYLE_MASK));
  621. }
  622. if (m & LOCK_MASK) {
  623. UPDATE(state, m, (LOCK_MASK));
  624. }
  625. print_flags("primitive", state & PRIMITIVE_MASK);
  626. print_flags("render style", state & RENDER_STYLE_MASK);
  627. if ((state & PRIMITIVE_MASK) != STRIPS &&
  628. ((state & RENDER_STYLE_MASK) == DRAW_ELTS ||
  629. (state & RENDER_STYLE_MASK) == ARRAY_ELT ||
  630. (state & PRIMITIVE_MASK) == POINTS))
  631. {
  632. fprintf(stderr, "enabling small arrays\n");
  633. /* Rendering any primitive with draw-element/array-element
  634. * --> Can't do strips here as ordering has been lost in
  635. * compaction process...
  636. */
  637. glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numuniq,
  638. compressed_data );
  639. glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numuniq,
  640. &compressed_data[0][3]);
  641. #ifdef GL_EXT_compiled_vertex_array
  642. if (allowed & LOCKED) {
  643. if (state & LOCKED) {
  644. glLockArraysEXT( 0, numuniq );
  645. } else {
  646. glUnlockArraysEXT();
  647. }
  648. }
  649. #endif
  650. }
  651. else if ((state & PRIMITIVE_MASK) == TRIANGLES &&
  652. (state & RENDER_STYLE_MASK) == DRAW_ARRAYS) {
  653. fprintf(stderr, "enabling big arrays\n");
  654. /* Only get here for TRIANGLES and drawarrays
  655. */
  656. glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
  657. expanded_data );
  658. glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), (numverts-2) * 3,
  659. &expanded_data[0][3]);
  660. #ifdef GL_EXT_compiled_vertex_array
  661. if (allowed & LOCKED) {
  662. if (state & LOCKED) {
  663. glLockArraysEXT( 0, (numverts-2)*3 );
  664. } else {
  665. glUnlockArraysEXT();
  666. }
  667. }
  668. #endif
  669. }
  670. else {
  671. fprintf(stderr, "enabling normal arrays\n");
  672. glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numverts, data );
  673. glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numverts, &data[0][3]);
  674. #ifdef GL_EXT_compiled_vertex_array
  675. if (allowed & LOCKED) {
  676. if (state & LOCKED) {
  677. glLockArraysEXT( 0, numverts );
  678. } else {
  679. glUnlockArraysEXT();
  680. }
  681. }
  682. #endif
  683. }
  684. }
  685. #endif
  686. if (m & DLIST_MASK) {
  687. UPDATE(state, m, DLIST_MASK);
  688. }
  689. if (m & MATERIAL_MASK) {
  690. UPDATE(state, m, MATERIAL_MASK);
  691. }
  692. print_flags("new flags", state);
  693. glutPostRedisplay();
  694. }
  695. static void Init(int argc, char *argv[])
  696. {
  697. GLfloat fogColor[4] = {0.5,1.0,0.5,1.0};
  698. xrot = 0;
  699. yrot = 0;
  700. dist = -6;
  701. plane[0] = 1.0;
  702. plane[1] = 0.0;
  703. plane[2] = -1.0;
  704. plane[3] = 0.0;
  705. glClearColor(0.0, 0.0, 1.0, 0.0);
  706. glEnable( GL_DEPTH_TEST );
  707. glEnable( GL_VERTEX_ARRAY_EXT );
  708. glEnable( GL_NORMAL_ARRAY_EXT );
  709. glMatrixMode(GL_PROJECTION);
  710. glLoadIdentity();
  711. glFrustum( -1.0, 1.0, -1.0, 1.0, 5, 25 );
  712. glMatrixMode(GL_MODELVIEW);
  713. glLoadIdentity();
  714. glClipPlane(GL_CLIP_PLANE0, plane);
  715. InitMaterials();
  716. set_matrix();
  717. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  718. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  719. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  720. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  721. /* Green fog is easy to see */
  722. glFogi(GL_FOG_MODE,GL_EXP2);
  723. glFogfv(GL_FOG_COLOR,fogColor);
  724. glFogf(GL_FOG_DENSITY,0.15);
  725. glHint(GL_FOG_HINT,GL_DONT_CARE);
  726. {
  727. static int firsttime = 1;
  728. if (firsttime) {
  729. firsttime = 0;
  730. compactify_arrays();
  731. expand_arrays();
  732. make_tri_indices();
  733. if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  734. printf("Error: couldn't load texture image\n");
  735. exit(1);
  736. }
  737. }
  738. }
  739. ModeMenu(SHADE_SMOOTH|
  740. LIT|
  741. POINT_FILTER|
  742. NO_USER_CLIP|
  743. NO_MATERIALS|
  744. NO_FOG|
  745. NO_STIPPLE|
  746. IMMEDIATE|
  747. STRIPS|
  748. UNLOCKED|
  749. GLVERTEX);
  750. if (PrintInfo) {
  751. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  752. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  753. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  754. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  755. }
  756. }
  757. static void Reshape(int width, int height)
  758. {
  759. glViewport(0, 0, (GLint)width, (GLint)height);
  760. }
  761. static void Key( unsigned char key, int x, int y )
  762. {
  763. (void) x;
  764. (void) y;
  765. switch (key) {
  766. case 27:
  767. exit(0);
  768. case 'f':
  769. ModeMenu((state ^ FOG_MASK) & FOG_MASK);
  770. break;
  771. case 's':
  772. ModeMenu((state ^ SHADE_MASK) & SHADE_MASK);
  773. break;
  774. case 't':
  775. ModeMenu((state ^ STIPPLE_MASK) & STIPPLE_MASK);
  776. break;
  777. case 'l':
  778. ModeMenu((state ^ LIGHT_MASK) & (LIT|UNLIT));
  779. break;
  780. case 'm':
  781. ModeMenu((state ^ MATERIAL_MASK) & MATERIAL_MASK);
  782. break;
  783. case 'c':
  784. ModeMenu((state ^ CLIP_MASK) & CLIP_MASK);
  785. break;
  786. case 'v':
  787. ModeMenu((LOCKED|IMMEDIATE|DRAW_ELTS|TRIANGLES) & allowed);
  788. break;
  789. case 'V':
  790. ModeMenu(UNLOCKED|IMMEDIATE|GLVERTEX|STRIPS);
  791. break;
  792. case 'b':
  793. Benchmark(5.0, 0);
  794. break;
  795. case 'B':
  796. Benchmark(0, 5.0);
  797. break;
  798. case 'i':
  799. dist += .25;
  800. set_matrix();
  801. glutPostRedisplay();
  802. break;
  803. case 'I':
  804. dist -= .25;
  805. set_matrix();
  806. glutPostRedisplay();
  807. break;
  808. case '-':
  809. case '_':
  810. plane[3] += 2.0;
  811. glMatrixMode(GL_MODELVIEW);
  812. glLoadIdentity();
  813. glClipPlane(GL_CLIP_PLANE0, plane);
  814. set_matrix();
  815. glutPostRedisplay();
  816. break;
  817. case '+':
  818. case '=':
  819. plane[3] -= 2.0;
  820. glMatrixMode(GL_MODELVIEW);
  821. glLoadIdentity();
  822. glClipPlane(GL_CLIP_PLANE0, plane);
  823. set_matrix();
  824. glutPostRedisplay();
  825. break;
  826. case ' ':
  827. Init(0,0);
  828. break;
  829. }
  830. }
  831. static void SpecialKey( int key, int x, int y )
  832. {
  833. (void) x;
  834. (void) y;
  835. switch (key) {
  836. case GLUT_KEY_LEFT:
  837. yrot -= 15.0;
  838. break;
  839. case GLUT_KEY_RIGHT:
  840. yrot += 15.0;
  841. break;
  842. case GLUT_KEY_UP:
  843. xrot += 15.0;
  844. break;
  845. case GLUT_KEY_DOWN:
  846. xrot -= 15.0;
  847. break;
  848. default:
  849. return;
  850. }
  851. set_matrix();
  852. glutPostRedisplay();
  853. }
  854. static GLint Args(int argc, char **argv)
  855. {
  856. GLint i;
  857. GLint mode = 0;
  858. for (i = 1; i < argc; i++) {
  859. if (strcmp(argv[i], "-sb") == 0) {
  860. doubleBuffer = GL_FALSE;
  861. }
  862. else if (strcmp(argv[i], "-db") == 0) {
  863. doubleBuffer = GL_TRUE;
  864. }
  865. else if (strcmp(argv[i], "-info") == 0) {
  866. PrintInfo = GL_TRUE;
  867. }
  868. else if (strcmp(argv[i], "-10") == 0) {
  869. maxverts = 10;
  870. }
  871. else if (strcmp(argv[i], "-100") == 0) {
  872. maxverts = 100;
  873. }
  874. else if (strcmp(argv[i], "-1000") == 0) {
  875. maxverts = 1000;
  876. }
  877. else {
  878. printf("%s (Bad option).\n", argv[i]);
  879. return QUIT;
  880. }
  881. }
  882. return mode;
  883. }
  884. int main(int argc, char **argv)
  885. {
  886. GLenum type;
  887. char *extensions;
  888. GLuint arg_mode = Args(argc, argv);
  889. if (arg_mode & QUIT)
  890. exit(0);
  891. read_surface( "isosurf.dat" );
  892. glutInitWindowPosition(0, 0);
  893. glutInitWindowSize(400, 400);
  894. type = GLUT_DEPTH;
  895. type |= GLUT_RGB;
  896. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  897. glutInitDisplayMode(type);
  898. if (glutCreateWindow("Isosurface") <= 0) {
  899. exit(0);
  900. }
  901. /* Make sure server supports the vertex array extension */
  902. extensions = (char *) glGetString( GL_EXTENSIONS );
  903. if (!strstr( extensions, "GL_EXT_vertex_array" ))
  904. {
  905. printf("Vertex arrays not supported by this renderer\n");
  906. allowed &= ~(LOCKED|DRAW_ARRAYS|DRAW_ELTS|ARRAY_ELT);
  907. }
  908. else if (!strstr( extensions, "GL_EXT_compiled_vertex_array" ))
  909. {
  910. printf("Compiled vertex arrays not supported by this renderer\n");
  911. allowed &= ~LOCKED;
  912. }
  913. Init(argc, argv);
  914. ModeMenu(arg_mode);
  915. glutCreateMenu(ModeMenu);
  916. glutAddMenuEntry("GL info", GLINFO);
  917. glutAddMenuEntry("", 0);
  918. glutAddMenuEntry("Lit", LIT);
  919. glutAddMenuEntry("Unlit", UNLIT);
  920. glutAddMenuEntry("Reflect", REFLECT);
  921. glutAddMenuEntry("", 0);
  922. glutAddMenuEntry("Smooth", SHADE_SMOOTH);
  923. glutAddMenuEntry("Flat", SHADE_FLAT);
  924. glutAddMenuEntry("", 0);
  925. glutAddMenuEntry("Fog", FOG);
  926. glutAddMenuEntry("No Fog", NO_FOG);
  927. glutAddMenuEntry("", 0);
  928. glutAddMenuEntry("Stipple", STIPPLE);
  929. glutAddMenuEntry("No Stipple", NO_STIPPLE);
  930. glutAddMenuEntry("", 0);
  931. glutAddMenuEntry("Polygon Mode Fill", POLYGON_FILL);
  932. glutAddMenuEntry("Polygon Mode Line", POLYGON_LINE);
  933. glutAddMenuEntry("", 0);
  934. glutAddMenuEntry("Point Filtered", POINT_FILTER);
  935. glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
  936. glutAddMenuEntry("", 0);
  937. glutAddMenuEntry("GL_TRIANGLES", TRIANGLES);
  938. glutAddMenuEntry("GL_TRIANGLE_STRIPS", STRIPS);
  939. glutAddMenuEntry("GL_POINTS", POINTS);
  940. glutAddMenuEntry("", 0);
  941. glutAddMenuEntry("Displaylist", DISPLAYLIST);
  942. glutAddMenuEntry("Immediate", IMMEDIATE);
  943. glutAddMenuEntry("", 0);
  944. if (allowed & LOCKED) {
  945. glutAddMenuEntry("Locked Arrays (CVA)", LOCKED);
  946. glutAddMenuEntry("Unlocked Arrays", UNLOCKED);
  947. glutAddMenuEntry("", 0);
  948. }
  949. glutAddMenuEntry("glVertex", GLVERTEX);
  950. if (allowed & DRAW_ARRAYS) {
  951. glutAddMenuEntry("glDrawElements", DRAW_ELTS);
  952. glutAddMenuEntry("glDrawArrays", DRAW_ARRAYS);
  953. glutAddMenuEntry("glArrayElement", ARRAY_ELT);
  954. }
  955. glutAddMenuEntry("", 0);
  956. glutAddMenuEntry("Quit", QUIT);
  957. glutAttachMenu(GLUT_RIGHT_BUTTON);
  958. glutReshapeFunc(Reshape);
  959. glutKeyboardFunc(Key);
  960. glutSpecialFunc(SpecialKey);
  961. glutDisplayFunc(Display);
  962. glutMainLoop();
  963. return 0;
  964. }