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.

stex3d.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /* $Id: stex3d.c,v 1.5 2000/06/27 17:04:43 brianp Exp $ */
  2. /*-----------------------------
  3. * stex3d.c GL example of the mesa 3d-texture extention to simulate procedural
  4. * texturing, it uses a perlin noise and turbulence functions.
  5. *
  6. * Author: Daniel Barrero
  7. * barrero@irit.fr
  8. * dbarrero@pegasus.uniandes.edu.co
  9. *
  10. * Converted to GLUT by brianp on 1/1/98
  11. *
  12. *
  13. * cc stex3d.c -o stex3d -lglut -lMesaGLU -lMesaGL -lX11 -lXext -lm
  14. *
  15. *---------------------------- */
  16. /*
  17. * $Log: stex3d.c,v $
  18. * Revision 1.5 2000/06/27 17:04:43 brianp
  19. * fixed compiler warnings
  20. *
  21. * Revision 1.4 2000/03/22 19:48:57 brianp
  22. * converted from GL_EXT_texture3D to GL 1.2
  23. *
  24. * Revision 1.3 1999/12/16 08:54:22 brianp
  25. * added a cast to malloc call
  26. *
  27. * Revision 1.2 1999/09/17 12:27:01 brianp
  28. * silenced some warnings
  29. *
  30. * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
  31. * Imported sources
  32. *
  33. * Revision 3.1 1998/06/09 01:53:49 brianp
  34. * main() should return an int
  35. *
  36. * Revision 3.0 1998/02/14 18:42:29 brianp
  37. * initial rev
  38. *
  39. */
  40. #include <string.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <math.h>
  44. #include <GL/gl.h>
  45. #include <GL/glut.h>
  46. /* function declarations */
  47. #ifndef M_PI
  48. #define M_PI 3.14159265358979323846
  49. #endif
  50. void init(void),
  51. printHelp(void),
  52. create3Dtexture(void),
  53. setDefaults(void),
  54. drawScene(void),
  55. resize(int w, int h),
  56. buildFigure(void),
  57. initNoise(void);
  58. float turbulence(float point[3], float lofreq, float hifreq);
  59. void KeyHandler( unsigned char key, int x, int y );
  60. GLenum parseCmdLine(int argc, char **argv);
  61. float noise3(float vec[3]);
  62. /* global variables */
  63. GLenum rgb, doubleBuffer, directRender, windType; /* visualization state*/
  64. float tex_width,tex_height,tex_depth; /* texture volume dimensions */
  65. unsigned char *voxels; /* texture data ptr */
  66. int angx,angy,angz;
  67. GLuint figure;
  68. /*function definitions */
  69. int main(int argc, char **argv)
  70. {
  71. if (parseCmdLine(argc, argv) == GL_FALSE) {
  72. exit(0);
  73. }
  74. glutInitWindowPosition(0, 0);
  75. glutInitWindowSize(400, 400);
  76. windType = (rgb) ? GLUT_RGB : GLUT_INDEX;
  77. windType |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  78. windType |= GLUT_DEPTH;
  79. glutInitDisplayMode(windType);
  80. if (glutCreateWindow("stex3d") <= 0) {
  81. exit(0);
  82. }
  83. /* init all */
  84. init();
  85. glutReshapeFunc(resize);
  86. glutKeyboardFunc(KeyHandler);
  87. glutDisplayFunc(drawScene);
  88. glutMainLoop();
  89. return 0;
  90. }
  91. void init()
  92. {
  93. /* init light */
  94. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  95. GLfloat mat_shininess[] = { 25.0 };
  96. GLfloat gray[] = { 0.6, 0.6, 0.6, 0.0 };
  97. GLfloat white[] = { 1.0, 1.0, 1.0, 0.0 };
  98. GLfloat light_position[] = { 0.0, 1.0, 1.0, 0.0 };
  99. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  100. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  101. glLightfv(GL_LIGHT1, GL_POSITION, light_position);
  102. glLightfv(GL_LIGHT1, GL_AMBIENT, gray);
  103. glLightfv(GL_LIGHT1, GL_DIFFUSE, white);
  104. glLightfv(GL_LIGHT1, GL_SPECULAR, white);
  105. glColorMaterial(GL_FRONT, GL_DIFFUSE);
  106. glEnable(GL_COLOR_MATERIAL);
  107. glEnable(GL_LIGHTING);
  108. glEnable(GL_LIGHT1);
  109. /* create torus for texturing */
  110. figure=glGenLists(1);
  111. buildFigure();
  112. /* tkSolidTorus(figure,0.3,1.2);*/
  113. /* start the noise function variables */
  114. initNoise();
  115. /* see if we have OpenGL 1.2 or later, for 3D texturing */
  116. {
  117. const char *version = (const char *) glGetString(GL_VERSION);
  118. if (strncmp(version, "1.0", 3) == 0 ||
  119. strncmp(version, "1.1", 3) == 0) {
  120. printf("Sorry, OpenGL 1.2 or later is required\n");
  121. exit(1);
  122. }
  123. }
  124. /* if texture is supported then generate the texture */
  125. create3Dtexture();
  126. glEnable(GL_TEXTURE_3D);
  127. /*
  128. glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA);
  129. glEnable(GL_BLEND);
  130. */
  131. glEnable(GL_DEPTH_TEST);
  132. glShadeModel(GL_FLAT);
  133. glColor3f(0.6,0.7,0.8);
  134. }
  135. void buildFigure(void)
  136. { GLint i, j;
  137. float theta1, phi1, theta2, phi2, rings, sides;
  138. float v0[03], v1[3], v2[3], v3[3];
  139. float t0[03], t1[3], t2[3], t3[3];
  140. float n0[3], n1[3], n2[3], n3[3];
  141. float innerRadius=0.4;
  142. float outerRadius=0.8;
  143. float scalFac;
  144. rings = 8;
  145. sides = 10;
  146. scalFac=1/(outerRadius*2);
  147. glNewList(figure, GL_COMPILE);
  148. for (i = 0; i < rings; i++) {
  149. theta1 = (float)i * 2.0 * M_PI / rings;
  150. theta2 = (float)(i + 1) * 2.0 * M_PI / rings;
  151. for (j = 0; j < sides; j++) {
  152. phi1 = (float)j * 2.0 * M_PI / sides;
  153. phi2 = (float)(j + 1) * 2.0 * M_PI / sides;
  154. v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
  155. v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
  156. v0[2] = innerRadius * sin(phi1);
  157. v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
  158. v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
  159. v1[2] = innerRadius * sin(phi1);
  160. v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
  161. v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
  162. v2[2] = innerRadius * sin(phi2);
  163. v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
  164. v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
  165. v3[2] = innerRadius * sin(phi2);
  166. n0[0] = cos(theta1) * (cos(phi1));
  167. n0[1] = -sin(theta1) * (cos(phi1));
  168. n0[2] = sin(phi1);
  169. n1[0] = cos(theta2) * (cos(phi1));
  170. n1[1] = -sin(theta2) * (cos(phi1));
  171. n1[2] = sin(phi1);
  172. n2[0] = cos(theta2) * (cos(phi2));
  173. n2[1] = -sin(theta2) * (cos(phi2));
  174. n2[2] = sin(phi2);
  175. n3[0] = cos(theta1) * (cos(phi2));
  176. n3[1] = -sin(theta1) * (cos(phi2));
  177. n3[2] = sin(phi2);
  178. t0[0] = v0[0]*scalFac + 0.5;
  179. t0[1] = v0[1]*scalFac + 0.5;
  180. t0[2] = v0[2]*scalFac + 0.5;
  181. t1[0] = v1[0]*scalFac + 0.5;
  182. t1[1] = v1[1]*scalFac + 0.5;
  183. t1[2] = v1[2]*scalFac + 0.5;
  184. t2[0] = v2[0]*scalFac + 0.5;
  185. t2[1] = v2[1]*scalFac + 0.5;
  186. t2[2] = v2[2]*scalFac + 0.5;
  187. t3[0] = v3[0]*scalFac + 0.5;
  188. t3[1] = v3[1]*scalFac + 0.5;
  189. t3[2] = v3[2]*scalFac + 0.5;
  190. glBegin(GL_POLYGON);
  191. glNormal3fv(n3); glTexCoord3fv(t3); glVertex3fv(v3);
  192. glNormal3fv(n2); glTexCoord3fv(t2); glVertex3fv(v2);
  193. glNormal3fv(n1); glTexCoord3fv(t1); glVertex3fv(v1);
  194. glNormal3fv(n0); glTexCoord3fv(t0); glVertex3fv(v0);
  195. glEnd();
  196. }
  197. }
  198. glEndList();
  199. }
  200. void create3Dtexture()
  201. {
  202. int i,j,k;
  203. unsigned char *vp;
  204. float vec[3];
  205. int tmp;
  206. printf("creating 3d textures...\n");
  207. voxels = (unsigned char *) malloc((size_t)(4*tex_width*tex_height*tex_depth));
  208. vp=voxels;
  209. for (i=0;i<tex_width;i++){
  210. vec[0]=i;
  211. for (j=0;j<tex_height;j++) {
  212. vec[1]=j;
  213. for (k=0;k<tex_depth;k++) {
  214. vec[2]=k;
  215. tmp=(sin(k*i*j+turbulence(vec,0.01,1))+1)*127.5;
  216. *vp++=0;
  217. *vp++=0;
  218. *vp++=tmp;
  219. *vp++=tmp+128;
  220. }
  221. }
  222. }
  223. printf("setting up 3d texture...\n");
  224. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  225. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  226. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  227. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  228. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  229. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
  230. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  231. glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
  232. tex_width, tex_height, tex_depth,
  233. 0, GL_RGBA, GL_UNSIGNED_BYTE, voxels);
  234. printf("finished setting up 3d texture image...\n");
  235. }
  236. void printHelp()
  237. {
  238. printf("\nUsage: stex3d <cmd line options>\n");
  239. printf(" cmd line options:\n");
  240. printf(" -help print this help!\n");
  241. printf(" -rgb RGBA mode. (Default)\n");
  242. printf(" -ci Color index mode.\n");
  243. printf(" -sb Single buffer mode. (Default)\n");
  244. printf(" -db Double buffer mode. \n");
  245. printf(" -dr Direct render mode.\n");
  246. printf(" -ir Indirect render mode. (Default)\n");
  247. printf(" -wxxx Width of the texture (Default=64)\n");
  248. printf(" -hxxx Height of the texture (Default=64)\n");
  249. printf(" -dxxx Depth of the texture (Default=64)\n");
  250. printf(" Keyboard Options:\n");
  251. printf(" 1 Object Texture coordinates (Default)\n");
  252. printf(" 2 Eye Texture coordinates \n");
  253. printf(" x rotate around x clockwise\n");
  254. printf(" X rotate around x counter clockwise\n");
  255. printf(" y rotate around y clockwise\n");
  256. printf(" Y rotate around y counter clockwise\n");
  257. printf(" z rotate around z clockwise\n");
  258. printf(" Z rotate around z counter clockwise\n");
  259. printf(" t enable 3-D texuring (Default)\n");
  260. printf(" T disable 3-D texuring\n");
  261. printf(" s smooth shading \n");
  262. printf(" S flat shading (Default)\n");
  263. }
  264. void setDefaults()
  265. {
  266. /* visualization defaults */
  267. rgb = GL_TRUE;
  268. doubleBuffer = GL_FALSE;
  269. directRender = GL_TRUE;
  270. angx=130;
  271. angy=30;
  272. angz=0;
  273. /* texture values */
  274. tex_width=64;
  275. tex_height=64;
  276. tex_depth=64;
  277. }
  278. GLenum parseCmdLine(int argc, char **argv)
  279. {
  280. GLint i;
  281. setDefaults();
  282. for (i = 1; i < argc; i++) {
  283. if (strcmp(argv[i], "-ci") == 0) {
  284. rgb = GL_FALSE;
  285. } else if (strcmp(argv[i], "-rgb") == 0) {
  286. rgb = GL_TRUE;
  287. } else if (strcmp(argv[i], "-sb") == 0) {
  288. doubleBuffer = GL_FALSE;
  289. } else if (strcmp(argv[i], "-db") == 0) {
  290. doubleBuffer = GL_TRUE;
  291. } else if (strcmp(argv[i], "-dr") == 0) {
  292. directRender = GL_TRUE;
  293. } else if (strcmp(argv[i], "-ir") == 0) {
  294. directRender = GL_FALSE;
  295. } else if (strstr(argv[i], "-w") == 0) {
  296. tex_width=atoi((argv[i])+2);
  297. } else if (strstr(argv[i], "-h") == 0) {
  298. tex_height=atoi((argv[i])+2);
  299. } else if (strstr(argv[i], "-d") == 0) {
  300. tex_depth=atoi((argv[i])+2);
  301. } else if (strcmp(argv[i], "-help") == 0) {
  302. printHelp();
  303. return GL_FALSE;
  304. } else {
  305. printf("%s (Bad option).\n", argv[i]);
  306. printHelp();
  307. return GL_FALSE;
  308. }
  309. }
  310. if(tex_width==0 || tex_height==0 || tex_depth==0) {
  311. printf("%s (Bad option).\n", "size parameters can't be 0");
  312. printHelp();
  313. return GL_FALSE;
  314. }
  315. return GL_TRUE;
  316. }
  317. void drawScene()
  318. {
  319. /* clear background, z buffer etc */
  320. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  321. glPushMatrix();
  322. glRotatef(angx,1.0,0.0,0.0);
  323. glRotatef(angy,0.0,1.0,0.0);
  324. glRotatef(angz,0.0,0.0,1.0);
  325. glCallList(figure);
  326. glPopMatrix();
  327. glFlush();
  328. if(doubleBuffer)
  329. glutSwapBuffers();
  330. ;
  331. }
  332. void resize(int w, int h)
  333. {
  334. glViewport(0, 0, (GLint)w, (GLint)h);
  335. glMatrixMode(GL_PROJECTION);
  336. glLoadIdentity();
  337. glOrtho(-2,2,-2,2,-5,10);
  338. glMatrixMode(GL_MODELVIEW);
  339. glLoadIdentity();
  340. glTranslatef(0,0,-5);
  341. }
  342. static void cleanEverything(void)
  343. {
  344. /* free(voxels); */
  345. }
  346. void KeyHandler( unsigned char key, int x, int y )
  347. {
  348. (void) x;
  349. (void) y;
  350. switch(key) {
  351. case 27:
  352. case 'q':
  353. case 'Q': /* quit game. */
  354. cleanEverything();
  355. exit(0);
  356. break;
  357. case 'x':
  358. angx+=10;
  359. break;
  360. case 'X':
  361. angx-=10;
  362. break;
  363. case 'y':
  364. angy+=10;
  365. break;
  366. case 'Y':
  367. angy-=10;
  368. break;
  369. case 'z':
  370. angz+=10;
  371. break;
  372. case 'Z':
  373. angz-=10;
  374. break;
  375. case 't':
  376. glEnable(GL_TEXTURE_3D);
  377. break;
  378. case 'T':
  379. glDisable(GL_TEXTURE_3D);
  380. break;
  381. case 's':
  382. glShadeModel(GL_SMOOTH);
  383. break;
  384. case 'S':
  385. glShadeModel(GL_FLAT);
  386. break;
  387. case '1':
  388. glDisable(GL_TEXTURE_GEN_S);
  389. glDisable(GL_TEXTURE_GEN_T);
  390. glDisable(GL_TEXTURE_GEN_R);
  391. break;
  392. case '2':
  393. glEnable(GL_TEXTURE_GEN_S);
  394. glEnable(GL_TEXTURE_GEN_T);
  395. glEnable(GL_TEXTURE_GEN_R);
  396. break;
  397. default:
  398. break;
  399. }
  400. glutPostRedisplay();
  401. }
  402. /*--------------------------------------------------------------------
  403. noise function over R3 - implemented by a pseudorandom tricubic spline
  404. EXCERPTED FROM SIGGRAPH 92, COURSE 23
  405. PROCEDURAL MODELING
  406. Ken Perlin
  407. New York University
  408. ----------------------------------------------------------------------*/
  409. #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
  410. #define B 256
  411. static int p[B + B + 2];
  412. static float g[B + B + 2][3];
  413. #define setup(i,b0,b1,r0,r1) \
  414. t = vec[i] + 10000.; \
  415. b0 = ((int)t) & (B-1); \
  416. b1 = (b0+1) & (B-1); \
  417. r0 = t - (int)t; \
  418. r1 = r0 - 1.;
  419. float noise3(float vec[3])
  420. {
  421. int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
  422. float rx0, rx1, ry0, ry1, rz0, rz1, *q, sx, sy, sz, a, b, c, d, t, u, v;
  423. register int i, j;
  424. setup(0, bx0,bx1, rx0,rx1);
  425. setup(1, by0,by1, ry0,ry1);
  426. setup(2, bz0,bz1, rz0,rz1);
  427. i = p[ bx0 ];
  428. j = p[ bx1 ];
  429. b00 = p[ i + by0 ];
  430. b10 = p[ j + by0 ];
  431. b01 = p[ i + by1 ];
  432. b11 = p[ j + by1 ];
  433. #define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
  434. #define surve(t) ( t * t * (3. - 2. * t) )
  435. #define lerp(t, a, b) ( a + t * (b - a) )
  436. sx = surve(rx0);
  437. sy = surve(ry0);
  438. sz = surve(rz0);
  439. q = g[ b00 + bz0 ] ; u = at(rx0,ry0,rz0);
  440. q = g[ b10 + bz0 ] ; v = at(rx1,ry0,rz0);
  441. a = lerp(sx, u, v);
  442. q = g[ b01 + bz0 ] ; u = at(rx0,ry1,rz0);
  443. q = g[ b11 + bz0 ] ; v = at(rx1,ry1,rz0);
  444. b = lerp(sx, u, v);
  445. c = lerp(sy, a, b); /* interpolate in y at lo x */
  446. q = g[ b00 + bz1 ] ; u = at(rx0,ry0,rz1);
  447. q = g[ b10 + bz1 ] ; v = at(rx1,ry0,rz1);
  448. a = lerp(sx, u, v);
  449. q = g[ b01 + bz1 ] ; u = at(rx0,ry1,rz1);
  450. q = g[ b11 + bz1 ] ; v = at(rx1,ry1,rz1);
  451. b = lerp(sx, u, v);
  452. d = lerp(sy, a, b); /* interpolate in y at hi x */
  453. return 1.5 * lerp(sz, c, d); /* interpolate in z */
  454. }
  455. void initNoise()
  456. {
  457. /*long random();*/
  458. int i, j, k;
  459. float v[3], s;
  460. /* Create an array of random gradient vectors uniformly on the unit sphere */
  461. /*srandom(1);*/
  462. srand(1);
  463. for (i = 0 ; i < B ; i++) {
  464. do { /* Choose uniformly in a cube */ for (j=0 ; j<3 ; j++)
  465. v[j] = (float)((rand() % (B + B)) - B) / B;
  466. s = DOT(v,v);
  467. } while (s > 1.0); /* If not in sphere try again */ s = sqrt(s);
  468. for (j = 0 ; j < 3 ; j++) /* Else normalize */
  469. g[i][j] = v[j] / s;
  470. }
  471. /* Create a pseudorandom permutation of [1..B] */
  472. for (i = 0 ; i < B ; i++)
  473. p[i] = i;
  474. for (i = B ; i > 0 ; i -= 2) {
  475. k = p[i];
  476. p[i] = p[j = rand() % B];
  477. p[j] = k;
  478. }
  479. /* Extend g and p arrays to allow for faster indexing */
  480. for (i = 0 ; i < B + 2 ; i++) {
  481. p[B + i] = p[i];
  482. for (j = 0 ; j < 3 ; j++)
  483. g[B + i][j] = g[i][j];
  484. }
  485. }
  486. float turbulence(float point[3], float lofreq, float hifreq)
  487. {
  488. float freq, t, p[3];
  489. p[0] = point[0] + 123.456;
  490. p[1] = point[1];
  491. p[2] = point[2];
  492. t = 0;
  493. for (freq = lofreq ; freq < hifreq ; freq *= 2.) {
  494. t += fabs(noise3(p)) / freq;
  495. p[0] *= 2.;
  496. p[1] *= 2.;
  497. p[2] *= 2.;
  498. }
  499. return t - 0.3; /* readjust to make mean value = 0.0 */
  500. }