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

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