Clone of mesa.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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