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

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