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 29KB

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