Clone of mesa.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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