Source code of Windows XP (NT5)
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.

1033 lines
18 KiB

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