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.

stex3d.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /* $Id: stex3d.c,v 1.7 2002/10/23 14:00: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. * Massive clean-up on 2002/10/23 by brianp
  12. *
  13. *
  14. * cc stex3d.c -o stex3d -lglut -lMesaGLU -lMesaGL -lX11 -lXext -lm
  15. *
  16. *---------------------------- */
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include <GL/gl.h>
  22. #include <GL/glut.h>
  23. #ifndef M_PI
  24. #define M_PI 3.14159265358979323846
  25. #endif
  26. #define NOISE_TEXTURE 1
  27. #define GRADIENT_TEXTURE 2
  28. #define TORUS 1
  29. #define SPHERE 2
  30. static int tex_width=64, tex_height=64, tex_depth=64;
  31. static float angx=0, angy=0, angz=0;
  32. static GLuint DList;
  33. static int texgen = 2, animate = 1, smooth = 1;
  34. static int CurTexture = NOISE_TEXTURE, CurObject = TORUS;
  35. static void
  36. BuildTorus(void)
  37. {
  38. GLint i, j;
  39. float theta1, phi1, theta2, phi2, rings, sides;
  40. float v0[03], v1[3], v2[3], v3[3];
  41. float t0[03], t1[3], t2[3], t3[3];
  42. float n0[3], n1[3], n2[3], n3[3];
  43. float innerRadius = 0.25;
  44. float outerRadius = 0.5;
  45. float scalFac;
  46. rings = 16;
  47. sides = 12;
  48. scalFac = 1 / (outerRadius * 2);
  49. glNewList(TORUS, GL_COMPILE);
  50. for (i = 0; i < rings; i++) {
  51. theta1 = (float) i *2.0 * M_PI / rings;
  52. theta2 = (float) (i + 1) * 2.0 * M_PI / rings;
  53. for (j = 0; j < sides; j++) {
  54. phi1 = (float) j *2.0 * M_PI / sides;
  55. phi2 = (float) (j + 1) * 2.0 * M_PI / sides;
  56. v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
  57. v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
  58. v0[2] = innerRadius * sin(phi1);
  59. v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
  60. v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
  61. v1[2] = innerRadius * sin(phi1);
  62. v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
  63. v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
  64. v2[2] = innerRadius * sin(phi2);
  65. v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
  66. v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
  67. v3[2] = innerRadius * sin(phi2);
  68. n0[0] = cos(theta1) * (cos(phi1));
  69. n0[1] = -sin(theta1) * (cos(phi1));
  70. n0[2] = sin(phi1);
  71. n1[0] = cos(theta2) * (cos(phi1));
  72. n1[1] = -sin(theta2) * (cos(phi1));
  73. n1[2] = sin(phi1);
  74. n2[0] = cos(theta2) * (cos(phi2));
  75. n2[1] = -sin(theta2) * (cos(phi2));
  76. n2[2] = sin(phi2);
  77. n3[0] = cos(theta1) * (cos(phi2));
  78. n3[1] = -sin(theta1) * (cos(phi2));
  79. n3[2] = sin(phi2);
  80. t0[0] = v0[0] * scalFac + 0.5;
  81. t0[1] = v0[1] * scalFac + 0.5;
  82. t0[2] = v0[2] * scalFac + 0.5;
  83. t1[0] = v1[0] * scalFac + 0.5;
  84. t1[1] = v1[1] * scalFac + 0.5;
  85. t1[2] = v1[2] * scalFac + 0.5;
  86. t2[0] = v2[0] * scalFac + 0.5;
  87. t2[1] = v2[1] * scalFac + 0.5;
  88. t2[2] = v2[2] * scalFac + 0.5;
  89. t3[0] = v3[0] * scalFac + 0.5;
  90. t3[1] = v3[1] * scalFac + 0.5;
  91. t3[2] = v3[2] * scalFac + 0.5;
  92. glBegin(GL_POLYGON);
  93. glNormal3fv(n3);
  94. glTexCoord3fv(t3);
  95. glVertex3fv(v3);
  96. glNormal3fv(n2);
  97. glTexCoord3fv(t2);
  98. glVertex3fv(v2);
  99. glNormal3fv(n1);
  100. glTexCoord3fv(t1);
  101. glVertex3fv(v1);
  102. glNormal3fv(n0);
  103. glTexCoord3fv(t0);
  104. glVertex3fv(v0);
  105. glEnd();
  106. }
  107. }
  108. glEndList();
  109. }
  110. /*--------------------------------------------------------------------
  111. noise function over R3 - implemented by a pseudorandom tricubic spline
  112. EXCERPTED FROM SIGGRAPH 92, COURSE 23
  113. PROCEDURAL MODELING
  114. Ken Perlin
  115. New York University
  116. ----------------------------------------------------------------------*/
  117. #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
  118. #define B 128
  119. static int p[B + B + 2];
  120. static float g[B + B + 2][3];
  121. #define setup(i,b0,b1,r0,r1) \
  122. t = vec[i] + 10000.; \
  123. b0 = ((int)t) & (B-1); \
  124. b1 = (b0+1) & (B-1); \
  125. r0 = t - (int)t; \
  126. r1 = r0 - 1.;
  127. static float
  128. noise3(float vec[3])
  129. {
  130. int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
  131. float rx0, rx1, ry0, ry1, rz0, rz1, *q, sx, sy, sz, a, b, c, d, t, u, v;
  132. register int i, j;
  133. setup(0, bx0, bx1, rx0, rx1);
  134. setup(1, by0, by1, ry0, ry1);
  135. setup(2, bz0, bz1, rz0, rz1);
  136. i = p[bx0];
  137. j = p[bx1];
  138. b00 = p[i + by0];
  139. b10 = p[j + by0];
  140. b01 = p[i + by1];
  141. b11 = p[j + by1];
  142. #define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
  143. #define surve(t) ( t * t * (3. - 2. * t) )
  144. #define lerp(t, a, b) ( a + t * (b - a) )
  145. sx = surve(rx0);
  146. sy = surve(ry0);
  147. sz = surve(rz0);
  148. q = g[b00 + bz0];
  149. u = at(rx0, ry0, rz0);
  150. q = g[b10 + bz0];
  151. v = at(rx1, ry0, rz0);
  152. a = lerp(sx, u, v);
  153. q = g[b01 + bz0];
  154. u = at(rx0, ry1, rz0);
  155. q = g[b11 + bz0];
  156. v = at(rx1, ry1, rz0);
  157. b = lerp(sx, u, v);
  158. c = lerp(sy, a, b); /* interpolate in y at lo x */
  159. q = g[b00 + bz1];
  160. u = at(rx0, ry0, rz1);
  161. q = g[b10 + bz1];
  162. v = at(rx1, ry0, rz1);
  163. a = lerp(sx, u, v);
  164. q = g[b01 + bz1];
  165. u = at(rx0, ry1, rz1);
  166. q = g[b11 + bz1];
  167. v = at(rx1, ry1, rz1);
  168. b = lerp(sx, u, v);
  169. d = lerp(sy, a, b); /* interpolate in y at hi x */
  170. return 1.5 * lerp(sz, c, d); /* interpolate in z */
  171. }
  172. static void
  173. initNoise(void)
  174. {
  175. /*long random(); */
  176. int i, j, k;
  177. float v[3], s;
  178. /* Create an array of random gradient vectors uniformly on the unit sphere */
  179. /*srandom(1); */
  180. srand(1);
  181. for (i = 0; i < B; i++) {
  182. do { /* Choose uniformly in a cube */
  183. for (j = 0; j < 3; j++)
  184. v[j] = (float) ((rand() % (B + B)) - B) / B;
  185. s = DOT(v, v);
  186. } while (s > 1.0); /* If not in sphere try again */
  187. s = sqrt(s);
  188. for (j = 0; j < 3; j++) /* Else normalize */
  189. g[i][j] = v[j] / s;
  190. }
  191. /* Create a pseudorandom permutation of [1..B] */
  192. for (i = 0; i < B; i++)
  193. p[i] = i;
  194. for (i = B; i > 0; i -= 2) {
  195. k = p[i];
  196. p[i] = p[j = rand() % B];
  197. p[j] = k;
  198. }
  199. /* Extend g and p arrays to allow for faster indexing */
  200. for (i = 0; i < B + 2; i++) {
  201. p[B + i] = p[i];
  202. for (j = 0; j < 3; j++)
  203. g[B + i][j] = g[i][j];
  204. }
  205. }
  206. static float
  207. turbulence(float point[3], float lofreq, float hifreq)
  208. {
  209. float freq, t, p[3];
  210. p[0] = point[0] + 123.456;
  211. p[1] = point[1];
  212. p[2] = point[2];
  213. t = 0;
  214. for (freq = lofreq; freq < hifreq; freq *= 2.) {
  215. t += fabs(noise3(p)) / freq;
  216. p[0] *= 2.;
  217. p[1] *= 2.;
  218. p[2] *= 2.;
  219. }
  220. return t - 0.3; /* readjust to make mean value = 0.0 */
  221. }
  222. static void
  223. create3Dtexture(void)
  224. {
  225. unsigned char *voxels = NULL;
  226. int i, j, k;
  227. unsigned char *vp;
  228. float vec[3];
  229. int tmp;
  230. printf("creating 3d textures...\n");
  231. voxels =
  232. (unsigned char *)
  233. malloc((size_t) (4 * tex_width * tex_height * tex_depth));
  234. vp = voxels;
  235. for (i = 0; i < tex_width; i++) {
  236. vec[0] = i;
  237. for (j = 0; j < tex_height; j++) {
  238. vec[1] = j;
  239. for (k = 0; k < tex_depth; k++) {
  240. vec[2] = k;
  241. tmp = (sin(k * i * j + turbulence(vec, 0.01, 1)) + 1) * 127.5;
  242. *vp++ = 0;
  243. *vp++ = 0;
  244. *vp++ = tmp;
  245. *vp++ = tmp + 128;
  246. }
  247. }
  248. }
  249. printf("setting up 3d texture...\n");
  250. glBindTexture(GL_TEXTURE_3D, NOISE_TEXTURE);
  251. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  252. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  253. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  254. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  255. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
  256. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  257. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  258. glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
  259. tex_width, tex_height, tex_depth,
  260. 0, GL_RGBA, GL_UNSIGNED_BYTE, voxels);
  261. free(voxels);
  262. printf("finished setting up 3d texture image.\n");
  263. }
  264. static void
  265. printHelp(void)
  266. {
  267. printf("\nUsage: stex3d <cmd line options>\n");
  268. printf(" cmd line options:\n");
  269. printf(" -wxxx Width of the texture (Default=64)\n");
  270. printf(" -hxxx Height of the texture (Default=64)\n");
  271. printf(" -dxxx Depth of the texture (Default=64)\n");
  272. printf(" Keyboard Options:\n");
  273. printf(" up/down rotate around X\n");
  274. printf(" left/right rotate around Y\n");
  275. printf(" z/Z rotate around Z\n");
  276. printf(" a toggle animation\n");
  277. printf(" s toggle smooth shading\n");
  278. printf(" t toggle texgen mode\n");
  279. printf(" o toggle object: torus/sphere\n");
  280. printf(" i toggle texture image: noise/gradient\n");
  281. }
  282. static GLenum
  283. parseCmdLine(int argc, char **argv)
  284. {
  285. GLint i;
  286. for (i = 1; i < argc; i++) {
  287. if (strstr(argv[i], "-w") == 0) {
  288. tex_width = atoi((argv[i]) + 2);
  289. }
  290. else if (strstr(argv[i], "-h") == 0) {
  291. tex_height = atoi((argv[i]) + 2);
  292. }
  293. else if (strstr(argv[i], "-d") == 0) {
  294. tex_depth = atoi((argv[i]) + 2);
  295. }
  296. else if (strcmp(argv[i], "-help") == 0) {
  297. printHelp();
  298. return GL_FALSE;
  299. }
  300. else {
  301. printf("%s (Bad option).\n", argv[i]);
  302. printHelp();
  303. return GL_FALSE;
  304. }
  305. }
  306. if (tex_width == 0 || tex_height == 0 || tex_depth == 0) {
  307. printf("%s (Bad option).\n", "size parameters can't be 0");
  308. printHelp();
  309. return GL_FALSE;
  310. }
  311. return GL_TRUE;
  312. }
  313. static void
  314. drawScene(void)
  315. {
  316. static const GLfloat sPlane[4] = { 0.5, 0, 0, -.5 };
  317. static const GLfloat tPlane[4] = { 0, 0.5, 0, -.5 };
  318. static const GLfloat rPlane[4] = { 0, 0, 0.5, -.5 };
  319. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  320. glPushMatrix();
  321. if (texgen == 2) {
  322. glTexGenfv(GL_S, GL_EYE_PLANE, sPlane);
  323. glTexGenfv(GL_T, GL_EYE_PLANE, tPlane);
  324. glTexGenfv(GL_R, GL_EYE_PLANE, rPlane);
  325. }
  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. if (texgen == 1) {
  330. glTexGenfv(GL_S, GL_EYE_PLANE, sPlane);
  331. glTexGenfv(GL_T, GL_EYE_PLANE, tPlane);
  332. glTexGenfv(GL_R, GL_EYE_PLANE, rPlane);
  333. }
  334. if (texgen) {
  335. glEnable(GL_TEXTURE_GEN_S);
  336. glEnable(GL_TEXTURE_GEN_T);
  337. glEnable(GL_TEXTURE_GEN_R);
  338. }
  339. else {
  340. glDisable(GL_TEXTURE_GEN_S);
  341. glDisable(GL_TEXTURE_GEN_T);
  342. glDisable(GL_TEXTURE_GEN_R);
  343. }
  344. glCallList(CurObject);
  345. glPopMatrix();
  346. glutSwapBuffers();
  347. }
  348. static void
  349. resize(int w, int h)
  350. {
  351. float ar = (float) w / (float) h;
  352. float ax = 0.6 * ar;
  353. float ay = 0.6;
  354. glViewport(0, 0, (GLint) w, (GLint) h);
  355. glMatrixMode(GL_PROJECTION);
  356. glLoadIdentity();
  357. glFrustum(-ax, ax, -ay, ay, 2, 20);
  358. /*glOrtho(-2, 2, -2, 2, -10, 10);*/
  359. glMatrixMode(GL_MODELVIEW);
  360. glLoadIdentity();
  361. glTranslatef(0, 0, -4);
  362. }
  363. static void
  364. Idle(void)
  365. {
  366. float t = glutGet(GLUT_ELAPSED_TIME);
  367. angx = 0.01 * t;
  368. angy = 0.03 * t;
  369. angz += 0;
  370. glutPostRedisplay();
  371. }
  372. static void
  373. SpecialKey(int k, int x, int y)
  374. {
  375. switch (k) {
  376. case GLUT_KEY_UP:
  377. angx += 5.0;
  378. break;
  379. case GLUT_KEY_DOWN:
  380. angx -= 5.0;
  381. break;
  382. case GLUT_KEY_LEFT:
  383. angy += 5.0;
  384. break;
  385. case GLUT_KEY_RIGHT:
  386. angy -= 5.0;
  387. break;
  388. default:
  389. return;
  390. }
  391. glutPostRedisplay();
  392. }
  393. static void
  394. KeyHandler(unsigned char key, int x, int y)
  395. {
  396. static const char *mode[] = {
  397. "glTexCoord3f (no texgen)",
  398. "texgen fixed to eye coords",
  399. "texgen fixed to object coords"
  400. };
  401. (void) x;
  402. (void) y;
  403. switch (key) {
  404. case 27:
  405. case 'q':
  406. case 'Q': /* quit game. */
  407. exit(0);
  408. break;
  409. case 'z':
  410. angz += 10;
  411. break;
  412. case 'Z':
  413. angz -= 10;
  414. break;
  415. case 's':
  416. smooth = !smooth;
  417. if (smooth)
  418. glShadeModel(GL_SMOOTH);
  419. else
  420. glShadeModel(GL_FLAT);
  421. break;
  422. case 't':
  423. texgen++;
  424. if (texgen > 2)
  425. texgen = 0;
  426. printf("Texgen: %s\n", mode[texgen]);
  427. break;
  428. case 'o':
  429. if (CurObject == TORUS)
  430. CurObject = SPHERE;
  431. else
  432. CurObject = TORUS;
  433. break;
  434. case 'i':
  435. if (CurTexture == NOISE_TEXTURE)
  436. CurTexture = GRADIENT_TEXTURE;
  437. else
  438. CurTexture = NOISE_TEXTURE;
  439. glBindTexture(GL_TEXTURE_3D, CurTexture);
  440. break;
  441. case 'a':
  442. animate = !animate;
  443. if (animate)
  444. glutIdleFunc(Idle);
  445. else
  446. glutIdleFunc(NULL);
  447. break;
  448. default:
  449. break;
  450. }
  451. glutPostRedisplay();
  452. }
  453. void
  454. create3Dgradient(void)
  455. {
  456. unsigned char *v;
  457. int i, j, k;
  458. unsigned char *voxels = NULL;
  459. voxels = (unsigned char *) malloc(4 * tex_width * tex_height * tex_depth);
  460. v = voxels;
  461. for (i = 0; i < tex_depth; i++) {
  462. for (j = 0; j < tex_height; j++) {
  463. for (k = 0; k < tex_width; k++) {
  464. GLint r = (255 * i) / (tex_depth - 1);
  465. GLint g = (255 * j) / (tex_height - 1);
  466. GLint b = (255 * k) / (tex_width - 1);
  467. *v++ = r;
  468. *v++ = g;
  469. *v++ = b;
  470. *v++ = 255;
  471. }
  472. }
  473. }
  474. glBindTexture(GL_TEXTURE_3D, GRADIENT_TEXTURE);
  475. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  476. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  477. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  478. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  479. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
  480. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  481. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  482. glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
  483. tex_width, tex_height, tex_depth,
  484. 0, GL_RGBA, GL_UNSIGNED_BYTE, voxels);
  485. free(voxels);
  486. }
  487. static void
  488. init(void)
  489. {
  490. static const GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  491. static const GLfloat mat_shininess[] = { 25.0 };
  492. static const GLfloat gray[] = { 0.6, 0.6, 0.6, 0.0 };
  493. static const GLfloat white[] = { 1.0, 1.0, 1.0, 0.0 };
  494. static const GLfloat light_position[] = { 0.0, 1.0, 1.0, 0.0 };
  495. int max;
  496. /* see if we have OpenGL 1.2 or later, for 3D texturing */
  497. {
  498. const char *version = (const char *) glGetString(GL_VERSION);
  499. if (strncmp(version, "1.0", 3) == 0 || strncmp(version, "1.1", 3) == 0) {
  500. printf("Sorry, OpenGL 1.2 or later is required\n");
  501. exit(1);
  502. }
  503. }
  504. printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
  505. glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &max);
  506. printf("GL_MAX_3D_TEXTURE_SIZE: %d\n", max);
  507. printf("Current 3D texture size: %d x %d x %d\n",
  508. tex_width, tex_height, tex_depth);
  509. /* init light */
  510. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  511. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  512. glLightfv(GL_LIGHT1, GL_POSITION, light_position);
  513. glLightfv(GL_LIGHT1, GL_AMBIENT, gray);
  514. glLightfv(GL_LIGHT1, GL_DIFFUSE, white);
  515. glLightfv(GL_LIGHT1, GL_SPECULAR, white);
  516. glColorMaterial(GL_FRONT, GL_DIFFUSE);
  517. glEnable(GL_COLOR_MATERIAL);
  518. glEnable(GL_LIGHTING);
  519. glEnable(GL_LIGHT1);
  520. glClearColor(.5, .5, .5, 0);
  521. /* create torus for texturing */
  522. DList = glGenLists(1);
  523. glNewList(SPHERE, GL_COMPILE);
  524. glutSolidSphere(0.95, 30, 15);
  525. glEndList();
  526. BuildTorus();
  527. create3Dgradient();
  528. initNoise();
  529. create3Dtexture();
  530. glEnable(GL_TEXTURE_3D);
  531. /*
  532. glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA);
  533. glEnable(GL_BLEND);
  534. */
  535. glEnable(GL_DEPTH_TEST);
  536. glColor3f(0.6, 0.7, 0.8);
  537. }
  538. int
  539. main(int argc, char **argv)
  540. {
  541. glutInit(&argc, argv);
  542. if (parseCmdLine(argc, argv) == GL_FALSE) {
  543. exit(0);
  544. }
  545. glutInitWindowPosition(0, 0);
  546. glutInitWindowSize(400, 400);
  547. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  548. if (glutCreateWindow("stex3d") <= 0) {
  549. exit(0);
  550. }
  551. init();
  552. printHelp();
  553. glutReshapeFunc(resize);
  554. glutKeyboardFunc(KeyHandler);
  555. glutSpecialFunc(SpecialKey);
  556. glutDisplayFunc(drawScene);
  557. if (animate)
  558. glutIdleFunc(Idle);
  559. glutMainLoop();
  560. return 0;
  561. }