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

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