Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

isosurf.c 25KB

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