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

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