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 11KB

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