Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

tunnel2.c 12KB

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