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

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