Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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