Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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