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.

tunnel.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * This program is under the GNU GPL.
  3. * Use at your own risk.
  4. *
  5. * written by David Bucciarelli (tech.hmw@plus.it)
  6. * Humanware s.r.l.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <time.h>
  12. #ifdef WIN32
  13. #include <windows.h>
  14. #endif
  15. #include <GL/glut.h>
  16. #include "../util/readtex.c"
  17. #include "tunneldat.c"
  18. #ifdef XMESA
  19. #include "GL/xmesa.h"
  20. static int fullscreen = 1;
  21. #endif
  22. static int WIDTH = 640;
  23. static int HEIGHT = 480;
  24. #define FRAME 50
  25. #define NUMBLOC 5
  26. #ifndef M_PI
  27. #define M_PI 3.1415926535
  28. #endif
  29. extern int striplength_skin_13[];
  30. extern float stripdata_skin_13[];
  31. extern int striplength_skin_12[];
  32. extern float stripdata_skin_12[];
  33. extern int striplength_skin_11[];
  34. extern float stripdata_skin_11[];
  35. extern int striplength_skin_9[];
  36. extern float stripdata_skin_9[];
  37. static int win = 0;
  38. static float obs[3] = { 1000.0, 0.0, 2.0 };
  39. static float dir[3];
  40. static float v = 0.5;
  41. static float alpha = 90.0;
  42. static float beta = 90.0;
  43. static int fog = 0;
  44. static int bfcull = 1;
  45. static int usetex = 1;
  46. static int cstrip = 0;
  47. static int help = 1;
  48. static int joyavailable = 0;
  49. static int joyactive = 0;
  50. static GLuint t1id, t2id;
  51. static void
  52. inittextures(void)
  53. {
  54. glGenTextures(1, &t1id);
  55. glBindTexture(GL_TEXTURE_2D, t1id);
  56. if (!LoadRGBMipmaps("../images/tile.rgb", GL_RGB)) {
  57. fprintf(stderr, "Error reading a texture.\n");
  58. exit(-1);
  59. }
  60. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  61. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  62. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  63. GL_LINEAR_MIPMAP_LINEAR);
  64. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  65. glGenTextures(1, &t2id);
  66. glBindTexture(GL_TEXTURE_2D, t2id);
  67. if (!LoadRGBMipmaps("../images/bw.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_LINEAR);
  75. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  76. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  77. }
  78. static void
  79. drawobjs(int *l, float *f)
  80. {
  81. int mend, j;
  82. if (cstrip) {
  83. float r = 0.33, g = 0.33, b = 0.33;
  84. for (; (*l) != 0;) {
  85. mend = *l++;
  86. r += 0.33;
  87. if (r > 1.0) {
  88. r = 0.33;
  89. g += 0.33;
  90. if (g > 1.0) {
  91. g = 0.33;
  92. b += 0.33;
  93. if (b > 1.0)
  94. b = 0.33;
  95. }
  96. }
  97. glColor3f(r, g, b);
  98. glBegin(GL_TRIANGLE_STRIP);
  99. for (j = 0; j < mend; j++) {
  100. f += 4;
  101. glTexCoord2fv(f);
  102. f += 2;
  103. glVertex3fv(f);
  104. f += 3;
  105. }
  106. glEnd();
  107. }
  108. }
  109. else
  110. for (; (*l) != 0;) {
  111. mend = *l++;
  112. glBegin(GL_TRIANGLE_STRIP);
  113. for (j = 0; j < mend; j++) {
  114. glColor4fv(f);
  115. f += 4;
  116. glTexCoord2fv(f);
  117. f += 2;
  118. glVertex3fv(f);
  119. f += 3;
  120. }
  121. glEnd();
  122. }
  123. }
  124. static float
  125. gettime(void)
  126. {
  127. static clock_t told = 0;
  128. clock_t tnew, ris;
  129. tnew = clock();
  130. ris = tnew - told;
  131. told = tnew;
  132. return (ris / (float) CLOCKS_PER_SEC);
  133. }
  134. static void
  135. calcposobs(void)
  136. {
  137. dir[0] = sin(alpha * M_PI / 180.0);
  138. dir[1] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0);
  139. dir[2] = cos(beta * M_PI / 180.0);
  140. obs[0] += v * dir[0];
  141. obs[1] += v * dir[1];
  142. obs[2] += v * dir[2];
  143. }
  144. static void
  145. special(int k, int x, int y)
  146. {
  147. switch (k) {
  148. case GLUT_KEY_LEFT:
  149. alpha -= 2.0;
  150. break;
  151. case GLUT_KEY_RIGHT:
  152. alpha += 2.0;
  153. break;
  154. case GLUT_KEY_DOWN:
  155. beta -= 2.0;
  156. break;
  157. case GLUT_KEY_UP:
  158. beta += 2.0;
  159. break;
  160. }
  161. }
  162. static void
  163. key(unsigned char k, int x, int y)
  164. {
  165. switch (k) {
  166. case 27:
  167. exit(0);
  168. break;
  169. case 'a':
  170. v += 0.01;
  171. break;
  172. case 'z':
  173. v -= 0.01;
  174. break;
  175. #ifdef XMESA
  176. case ' ':
  177. fullscreen = (!fullscreen);
  178. XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
  179. break;
  180. #endif
  181. case 'j':
  182. joyactive = (!joyactive);
  183. break;
  184. case 'h':
  185. help = (!help);
  186. break;
  187. case 'f':
  188. fog = (!fog);
  189. break;
  190. case 't':
  191. usetex = (!usetex);
  192. break;
  193. case 'b':
  194. if (bfcull) {
  195. glDisable(GL_CULL_FACE);
  196. bfcull = 0;
  197. }
  198. else {
  199. glEnable(GL_CULL_FACE);
  200. bfcull = 1;
  201. }
  202. break;
  203. case 'm':
  204. cstrip = (!cstrip);
  205. break;
  206. case 'd':
  207. fprintf(stderr, "Deleting textures...\n");
  208. glDeleteTextures(1, &t1id);
  209. glDeleteTextures(1, &t2id);
  210. fprintf(stderr, "Loading textures...\n");
  211. inittextures();
  212. fprintf(stderr, "Done.\n");
  213. break;
  214. }
  215. }
  216. static void
  217. reshape(int w, int h)
  218. {
  219. WIDTH = w;
  220. HEIGHT = h;
  221. glMatrixMode(GL_PROJECTION);
  222. glLoadIdentity();
  223. gluPerspective(80.0, w / (float) h, 1.0, 50.0);
  224. glMatrixMode(GL_MODELVIEW);
  225. glLoadIdentity();
  226. glViewport(0, 0, w, h);
  227. }
  228. static void
  229. printstring(void *font, char *string)
  230. {
  231. int len, i;
  232. len = (int) strlen(string);
  233. for (i = 0; i < len; i++)
  234. glutBitmapCharacter(font, string[i]);
  235. }
  236. static void
  237. printhelp(void)
  238. {
  239. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  240. glColor4f(0.0, 0.0, 0.0, 0.5);
  241. glRecti(40, 40, 600, 440);
  242. glColor3f(1.0, 0.0, 0.0);
  243. glRasterPos2i(300, 420);
  244. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Help");
  245. glRasterPos2i(60, 390);
  246. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Togle Help");
  247. glRasterPos2i(60, 360);
  248. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Togle Textures");
  249. glRasterPos2i(60, 330);
  250. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Togle Fog");
  251. glRasterPos2i(60, 300);
  252. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "m - Togle strips");
  253. glRasterPos2i(60, 270);
  254. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Togle Back face culling");
  255. glRasterPos2i(60, 240);
  256. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Arrow Keys - Rotate");
  257. glRasterPos2i(60, 210);
  258. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "a - Increase velocity");
  259. glRasterPos2i(60, 180);
  260. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "z - Decrease velocity");
  261. glRasterPos2i(60, 150);
  262. if (joyavailable)
  263. printstring(GLUT_BITMAP_TIMES_ROMAN_24,
  264. "j - Togle jostick control (Joystick control available)");
  265. else
  266. printstring(GLUT_BITMAP_TIMES_ROMAN_24,
  267. "(No Joystick control available)");
  268. }
  269. static void
  270. dojoy(void)
  271. {
  272. #ifdef WIN32
  273. static UINT max[2] = { 0, 0 };
  274. static UINT min[2] = { 0xffffffff, 0xffffffff }, center[2];
  275. MMRESULT res;
  276. JOYINFO joy;
  277. res = joyGetPos(JOYSTICKID1, &joy);
  278. if (res == JOYERR_NOERROR) {
  279. joyavailable = 1;
  280. if (max[0] < joy.wXpos)
  281. max[0] = joy.wXpos;
  282. if (min[0] > joy.wXpos)
  283. min[0] = joy.wXpos;
  284. center[0] = (max[0] + min[0]) / 2;
  285. if (max[1] < joy.wYpos)
  286. max[1] = joy.wYpos;
  287. if (min[1] > joy.wYpos)
  288. min[1] = joy.wYpos;
  289. center[1] = (max[1] + min[1]) / 2;
  290. if (joyactive) {
  291. if (fabs(center[0] - (float) joy.wXpos) > 0.1 * (max[0] - min[0]))
  292. alpha -=
  293. 2.0 * (center[0] - (float) joy.wXpos) / (max[0] - min[0]);
  294. if (fabs(center[1] - (float) joy.wYpos) > 0.1 * (max[1] - min[1]))
  295. beta += 2.0 * (center[1] - (float) joy.wYpos) / (max[1] - min[1]);
  296. if (joy.wButtons & JOY_BUTTON1)
  297. v += 0.01;
  298. if (joy.wButtons & JOY_BUTTON2)
  299. v -= 0.01;
  300. }
  301. }
  302. else
  303. joyavailable = 0;
  304. #endif
  305. }
  306. static void
  307. draw(void)
  308. {
  309. static int count = 0;
  310. static char frbuf[80];
  311. int i;
  312. float fr, base, offset;
  313. dojoy();
  314. glClear(GL_COLOR_BUFFER_BIT);
  315. if (usetex)
  316. glEnable(GL_TEXTURE_2D);
  317. else
  318. glDisable(GL_TEXTURE_2D);
  319. if (fog)
  320. glEnable(GL_FOG);
  321. else
  322. glDisable(GL_FOG);
  323. glShadeModel(GL_SMOOTH);
  324. glPushMatrix();
  325. calcposobs();
  326. gluLookAt(obs[0], obs[1], obs[2],
  327. obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2],
  328. 0.0, 0.0, 1.0);
  329. if (dir[0] > 0) {
  330. offset = 8.0;
  331. base = obs[0] - fmod(obs[0], 8.0);
  332. }
  333. else {
  334. offset = -8.0;
  335. base = obs[0] + (8.0 - fmod(obs[0], 8.0));
  336. }
  337. glPushMatrix();
  338. glTranslatef(base - offset / 2.0, 0.0, 0.0);
  339. for (i = 0; i < NUMBLOC; i++) {
  340. glTranslatef(offset, 0.0, 0.0);
  341. glBindTexture(GL_TEXTURE_2D, t1id);
  342. drawobjs(striplength_skin_11, stripdata_skin_11);
  343. glBindTexture(GL_TEXTURE_2D, t2id);
  344. drawobjs(striplength_skin_12, stripdata_skin_12);
  345. drawobjs(striplength_skin_9, stripdata_skin_9);
  346. drawobjs(striplength_skin_13, stripdata_skin_13);
  347. }
  348. glPopMatrix();
  349. glPopMatrix();
  350. if ((count % FRAME) == 0) {
  351. fr = gettime();
  352. sprintf(frbuf, "Frame rate: %f", FRAME / fr);
  353. }
  354. glDisable(GL_TEXTURE_2D);
  355. glDisable(GL_FOG);
  356. glShadeModel(GL_FLAT);
  357. glMatrixMode(GL_PROJECTION);
  358. glPushMatrix();
  359. glLoadIdentity();
  360. glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0);
  361. glMatrixMode(GL_MODELVIEW);
  362. glLoadIdentity();
  363. glColor3f(1.0, 0.0, 0.0);
  364. glRasterPos2i(10, 10);
  365. printstring(GLUT_BITMAP_HELVETICA_18, frbuf);
  366. glRasterPos2i(350, 470);
  367. printstring(GLUT_BITMAP_HELVETICA_10,
  368. "Tunnel V1.5 Written by David Bucciarelli (tech.hmw@plus.it)");
  369. if (help)
  370. printhelp();
  371. glMatrixMode(GL_PROJECTION);
  372. glPopMatrix();
  373. glMatrixMode(GL_MODELVIEW);
  374. glutSwapBuffers();
  375. count++;
  376. }
  377. int
  378. main(int ac, char **av)
  379. {
  380. float fogcolor[4] = { 0.7, 0.7, 0.7, 1.0 };
  381. fprintf(stderr,
  382. "Tunnel V1.5\nWritten by David Bucciarelli (tech.hmw@plus.it)\n");
  383. glutInitWindowPosition(0, 0);
  384. glutInitWindowSize(WIDTH, HEIGHT);
  385. glutInit(&ac, av);
  386. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  387. if (!(win = glutCreateWindow("Tunnel"))) {
  388. fprintf(stderr, "Error, couldn't open window\n");
  389. return -1;
  390. }
  391. glMatrixMode(GL_PROJECTION);
  392. glLoadIdentity();
  393. gluPerspective(80.0, WIDTH / (float) HEIGHT, 1.0, 50.0);
  394. glMatrixMode(GL_MODELVIEW);
  395. glShadeModel(GL_SMOOTH);
  396. glDisable(GL_DEPTH_TEST);
  397. glEnable(GL_CULL_FACE);
  398. glEnable(GL_TEXTURE_2D);
  399. glEnable(GL_FOG);
  400. glFogi(GL_FOG_MODE, GL_EXP2);
  401. glFogfv(GL_FOG_COLOR, fogcolor);
  402. glFogf(GL_FOG_DENSITY, 0.06);
  403. glHint(GL_FOG_HINT, GL_NICEST);
  404. inittextures();
  405. glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]);
  406. glClear(GL_COLOR_BUFFER_BIT);
  407. calcposobs();
  408. glutReshapeFunc(reshape);
  409. glutDisplayFunc(draw);
  410. glutKeyboardFunc(key);
  411. glutSpecialFunc(special);
  412. glutIdleFunc(draw);
  413. glEnable(GL_BLEND);
  414. /*glBlendFunc(GL_SRC_ALPHA_SATURATE,GL_ONE); */
  415. /*glEnable(GL_POLYGON_SMOOTH); */
  416. glutMainLoop();
  417. return 0;
  418. }