Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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