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.

1059 lines
20 KiB

  1. #undef FLAT_SHADING
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <GL/gl.h>
  7. #include "gl42ogl.h"
  8. #include "tk.h"
  9. #include "insect.h"
  10. #include "insectco.h"
  11. GLushort ls = 0xaaaa;
  12. unsigned char halftone[] = {
  13. 0xAA, 0xAA, 0xAA, 0xAA,
  14. 0x55, 0x55, 0x55, 0x55,
  15. 0xAA, 0xAA, 0xAA, 0xAA,
  16. 0x55, 0x55, 0x55, 0x55,
  17. 0xAA, 0xAA, 0xAA, 0xAA,
  18. 0x55, 0x55, 0x55, 0x55,
  19. 0xAA, 0xAA, 0xAA, 0xAA,
  20. 0x55, 0x55, 0x55, 0x55,
  21. 0xAA, 0xAA, 0xAA, 0xAA,
  22. 0x55, 0x55, 0x55, 0x55,
  23. 0xAA, 0xAA, 0xAA, 0xAA,
  24. 0x55, 0x55, 0x55, 0x55,
  25. 0xAA, 0xAA, 0xAA, 0xAA,
  26. 0x55, 0x55, 0x55, 0x55,
  27. 0xAA, 0xAA, 0xAA, 0xAA,
  28. 0x55, 0x55, 0x55, 0x55,
  29. 0xAA, 0xAA, 0xAA, 0xAA,
  30. 0x55, 0x55, 0x55, 0x55,
  31. 0xAA, 0xAA, 0xAA, 0xAA,
  32. 0x55, 0x55, 0x55, 0x55,
  33. 0xAA, 0xAA, 0xAA, 0xAA,
  34. 0x55, 0x55, 0x55, 0x55,
  35. 0xAA, 0xAA, 0xAA, 0xAA,
  36. 0x55, 0x55, 0x55, 0x55,
  37. 0xAA, 0xAA, 0xAA, 0xAA,
  38. 0x55, 0x55, 0x55, 0x55,
  39. 0xAA, 0xAA, 0xAA, 0xAA,
  40. 0x55, 0x55, 0x55, 0x55,
  41. 0xAA, 0xAA, 0xAA, 0xAA,
  42. 0x55, 0x55, 0x55, 0x55,
  43. 0xAA, 0xAA, 0xAA, 0xAA,
  44. 0x55, 0x55, 0x55, 0x55,
  45. };
  46. GLfloat sdepth = 10.;
  47. float light[3],
  48. phi = PI / 4.0,
  49. theta = PI / 4.0;
  50. GLfloat ctheta = -900, cphi, cvtheta, cvphi;
  51. float fabso (float a );
  52. /* Changes for ECLIPSE 8 bit machine */
  53. /* don't use 7 and 8 for appearances. These are the text standards */
  54. unsigned short ECLIPSE8_RAMP[10] = {4, 5, 6, 8, 9, 10, 11, 12, 13, 14};
  55. GLboolean is_8bit;
  56. short savearray[384];
  57. /** change for new window control */
  58. float halfwinx, halfwiny;
  59. long worgx, worgy;
  60. long wsizex, wsizey;
  61. long pikx, piky;
  62. GLboolean follow;
  63. float px, py;
  64. float light[3];
  65. float cx, cy, cz, cvx, cvy, cvz;
  66. float dmr[6], fr[6];
  67. GLfloat knee[6];
  68. GLfloat hip_phi[6];
  69. GLfloat hip_theta[6];
  70. GLboolean legup[6];
  71. float legx[6], legy[6];
  72. enum {
  73. NOTHING,
  74. FORWARD,
  75. BACKWARD,
  76. LEFT,
  77. MIDDLE
  78. };
  79. short function;
  80. /* mymakerange -- color ramp utilities */
  81. void
  82. mymakerange (unsigned short a, unsigned short b, GLubyte red1, GLubyte red2,
  83. GLubyte green1, GLubyte green2, GLubyte blue1, GLubyte blue2)
  84. {
  85. float i;
  86. int j;
  87. float dr,
  88. dg,
  89. db;
  90. i = (float) (b - a);
  91. dr = (float) (red2 - red1) / i;
  92. dg = (float) (green2 - green1) / i;
  93. db = (float) (blue2 - blue1) / i;
  94. for (j = 0; j <= (int) i; j++)
  95. mapcolor ((unsigned short) j + a,
  96. (short) (dr * (float) j + red1),
  97. (short) (dg * (float) j + green1),
  98. (short) (db * (float) j + blue1));
  99. /* mapcolor ((unsigned short) j + a,
  100. (GLubyte) (dr * (float) j + red1),
  101. (GLubyte) (dg * (float) j + green1),
  102. (GLubyte) (db * (float) j + blue1));
  103. */
  104. }
  105. /* Set up Eclipse 8 bit color ramp */
  106. void
  107. make_eclipse8_range(unsigned short e_ramp[], int red1, int red2,
  108. int green1, int green2, int blue1, int blue2)
  109. {
  110. int i;
  111. float rinc, ginc, binc;
  112. rinc = (float)(red2 - red1) / (float)ECLIPSE8_NCOLORS;
  113. ginc = (float)(green2 - green1) / (float)ECLIPSE8_NCOLORS;
  114. binc = (float)(blue2 - blue1) / (float)ECLIPSE8_NCOLORS;
  115. for (i = 0; i < ECLIPSE8_NCOLORS; i++) {
  116. mapcolor(e_ramp[i],
  117. (short)(i * rinc + red1),
  118. (short)(i * ginc + green1),
  119. (short)(i * binc + blue1));
  120. }
  121. }
  122. /* setupcolors -- load color map */
  123. /* Changed for ECLIPSE 8 bit machine */
  124. void
  125. setupcolors (void) {
  126. if (!is_8bit) {
  127. mapcolor (GRAY, 128, 128, 128);
  128. mapcolor (GRID, 128, 200, 250);
  129. mapcolor (SKYBLUE, 50, 50, 150);
  130. mymakerange (RAMPB5, RAMPE5, 125, 250, 125, 250, 0, 0);
  131. mymakerange (RAMPB4, RAMPE4, 100, 200, 125, 250, 0, 0);
  132. mymakerange (RAMPB3, RAMPE3, 75, 150, 125, 250, 0, 0);
  133. mymakerange (RAMPB2, RAMPE2, 50, 100, 125, 250, 0, 0);
  134. mymakerange (RAMPB, RAMPE, 25, 50, 125, 250, 0, 0);
  135. } else {
  136. mapcolor (ECLIPSE8_GRAY, 128, 128, 128);
  137. mapcolor (ECLIPSE8_GRID, 128, 200, 250);
  138. mapcolor (ECLIPSE8_SKYBLUE, 50, 50, 150);
  139. /*
  140. mapcolor (BLACK, 0, 0, 0);
  141. mapcolor (COLORS, 255, 0, 0);
  142. mapcolor (COLOR1, 0, 255, 0);
  143. mapcolor (COLOR2, 255, 255, 0);
  144. */
  145. make_eclipse8_range(ECLIPSE8_RAMP, 25, 250, 125, 250, 0, 0);
  146. }
  147. }
  148. /**** New routines for ECLIPSE 8 bit machine */
  149. /* reduces color index value to the lower 16 indices in the color table
  150. see ECLIPSE8_RAMP[] for which entries are used for ramp */
  151. short
  152. reduce_index (short c)
  153. {
  154. c = c - RAMPB + 4;
  155. while (c > 13)
  156. c -= 10;
  157. if (c > 6)
  158. c++;
  159. return(c);
  160. }
  161. void
  162. getcoords (void)
  163. {
  164. pikx = getvaluator(MOUSEX);
  165. piky = getvaluator(MOUSEY);
  166. if (pikx <= worgx || pikx >= worgx + wsizex ||
  167. piky <= worgy || piky >= worgy + wsizey) {
  168. pikx = worgx + wsizex / 2 + 1;
  169. piky = worgy + wsizey / 2 + 1;
  170. }
  171. }
  172. static void
  173. getMouseCoords (void) {
  174. int x, y;
  175. tkGetMouseLoc (&x, &y);
  176. pikx = x;
  177. piky = wsizey - y;
  178. if ( (pikx < 0) || (pikx > wsizex) ||
  179. (piky < 0) || (piky > wsizey) ) {
  180. pikx = wsizex / 2 + 1;
  181. piky = wsizey / 2 + 1;
  182. }
  183. }
  184. int
  185. l05 (int i)
  186. {
  187. if (i < 0)
  188. return (i + 6);
  189. if (i > 5)
  190. return (i - 6);
  191. return (i);
  192. }
  193. int
  194. sgn (float i)
  195. {
  196. if (i < 0)
  197. return (-1);
  198. if (i > 0)
  199. return (1);
  200. return (0);
  201. }
  202. static void
  203. fixWindow (void) {
  204. halfwinx = (float)wsizex / 2.0;
  205. halfwiny = (float)wsizey / 2.0;
  206. glViewport (0, 0, wsizex, wsizey);
  207. glPopMatrix();
  208. glPushMatrix();
  209. gluPerspective (80, wsizex / (float) wsizey, 0.01, 131072);
  210. }
  211. /* draw_shadow -- draw halftone shape of insect onto the floor */
  212. void
  213. draw_shadow (void) {
  214. int leg;
  215. glPushMatrix ();
  216. glCallList (shadow);
  217. glEnable (GL_POLYGON_STIPPLE);
  218. glTranslatef (px, py, 1.0);
  219. glCallList (body_shadow);
  220. for (leg = 0; leg < 6; leg++) {
  221. glPushMatrix ();
  222. /*
  223. glRotatef ((GLfloat) (-leg * 600), 0, 0, 1);
  224. */
  225. glRotatef ((GLfloat) (-leg * 60), 0, 0, 1);
  226. glTranslatef (0.0, 0.5, 0.0);
  227. glCallList (hip_shadow);
  228. /*
  229. glRotatef (hip_phi[leg], 0, 0, 1);
  230. glRotatef (hip_theta[leg], 1, 0, 0);
  231. */
  232. glRotatef (hip_phi[leg] / 10, 0, 0, 1);
  233. glRotatef (hip_theta[leg]/10, 1, 0, 0);
  234. glCallList (thigh_shadow);
  235. glCallList (kneeball_shadow);
  236. glTranslatef (0.0, 1.0, 0.0);
  237. /*
  238. glRotatef (knee[leg], 1, 0, 0);
  239. */
  240. glRotatef (knee[leg]/10, 1, 0, 0);
  241. glCallList (shin_shadow);
  242. glPopMatrix ();
  243. }
  244. glDisable (GL_POLYGON_STIPPLE);
  245. glPopMatrix ();
  246. }
  247. /* draw_hind -- draw a rear leg. First draw hip, then shin, then thigh
  248. and knee joint */
  249. void
  250. draw_hind (int leg)
  251. {
  252. glPushMatrix ();
  253. /*
  254. glRotatef ((GLfloat) (-leg * 600), 0, 0, 1);
  255. */
  256. glRotatef ((GLfloat) (-leg * 60), 0, 0, 1);
  257. glTranslatef (0.0, 0.5, 0.0);
  258. glCallList (hip[leg]);
  259. /*
  260. glRotatef (hip_phi[leg], 0, 0, 1);
  261. glRotatef (hip_theta[leg], 1, 0, 0);
  262. */
  263. glRotatef (hip_phi[leg]/10, 0, 0, 1);
  264. glRotatef (hip_theta[leg]/10, 1, 0, 0);
  265. glPushMatrix (); /* draw thigh */
  266. glTranslatef (0.0, 1.0, 0.0);
  267. /*
  268. glRotatef (knee[leg], 1, 0, 0);
  269. */
  270. glRotatef (knee[leg]/10, 1, 0, 0);
  271. glCallList (shin[leg]);
  272. glPopMatrix ();
  273. if (cz < -5.0) {
  274. glCallList (kneeball[leg]);
  275. glCallList (thigh[leg]);
  276. }
  277. else {
  278. glCallList (kneeball[leg]);
  279. glCallList (thigh[leg]);
  280. }
  281. glPopMatrix ();
  282. }
  283. /* draw_fore -- draw a front leg. First draw hip, then thigh,
  284. knee joint and finally shin */
  285. void
  286. draw_fore (int leg)
  287. {
  288. glPushMatrix ();
  289. /*
  290. glRotatef ((GLfloat) (-leg * 600), 0, 0, 1);
  291. */
  292. glRotatef ((GLfloat) (-leg * 60), 0, 0, 1);
  293. glTranslatef (0.0, 0.5, 0.0);
  294. glCallList (hip[leg]);
  295. /*
  296. glRotatef (hip_phi[leg], 0, 0, 1);
  297. glRotatef (hip_theta[leg], 1, 0, 0);
  298. */
  299. glRotatef (hip_phi[leg]/10, 0, 0, 1);
  300. glRotatef (hip_theta[leg]/10, 1, 0, 0);
  301. glCallList (thigh[leg]);
  302. glCallList (kneeball[leg]);
  303. glTranslatef (0.0, 1.0, 0.0);
  304. /*
  305. glRotatef (knee[leg], 1, 0, 0);
  306. */
  307. glRotatef (knee[leg]/10, 1, 0, 0);
  308. glCallList (shin[leg]);
  309. glPopMatrix ();
  310. }
  311. /* draw_insect -- draw rear legs, body and forelegs of insect
  312. the order of drawing the objects is important to
  313. insure proper hidden surface elimination -- painter's algorithm */
  314. void
  315. draw_insect (void) {
  316. GLfloat a;
  317. float o;
  318. int order;
  319. o = atan2 (cy + py, cx + px) + PI - (PI / 3.0);
  320. order = l05 ((int) integer (o / (PI / 3.0)));
  321. glPushMatrix (); /* world */
  322. glTranslatef (px, py, 1.0);
  323. draw_hind (l05 (3 - order));
  324. draw_hind (l05 (4 - order));
  325. draw_hind (l05 (2 - order));
  326. glCallList (body);
  327. draw_fore (l05 (5 - order));
  328. draw_fore (l05 (1 - order));
  329. draw_fore (l05 (0 - order));
  330. glPopMatrix ();
  331. }
  332. /* spin_scene -- poll input devices, keyboard and mouse
  333. move eye position based upon user input */
  334. void
  335. spin_scene (void) {
  336. float sin1,
  337. cos1,
  338. sin2,
  339. cos2,
  340. t;
  341. int mx,
  342. my;
  343. /* big change to keep movement relative to window */
  344. /* check if still in window x and y are globals - see getcoords */
  345. /*
  346. getcoords ();
  347. mx = 64 * ((pikx - worgx) - (wsizex / 2)) / wsizex;
  348. my = 64 * ((piky - worgy) - (wsizey / 2)) / wsizey;
  349. */
  350. getMouseCoords();
  351. mx = 64 * (pikx - (wsizex/2)) / wsizex;
  352. my = 64 * (piky - (wsizey/2)) / wsizey;
  353. /*
  354. mx = (getvaluator (MOUSEX) - 640) / 16;
  355. my = (getvaluator (MOUSEY) - 512) / 16;
  356. */
  357. switch (function) {
  358. case BACKWARD:
  359. gl_sincos (ctheta, &sin1, &cos1);
  360. gl_sincos (cphi, &sin2, &cos2);
  361. cvz -= cos1;
  362. cvx -= sin2 * sin1;
  363. cvy -= cos2 * sin1;
  364. function = NOTHING;
  365. break;
  366. case FORWARD:
  367. gl_sincos (ctheta, &sin1, &cos1);
  368. gl_sincos (cphi, &sin2, &cos2);
  369. cvz += cos1;
  370. cvx += sin2 * sin1;
  371. cvy += cos2 * sin1;
  372. function = NOTHING;
  373. break;
  374. case LEFT:
  375. cvz = (float) - my;
  376. gl_sincos (cphi, &sin1, &cos1);
  377. cvx = cos1 * (float) (-mx);
  378. cvy = -sin1 * (float) (-mx);
  379. break;
  380. default:
  381. cvx = cvx * 0.7;
  382. cvy = cvy * 0.7;
  383. cvz = cvz * 0.7;
  384. break;
  385. }
  386. if (function == MIDDLE) {
  387. cvtheta = my;
  388. cvphi = mx;
  389. }
  390. else {
  391. cvtheta += -cvtheta / 4;
  392. if ((cvtheta < 4) && (cvtheta > -4))
  393. cvtheta = 0;
  394. cvphi += -cvphi / 4;
  395. if ((cvphi < 4) && (cvphi > -4))
  396. cvphi = 0;
  397. if (function != LEFT) function = NOTHING;
  398. }
  399. cx += cvx * 0.0078125;
  400. cy += cvy * 0.0078125;
  401. if (cz > 0.0) {
  402. cz = 0.0;
  403. cvz = 0.0;
  404. }
  405. else
  406. cz += cvz * 0.0078125;
  407. ctheta += cvtheta;
  408. cphi += cvphi;
  409. }
  410. GLfloat degrees (float a)
  411. {
  412. return ((GLfloat) (a * 1800.0 / PI));
  413. }
  414. void
  415. getstuff (void) {
  416. int x,
  417. y,
  418. i;
  419. int tr;
  420. legup[0] = GL_FALSE;
  421. legup[2] = GL_FALSE;
  422. legup[4] = GL_FALSE;
  423. legup[1] = GL_TRUE;
  424. legup[3] = GL_TRUE;
  425. legup[5] = GL_TRUE;
  426. px = 0.0;
  427. py = 0.0;
  428. for (i = 0; i < 6; i++) {
  429. legx[i] = 30.0 / 2.0 + (float) i;
  430. legy[i] = 30.0 / 2.0 + (float) i;
  431. }
  432. }
  433. void
  434. dolegs (void) {
  435. int i;
  436. float r,
  437. l,
  438. gx,
  439. gy,
  440. k,
  441. t,
  442. a,
  443. ux,
  444. uy;
  445. int leg,
  446. tr;
  447. for (leg = 0; leg < 6; leg++) {
  448. gx = legx[leg] - 30.0 / 2.0;
  449. gy = legy[leg] - 30.0 / 2.0;
  450. ux = gx / (30.0 / 2.0);
  451. uy = gy / (30.0 / 2.0);
  452. switch (leg) {
  453. case 0:
  454. gx += 0.0;
  455. gy += 30.0;
  456. break;
  457. case 1:
  458. gx += (30.0 * 0.8660254);
  459. gy += (30.0 * 0.5);
  460. break;
  461. case 2:
  462. gx += (30.0 * 0.8660254);
  463. gy += (30.0 * -0.5);
  464. break;
  465. case 3:
  466. gx += 0.0;
  467. gy += -30.0;
  468. break;
  469. case 4:
  470. gx += (30.0 * -0.8660254);
  471. gy += (30.0 * -0.5);;
  472. break;
  473. case 5:
  474. gx += (30.0 * -0.8660254);
  475. gy += (30.0 * 0.5);
  476. break;
  477. }
  478. r = sqrt ((gx * gx) + (gy * gy)) / 30.0;
  479. l = sqrt (1.0 + (r * r));
  480. k = acos ((5.0 - (l * l)) / 4.0);
  481. knee[leg] = (GLfloat) degrees (k);
  482. t = (2.0 * sin (k)) / l;
  483. if (t > 1.0)
  484. t = 1.0;
  485. a = asin (t);
  486. if (l < 1.7320508)
  487. a = PI - a;
  488. hip_theta[leg] =
  489. (GLfloat) (degrees (a - atan2 (1.0, r)));
  490. if (gx == 0.0) {
  491. hip_phi[leg] = (GLfloat) (900 * sgn (gy));
  492. }
  493. else {
  494. hip_phi[leg] = (GLfloat) (degrees (atan2 (gy, gx)));
  495. }
  496. hip_phi[leg] += (-900 + 600 * leg);
  497. if (legup[leg]) {
  498. hip_theta[leg] += (GLfloat)
  499. (200.0 * ((fr[leg] / 2.0) - fabso (dmr[leg] - (fr[leg] / 2.0))));
  500. }
  501. }
  502. }
  503. void
  504. move_insect (void) {
  505. register int i;
  506. register float mx,
  507. my,
  508. vx,
  509. vy,
  510. dx,
  511. dy,
  512. dr,
  513. lx,
  514. ly,
  515. lr,
  516. dmx,
  517. dmy;
  518. float s,
  519. c;
  520. /* mx = (float) getvaluator (MOUSEX) / 640.0 - 1.0;
  521. my = (float) getvaluator (MOUSEY) / 512.0 - 1.0;
  522. */
  523. /* changed to keep input within the window.
  524. x and y are globals - see getcoords */
  525. /*
  526. getcoords ();
  527. mx = ((float)pikx - (float)worgx) / halfwinx - 1.0;
  528. my = ((float)piky - (float)worgy) / halfwiny - 1.0;
  529. */
  530. getMouseCoords();
  531. mx = pikx / halfwinx - 1.0;
  532. my = piky / halfwiny - 1.0;
  533. gl_sincos (cphi, &s, &c);
  534. dx = mx * c + my * s;
  535. dy = -mx * s + my * c;
  536. mx = dx;
  537. my = dy;
  538. px += mx / (float) (RES);
  539. py += my / (float) (RES);
  540. if (follow) {
  541. cx -= mx / (float) (RES);
  542. cy -= my / (float) (RES);
  543. }
  544. dr = sqrt (mx * mx + my * my);
  545. dx = mx / dr;
  546. dy = my / dr;
  547. for (i = 0; i < 6; i++) {
  548. lx = legx[i] - (float) (RES / 2);
  549. ly = legy[i] - (float) (RES / 2);
  550. lr = (float) (RES / 2);
  551. lx = lx / lr;
  552. ly = ly / lr;
  553. dmx = (dx - lx);
  554. dmy = (dy - ly);
  555. dmr[i] = sqrt (dmx * dmx + dmy * dmy);
  556. if (legup[i]) {
  557. dmx = 3 * dr * dmx / dmr[i];
  558. dmy = 3 * dr * dmy / dmr[i];
  559. legx[i] += dmx;
  560. legy[i] += dmy;
  561. if ((dmr[i]) < 0.15) {
  562. legup[i] = GL_FALSE;
  563. }
  564. }
  565. else {
  566. legx[i] -= mx;
  567. legy[i] -= my;
  568. if (!legup[l05 (i - 1)] && !legup[l05 (i + 1)] &&
  569. (dmr[i] > REACH) &&
  570. ((lx * dx + ly * dy) < 0.0)) {
  571. legup[i] = GL_TRUE;
  572. fr[i] = dmr[i];
  573. legx[i] += dmx;
  574. legy[i] += dmy;
  575. }
  576. }
  577. }
  578. }
  579. void
  580. rotate60 (char c, int n, GLfloat a[][3])
  581. {
  582. int i,
  583. j,
  584. l;
  585. float nx,
  586. ny;
  587. switch (c) {
  588. case 'z':
  589. i = 0;
  590. j = 1;
  591. break;
  592. case 'y':
  593. i = 2;
  594. j = 0;
  595. break;
  596. case 'x':
  597. i = 1;
  598. j = 2;
  599. break;
  600. };
  601. for (l = 0; l < n; l++) {
  602. nx = a[l][i] * COS60 - a[l][j] * SIN60;
  603. ny = a[l][i] * SIN60 + a[l][j] * COS60;
  604. a[l][i] = nx;
  605. a[l][j] = ny;
  606. }
  607. }
  608. void
  609. getpolycolor (int p, float pts[][3])
  610. {
  611. float norm[3];
  612. float v1[3],
  613. v2[3],
  614. cons;
  615. int i,
  616. get;
  617. float c;
  618. for (i = 0; i < 3; i++)
  619. norm[i] = 0.0;
  620. i = 0;
  621. get = 1;
  622. i = 1;
  623. v1[0] = pts[1][0] - pts[0][0];
  624. v1[1] = pts[1][1] - pts[0][1];
  625. v1[2] = pts[1][2] - pts[0][2];
  626. v2[0] = pts[2][0] - pts[0][0];
  627. v2[1] = pts[2][1] - pts[0][1];
  628. v2[2] = pts[2][2] - pts[0][2];
  629. norm[0] = v1[1] * v2[2] - v1[2] * v2[1];
  630. norm[1] = v1[2] * v2[0] - v1[0] * v2[2];
  631. norm[2] = v1[0] * v2[1] - v1[1] * v2[0];
  632. cons = sqrt ((norm[0] * norm[0]) +
  633. (norm[1] * norm[1]) + (norm[2] * norm[2]));
  634. for (i = 0; i < 3; i++)
  635. norm[i] = norm[i] / cons;
  636. c = dot (norm, light);
  637. if (c < 0.0)
  638. c = 0.0;
  639. if (c > 1.0)
  640. c = 1.0;
  641. switch (p) {
  642. case 0:
  643. c = (float) (RAMPE - RAMPB) * c + (float) (RAMPB);
  644. if (((unsigned short) c) > RAMPE) c = (float) RAMPE;
  645. #ifdef FLAT_SHADING
  646. c = COLOR1;
  647. #endif
  648. break;
  649. case 1:
  650. c = (float) (RAMPE2 - RAMPB2) * c + (float) (RAMPB2);
  651. if (((unsigned short) c) > RAMPE2) c = (float) RAMPE2;
  652. #ifdef FLAT_SHADING
  653. c = COLOR2;
  654. #endif
  655. break;
  656. case 2:
  657. c = (float) (RAMPE3 - RAMPB3) * c + (float) (RAMPB3);
  658. if (((unsigned short) c) > RAMPE3) c = (float) RAMPE3;
  659. #ifdef FLAT_SHADING
  660. c = COLOR2;
  661. #endif
  662. break;
  663. case 3:
  664. c = (float) (RAMPE4 - RAMPB4) * c + (float) (RAMPB4);
  665. if (((unsigned short) c) > RAMPE4) c = (float) RAMPE4;
  666. #ifdef FLAT_SHADING
  667. c = COLOR2;
  668. #endif
  669. break;
  670. case 4:
  671. c = (float) (RAMPE5 - RAMPB5) * c + (float) (RAMPB5);
  672. if (((unsigned short) c) > RAMPE5) c = (float) RAMPE5;
  673. #ifdef FLAT_SHADING
  674. c = COLOR2;
  675. #endif
  676. break;
  677. }
  678. /* Changed for 8 bit ECLIPSE machine */
  679. if (is_8bit)
  680. c = (float)reduce_index((int)c);
  681. glIndexi (c);
  682. }
  683. GLboolean
  684. lit (int p, float pts[][3])
  685. {
  686. float norm[3];
  687. float v1[3],
  688. v2[3],
  689. cons;
  690. int i,
  691. get;
  692. float c;
  693. for (i = 0; i < 3; i++)
  694. norm[i] = 0.0;
  695. i = 0;
  696. get = 1;
  697. i = 1;
  698. v1[0] = pts[1][0] - pts[0][0];
  699. v1[1] = pts[1][1] - pts[0][1];
  700. v1[2] = pts[1][2] - pts[0][2];
  701. v2[0] = pts[2][0] - pts[0][0];
  702. v2[1] = pts[2][1] - pts[0][1];
  703. v2[2] = pts[2][2] - pts[0][2];
  704. norm[0] = v1[1] * v2[2] - v1[2] * v2[1];
  705. norm[1] = v1[2] * v2[0] - v1[0] * v2[2];
  706. norm[2] = v1[0] * v2[1] - v1[1] * v2[0];
  707. cons = sqrt ((norm[0] * norm[0]) +
  708. (norm[1] * norm[1]) + (norm[2] * norm[2]));
  709. for (i = 0; i < 3; i++)
  710. norm[i] = norm[i] / cons;
  711. c = dot (norm, light);
  712. return (c > 0.0);
  713. }
  714. float dot (float vec1[3], float vec2[3])
  715. {
  716. float xx;
  717. xx = (vec1[1] * vec2[1])
  718. + (vec1[2] * vec2[2])
  719. + (vec1[0] * vec2[0]);
  720. return ((float) xx);
  721. }
  722. void
  723. getlightvector (void) {
  724. float f;
  725. light[2] = cos (theta);
  726. f = sin (theta);
  727. light[1] = -sin (phi) * f;
  728. light[0] = -cos (phi) * f;
  729. }
  730. float integer (float x)
  731. {
  732. if (x < 0.0)
  733. x -= 1.0;
  734. x = (float) (int) x;
  735. return (x);
  736. }
  737. float frac (float x)
  738. {
  739. return (x - integer (x));
  740. }
  741. float
  742. fabso (float x)
  743. {
  744. if (x < 0.0)
  745. return (-x);
  746. else
  747. return (x);
  748. }
  749. void
  750. drawAll (void) {
  751. /* new for ECLIPSE 8 bit version */
  752. if (is_8bit)
  753. glClearIndex (ECLIPSE8_SKYBLUE);
  754. else
  755. glClearIndex (SKYBLUE);
  756. glClear (GL_COLOR_BUFFER_BIT);
  757. glPushMatrix();
  758. doViewit();
  759. glCallList (screen);
  760. draw_shadow();
  761. draw_insect();
  762. glPopMatrix();
  763. }
  764. static void Reshape(int width, int height)
  765. {
  766. wsizex = width;
  767. wsizey = height;
  768. fixWindow();
  769. }
  770. static GLenum Key(int key, GLenum mask)
  771. {
  772. switch (key) {
  773. case TK_ESCAPE:
  774. tkQuit();
  775. break;
  776. case TK_A:
  777. case TK_a:
  778. function = FORWARD;
  779. break;
  780. case TK_Z:
  781. case TK_z:
  782. function = BACKWARD;
  783. break;
  784. case TK_F:
  785. case TK_f:
  786. follow = !follow;
  787. break;
  788. default:
  789. return GL_FALSE;
  790. }
  791. return GL_TRUE;
  792. }
  793. static GLenum MouseUp(int mouseX, int mouseY, GLenum button)
  794. {
  795. switch (button) {
  796. case TK_LEFTBUTTON:
  797. function = NOTHING;
  798. break;
  799. case TK_MIDDLEBUTTON:
  800. function = NOTHING;
  801. break;
  802. default:
  803. return GL_FALSE;
  804. }
  805. return GL_TRUE;
  806. }
  807. static GLenum MouseDown(int mouseX, int mouseY, GLenum button)
  808. {
  809. switch (button) {
  810. case TK_LEFTBUTTON:
  811. function = LEFT;
  812. break;
  813. case TK_MIDDLEBUTTON:
  814. function = MIDDLE;
  815. break;
  816. default:
  817. return GL_FALSE;
  818. }
  819. return GL_TRUE;
  820. }
  821. static void animate (void) {
  822. spin_scene();
  823. move_insect();
  824. dolegs();
  825. drawAll();
  826. tkSwapBuffers();
  827. }
  828. /* main routine -- handle tokens of window manager
  829. -- display shadow and insect
  830. */
  831. void
  832. main (int argc, char *argv[]) {
  833. int i,
  834. j,
  835. k;
  836. short dev,
  837. val;
  838. long nplanes;
  839. GLboolean attached;
  840. follow = GL_TRUE;
  841. wsizex = 500;
  842. wsizey = 400;
  843. worgx = 252;
  844. worgy = 184;
  845. tkInitPosition(0, 0, wsizex, wsizey);
  846. tkInitDisplayMode(TK_INDEX|TK_DOUBLE|TK_DIRECT|TK_DEPTH16);
  847. if (tkInitWindow("Insect") == GL_FALSE) {
  848. tkQuit();
  849. }
  850. glPolygonStipple ((unsigned char *) halftone);
  851. glShadeModel(GL_FLAT);
  852. getlightvector ();
  853. /* Changes for ECLIPSE 8 bit machine */
  854. /*
  855. * Machines with enough bitplanes will use colormap entries CMAPSTART
  856. * to CMAPSTART+127. If the machine doesn't have enough bitplanes,
  857. * only colors 0 to 15 will be used (it is assumed that all machines
  858. * have at least 4 bitplanes in colormap double-buffered mode).
  859. */
  860. #ifdef ECLIPSE
  861. nplanes = glGetI(GD_BITS_NORM_DBL_CMODE);
  862. /* save color map in savearray */
  863. if ((1<<nplanes) > (CMAPSTART+127)) {
  864. is_8bit = GL_FALSE;
  865. }
  866. else {
  867. is_8bit = GL_TRUE;
  868. }
  869. #else
  870. nplanes = 4;
  871. is_8bit = GL_TRUE;
  872. #endif
  873. setupcolors ();
  874. /* initialize transformation stuff */
  875. cx = 0.0;
  876. cy = 10.0;
  877. cz = -2.0;
  878. cvx = cvy = cvz = 0.0;
  879. ctheta = -900;
  880. cphi = 0;
  881. cvtheta = cvphi = 0.0;
  882. function = NOTHING;
  883. glPushMatrix();
  884. fixWindow();
  885. createobjects ();
  886. getstuff ();
  887. spin_scene ();
  888. move_insect ();
  889. dolegs ();
  890. tkExposeFunc(Reshape);
  891. tkReshapeFunc(Reshape);
  892. tkKeyDownFunc(Key);
  893. tkMouseDownFunc(MouseDown);
  894. tkMouseUpFunc(MouseUp);
  895. tkIdleFunc(animate);
  896. tkExec();
  897. glPopMatrix();
  898. }