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.

sphere.c 17KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and
  5. * its documentation for any purpose is hereby granted without fee, provided
  6. * that (i) the above copyright notices and this permission notice appear in
  7. * all copies of the software and related documentation, and (ii) the name of
  8. * Silicon Graphics may not be used in any advertising or
  9. * publicity relating to the software without the specific, prior written
  10. * permission of Silicon Graphics.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13. * ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. /* BEP: renamed "nearest" as "nnearest" to avoid math.h collision on AIX */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #include <stdlib.h>
  29. #include <GL/glut.h>
  30. #include "../util/readtex.c"
  31. #ifndef PI
  32. #define PI 3.14159265358979323846
  33. #endif
  34. GLenum doubleBuffer;
  35. int W = 400, H = 400;
  36. char *imageFileName = "../images/reflect.rgb";
  37. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  38. float decal[] = {GL_DECAL};
  39. float modulate[] = {GL_MODULATE};
  40. float repeat[] = {GL_REPEAT};
  41. float clamp[] = {GL_CLAMP};
  42. float nnearest[] = {GL_NEAREST};
  43. float linear[] = {GL_LINEAR};
  44. float nearest_mipmap_nearest[] = {GL_NEAREST_MIPMAP_NEAREST};
  45. float nearest_mipmap_linear[] = {GL_NEAREST_MIPMAP_LINEAR};
  46. float linear_mipmap_nearest[] = {GL_LINEAR_MIPMAP_NEAREST};
  47. float linear_mipmap_linear[] = {GL_LINEAR_MIPMAP_LINEAR};
  48. GLint sphereMap[] = {GL_SPHERE_MAP};
  49. float xRotation = 0.0, yRotation = 0.0;
  50. float zTranslate = -3.0;
  51. GLenum autoRotate = GL_TRUE;
  52. GLboolean isLit = GL_TRUE;
  53. GLboolean isFogged = GL_FALSE;
  54. GLboolean doTexture = GL_TRUE;
  55. float *textureEnvironment = modulate;
  56. int cube, cage, cylinder, torus, genericObject;
  57. float c[6][4][4][3] = {
  58. {
  59. {
  60. {
  61. 1.0, 1.0, -1.0
  62. },
  63. {
  64. 0.0, 1.0, -1.0
  65. },
  66. {
  67. 0.0, 0.0, -1.0
  68. },
  69. {
  70. 1.0, 0.0, -1.0
  71. },
  72. },
  73. {
  74. {
  75. 0.0, 1.0, -1.0
  76. },
  77. {
  78. -1.0, 1.0, -1.0
  79. },
  80. {
  81. -1.0, 0.0, -1.0
  82. },
  83. {
  84. 0.0, 0.0, -1.0
  85. },
  86. },
  87. {
  88. {
  89. 0.0, 0.0, -1.0
  90. },
  91. {
  92. -1.0, 0.0, -1.0
  93. },
  94. {
  95. -1.0, -1.0, -1.0
  96. },
  97. {
  98. 0.0, -1.0, -1.0
  99. },
  100. },
  101. {
  102. {
  103. 1.0, 0.0, -1.0
  104. },
  105. {
  106. 0.0, 0.0, -1.0
  107. },
  108. {
  109. 0.0, -1.0, -1.0
  110. },
  111. {
  112. 1.0, -1.0, -1.0
  113. },
  114. },
  115. },
  116. {
  117. {
  118. {
  119. 1.0, 1.0, 1.0
  120. },
  121. {
  122. 1.0, 1.0, 0.0
  123. },
  124. {
  125. 1.0, 0.0, 0.0
  126. },
  127. {
  128. 1.0, 0.0, 1.0
  129. },
  130. },
  131. {
  132. {
  133. 1.0, 1.0, 0.0
  134. },
  135. {
  136. 1.0, 1.0, -1.0
  137. },
  138. {
  139. 1.0, 0.0, -1.0
  140. },
  141. {
  142. 1.0, 0.0, 0.0
  143. },
  144. },
  145. {
  146. {
  147. 1.0, 0.0, -1.0
  148. },
  149. {
  150. 1.0, -1.0, -1.0
  151. },
  152. {
  153. 1.0, -1.0, 0.0
  154. },
  155. {
  156. 1.0, 0.0, 0.0
  157. },
  158. },
  159. {
  160. {
  161. 1.0, 0.0, 0.0
  162. },
  163. {
  164. 1.0, -1.0, 0.0
  165. },
  166. {
  167. 1.0, -1.0, 1.0
  168. },
  169. {
  170. 1.0, 0.0, 1.0
  171. },
  172. },
  173. },
  174. {
  175. {
  176. {
  177. -1.0, 1.0, 1.0
  178. },
  179. {
  180. 0.0, 1.0, 1.0
  181. },
  182. {
  183. 0.0, 0.0, 1.0
  184. },
  185. {
  186. -1.0, 0.0, 1.0
  187. },
  188. },
  189. {
  190. {
  191. 0.0, 1.0, 1.0
  192. },
  193. {
  194. 1.0, 1.0, 1.0
  195. },
  196. {
  197. 1.0, 0.0, 1.0
  198. },
  199. {
  200. 0.0, 0.0, 1.0
  201. },
  202. },
  203. {
  204. {
  205. 1.0, 0.0, 1.0
  206. },
  207. {
  208. 1.0, -1.0, 1.0
  209. },
  210. {
  211. 0.0, -1.0, 1.0
  212. },
  213. {
  214. 0.0, 0.0, 1.0
  215. },
  216. },
  217. {
  218. {
  219. 0.0, -1.0, 1.0
  220. },
  221. {
  222. -1.0, -1.0, 1.0
  223. },
  224. {
  225. -1.0, 0.0, 1.0
  226. },
  227. {
  228. 0.0, 0.0, 1.0
  229. },
  230. },
  231. },
  232. {
  233. {
  234. {
  235. -1.0, 1.0, -1.0
  236. },
  237. {
  238. -1.0, 1.0, 0.0
  239. },
  240. {
  241. -1.0, 0.0, 0.0
  242. },
  243. {
  244. -1.0, 0.0, -1.0
  245. },
  246. },
  247. {
  248. {
  249. -1.0, 1.0, 0.0
  250. },
  251. {
  252. -1.0, 1.0, 1.0
  253. },
  254. {
  255. -1.0, 0.0, 1.0
  256. },
  257. {
  258. -1.0, 0.0, 0.0
  259. },
  260. },
  261. {
  262. {
  263. -1.0, 0.0, 1.0
  264. },
  265. {
  266. -1.0, -1.0, 1.0
  267. },
  268. {
  269. -1.0, -1.0, 0.0
  270. },
  271. {
  272. -1.0, 0.0, 0.0
  273. },
  274. },
  275. {
  276. {
  277. -1.0, -1.0, 0.0
  278. },
  279. {
  280. -1.0, -1.0, -1.0
  281. },
  282. {
  283. -1.0, 0.0, -1.0
  284. },
  285. {
  286. -1.0, 0.0, 0.0
  287. },
  288. },
  289. },
  290. {
  291. {
  292. {
  293. -1.0, 1.0, 1.0
  294. },
  295. {
  296. -1.0, 1.0, 0.0
  297. },
  298. {
  299. 0.0, 1.0, 0.0
  300. },
  301. {
  302. 0.0, 1.0, 1.0
  303. },
  304. },
  305. {
  306. {
  307. -1.0, 1.0, 0.0
  308. },
  309. {
  310. -1.0, 1.0, -1.0
  311. },
  312. {
  313. 0.0, 1.0, -1.0
  314. },
  315. {
  316. 0.0, 1.0, 0.0
  317. },
  318. },
  319. {
  320. {
  321. 0.0, 1.0, -1.0
  322. },
  323. {
  324. 1.0, 1.0, -1.0
  325. },
  326. {
  327. 1.0, 1.0, 0.0
  328. },
  329. {
  330. 0.0, 1.0, 0.0
  331. },
  332. },
  333. {
  334. {
  335. 1.0, 1.0, 0.0
  336. },
  337. {
  338. 1.0, 1.0, 1.0
  339. },
  340. {
  341. 0.0, 1.0, 1.0
  342. },
  343. {
  344. 0.0, 1.0, 0.0
  345. },
  346. },
  347. },
  348. {
  349. {
  350. {
  351. -1.0, -1.0, -1.0
  352. },
  353. {
  354. -1.0, -1.0, 0.0
  355. },
  356. {
  357. 0.0, -1.0, 0.0
  358. },
  359. {
  360. 0.0, -1.0, -1.0
  361. },
  362. },
  363. {
  364. {
  365. -1.0, -1.0, 0.0
  366. },
  367. {
  368. -1.0, -1.0, 1.0
  369. },
  370. {
  371. 0.0, -1.0, 1.0
  372. },
  373. {
  374. 0.0, -1.0, 0.0
  375. },
  376. },
  377. {
  378. {
  379. 0.0, -1.0, 1.0
  380. },
  381. {
  382. 1.0, -1.0, 1.0
  383. },
  384. {
  385. 1.0, -1.0, 0.0
  386. },
  387. {
  388. 0.0, -1.0, 0.0
  389. },
  390. },
  391. {
  392. {
  393. 1.0, -1.0, 0.0
  394. },
  395. {
  396. 1.0, -1.0, -1.0
  397. },
  398. {
  399. 0.0, -1.0, -1.0
  400. },
  401. {
  402. 0.0, -1.0, 0.0
  403. },
  404. },
  405. }
  406. };
  407. float n[6][3] = {
  408. {
  409. 0.0, 0.0, -1.0
  410. },
  411. {
  412. 1.0, 0.0, 0.0
  413. },
  414. {
  415. 0.0, 0.0, 1.0
  416. },
  417. {
  418. -1.0, 0.0, 0.0
  419. },
  420. {
  421. 0.0, 1.0, 0.0
  422. },
  423. {
  424. 0.0, -1.0, 0.0
  425. }
  426. };
  427. GLfloat identity[16] = {
  428. 1, 0, 0, 0,
  429. 0, 1, 0, 0,
  430. 0, 0, 1, 0,
  431. 0, 0, 0, 1,
  432. };
  433. void BuildCylinder(int numEdges)
  434. {
  435. int i, top = 1.0, bottom = -1.0;
  436. float x[100], y[100], angle;
  437. for (i = 0; i <= numEdges; i++) {
  438. angle = i * 2.0 * PI / numEdges;
  439. x[i] = cos(angle); /* was cosf() */
  440. y[i] = sin(angle); /* was sinf() */
  441. }
  442. glNewList(cylinder, GL_COMPILE);
  443. glBegin(GL_TRIANGLE_STRIP);
  444. for (i = 0; i <= numEdges; i++) {
  445. glNormal3f(x[i], y[i], 0.0);
  446. glVertex3f(x[i], y[i], bottom);
  447. glVertex3f(x[i], y[i], top);
  448. }
  449. glEnd();
  450. glBegin(GL_TRIANGLE_FAN);
  451. glNormal3f(0.0, 0.0, 1.0);
  452. glVertex3f(0.0, 0.0, top);
  453. for (i = 0; i <= numEdges; i++) {
  454. glVertex3f(x[i], -y[i], top);
  455. }
  456. glEnd();
  457. glBegin(GL_TRIANGLE_FAN);
  458. glNormal3f(0.0, 0.0, -1.0);
  459. glVertex3f(0.0, 0.0, bottom);
  460. for (i = 0; i <= numEdges; i++) {
  461. glVertex3f(x[i], y[i], bottom);
  462. }
  463. glEnd();
  464. glEndList();
  465. }
  466. void BuildTorus(float rc, int numc, float rt, int numt)
  467. {
  468. int i, j, k;
  469. double s, t;
  470. double x, y, z;
  471. double pi, twopi;
  472. pi = 3.14159265358979323846;
  473. twopi = 2.0 * pi;
  474. glNewList(torus, GL_COMPILE);
  475. for (i = 0; i < numc; i++) {
  476. glBegin(GL_QUAD_STRIP);
  477. for (j = 0; j <= numt; j++) {
  478. for (k = 0; k <= 1; k++) {
  479. s = (i + k) % numc + 0.5;
  480. t = j % numt;
  481. x = cos(t*twopi/numt) * cos(s*twopi/numc);
  482. y = sin(t*twopi/numt) * cos(s*twopi/numc);
  483. z = sin(s*twopi/numc);
  484. glNormal3f(x, y, z);
  485. x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
  486. y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
  487. z = rc * sin(s*twopi/numc);
  488. glVertex3f(x, y, z);
  489. }
  490. }
  491. glEnd();
  492. }
  493. glEndList();
  494. }
  495. void BuildCage(void)
  496. {
  497. int i;
  498. float inc;
  499. float right, left, top, bottom, front, back;
  500. front = 0.0;
  501. back = -8.0;
  502. left = -4.0;
  503. bottom = -4.0;
  504. right = 4.0;
  505. top = 4.0;
  506. inc = 2.0 * 4.0 * 0.1;
  507. glNewList(cage, GL_COMPILE);
  508. for (i = 0; i < 10; i++) {
  509. /*
  510. ** Back
  511. */
  512. glBegin(GL_LINES);
  513. glVertex3f(left+i*inc, top, back);
  514. glVertex3f(left+i*inc, bottom, back);
  515. glEnd();
  516. glBegin(GL_LINES);
  517. glVertex3f(right, bottom+i*inc, back);
  518. glVertex3f(left, bottom+i*inc, back);
  519. glEnd();
  520. /*
  521. ** Front
  522. */
  523. glBegin(GL_LINES);
  524. glVertex3f(left+i*inc, top, front);
  525. glVertex3f(left+i*inc, bottom, front);
  526. glEnd();
  527. glBegin(GL_LINES);
  528. glVertex3f(right, bottom+i*inc, front);
  529. glVertex3f(left, bottom+i*inc, front);
  530. glEnd();
  531. /*
  532. ** Left
  533. */
  534. glBegin(GL_LINES);
  535. glVertex3f(left, bottom+i*inc, front);
  536. glVertex3f(left, bottom+i*inc, back);
  537. glEnd();
  538. glBegin(GL_LINES);
  539. glVertex3f(left, top, back+i*inc);
  540. glVertex3f(left, bottom, back+i*inc);
  541. glEnd();
  542. /*
  543. ** Right
  544. */
  545. glBegin(GL_LINES);
  546. glVertex3f(right, top-i*inc, front);
  547. glVertex3f(right, top-i*inc, back);
  548. glEnd();
  549. glBegin(GL_LINES);
  550. glVertex3f(right, top, back+i*inc);
  551. glVertex3f(right, bottom, back+i*inc);
  552. glEnd();
  553. /*
  554. ** Top
  555. */
  556. glBegin(GL_LINES);
  557. glVertex3f(left+i*inc, top, front);
  558. glVertex3f(left+i*inc, top, back);
  559. glEnd();
  560. glBegin(GL_LINES);
  561. glVertex3f(right, top, back+i*inc);
  562. glVertex3f(left, top, back+i*inc);
  563. glEnd();
  564. /*
  565. ** Bottom
  566. */
  567. glBegin(GL_LINES);
  568. glVertex3f(right-i*inc, bottom, front);
  569. glVertex3f(right-i*inc, bottom, back);
  570. glEnd();
  571. glBegin(GL_LINES);
  572. glVertex3f(right, bottom, back+i*inc);
  573. glVertex3f(left, bottom, back+i*inc);
  574. glEnd();
  575. }
  576. glEndList();
  577. }
  578. void BuildCube(void)
  579. {
  580. int i, j;
  581. glNewList(cube, GL_COMPILE);
  582. for (i = 0; i < 6; i++) {
  583. for (j = 0; j < 4; j++) {
  584. glNormal3fv(n[i]);
  585. glBegin(GL_POLYGON);
  586. glVertex3fv(c[i][j][0]);
  587. glVertex3fv(c[i][j][1]);
  588. glVertex3fv(c[i][j][2]);
  589. glVertex3fv(c[i][j][3]);
  590. glEnd();
  591. }
  592. }
  593. glEndList();
  594. }
  595. void BuildLists(void)
  596. {
  597. cube = glGenLists(1);
  598. BuildCube();
  599. cage = glGenLists(2);
  600. BuildCage();
  601. cylinder = glGenLists(3);
  602. BuildCylinder(60);
  603. torus = glGenLists(4);
  604. BuildTorus(0.65, 20, .85, 65);
  605. genericObject = torus;
  606. }
  607. void SetDefaultSettings(void)
  608. {
  609. magFilter = nnearest;
  610. minFilter = nnearest;
  611. sWrapMode = repeat;
  612. tWrapMode = repeat;
  613. textureEnvironment = modulate;
  614. autoRotate = GL_TRUE;
  615. }
  616. unsigned char *AlphaPadImage(int bufSize, unsigned char *inData, int alpha)
  617. {
  618. unsigned char *outData, *out_ptr, *in_ptr;
  619. int i;
  620. outData = (unsigned char *) malloc(bufSize * 4);
  621. out_ptr = outData;
  622. in_ptr = inData;
  623. for (i = 0; i < bufSize; i++) {
  624. *out_ptr++ = *in_ptr++;
  625. *out_ptr++ = *in_ptr++;
  626. *out_ptr++ = *in_ptr++;
  627. *out_ptr++ = alpha;
  628. }
  629. free (inData);
  630. return outData;
  631. }
  632. void Init(void)
  633. {
  634. float ambient[] = {0.0, 0.0, 0.0, 1.0};
  635. float diffuse[] = {1.0, 1.0, 1.0, 1.0};
  636. float specular[] = {1.0, 1.0, 1.0, 1.0};
  637. float position[] = {0.0, 0.0, 4.0, 0.0};
  638. float fog_color[] = {0.0, 0.0, 0.0, 1.0};
  639. float mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  640. float mat_shininess[] = {90.0};
  641. float mat_specular[] = {1.0, 1.0, 1.0, 1.0};
  642. float mat_diffuse[] = {0.8, 0.8, 0.8, 1.0};
  643. float lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0};
  644. float lmodel_twoside[] = {GL_TRUE};
  645. int w, h;
  646. GLenum format;
  647. char *image;
  648. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  649. SetDefaultSettings();
  650. image = LoadRGBImage(imageFileName, &w, &h, &format);
  651. if (!image) {
  652. printf("Error: couldn't load %s\n", imageFileName);
  653. exit(1);
  654. }
  655. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  656. gluBuild2DMipmaps(GL_TEXTURE_2D, format, w, h,
  657. GL_RGB, GL_UNSIGNED_BYTE, image);
  658. free(image);
  659. glFogf(GL_FOG_DENSITY, 0.125);
  660. glFogi(GL_FOG_MODE, GL_LINEAR);
  661. glFogf(GL_FOG_START, 4.0);
  662. glFogf(GL_FOG_END, 8.5);
  663. glFogfv(GL_FOG_COLOR, fog_color);
  664. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  665. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  666. glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  667. glLightfv(GL_LIGHT0, GL_POSITION, position);
  668. glEnable(GL_LIGHT0);
  669. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  670. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  671. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  672. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  673. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  674. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  675. glShadeModel(GL_SMOOTH);
  676. glClearColor(0.0, 0.0, 0.0, 0.0);
  677. glEnable(GL_DEPTH_TEST);
  678. glFrontFace(GL_CW);
  679. glEnable(GL_CULL_FACE);
  680. glCullFace(GL_BACK);
  681. glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
  682. glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
  683. glEnable(GL_TEXTURE_GEN_S);
  684. glEnable(GL_TEXTURE_GEN_T);
  685. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  686. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  687. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  688. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  689. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  690. BuildLists();
  691. }
  692. void ReInit(void)
  693. {
  694. if (genericObject == torus) {
  695. glEnable(GL_DEPTH_TEST);
  696. } else {
  697. glDisable(GL_DEPTH_TEST);
  698. }
  699. glEnable(GL_DEPTH_TEST);
  700. #if 0
  701. if (isFogged) {
  702. textureEnvironment = modulate;
  703. }
  704. #endif
  705. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  706. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  707. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  708. }
  709. void Draw(void)
  710. {
  711. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  712. /* draw cage */
  713. if (isFogged)
  714. glEnable(GL_FOG);
  715. else
  716. glDisable(GL_FOG);
  717. glColor3f(1, 1, 1);
  718. glDisable(GL_LIGHTING);
  719. glDisable(GL_TEXTURE_2D);
  720. glCallList(cage);
  721. /* draw object */
  722. if (isLit)
  723. glEnable(GL_LIGHTING);
  724. else
  725. glColor3f(1.0, 0.5, 0.2);
  726. if (doTexture)
  727. glEnable(GL_TEXTURE_2D);
  728. glPushMatrix();
  729. glTranslatef(0.0, 0.0, zTranslate);
  730. glRotatef(xRotation, 1, 0, 0);
  731. glRotatef(yRotation, 0, 1, 0);
  732. glCallList(genericObject);
  733. glPopMatrix();
  734. glFlush();
  735. glutSwapBuffers();
  736. }
  737. void Reshape(int width, int height)
  738. {
  739. W = width;
  740. H = height;
  741. ReInit();
  742. glViewport( 0, 0, width, height ); /*new*/
  743. glMatrixMode(GL_PROJECTION);
  744. glLoadIdentity();
  745. glFrustum(-0.2, 0.2, -0.2, 0.2, 0.15, 9.0);
  746. glMatrixMode(GL_MODELVIEW);
  747. }
  748. void Idle(void)
  749. {
  750. xRotation += .75;
  751. yRotation += .375;
  752. glutPostRedisplay();
  753. }
  754. void Key2(int key, int x, int y)
  755. {
  756. switch (key) {
  757. case GLUT_KEY_LEFT:
  758. yRotation -= 0.5;
  759. autoRotate = GL_FALSE;
  760. ReInit();
  761. break;
  762. case GLUT_KEY_RIGHT:
  763. yRotation += 0.5;
  764. autoRotate = GL_FALSE;
  765. ReInit();
  766. break;
  767. case GLUT_KEY_UP:
  768. xRotation -= 0.5;
  769. autoRotate = GL_FALSE;
  770. ReInit();
  771. break;
  772. case GLUT_KEY_DOWN:
  773. xRotation += 0.5;
  774. autoRotate = GL_FALSE;
  775. ReInit();
  776. break;
  777. default:
  778. return;
  779. }
  780. glutPostRedisplay();
  781. }
  782. void Key(unsigned char key, int x, int y)
  783. {
  784. switch (key) {
  785. case 27:
  786. /* free(image->data);*/
  787. exit(1);
  788. case 'a':
  789. autoRotate = !autoRotate;
  790. if (autoRotate)
  791. glutIdleFunc(Idle);
  792. else
  793. glutIdleFunc(NULL);
  794. ReInit();
  795. break;
  796. case 'o':
  797. if (genericObject == cube) {
  798. genericObject = cylinder;
  799. }
  800. else if (genericObject == cylinder) {
  801. genericObject = torus;
  802. }
  803. else {
  804. genericObject = cube;
  805. }
  806. ReInit();
  807. break;
  808. case 'd':
  809. textureEnvironment = decal;
  810. ReInit();
  811. break;
  812. case 'm':
  813. textureEnvironment = modulate;
  814. ReInit();
  815. break;
  816. case 'l':
  817. isLit = !isLit;
  818. ReInit();
  819. break;
  820. case 'f':
  821. isFogged = !isFogged;
  822. ReInit();
  823. break;
  824. case 't':
  825. doTexture = !doTexture;
  826. ReInit();
  827. break;
  828. case '0':
  829. magFilter = nnearest;
  830. ReInit();
  831. break;
  832. case '1':
  833. magFilter = linear;
  834. ReInit();
  835. break;
  836. case '2':
  837. minFilter = nnearest;
  838. ReInit();
  839. break;
  840. case '3':
  841. minFilter = linear;
  842. ReInit();
  843. break;
  844. case '4':
  845. minFilter = nearest_mipmap_nearest;
  846. ReInit();
  847. break;
  848. case '5':
  849. minFilter = nearest_mipmap_linear;
  850. ReInit();
  851. break;
  852. case '6':
  853. minFilter = linear_mipmap_nearest;
  854. ReInit();
  855. break;
  856. case '7':
  857. minFilter = linear_mipmap_linear;
  858. ReInit();
  859. break;
  860. default:
  861. return;
  862. }
  863. glutPostRedisplay();
  864. }
  865. GLenum Args(int argc, char **argv)
  866. {
  867. GLint i;
  868. doubleBuffer = GL_TRUE;
  869. for (i = 1; i < argc; i++) {
  870. if (strcmp(argv[i], "-sb") == 0) {
  871. doubleBuffer = GL_FALSE;
  872. } else if (strcmp(argv[i], "-db") == 0) {
  873. doubleBuffer = GL_TRUE;
  874. } else if (strcmp(argv[i], "-f") == 0) {
  875. if (i+1 >= argc || argv[i+1][0] == '-') {
  876. printf("-f (No file name).\n");
  877. return GL_FALSE;
  878. } else {
  879. imageFileName = argv[++i];
  880. }
  881. } else {
  882. printf("%s (Bad option).\n", argv[i]);
  883. return GL_FALSE;
  884. }
  885. }
  886. return GL_TRUE;
  887. }
  888. int main(int argc, char **argv)
  889. {
  890. GLenum type;
  891. glutInit(&argc, argv);
  892. if (Args(argc, argv) == GL_FALSE) {
  893. exit(1);
  894. }
  895. if (imageFileName == 0) {
  896. printf("No image file.\n");
  897. exit(1);
  898. }
  899. glutInitWindowPosition(0, 0); glutInitWindowSize( W, H);
  900. type = GLUT_RGB | GLUT_DEPTH;
  901. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  902. glutInitDisplayMode(type);
  903. if (glutCreateWindow("Texture Test") == GL_FALSE) {
  904. exit(1);
  905. }
  906. Init();
  907. glutReshapeFunc(Reshape);
  908. glutKeyboardFunc(Key);
  909. glutSpecialFunc(Key2);
  910. glutDisplayFunc(Draw);
  911. glutIdleFunc(Idle);
  912. glutMainLoop();
  913. return 0;
  914. }