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.

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