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

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