Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

tunnel.c 11KB

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