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 15KB

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