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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. cleanup(void)
  169. {
  170. glDeleteTextures(1, &t1id);
  171. glDeleteTextures(1, &t2id);
  172. }
  173. static void
  174. key(unsigned char k, int x, int y)
  175. {
  176. switch (k) {
  177. case 27:
  178. glutDestroyWindow(channel[0]);
  179. glutDestroyWindow(channel[1]);
  180. cleanup();
  181. exit(0);
  182. break;
  183. case 'a':
  184. v += 5.;
  185. break;
  186. case 'z':
  187. v -= 5.;
  188. break;
  189. #ifdef XMESA
  190. case ' ':
  191. fullscreen = (!fullscreen);
  192. glutSetWindow(channel[0]);
  193. XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
  194. glutSetWindow(channel[1]);
  195. XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
  196. break;
  197. #endif
  198. case 'j':
  199. joyactive = (!joyactive);
  200. break;
  201. case 'h':
  202. help = (!help);
  203. break;
  204. case 'f':
  205. fog = (!fog);
  206. break;
  207. case 't':
  208. usetex = (!usetex);
  209. break;
  210. case 'b':
  211. if (bfcull) {
  212. glDisable(GL_CULL_FACE);
  213. bfcull = 0;
  214. }
  215. else {
  216. glEnable(GL_CULL_FACE);
  217. bfcull = 1;
  218. }
  219. break;
  220. case 'm':
  221. cstrip = (!cstrip);
  222. break;
  223. case 'd':
  224. fprintf(stderr, "Deleting textures...\n");
  225. glDeleteTextures(1, &t1id);
  226. glDeleteTextures(1, &t2id);
  227. fprintf(stderr, "Loading textures...\n");
  228. inittextures();
  229. fprintf(stderr, "Done.\n");
  230. break;
  231. }
  232. }
  233. static void
  234. reshapechannel0(int w, int h)
  235. {
  236. float ratio;
  237. WIDTHC0 = w;
  238. HEIGHTC0 = h;
  239. glMatrixMode(GL_PROJECTION);
  240. glLoadIdentity();
  241. ratio = 0.5f * w / (float) h;
  242. glFrustum(-2.0, 0.0, -1.0 * ratio, 1.0 * ratio, 1.0, 60.0);
  243. glMatrixMode(GL_MODELVIEW);
  244. glLoadIdentity();
  245. glViewport(0, 0, w, h);
  246. }
  247. static void
  248. reshapechannel1(int w, int h)
  249. {
  250. float ratio;
  251. WIDTHC1 = w;
  252. HEIGHTC1 = h;
  253. glMatrixMode(GL_PROJECTION);
  254. glLoadIdentity();
  255. ratio = 0.5f * w / (float) h;
  256. glFrustum(0.0, 2.0, -1.0 * ratio, 1.0 * ratio, 1.0, 60.0);
  257. glMatrixMode(GL_MODELVIEW);
  258. glLoadIdentity();
  259. glViewport(0, 0, w, h);
  260. }
  261. static void
  262. printstring(void *font, char *string)
  263. {
  264. int len, i;
  265. len = (int) strlen(string);
  266. for (i = 0; i < len; i++)
  267. glutBitmapCharacter(font, string[i]);
  268. }
  269. static void
  270. printhelp(void)
  271. {
  272. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  273. glColor4f(0.0, 0.0, 0.0, 0.5);
  274. glRecti(40, 40, 600, 440);
  275. glColor3f(1.0, 0.0, 0.0);
  276. glRasterPos2i(300, 420);
  277. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Help");
  278. glRasterPos2i(60, 390);
  279. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Toggle Help");
  280. glRasterPos2i(60, 360);
  281. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Toggle Textures");
  282. glRasterPos2i(60, 330);
  283. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Toggle Fog");
  284. glRasterPos2i(60, 300);
  285. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "m - Toggle strips");
  286. glRasterPos2i(60, 270);
  287. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Toggle Back face culling");
  288. glRasterPos2i(60, 240);
  289. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Arrow Keys - Rotate");
  290. glRasterPos2i(60, 210);
  291. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "a - Increase velocity");
  292. glRasterPos2i(60, 180);
  293. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "z - Decrease velocity");
  294. glRasterPos2i(60, 150);
  295. if (joyavailable)
  296. printstring(GLUT_BITMAP_TIMES_ROMAN_24,
  297. "j - Toggle jostick control (Joystick control available)");
  298. else
  299. printstring(GLUT_BITMAP_TIMES_ROMAN_24,
  300. "(No Joystick control available)");
  301. }
  302. static void
  303. dojoy(void)
  304. {
  305. #ifdef WIN32
  306. static UINT max[2] = { 0, 0 };
  307. static UINT min[2] = { 0xffffffff, 0xffffffff }, center[2];
  308. MMRESULT res;
  309. JOYINFO joy;
  310. res = joyGetPos(JOYSTICKID1, &joy);
  311. if (res == JOYERR_NOERROR) {
  312. joyavailable = 1;
  313. if (max[0] < joy.wXpos)
  314. max[0] = joy.wXpos;
  315. if (min[0] > joy.wXpos)
  316. min[0] = joy.wXpos;
  317. center[0] = (max[0] + min[0]) / 2;
  318. if (max[1] < joy.wYpos)
  319. max[1] = joy.wYpos;
  320. if (min[1] > joy.wYpos)
  321. min[1] = joy.wYpos;
  322. center[1] = (max[1] + min[1]) / 2;
  323. if (joyactive) {
  324. if (fabs(center[0] - (float) joy.wXpos) > 0.1 * (max[0] - min[0]))
  325. alpha -=
  326. 2.0 * (center[0] - (float) joy.wXpos) / (max[0] - min[0]);
  327. if (fabs(center[1] - (float) joy.wYpos) > 0.1 * (max[1] - min[1]))
  328. beta += 2.0 * (center[1] - (float) joy.wYpos) / (max[1] - min[1]);
  329. if (joy.wButtons & JOY_BUTTON1)
  330. v += 0.01;
  331. if (joy.wButtons & JOY_BUTTON2)
  332. v -= 0.01;
  333. }
  334. }
  335. else
  336. joyavailable = 0;
  337. #endif
  338. }
  339. static void
  340. draw(void)
  341. {
  342. static char frbuf[80] = "";
  343. int i;
  344. float base, offset;
  345. dojoy();
  346. glClear(GL_COLOR_BUFFER_BIT);
  347. glClear(GL_COLOR_BUFFER_BIT);
  348. if (usetex)
  349. glEnable(GL_TEXTURE_2D);
  350. else
  351. glDisable(GL_TEXTURE_2D);
  352. if (fog)
  353. glEnable(GL_FOG);
  354. else
  355. glDisable(GL_FOG);
  356. glShadeModel(GL_SMOOTH);
  357. glPushMatrix();
  358. calcposobs();
  359. gluLookAt(obs[0], obs[1], obs[2],
  360. obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2],
  361. 0.0, 0.0, 1.0);
  362. if (dir[0] > 0) {
  363. offset = 8.0;
  364. base = obs[0] - fmod(obs[0], 8.0);
  365. }
  366. else {
  367. offset = -8.0;
  368. base = obs[0] + (8.0 - fmod(obs[0], 8.0));
  369. }
  370. glPushMatrix();
  371. glTranslatef(base - offset / 2.0, 0.0, 0.0);
  372. for (i = 0; i < NUMBLOC; i++) {
  373. glTranslatef(offset, 0.0, 0.0);
  374. glBindTexture(GL_TEXTURE_2D, t1id);
  375. drawobjs(striplength_skin_11, stripdata_skin_11);
  376. glBindTexture(GL_TEXTURE_2D, t2id);
  377. drawobjs(striplength_skin_12, stripdata_skin_12);
  378. drawobjs(striplength_skin_9, stripdata_skin_9);
  379. drawobjs(striplength_skin_13, stripdata_skin_13);
  380. }
  381. glPopMatrix();
  382. glPopMatrix();
  383. glDisable(GL_TEXTURE_2D);
  384. glDisable(GL_FOG);
  385. glShadeModel(GL_FLAT);
  386. glMatrixMode(GL_PROJECTION);
  387. glPushMatrix();
  388. glLoadIdentity();
  389. glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0);
  390. glMatrixMode(GL_MODELVIEW);
  391. glLoadIdentity();
  392. glColor3f(1.0, 0.0, 0.0);
  393. glRasterPos2i(10, 10);
  394. printstring(GLUT_BITMAP_HELVETICA_18, frbuf);
  395. glRasterPos2i(350, 470);
  396. printstring(GLUT_BITMAP_HELVETICA_10,
  397. "Tunnel2 V1.0 Written by David Bucciarelli (tech.hmw@plus.it)");
  398. if (help)
  399. printhelp();
  400. glMatrixMode(GL_PROJECTION);
  401. glPopMatrix();
  402. glMatrixMode(GL_MODELVIEW);
  403. Frames++;
  404. {
  405. GLint t = glutGet(GLUT_ELAPSED_TIME);
  406. if (t - T0 >= 2000) {
  407. GLfloat seconds = (t - T0) / 1000.0;
  408. GLfloat fps = Frames / seconds;
  409. sprintf(frbuf, "Frame rate: %f", fps);
  410. T0 = t;
  411. Frames = 0;
  412. }
  413. }
  414. }
  415. static void
  416. drawchannel0(void)
  417. {
  418. glutSetWindow(channel[0]);
  419. draw();
  420. glutSwapBuffers();
  421. }
  422. static void
  423. drawchannel1(void)
  424. {
  425. glutSetWindow(channel[1]);
  426. draw();
  427. glutSwapBuffers();
  428. }
  429. static void
  430. drawall(void)
  431. {
  432. glutSetWindow(channel[0]);
  433. draw();
  434. glutSetWindow(channel[1]);
  435. draw();
  436. glutSetWindow(channel[0]);
  437. glutSwapBuffers();
  438. glutSetWindow(channel[1]);
  439. glutSwapBuffers();
  440. }
  441. static void
  442. init(void)
  443. {
  444. float fogcolor[4] = { 0.7, 0.7, 0.7, 1.0 };
  445. glShadeModel(GL_SMOOTH);
  446. glDisable(GL_DEPTH_TEST);
  447. glEnable(GL_CULL_FACE);
  448. glEnable(GL_TEXTURE_2D);
  449. glEnable(GL_FOG);
  450. glFogi(GL_FOG_MODE, GL_EXP2);
  451. glFogfv(GL_FOG_COLOR, fogcolor);
  452. glFogf(GL_FOG_DENSITY, 0.06);
  453. glHint(GL_FOG_HINT, GL_NICEST);
  454. glEnable(GL_BLEND);
  455. /*
  456. glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
  457. glEnable(GL_POLYGON_SMOOTH);
  458. */
  459. glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]);
  460. glClear(GL_COLOR_BUFFER_BIT);
  461. }
  462. int
  463. main(int ac, char **av)
  464. {
  465. fprintf(stderr,
  466. "Tunnel2 V1.0\nWritten by David Bucciarelli (tech.hmw@plus.it)\n");
  467. glutInitWindowSize(WIDTHC0, HEIGHTC0);
  468. glutInit(&ac, av);
  469. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  470. #ifdef FX
  471. if (fxMesaSelectCurrentBoard(0) < 0) {
  472. fprintf(stderr, "The first Voodoo Graphics board is missing !?!?\n");
  473. return -1;
  474. }
  475. #endif
  476. if (!(channel[0] = glutCreateWindow("Channel 0"))) {
  477. fprintf(stderr, "Error, couldn't open window\n");
  478. return -1;
  479. }
  480. reshapechannel0(WIDTHC0, HEIGHTC0);
  481. init();
  482. inittextures();
  483. glutDisplayFunc(drawchannel0);
  484. glutReshapeFunc(reshapechannel0);
  485. glutKeyboardFunc(key);
  486. glutSpecialFunc(special);
  487. #ifdef FX
  488. if (fxMesaSelectCurrentBoard(1) < 0) {
  489. fprintf(stderr, "The second Voodoo Graphics board is missing !\n");
  490. exit(-1);
  491. }
  492. #endif
  493. glutInitWindowPosition(WIDTHC0, 0);
  494. glutInitWindowSize(WIDTHC1, HEIGHTC1);
  495. if (!(channel[1] = glutCreateWindow("Channel 1"))) {
  496. fprintf(stderr, "Error, couldn't open window\n");
  497. exit(-1);
  498. }
  499. reshapechannel1(WIDTHC1, HEIGHTC1);
  500. init();
  501. inittextures();
  502. glutDisplayFunc(drawchannel1);
  503. glutReshapeFunc(reshapechannel1);
  504. glutKeyboardFunc(key);
  505. glutSpecialFunc(special);
  506. glutIdleFunc(drawall);
  507. calcposobs();
  508. glutMainLoop();
  509. cleanup();
  510. return 0;
  511. }