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

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