Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

sphere.c 18KB

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