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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 "readtex.h"
  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. GLubyte *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. static double t0 = -1.;
  751. double t, dt;
  752. t = glutGet(GLUT_ELAPSED_TIME) / 1000.;
  753. if (t0 < 0.)
  754. t0 = t;
  755. dt = t - t0;
  756. t0 = t;
  757. xRotation += .75*60.*dt;
  758. yRotation += .375*60.*dt;
  759. glutPostRedisplay();
  760. }
  761. void Key2(int key, int x, int y)
  762. {
  763. switch (key) {
  764. case GLUT_KEY_LEFT:
  765. yRotation -= 0.5;
  766. autoRotate = GL_FALSE;
  767. ReInit();
  768. break;
  769. case GLUT_KEY_RIGHT:
  770. yRotation += 0.5;
  771. autoRotate = GL_FALSE;
  772. ReInit();
  773. break;
  774. case GLUT_KEY_UP:
  775. xRotation -= 0.5;
  776. autoRotate = GL_FALSE;
  777. ReInit();
  778. break;
  779. case GLUT_KEY_DOWN:
  780. xRotation += 0.5;
  781. autoRotate = GL_FALSE;
  782. ReInit();
  783. break;
  784. default:
  785. return;
  786. }
  787. glutPostRedisplay();
  788. }
  789. void Key(unsigned char key, int x, int y)
  790. {
  791. switch (key) {
  792. case 27:
  793. /* free(image->data);*/
  794. exit(1);
  795. case 'a':
  796. autoRotate = !autoRotate;
  797. if (autoRotate)
  798. glutIdleFunc(Idle);
  799. else
  800. glutIdleFunc(NULL);
  801. ReInit();
  802. break;
  803. case 'o':
  804. if (genericObject == cube) {
  805. genericObject = cylinder;
  806. }
  807. else if (genericObject == cylinder) {
  808. genericObject = torus;
  809. }
  810. else {
  811. genericObject = cube;
  812. }
  813. ReInit();
  814. break;
  815. case 'd':
  816. textureEnvironment = decal;
  817. ReInit();
  818. break;
  819. case 'm':
  820. textureEnvironment = modulate;
  821. ReInit();
  822. break;
  823. case 'l':
  824. isLit = !isLit;
  825. ReInit();
  826. break;
  827. case 'f':
  828. isFogged = !isFogged;
  829. ReInit();
  830. break;
  831. case 't':
  832. doTexture = !doTexture;
  833. ReInit();
  834. break;
  835. case '0':
  836. magFilter = nnearest;
  837. ReInit();
  838. break;
  839. case '1':
  840. magFilter = linear;
  841. ReInit();
  842. break;
  843. case '2':
  844. minFilter = nnearest;
  845. ReInit();
  846. break;
  847. case '3':
  848. minFilter = linear;
  849. ReInit();
  850. break;
  851. case '4':
  852. minFilter = nearest_mipmap_nearest;
  853. ReInit();
  854. break;
  855. case '5':
  856. minFilter = nearest_mipmap_linear;
  857. ReInit();
  858. break;
  859. case '6':
  860. minFilter = linear_mipmap_nearest;
  861. ReInit();
  862. break;
  863. case '7':
  864. minFilter = linear_mipmap_linear;
  865. ReInit();
  866. break;
  867. default:
  868. return;
  869. }
  870. glutPostRedisplay();
  871. }
  872. GLenum Args(int argc, char **argv)
  873. {
  874. GLint i;
  875. doubleBuffer = GL_TRUE;
  876. for (i = 1; i < argc; i++) {
  877. if (strcmp(argv[i], "-sb") == 0) {
  878. doubleBuffer = GL_FALSE;
  879. } else if (strcmp(argv[i], "-db") == 0) {
  880. doubleBuffer = GL_TRUE;
  881. } else if (strcmp(argv[i], "-f") == 0) {
  882. if (i+1 >= argc || argv[i+1][0] == '-') {
  883. printf("-f (No file name).\n");
  884. return GL_FALSE;
  885. } else {
  886. imageFileName = argv[++i];
  887. }
  888. } else {
  889. printf("%s (Bad option).\n", argv[i]);
  890. return GL_FALSE;
  891. }
  892. }
  893. return GL_TRUE;
  894. }
  895. int main(int argc, char **argv)
  896. {
  897. GLenum type;
  898. glutInit(&argc, argv);
  899. if (Args(argc, argv) == GL_FALSE) {
  900. exit(1);
  901. }
  902. if (imageFileName == 0) {
  903. printf("No image file.\n");
  904. exit(1);
  905. }
  906. glutInitWindowPosition(0, 0); glutInitWindowSize( W, H);
  907. type = GLUT_RGB | GLUT_DEPTH;
  908. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  909. glutInitDisplayMode(type);
  910. if (glutCreateWindow("Texture Test") == GL_FALSE) {
  911. exit(1);
  912. }
  913. Init();
  914. glutReshapeFunc(Reshape);
  915. glutKeyboardFunc(Key);
  916. glutSpecialFunc(Key2);
  917. glutDisplayFunc(Draw);
  918. glutIdleFunc(Idle);
  919. glutMainLoop();
  920. return 0;
  921. }