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.

tunnel2.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * This program is under the GNU GPL.
  3. * Use at your own risk.
  4. *
  5. * You need TWO Voodoo Graphics boards in order to run
  6. * this demo !
  7. *
  8. * written by David Bucciarelli (tech.hmw@plus.it)
  9. * Humanware s.r.l.
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <math.h>
  14. #include <string.h>
  15. #ifdef WIN32
  16. #include <windows.h>
  17. #endif
  18. #include <GL/glut.h>
  19. #include "readtex.h"
  20. #include "tunneldat.h"
  21. #ifdef FX
  22. #endif
  23. #ifdef XMESA
  24. #include "GL/xmesa.h"
  25. static int fullscreen = 1;
  26. #endif
  27. #ifdef FX
  28. GLint fxMesaSelectCurrentBoard(int);
  29. #endif
  30. static int WIDTHC0 = 640;
  31. static int HEIGHTC0 = 480;
  32. static int WIDTHC1 = 640;
  33. static int HEIGHTC1 = 480;
  34. static GLint T0 = 0;
  35. static GLint Frames = 0;
  36. #define NUMBLOC 5
  37. #ifndef M_PI
  38. #define M_PI 3.1415926535
  39. #endif
  40. static float obs[3] = { 1000.0, 0.0, 2.0 };
  41. static float dir[3];
  42. static float v = 30.;
  43. static float alpha = 90.0;
  44. static float beta = 90.0;
  45. static int fog = 1;
  46. static int bfcull = 1;
  47. static int usetex = 1;
  48. static int cstrip = 0;
  49. static int help = 1;
  50. static int joyavailable = 0;
  51. static int joyactive = 0;
  52. static int channel[2];
  53. static GLuint t1id, t2id;
  54. static void
  55. inittextures(void)
  56. {
  57. glGenTextures(1, &t1id);
  58. glBindTexture(GL_TEXTURE_2D, t1id);
  59. if (!LoadRGBMipmaps("../images/tile.rgb", GL_RGB)) {
  60. fprintf(stderr, "Error reading a texture.\n");
  61. exit(-1);
  62. }
  63. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  64. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  65. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  66. GL_LINEAR_MIPMAP_NEAREST);
  67. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  68. glGenTextures(1, &t2id);
  69. glBindTexture(GL_TEXTURE_2D, t2id);
  70. if (!LoadRGBMipmaps("../images/bw.rgb", GL_RGB)) {
  71. fprintf(stderr, "Error reading a texture.\n");
  72. exit(-1);
  73. }
  74. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  75. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  76. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  77. GL_LINEAR_MIPMAP_LINEAR);
  78. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  79. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  80. }
  81. static void
  82. drawobjs(const int *l, const float *f)
  83. {
  84. int mend, j;
  85. if (cstrip) {
  86. float r = 0.33, g = 0.33, b = 0.33;
  87. for (; (*l) != 0;) {
  88. mend = *l++;
  89. r += 0.33;
  90. if (r > 1.0) {
  91. r = 0.33;
  92. g += 0.33;
  93. if (g > 1.0) {
  94. g = 0.33;
  95. b += 0.33;
  96. if (b > 1.0)
  97. b = 0.33;
  98. }
  99. }
  100. glColor3f(r, g, b);
  101. glBegin(GL_TRIANGLE_STRIP);
  102. for (j = 0; j < mend; j++) {
  103. f += 4;
  104. glTexCoord2fv(f);
  105. f += 2;
  106. glVertex3fv(f);
  107. f += 3;
  108. }
  109. glEnd();
  110. }
  111. }
  112. else
  113. for (; (*l) != 0;) {
  114. mend = *l++;
  115. glBegin(GL_TRIANGLE_STRIP);
  116. for (j = 0; j < mend; j++) {
  117. glColor4fv(f);
  118. f += 4;
  119. glTexCoord2fv(f);
  120. f += 2;
  121. glVertex3fv(f);
  122. f += 3;
  123. }
  124. glEnd();
  125. }
  126. }
  127. static void
  128. calcposobs(void)
  129. {
  130. static double t0 = -1.;
  131. double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  132. if (t0 < 0.0)
  133. t0 = t;
  134. dt = t - t0;
  135. t0 = t;
  136. dir[0] = sin(alpha * M_PI / 180.0);
  137. dir[1] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0);
  138. dir[2] = cos(beta * M_PI / 180.0);
  139. if (dir[0] < 1.0e-5 && dir[0] > -1.0e-5)
  140. dir[0] = 0;
  141. if (dir[1] < 1.0e-5 && dir[1] > -1.0e-5)
  142. dir[1] = 0;
  143. if (dir[2] < 1.0e-5 && dir[2] > -1.0e-5)
  144. dir[2] = 0;
  145. obs[0] += v * dir[0] * dt;
  146. obs[1] += v * dir[1] * dt;
  147. obs[2] += v * dir[2] * dt;
  148. }
  149. static void
  150. special(int k, int x, int y)
  151. {
  152. switch (k) {
  153. case GLUT_KEY_LEFT:
  154. alpha -= 2.0;
  155. break;
  156. case GLUT_KEY_RIGHT:
  157. alpha += 2.0;
  158. break;
  159. case GLUT_KEY_DOWN:
  160. beta -= 2.0;
  161. break;
  162. case GLUT_KEY_UP:
  163. beta += 2.0;
  164. break;
  165. }
  166. }
  167. static void
  168. key(unsigned char k, int x, int y)
  169. {
  170. switch (k) {
  171. case 27:
  172. glutDestroyWindow(channel[0]);
  173. glutDestroyWindow(channel[1]);
  174. exit(0);
  175. break;
  176. case 'a':
  177. v += 5.;
  178. break;
  179. case 'z':
  180. v -= 5.;
  181. break;
  182. #ifdef XMESA
  183. case ' ':
  184. fullscreen = (!fullscreen);
  185. glutSetWindow(channel[0]);
  186. XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
  187. glutSetWindow(channel[1]);
  188. XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
  189. break;
  190. #endif
  191. case 'j':
  192. joyactive = (!joyactive);
  193. break;
  194. case 'h':
  195. help = (!help);
  196. break;
  197. case 'f':
  198. fog = (!fog);
  199. break;
  200. case 't':
  201. usetex = (!usetex);
  202. break;
  203. case 'b':
  204. if (bfcull) {
  205. glDisable(GL_CULL_FACE);
  206. bfcull = 0;
  207. }
  208. else {
  209. glEnable(GL_CULL_FACE);
  210. bfcull = 1;
  211. }
  212. break;
  213. case 'm':
  214. cstrip = (!cstrip);
  215. break;
  216. case 'd':
  217. fprintf(stderr, "Deleting textures...\n");
  218. glDeleteTextures(1, &t1id);
  219. glDeleteTextures(1, &t2id);
  220. fprintf(stderr, "Loading textures...\n");
  221. inittextures();
  222. fprintf(stderr, "Done.\n");
  223. break;
  224. }
  225. }
  226. static void
  227. reshapechannel0(int w, int h)
  228. {
  229. float ratio;
  230. WIDTHC0 = w;
  231. HEIGHTC0 = h;
  232. glMatrixMode(GL_PROJECTION);
  233. glLoadIdentity();
  234. ratio = 0.5f * w / (float) h;
  235. glFrustum(-2.0, 0.0, -1.0 * ratio, 1.0 * ratio, 1.0, 60.0);
  236. glMatrixMode(GL_MODELVIEW);
  237. glLoadIdentity();
  238. glViewport(0, 0, w, h);
  239. }
  240. static void
  241. reshapechannel1(int w, int h)
  242. {
  243. float ratio;
  244. WIDTHC1 = w;
  245. HEIGHTC1 = h;
  246. glMatrixMode(GL_PROJECTION);
  247. glLoadIdentity();
  248. ratio = 0.5f * w / (float) h;
  249. glFrustum(0.0, 2.0, -1.0 * ratio, 1.0 * ratio, 1.0, 60.0);
  250. glMatrixMode(GL_MODELVIEW);
  251. glLoadIdentity();
  252. glViewport(0, 0, w, h);
  253. }
  254. static void
  255. printstring(void *font, char *string)
  256. {
  257. int len, i;
  258. len = (int) strlen(string);
  259. for (i = 0; i < len; i++)
  260. glutBitmapCharacter(font, string[i]);
  261. }
  262. static void
  263. printhelp(void)
  264. {
  265. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  266. glColor4f(0.0, 0.0, 0.0, 0.5);
  267. glRecti(40, 40, 600, 440);
  268. glColor3f(1.0, 0.0, 0.0);
  269. glRasterPos2i(300, 420);
  270. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Help");
  271. glRasterPos2i(60, 390);
  272. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Toggle Help");
  273. glRasterPos2i(60, 360);
  274. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Toggle Textures");
  275. glRasterPos2i(60, 330);
  276. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Toggle Fog");
  277. glRasterPos2i(60, 300);
  278. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "m - Toggle strips");
  279. glRasterPos2i(60, 270);
  280. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Toggle Back face culling");
  281. glRasterPos2i(60, 240);
  282. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Arrow Keys - Rotate");
  283. glRasterPos2i(60, 210);
  284. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "a - Increase velocity");
  285. glRasterPos2i(60, 180);
  286. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "z - Decrease velocity");
  287. glRasterPos2i(60, 150);
  288. if (joyavailable)
  289. printstring(GLUT_BITMAP_TIMES_ROMAN_24,
  290. "j - Toggle jostick control (Joystick control available)");
  291. else
  292. printstring(GLUT_BITMAP_TIMES_ROMAN_24,
  293. "(No Joystick control available)");
  294. }
  295. static void
  296. dojoy(void)
  297. {
  298. #ifdef WIN32
  299. static UINT max[2] = { 0, 0 };
  300. static UINT min[2] = { 0xffffffff, 0xffffffff }, center[2];
  301. MMRESULT res;
  302. JOYINFO joy;
  303. res = joyGetPos(JOYSTICKID1, &joy);
  304. if (res == JOYERR_NOERROR) {
  305. joyavailable = 1;
  306. if (max[0] < joy.wXpos)
  307. max[0] = joy.wXpos;
  308. if (min[0] > joy.wXpos)
  309. min[0] = joy.wXpos;
  310. center[0] = (max[0] + min[0]) / 2;
  311. if (max[1] < joy.wYpos)
  312. max[1] = joy.wYpos;
  313. if (min[1] > joy.wYpos)
  314. min[1] = joy.wYpos;
  315. center[1] = (max[1] + min[1]) / 2;
  316. if (joyactive) {
  317. if (fabs(center[0] - (float) joy.wXpos) > 0.1 * (max[0] - min[0]))
  318. alpha -=
  319. 2.0 * (center[0] - (float) joy.wXpos) / (max[0] - min[0]);
  320. if (fabs(center[1] - (float) joy.wYpos) > 0.1 * (max[1] - min[1]))
  321. beta += 2.0 * (center[1] - (float) joy.wYpos) / (max[1] - min[1]);
  322. if (joy.wButtons & JOY_BUTTON1)
  323. v += 0.01;
  324. if (joy.wButtons & JOY_BUTTON2)
  325. v -= 0.01;
  326. }
  327. }
  328. else
  329. joyavailable = 0;
  330. #endif
  331. }
  332. static void
  333. draw(void)
  334. {
  335. static char frbuf[80] = "";
  336. int i;
  337. float base, offset;
  338. dojoy();
  339. glClear(GL_COLOR_BUFFER_BIT);
  340. glClear(GL_COLOR_BUFFER_BIT);
  341. if (usetex)
  342. glEnable(GL_TEXTURE_2D);
  343. else
  344. glDisable(GL_TEXTURE_2D);
  345. if (fog)
  346. glEnable(GL_FOG);
  347. else
  348. glDisable(GL_FOG);
  349. glShadeModel(GL_SMOOTH);
  350. glPushMatrix();
  351. calcposobs();
  352. gluLookAt(obs[0], obs[1], obs[2],
  353. obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2],
  354. 0.0, 0.0, 1.0);
  355. if (dir[0] > 0) {
  356. offset = 8.0;
  357. base = obs[0] - fmod(obs[0], 8.0);
  358. }
  359. else {
  360. offset = -8.0;
  361. base = obs[0] + (8.0 - fmod(obs[0], 8.0));
  362. }
  363. glPushMatrix();
  364. glTranslatef(base - offset / 2.0, 0.0, 0.0);
  365. for (i = 0; i < NUMBLOC; i++) {
  366. glTranslatef(offset, 0.0, 0.0);
  367. glBindTexture(GL_TEXTURE_2D, t1id);
  368. drawobjs(striplength_skin_11, stripdata_skin_11);
  369. glBindTexture(GL_TEXTURE_2D, t2id);
  370. drawobjs(striplength_skin_12, stripdata_skin_12);
  371. drawobjs(striplength_skin_9, stripdata_skin_9);
  372. drawobjs(striplength_skin_13, stripdata_skin_13);
  373. }
  374. glPopMatrix();
  375. glPopMatrix();
  376. glDisable(GL_TEXTURE_2D);
  377. glDisable(GL_FOG);
  378. glShadeModel(GL_FLAT);
  379. glMatrixMode(GL_PROJECTION);
  380. glPushMatrix();
  381. glLoadIdentity();
  382. glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0);
  383. glMatrixMode(GL_MODELVIEW);
  384. glLoadIdentity();
  385. glColor3f(1.0, 0.0, 0.0);
  386. glRasterPos2i(10, 10);
  387. printstring(GLUT_BITMAP_HELVETICA_18, frbuf);
  388. glRasterPos2i(350, 470);
  389. printstring(GLUT_BITMAP_HELVETICA_10,
  390. "Tunnel2 V1.0 Written by David Bucciarelli (tech.hmw@plus.it)");
  391. if (help)
  392. printhelp();
  393. glMatrixMode(GL_PROJECTION);
  394. glPopMatrix();
  395. glMatrixMode(GL_MODELVIEW);
  396. Frames++;
  397. {
  398. GLint t = glutGet(GLUT_ELAPSED_TIME);
  399. if (t - T0 >= 2000) {
  400. GLfloat seconds = (t - T0) / 1000.0;
  401. GLfloat fps = Frames / seconds;
  402. sprintf(frbuf, "Frame rate: %f", fps);
  403. T0 = t;
  404. Frames = 0;
  405. }
  406. }
  407. }
  408. static void
  409. drawchannel0(void)
  410. {
  411. glutSetWindow(channel[0]);
  412. draw();
  413. glutSwapBuffers();
  414. }
  415. static void
  416. drawchannel1(void)
  417. {
  418. glutSetWindow(channel[1]);
  419. draw();
  420. glutSwapBuffers();
  421. }
  422. static void
  423. drawall(void)
  424. {
  425. glutSetWindow(channel[0]);
  426. draw();
  427. glutSetWindow(channel[1]);
  428. draw();
  429. glutSetWindow(channel[0]);
  430. glutSwapBuffers();
  431. glutSetWindow(channel[1]);
  432. glutSwapBuffers();
  433. }
  434. static void
  435. init(void)
  436. {
  437. float fogcolor[4] = { 0.7, 0.7, 0.7, 1.0 };
  438. glShadeModel(GL_SMOOTH);
  439. glDisable(GL_DEPTH_TEST);
  440. glEnable(GL_CULL_FACE);
  441. glEnable(GL_TEXTURE_2D);
  442. glEnable(GL_FOG);
  443. glFogi(GL_FOG_MODE, GL_EXP2);
  444. glFogfv(GL_FOG_COLOR, fogcolor);
  445. glFogf(GL_FOG_DENSITY, 0.06);
  446. glHint(GL_FOG_HINT, GL_NICEST);
  447. glEnable(GL_BLEND);
  448. /*
  449. glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
  450. glEnable(GL_POLYGON_SMOOTH);
  451. */
  452. glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]);
  453. glClear(GL_COLOR_BUFFER_BIT);
  454. }
  455. int
  456. main(int ac, char **av)
  457. {
  458. fprintf(stderr,
  459. "Tunnel2 V1.0\nWritten by David Bucciarelli (tech.hmw@plus.it)\n");
  460. glutInitWindowPosition(0, 0);
  461. glutInitWindowSize(WIDTHC0, HEIGHTC0);
  462. glutInit(&ac, av);
  463. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  464. #ifdef FX
  465. if (fxMesaSelectCurrentBoard(0) < 0) {
  466. fprintf(stderr, "The first Voodoo Graphics board is missing !?!?\n");
  467. return -1;
  468. }
  469. #endif
  470. if (!(channel[0] = glutCreateWindow("Channel 0"))) {
  471. fprintf(stderr, "Error, couldn't open window\n");
  472. return -1;
  473. }
  474. reshapechannel0(WIDTHC0, HEIGHTC0);
  475. init();
  476. inittextures();
  477. glutDisplayFunc(drawchannel0);
  478. glutReshapeFunc(reshapechannel0);
  479. glutKeyboardFunc(key);
  480. glutSpecialFunc(special);
  481. #ifdef FX
  482. if (fxMesaSelectCurrentBoard(1) < 0) {
  483. fprintf(stderr, "The second Voodoo Graphics board is missing !\n");
  484. exit(-1);
  485. }
  486. #endif
  487. glutInitWindowPosition(WIDTHC0, 0);
  488. glutInitWindowSize(WIDTHC1, HEIGHTC1);
  489. if (!(channel[1] = glutCreateWindow("Channel 1"))) {
  490. fprintf(stderr, "Error, couldn't open window\n");
  491. exit(-1);
  492. }
  493. reshapechannel1(WIDTHC1, HEIGHTC1);
  494. init();
  495. inittextures();
  496. glutDisplayFunc(drawchannel1);
  497. glutReshapeFunc(reshapechannel1);
  498. glutKeyboardFunc(key);
  499. glutSpecialFunc(special);
  500. glutIdleFunc(drawall);
  501. calcposobs();
  502. glutMainLoop();
  503. return 0;
  504. }