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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. cleanup(void)
  170. {
  171. glDeleteTextures(1, &t1id);
  172. glDeleteTextures(1, &t2id);
  173. }
  174. static void
  175. key(unsigned char k, int x, int y)
  176. {
  177. switch (k) {
  178. case 27:
  179. cleanup();
  180. exit(0);
  181. break;
  182. case 'a':
  183. v += 5.;
  184. break;
  185. case 'z':
  186. v -= 5.;
  187. break;
  188. #ifdef XMESA
  189. case ' ':
  190. fullscreen = (!fullscreen);
  191. XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
  192. break;
  193. #endif
  194. case 'j':
  195. joyactive = (!joyactive);
  196. break;
  197. case 'h':
  198. help = (!help);
  199. break;
  200. case 'f':
  201. fog = (!fog);
  202. break;
  203. case 't':
  204. usetex = (!usetex);
  205. break;
  206. case 'b':
  207. if (bfcull) {
  208. glDisable(GL_CULL_FACE);
  209. bfcull = 0;
  210. }
  211. else {
  212. glEnable(GL_CULL_FACE);
  213. bfcull = 1;
  214. }
  215. break;
  216. case 'm':
  217. cstrip = (!cstrip);
  218. break;
  219. case 'd':
  220. fprintf(stderr, "Deleting textures...\n");
  221. glDeleteTextures(1, &t1id);
  222. glDeleteTextures(1, &t2id);
  223. fprintf(stderr, "Loading textures...\n");
  224. inittextures();
  225. fprintf(stderr, "Done.\n");
  226. break;
  227. case 'n':
  228. NiceFog = !NiceFog;
  229. printf("NiceFog %d\n", NiceFog);
  230. break;
  231. }
  232. glutPostRedisplay();
  233. }
  234. static void
  235. reshape(int w, int h)
  236. {
  237. WIDTH = w;
  238. HEIGHT = h;
  239. glMatrixMode(GL_PROJECTION);
  240. glLoadIdentity();
  241. gluPerspective(80.0, w / (float) h, 1.0, 50.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 - Toggle Help");
  265. glRasterPos2i(60, 360);
  266. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Toggle Textures");
  267. glRasterPos2i(60, 330);
  268. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Toggle Fog");
  269. glRasterPos2i(60, 300);
  270. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "m - Toggle strips");
  271. glRasterPos2i(60, 270);
  272. printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Toggle 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 - Toggle 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. if (NiceFog)
  331. glHint(GL_FOG_HINT, GL_NICEST);
  332. else
  333. glHint(GL_FOG_HINT, GL_DONT_CARE);
  334. dojoy();
  335. glClear(GL_COLOR_BUFFER_BIT);
  336. if (usetex)
  337. glEnable(GL_TEXTURE_2D);
  338. else
  339. glDisable(GL_TEXTURE_2D);
  340. if (fog)
  341. glEnable(GL_FOG);
  342. else
  343. glDisable(GL_FOG);
  344. glShadeModel(GL_SMOOTH);
  345. glPushMatrix();
  346. calcposobs();
  347. gluLookAt(obs[0], obs[1], obs[2],
  348. obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2],
  349. 0.0, 0.0, 1.0);
  350. if (dir[0] > 0) {
  351. offset = 8.0;
  352. base = obs[0] - fmod(obs[0], 8.0);
  353. }
  354. else {
  355. offset = -8.0;
  356. base = obs[0] + (8.0 - fmod(obs[0], 8.0));
  357. }
  358. glPushMatrix();
  359. glTranslatef(base - offset / 2.0, 0.0, 0.0);
  360. for (i = 0; i < NUMBLOC; i++) {
  361. glTranslatef(offset, 0.0, 0.0);
  362. glBindTexture(GL_TEXTURE_2D, t1id);
  363. drawobjs(striplength_skin_11, stripdata_skin_11);
  364. glBindTexture(GL_TEXTURE_2D, t2id);
  365. drawobjs(striplength_skin_12, stripdata_skin_12);
  366. drawobjs(striplength_skin_9, stripdata_skin_9);
  367. drawobjs(striplength_skin_13, stripdata_skin_13);
  368. }
  369. glPopMatrix();
  370. glPopMatrix();
  371. glDisable(GL_TEXTURE_2D);
  372. glDisable(GL_FOG);
  373. glShadeModel(GL_FLAT);
  374. glMatrixMode(GL_PROJECTION);
  375. glPushMatrix();
  376. glLoadIdentity();
  377. glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0);
  378. glMatrixMode(GL_MODELVIEW);
  379. glLoadIdentity();
  380. glColor3f(1.0, 0.0, 0.0);
  381. glRasterPos2i(10, 10);
  382. printstring(GLUT_BITMAP_HELVETICA_18, frbuf);
  383. glRasterPos2i(350, 470);
  384. printstring(GLUT_BITMAP_HELVETICA_10,
  385. "Tunnel V1.5 Written by David Bucciarelli (tech.hmw@plus.it)");
  386. if (help)
  387. printhelp();
  388. glMatrixMode(GL_PROJECTION);
  389. glPopMatrix();
  390. glMatrixMode(GL_MODELVIEW);
  391. glutSwapBuffers();
  392. Frames++;
  393. {
  394. GLint t = glutGet(GLUT_ELAPSED_TIME);
  395. if (t - T0 >= 2000) {
  396. GLfloat seconds = (t - T0) / 1000.0;
  397. GLfloat fps = Frames / seconds;
  398. sprintf(frbuf, "Frame rate: %f", fps);
  399. T0 = t;
  400. Frames = 0;
  401. }
  402. }
  403. }
  404. static void
  405. idle(void)
  406. {
  407. glutPostRedisplay();
  408. }
  409. int
  410. main(int ac, char **av)
  411. {
  412. float fogcolor[4] = { 0.7, 0.7, 0.7, 1.0 };
  413. fprintf(stderr,
  414. "Tunnel V1.5\nWritten by David Bucciarelli (tech.hmw@plus.it)\n");
  415. glutInitWindowSize(WIDTH, HEIGHT);
  416. glutInit(&ac, av);
  417. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  418. if (!(win = glutCreateWindow("Tunnel"))) {
  419. fprintf(stderr, "Error, couldn't open window\n");
  420. return -1;
  421. }
  422. glMatrixMode(GL_PROJECTION);
  423. glLoadIdentity();
  424. gluPerspective(80.0, WIDTH / (float) HEIGHT, 1.0, 50.0);
  425. glMatrixMode(GL_MODELVIEW);
  426. glShadeModel(GL_SMOOTH);
  427. glDisable(GL_DEPTH_TEST);
  428. glEnable(GL_CULL_FACE);
  429. glEnable(GL_TEXTURE_2D);
  430. glEnable(GL_FOG);
  431. glFogi(GL_FOG_MODE, GL_EXP2);
  432. glFogfv(GL_FOG_COLOR, fogcolor);
  433. glFogf(GL_FOG_DENSITY, 0.06);
  434. glHint(GL_FOG_HINT, GL_NICEST);
  435. inittextures();
  436. glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]);
  437. glClear(GL_COLOR_BUFFER_BIT);
  438. calcposobs();
  439. glutReshapeFunc(reshape);
  440. glutDisplayFunc(draw);
  441. glutKeyboardFunc(key);
  442. glutSpecialFunc(special);
  443. glutIdleFunc(idle);
  444. glEnable(GL_BLEND);
  445. /*glBlendFunc(GL_SRC_ALPHA_SATURATE,GL_ONE); */
  446. /*glEnable(GL_POLYGON_SMOOTH); */
  447. glutMainLoop();
  448. cleanup();
  449. return 0;
  450. }