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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /* $Id: isosurf.c,v 1.4 1999/10/21 16:39:06 brianp 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. /*
  25. * $Log: isosurf.c,v $
  26. * Revision 1.4 1999/10/21 16:39:06 brianp
  27. * added -info command line option
  28. *
  29. * Revision 1.3 1999/09/08 22:14:31 brianp
  30. * minor changes. always call compactify_arrays()
  31. *
  32. * Revision 1.2 1999/09/03 14:56:40 keithw
  33. * Fog, displaylist and zoom operations
  34. *
  35. * Revision 3.4 1999/04/24 01:10:47 keithw
  36. * clip planes, materials
  37. *
  38. * Revision 3.3 1999/03/31 19:42:14 keithw
  39. * support for cva
  40. *
  41. * Revision 3.1 1998/11/01 20:30:20 brianp
  42. * added benchmark feature (b key)
  43. *
  44. * Revision 3.0 1998/02/14 18:42:29 brianp
  45. * initial rev
  46. *
  47. */
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <math.h>
  53. #include "GL/glut.h"
  54. #include "../util/readtex.c" /* I know, this is a hack. KW: me too. */
  55. #define TEXTURE_FILE "../images/reflect.rgb"
  56. #define LIT 0x1
  57. #define UNLIT 0x2
  58. #define TEXTURE 0x4
  59. #define NO_TEXTURE 0x8
  60. #define REFLECT 0x10
  61. #define NO_REFLECT 0x20
  62. #define POINT_FILTER 0x40
  63. #define LINEAR_FILTER 0x80
  64. #define GLVERTEX 0x100
  65. #define DRAW_ARRAYS 0x200 /* or draw_elts, if compiled */
  66. #define ARRAY_ELT 0x400
  67. #define COMPILED 0x800
  68. #define IMMEDIATE 0x1000
  69. #define SHADE_SMOOTH 0x2000
  70. #define SHADE_FLAT 0x4000
  71. #define TRIANGLES 0x8000
  72. #define STRIPS 0x10000
  73. #define USER_CLIP 0x20000
  74. #define NO_USER_CLIP 0x40000
  75. #define MATERIALS 0x80000
  76. #define NO_MATERIALS 0x100000
  77. #define FOG 0x200000
  78. #define NO_FOG 0x400000
  79. #define QUIT 0x800000
  80. #define DISPLAYLIST 0x1000000
  81. #define LIGHT_MASK (LIT|UNLIT)
  82. #define TEXTURE_MASK (TEXTURE|NO_TEXTURE)
  83. #define REFLECT_MASK (REFLECT|NO_REFLECT)
  84. #define FILTER_MASK (POINT_FILTER|LINEAR_FILTER)
  85. #define RENDER_STYLE_MASK (GLVERTEX|DRAW_ARRAYS|ARRAY_ELT)
  86. #define COMPILED_MASK (COMPILED|IMMEDIATE|DISPLAYLIST)
  87. #define MATERIAL_MASK (MATERIALS|NO_MATERIALS)
  88. #define PRIMITIVE_MASK (TRIANGLES|STRIPS)
  89. #define CLIP_MASK (USER_CLIP|NO_USER_CLIP)
  90. #define SHADE_MASK (SHADE_SMOOTH|SHADE_FLAT)
  91. #define FOG_MASK (FOG|NO_FOG)
  92. #define MAXVERTS 10000
  93. static float data[MAXVERTS][6];
  94. static float compressed_data[MAXVERTS][6];
  95. static GLuint indices[MAXVERTS];
  96. static GLuint tri_indices[MAXVERTS*3];
  97. static GLfloat col[100][4];
  98. static GLint numverts, num_tri_verts, numuniq;
  99. static GLfloat xrot;
  100. static GLfloat yrot;
  101. static GLfloat dist = -6;
  102. static GLint state, allowed = ~0;
  103. static GLboolean doubleBuffer = GL_TRUE;
  104. static GLdouble plane[4] = {1.0, 0.0, -1.0, 0.0};
  105. static GLuint surf1;
  106. static GLboolean PrintInfo = GL_FALSE;
  107. /* forward decl */
  108. int BuildList( int mode );
  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. struct data_idx {
  129. float *data;
  130. int idx;
  131. int uniq_idx;
  132. };
  133. #define COMPARE_FUNC( AXIS ) \
  134. static int compare_axis_##AXIS( const void *a, const void *b ) \
  135. { \
  136. float t = ( (*(struct data_idx *)a).data[AXIS] - \
  137. (*(struct data_idx *)b).data[AXIS] ); \
  138. \
  139. if (t < 0) return -1; \
  140. if (t > 0) return 1; \
  141. return 0; \
  142. }
  143. COMPARE_FUNC(0)
  144. COMPARE_FUNC(1)
  145. COMPARE_FUNC(2)
  146. COMPARE_FUNC(3)
  147. COMPARE_FUNC(4)
  148. COMPARE_FUNC(5)
  149. COMPARE_FUNC(6)
  150. int (*(compare[7]))( const void *a, const void *b ) =
  151. {
  152. compare_axis_0,
  153. compare_axis_1,
  154. compare_axis_2,
  155. compare_axis_3,
  156. compare_axis_4,
  157. compare_axis_5,
  158. compare_axis_6,
  159. };
  160. #define VEC_ELT(f, s, i) (float *)(((char *)f) + s * i)
  161. static int sort_axis( int axis,
  162. int vec_size,
  163. int vec_stride,
  164. struct data_idx *indices,
  165. int start,
  166. int finish,
  167. float *out,
  168. int uniq,
  169. const float fudge )
  170. {
  171. int i;
  172. if (finish-start > 2)
  173. {
  174. qsort( indices+start, finish-start, sizeof(*indices), compare[axis] );
  175. }
  176. else if (indices[start].data[axis] > indices[start+1].data[axis])
  177. {
  178. struct data_idx tmp = indices[start];
  179. indices[start] = indices[start+1];
  180. indices[start+1] = tmp;
  181. }
  182. if (axis == vec_size-1) {
  183. for (i = start ; i < finish ; ) {
  184. float max = indices[i].data[axis] + fudge;
  185. float *dest = VEC_ELT(out, vec_stride, uniq);
  186. int j;
  187. for (j = 0 ; j < vec_size ; j++)
  188. dest[j] = indices[i].data[j];
  189. for ( ; i < finish && max >= indices[i].data[axis]; i++)
  190. indices[i].uniq_idx = uniq;
  191. uniq++;
  192. }
  193. } else {
  194. for (i = start ; i < finish ; ) {
  195. int j = i + 1;
  196. float max = indices[i].data[axis] + fudge;
  197. while (j < finish && max >= indices[j].data[axis]) j++;
  198. if (j == i+1) {
  199. float *dest = VEC_ELT(out, vec_stride, uniq);
  200. int k;
  201. indices[i].uniq_idx = uniq;
  202. for (k = 0 ; k < vec_size ; k++)
  203. dest[k] = indices[i].data[k];
  204. uniq++;
  205. } else {
  206. uniq = sort_axis( axis+1, vec_size, vec_stride,
  207. indices, i, j, out, uniq, fudge );
  208. }
  209. i = j;
  210. }
  211. }
  212. return uniq;
  213. }
  214. static void extract_indices1( const struct data_idx *in, unsigned int *out,
  215. int n )
  216. {
  217. int i;
  218. for ( i = 0 ; i < n ; i++ ) {
  219. out[in[i].idx] = in[i].uniq_idx;
  220. }
  221. }
  222. static void compactify_arrays(void)
  223. {
  224. int i;
  225. struct data_idx *ind;
  226. ind = (struct data_idx *) malloc( sizeof(struct data_idx) * numverts );
  227. for (i = 0 ; i < numverts ; i++) {
  228. ind[i].idx = i;
  229. ind[i].data = data[i];
  230. }
  231. numuniq = sort_axis(0,
  232. sizeof(compressed_data[0])/sizeof(float),
  233. sizeof(compressed_data[0]),
  234. ind,
  235. 0,
  236. numverts,
  237. (float *)compressed_data,
  238. 0,
  239. 1e-6);
  240. printf("Nr unique vertex/normal pairs: %d\n", numuniq);
  241. extract_indices1( ind, indices, numverts );
  242. free( ind );
  243. }
  244. static float myrand( float max )
  245. {
  246. return max*rand()/(RAND_MAX+1.0);
  247. }
  248. static void make_tri_indices( void )
  249. {
  250. unsigned int *v = tri_indices;
  251. unsigned int parity = 0;
  252. unsigned int i, j;
  253. for (j=2;j<numverts;j++,parity^=1) {
  254. if (parity) {
  255. *v++ = indices[j-1];
  256. *v++ = indices[j-2];
  257. *v++ = indices[j];
  258. } else {
  259. *v++ = indices[j-2];
  260. *v++ = indices[j-1];
  261. *v++ = indices[j];
  262. }
  263. }
  264. num_tri_verts = v - tri_indices;
  265. printf("num_tri_verts: %d\n", num_tri_verts);
  266. for (i = j = 0 ; i < num_tri_verts ; i += 600, j++) {
  267. col[j][3] = 1;
  268. col[j][2] = myrand(1);
  269. col[j][1] = myrand(1);
  270. col[j][0] = myrand(1);
  271. }
  272. }
  273. #define MIN(x,y) (x < y) ? x : y
  274. static void draw_surface( int with_state )
  275. {
  276. GLuint i, j;
  277. switch (with_state & (COMPILED_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK)) {
  278. #ifdef GL_EXT_vertex_array
  279. case (COMPILED|DRAW_ARRAYS|STRIPS):
  280. glDrawElements( GL_TRIANGLE_STRIP, numverts, GL_UNSIGNED_INT, indices );
  281. break;
  282. case (COMPILED|ARRAY_ELT|STRIPS):
  283. glBegin( GL_TRIANGLE_STRIP );
  284. for (i = 0 ; i < numverts ; i++)
  285. glArrayElement( indices[i] );
  286. glEnd();
  287. break;
  288. case (COMPILED|DRAW_ARRAYS|TRIANGLES):
  289. case (IMMEDIATE|DRAW_ARRAYS|TRIANGLES):
  290. if (with_state & MATERIALS) {
  291. for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
  292. GLuint nr = MIN(num_tri_verts-i, 600);
  293. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
  294. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
  295. glDrawElements( GL_TRIANGLES, nr, GL_UNSIGNED_INT, tri_indices+i );
  296. }
  297. } else {
  298. glDrawElements( GL_TRIANGLES, num_tri_verts, GL_UNSIGNED_INT,
  299. tri_indices );
  300. }
  301. break;
  302. /* Uses the original arrays (including duplicate elements):
  303. */
  304. case (IMMEDIATE|DRAW_ARRAYS|STRIPS):
  305. glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
  306. break;
  307. /* Uses the original arrays (including duplicate elements):
  308. */
  309. case (IMMEDIATE|ARRAY_ELT|STRIPS):
  310. glBegin( GL_TRIANGLE_STRIP );
  311. for (i = 0 ; i < numverts ; i++)
  312. glArrayElement( i );
  313. glEnd();
  314. break;
  315. case (IMMEDIATE|ARRAY_ELT|TRIANGLES):
  316. case (COMPILED|ARRAY_ELT|TRIANGLES):
  317. if (with_state & MATERIALS) {
  318. for (j = i = 0 ; i < num_tri_verts ; i += 600, j++) {
  319. GLuint nr = MIN(num_tri_verts-i, 600);
  320. GLuint k;
  321. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
  322. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
  323. glBegin( GL_TRIANGLES );
  324. for (k = 0 ; k < nr ; k++)
  325. glArrayElement( tri_indices[i+k] );
  326. glEnd();
  327. }
  328. } else {
  329. glBegin( GL_TRIANGLES );
  330. for (i = 0 ; i < num_tri_verts ; i++)
  331. glArrayElement( tri_indices[i] );
  332. glEnd();
  333. }
  334. break;
  335. case (IMMEDIATE|GLVERTEX|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. GLuint k;
  340. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col[j]);
  341. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col[j]);
  342. glBegin( GL_TRIANGLES );
  343. for (k = 0 ; k < nr ; k++) {
  344. glNormal3fv( &compressed_data[tri_indices[i+k]][3] );
  345. glVertex3fv( &compressed_data[tri_indices[i+k]][0] );
  346. }
  347. glEnd();
  348. }
  349. } else {
  350. glBegin( GL_TRIANGLES );
  351. for (i = 0 ; i < num_tri_verts ; i++) {
  352. glNormal3fv( &compressed_data[tri_indices[i]][3] );
  353. glVertex3fv( &compressed_data[tri_indices[i]][0] );
  354. }
  355. glEnd();
  356. }
  357. break;
  358. case (DISPLAYLIST|GLVERTEX|STRIPS):
  359. if (!surf1)
  360. surf1 = BuildList( GL_COMPILE_AND_EXECUTE );
  361. else
  362. glCallList(surf1);
  363. break;
  364. #endif
  365. /* Uses the original arrays (including duplicate elements):
  366. */
  367. default:
  368. glBegin( GL_TRIANGLE_STRIP );
  369. for (i=0;i<numverts;i++) {
  370. glNormal3fv( &data[i][3] );
  371. glVertex3fv( &data[i][0] );
  372. }
  373. glEnd();
  374. }
  375. }
  376. static void Display(void)
  377. {
  378. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  379. draw_surface( state );
  380. glFlush();
  381. if (doubleBuffer) glutSwapBuffers();
  382. }
  383. int BuildList( int mode )
  384. {
  385. int rv = glGenLists(1);
  386. glNewList(rv, mode );
  387. draw_surface( IMMEDIATE|GLVERTEX|STRIPS );
  388. glEndList();
  389. return rv;
  390. }
  391. /* KW: only do this when necessary, so CVA can re-use results.
  392. */
  393. static void set_matrix( void )
  394. {
  395. glMatrixMode(GL_MODELVIEW);
  396. glLoadIdentity();
  397. glTranslatef( 0.0, 0.0, dist );
  398. glRotatef( yrot, 0.0, 1.0, 0.0 );
  399. glRotatef( xrot, 1.0, 0.0, 0.0 );
  400. }
  401. static void Benchmark( float xdiff, float ydiff )
  402. {
  403. int startTime, endTime;
  404. int draws;
  405. double seconds, fps, triPerSecond;
  406. printf("Benchmarking...\n");
  407. draws = 0;
  408. startTime = glutGet(GLUT_ELAPSED_TIME);
  409. xrot = 0.0;
  410. do {
  411. xrot += xdiff;
  412. yrot += ydiff;
  413. set_matrix();
  414. Display();
  415. draws++;
  416. endTime = glutGet(GLUT_ELAPSED_TIME);
  417. } while (endTime - startTime < 5000); /* 5 seconds */
  418. /* Results */
  419. seconds = (double) (endTime - startTime) / 1000.0;
  420. triPerSecond = (numverts - 2) * draws / seconds;
  421. fps = draws / seconds;
  422. printf("Result: triangles/sec: %g fps: %g\n", triPerSecond, fps);
  423. }
  424. static void InitMaterials(void)
  425. {
  426. static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  427. static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
  428. static float position0[] = {0.0, 0.0, 20.0, 0.0};
  429. static float position1[] = {0.0, 0.0, -20.0, 0.0};
  430. static float front_mat_shininess[] = {60.0};
  431. static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
  432. static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
  433. /*
  434. static float back_mat_shininess[] = {60.0};
  435. static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
  436. static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  437. */
  438. static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  439. static float lmodel_twoside[] = {GL_FALSE};
  440. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  441. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  442. glLightfv(GL_LIGHT0, GL_POSITION, position0);
  443. glEnable(GL_LIGHT0);
  444. glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
  445. glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
  446. glLightfv(GL_LIGHT1, GL_POSITION, position1);
  447. glEnable(GL_LIGHT1);
  448. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  449. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  450. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
  451. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
  452. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
  453. }
  454. #define UPDATE(o,n,mask) (o&=~mask, o|=n&mask)
  455. #define CHANGED(o,n,mask) ((n&mask) && \
  456. (n&mask) != (o&mask) ? UPDATE(o,n,mask) : 0)
  457. static void ModeMenu(int m)
  458. {
  459. m &= allowed;
  460. if (!m) return;
  461. if (m==QUIT)
  462. exit(0);
  463. if (CHANGED(state, m, FILTER_MASK)) {
  464. if (m & LINEAR_FILTER) {
  465. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  466. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  467. } else {
  468. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  469. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  470. }
  471. }
  472. if (CHANGED(state, m, LIGHT_MASK)) {
  473. if (m & LIT)
  474. glEnable(GL_LIGHTING);
  475. else
  476. glDisable(GL_LIGHTING);
  477. }
  478. if (CHANGED(state, m, SHADE_MASK)) {
  479. if (m & SHADE_SMOOTH)
  480. glShadeModel(GL_SMOOTH);
  481. else
  482. glShadeModel(GL_FLAT);
  483. }
  484. if (CHANGED(state, m, TEXTURE_MASK)) {
  485. if (m & TEXTURE)
  486. glEnable(GL_TEXTURE_2D);
  487. else
  488. glDisable(GL_TEXTURE_2D);
  489. }
  490. if (CHANGED(state, m, REFLECT_MASK)) {
  491. if (m & REFLECT) {
  492. glEnable(GL_TEXTURE_GEN_S);
  493. glEnable(GL_TEXTURE_GEN_T);
  494. } else {
  495. glDisable(GL_TEXTURE_GEN_S);
  496. glDisable(GL_TEXTURE_GEN_T);
  497. }
  498. }
  499. if (CHANGED(state, m, CLIP_MASK)) {
  500. if (m & USER_CLIP) {
  501. glEnable(GL_CLIP_PLANE0);
  502. } else {
  503. glDisable(GL_CLIP_PLANE0);
  504. }
  505. }
  506. if (CHANGED(state, m, FOG_MASK)) {
  507. if (m & FOG)
  508. {
  509. glEnable(GL_FOG);
  510. printf("FOG enable\n");
  511. }
  512. else
  513. {
  514. glDisable(GL_FOG);
  515. printf("FOG disable\n");
  516. }
  517. }
  518. #ifdef GL_EXT_vertex_array
  519. if (CHANGED(state, m, (COMPILED_MASK|RENDER_STYLE_MASK|PRIMITIVE_MASK)))
  520. {
  521. if ((m & (COMPILED_MASK|PRIMITIVE_MASK)) == (IMMEDIATE|STRIPS))
  522. {
  523. glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numverts, data );
  524. glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numverts, &data[0][3]);
  525. }
  526. else
  527. {
  528. glVertexPointerEXT( 3, GL_FLOAT, sizeof(data[0]), numuniq,
  529. compressed_data );
  530. glNormalPointerEXT( GL_FLOAT, sizeof(data[0]), numuniq,
  531. &compressed_data[0][3]);
  532. }
  533. #ifdef GL_EXT_compiled_vertex_array
  534. if (m & COMPILED) {
  535. glLockArraysEXT( 0, numuniq );
  536. } else {
  537. glUnlockArraysEXT();
  538. }
  539. #endif
  540. }
  541. #endif
  542. if (m & (RENDER_STYLE_MASK|PRIMITIVE_MASK)) {
  543. UPDATE(state, m, (RENDER_STYLE_MASK|PRIMITIVE_MASK));
  544. }
  545. if (m & MATERIAL_MASK) {
  546. UPDATE(state, m, MATERIAL_MASK);
  547. }
  548. glutPostRedisplay();
  549. }
  550. static void Init(int argc, char *argv[])
  551. {
  552. GLfloat fogColor[4] = {0.5,1.0,0.5,1.0};
  553. glClearColor(0.0, 0.0, 0.0, 0.0);
  554. glEnable( GL_DEPTH_TEST );
  555. glEnable( GL_VERTEX_ARRAY_EXT );
  556. glEnable( GL_NORMAL_ARRAY_EXT );
  557. InitMaterials();
  558. glMatrixMode(GL_PROJECTION);
  559. glLoadIdentity();
  560. glFrustum( -1.0, 1.0, -1.0, 1.0, 5, 25 );
  561. glMatrixMode(GL_MODELVIEW);
  562. glLoadIdentity();
  563. glClipPlane(GL_CLIP_PLANE0, plane);
  564. set_matrix();
  565. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  566. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  567. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  568. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  569. if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  570. printf("Error: couldn't load texture image\n");
  571. exit(1);
  572. }
  573. /* Green fog is easy to see */
  574. glFogi(GL_FOG_MODE,GL_EXP2);
  575. glFogfv(GL_FOG_COLOR,fogColor);
  576. glFogf(GL_FOG_DENSITY,0.15);
  577. glHint(GL_FOG_HINT,GL_DONT_CARE);
  578. compactify_arrays();
  579. make_tri_indices();
  580. surf1 = BuildList( GL_COMPILE );
  581. ModeMenu(SHADE_SMOOTH|
  582. LIT|
  583. NO_TEXTURE|
  584. NO_REFLECT|
  585. POINT_FILTER|
  586. IMMEDIATE|
  587. NO_USER_CLIP|
  588. NO_MATERIALS|
  589. NO_FOG|
  590. GLVERTEX);
  591. if (PrintInfo) {
  592. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  593. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  594. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  595. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  596. }
  597. }
  598. static void Reshape(int width, int height)
  599. {
  600. glViewport(0, 0, (GLint)width, (GLint)height);
  601. }
  602. static void Key( unsigned char key, int x, int y )
  603. {
  604. (void) x;
  605. (void) y;
  606. switch (key) {
  607. case 27:
  608. exit(0);
  609. case 'f':
  610. ModeMenu((state ^ FOG_MASK) & FOG_MASK);
  611. break;
  612. case 's':
  613. ModeMenu((state ^ SHADE_MASK) & SHADE_MASK);
  614. break;
  615. case 'l':
  616. ModeMenu((state ^ LIGHT_MASK) & LIGHT_MASK);
  617. break;
  618. case 'm':
  619. ModeMenu((state ^ MATERIAL_MASK) & MATERIAL_MASK);
  620. break;
  621. case 'c':
  622. ModeMenu((state ^ CLIP_MASK) & CLIP_MASK);
  623. break;
  624. case 'b':
  625. Benchmark(5.0, 0);
  626. break;
  627. case 'B':
  628. Benchmark(0, 5.0);
  629. break;
  630. case 'i':
  631. dist += .25;
  632. set_matrix();
  633. glutPostRedisplay();
  634. break;
  635. case 'I':
  636. dist -= .25;
  637. set_matrix();
  638. glutPostRedisplay();
  639. break;
  640. case '-':
  641. case '_':
  642. plane[3] += 2.0;
  643. glMatrixMode(GL_MODELVIEW);
  644. glLoadIdentity();
  645. glClipPlane(GL_CLIP_PLANE0, plane);
  646. set_matrix();
  647. glutPostRedisplay();
  648. break;
  649. case '+':
  650. case '=':
  651. plane[3] -= 2.0;
  652. glMatrixMode(GL_MODELVIEW);
  653. glLoadIdentity();
  654. glClipPlane(GL_CLIP_PLANE0, plane);
  655. set_matrix();
  656. glutPostRedisplay();
  657. break;
  658. }
  659. }
  660. static void SpecialKey( int key, int x, int y )
  661. {
  662. (void) x;
  663. (void) y;
  664. switch (key) {
  665. case GLUT_KEY_LEFT:
  666. yrot -= 15.0;
  667. break;
  668. case GLUT_KEY_RIGHT:
  669. yrot += 15.0;
  670. break;
  671. case GLUT_KEY_UP:
  672. xrot += 15.0;
  673. break;
  674. case GLUT_KEY_DOWN:
  675. xrot -= 15.0;
  676. break;
  677. default:
  678. return;
  679. }
  680. set_matrix();
  681. glutPostRedisplay();
  682. }
  683. static GLint Args(int argc, char **argv)
  684. {
  685. GLint i;
  686. GLint mode = 0;
  687. for (i = 1; i < argc; i++) {
  688. if (strcmp(argv[i], "-sb") == 0) {
  689. doubleBuffer = GL_FALSE;
  690. }
  691. else if (strcmp(argv[i], "-db") == 0) {
  692. doubleBuffer = GL_TRUE;
  693. }
  694. else if (strcmp(argv[i], "-info") == 0) {
  695. PrintInfo = GL_TRUE;
  696. }
  697. else {
  698. printf("%s (Bad option).\n", argv[i]);
  699. return QUIT;
  700. }
  701. }
  702. return mode;
  703. }
  704. int main(int argc, char **argv)
  705. {
  706. GLenum type;
  707. char *extensions;
  708. GLuint arg_mode = Args(argc, argv);
  709. if (arg_mode & QUIT)
  710. exit(0);
  711. read_surface( "isosurf.dat" );
  712. glutInitWindowPosition(0, 0);
  713. glutInitWindowSize(400, 400);
  714. type = GLUT_DEPTH;
  715. type |= GLUT_RGB;
  716. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  717. glutInitDisplayMode(type);
  718. if (glutCreateWindow("Isosurface") <= 0) {
  719. exit(0);
  720. }
  721. /* Make sure server supports the vertex array extension */
  722. extensions = (char *) glGetString( GL_EXTENSIONS );
  723. if (!strstr( extensions, "GL_EXT_vertex_array" ))
  724. {
  725. printf("Vertex arrays not supported by this renderer\n");
  726. allowed &= ~(COMPILED|DRAW_ARRAYS|ARRAY_ELT);
  727. }
  728. else if (!strstr( extensions, "GL_EXT_compiled_vertex_array" ))
  729. {
  730. printf("Compiled vertex arrays not supported by this renderer\n");
  731. allowed &= ~COMPILED;
  732. }
  733. Init(argc, argv);
  734. ModeMenu(arg_mode);
  735. glutCreateMenu(ModeMenu);
  736. glutAddMenuEntry("Lit", LIT|NO_TEXTURE|NO_REFLECT);
  737. glutAddMenuEntry("Unlit", UNLIT|NO_TEXTURE|NO_REFLECT);
  738. /* glutAddMenuEntry("Textured", TEXTURE); */
  739. glutAddMenuEntry("Reflect", TEXTURE|REFLECT);
  740. glutAddMenuEntry("", 0);
  741. glutAddMenuEntry("Smooth", SHADE_SMOOTH);
  742. glutAddMenuEntry("Flat", SHADE_FLAT);
  743. glutAddMenuEntry("", 0);
  744. glutAddMenuEntry("Fog", FOG);
  745. glutAddMenuEntry("No Fog", NO_FOG);
  746. glutAddMenuEntry("", 0);
  747. glutAddMenuEntry("Point Filtered", POINT_FILTER);
  748. glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
  749. glutAddMenuEntry("", 0);
  750. glutAddMenuEntry("glVertex (STRIPS)", IMMEDIATE|GLVERTEX|STRIPS);
  751. glutAddMenuEntry("glVertex (TRIANGLES)", IMMEDIATE|GLVERTEX|TRIANGLES);
  752. glutAddMenuEntry("", 0);
  753. glutAddMenuEntry("glVertex display list (STRIPS)",
  754. DISPLAYLIST|GLVERTEX|STRIPS);
  755. glutAddMenuEntry("", 0);
  756. if (allowed & DRAW_ARRAYS) {
  757. glutAddMenuEntry("DrawArrays (STRIPS)",
  758. IMMEDIATE|DRAW_ARRAYS|STRIPS);
  759. glutAddMenuEntry("ArrayElement (STRIPS)",
  760. IMMEDIATE|ARRAY_ELT|STRIPS);
  761. glutAddMenuEntry("DrawElements (TRIANGLES)",
  762. IMMEDIATE|DRAW_ARRAYS|TRIANGLES);
  763. glutAddMenuEntry("ArrayElement (TRIANGLES)",
  764. IMMEDIATE|ARRAY_ELT|TRIANGLES);
  765. glutAddMenuEntry("", 0);
  766. }
  767. if (allowed & COMPILED) {
  768. glutAddMenuEntry("Compiled DrawElements (TRIANGLES)",
  769. COMPILED|DRAW_ARRAYS|TRIANGLES);
  770. glutAddMenuEntry("Compiled DrawElements (STRIPS)",
  771. COMPILED|DRAW_ARRAYS|STRIPS);
  772. glutAddMenuEntry("Compiled ArrayElement (TRIANGLES)",
  773. COMPILED|ARRAY_ELT|TRIANGLES);
  774. glutAddMenuEntry("Compiled ArrayElement (STRIPS)",
  775. COMPILED|ARRAY_ELT|STRIPS);
  776. glutAddMenuEntry("", 0);
  777. }
  778. glutAddMenuEntry("Quit", QUIT);
  779. glutAttachMenu(GLUT_RIGHT_BUTTON);
  780. glutReshapeFunc(Reshape);
  781. glutKeyboardFunc(Key);
  782. glutSpecialFunc(SpecialKey);
  783. glutDisplayFunc(Display);
  784. glutMainLoop();
  785. return 0;
  786. }