fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #define _USE_MATH_DEFINES
  6. #include <math.h>
  7.  
  8. #ifdef __APPLE__
  9. #include <GLUT/glut.h>
  10. #else
  11. #include <GL/glut.h>
  12. #endif
  13.  
  14. #define DEGRAD 57.295779513
  15. #define K 1024
  16.  
  17. #define MAXTICKS 60
  18.  
  19. typedef struct viewer {
  20. GLfloat x, y, z;
  21. GLfloat theta, phi;
  22. GLfloat vx, vy, vz;
  23. } VIEWER;
  24.  
  25. typedef struct monstertype {
  26. GLfloat x, y, z;
  27. GLfloat heading;
  28. GLfloat leftleg, rightleg;
  29. } MONSTER;
  30.  
  31. typedef struct actiontype {
  32. GLfloat startangle;
  33. GLfloat angle[MAXTICKS];
  34. GLfloat slope[MAXTICKS];
  35. int currentphase;
  36. } ACTION;
  37.  
  38. ACTION kick;
  39.  
  40. int automode = 0;
  41.  
  42. GLfloat pos1[] = { 1.0, 3.0, 4.0, 1.0 };
  43. GLfloat pos2[] = { -1.0, 3.0, 4.0, 1.0 };
  44. GLfloat amb[] = { 0.5, 0.5, 0.5, 1.0 };
  45. GLfloat dif[] = { 0.5, 0.5, 0.5, 1.0 };
  46. GLfloat spe[] = { 0.9, 0.9, 0.9, 1.0 };
  47.  
  48. GLfloat matamb[] = { 0.30, 0.30, 0.30, 1.0 };
  49. GLfloat matdif[] = { 0.50, 0.50, 0.50, 1.0 };
  50. GLfloat matspe[] = { 0.10, 0.10, 0.10, 1.0 };
  51.  
  52. GLfloat objamb[] = { 0.60, 0.10, 0.10, 1.0 };
  53. GLfloat objdif[] = { 0.10, 0.10, 0.10, 1.0 };
  54. GLfloat objspe[] = { 0.10, 0.10, 0.80, 1.0 };
  55.  
  56. GLUquadric *body;
  57. MONSTER bob;
  58.  
  59. double pov;
  60. VIEWER moi;
  61. int xOrigin = 0;
  62. int yOrigin = 0;
  63. int lastx = 400;
  64. int lasty = 300;
  65.  
  66. void display();
  67. void animate();
  68. void reshape(int w, int h);
  69. void arrows(int c, int x, int y);
  70. void keyboard(unsigned char c, int x, int y);
  71. void blocks(int x, int y, int z);
  72. void move_viewer(int dir);
  73. //non-functional
  74. void mouseMove(int x, int y);
  75.  
  76. void init();
  77. void update_viewer();
  78. void monster(MONSTER *p);
  79.  
  80. int main(int argc, char *argv[])
  81. {
  82. srand(time(NULL));
  83. pov = (argc == 1) ? 60.0 : atof(argv[1]);
  84. glutInit(&argc, argv);
  85. glutInitWindowSize(800, 600);
  86. glutInitWindowPosition(400, 100);
  87.  
  88. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
  89. glutCreateWindow("V0.1");
  90.  
  91. glutKeyboardFunc(keyboard);
  92. glutDisplayFunc(display);
  93. glutSpecialFunc(arrows);
  94. glutReshapeFunc(reshape);
  95. glutIdleFunc(animate);
  96.  
  97. // mouse func's
  98. glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  99. glutPassiveMotionFunc(mouseMove);
  100.  
  101. init();
  102. display();
  103.  
  104. glutMainLoop();
  105. }
  106. void move_viewer(int dir)
  107. {
  108. GLfloat tx, tz, tt;
  109. int k;
  110.  
  111. /* for forward and backward movement */
  112. switch (dir){
  113. case 1:
  114. //tx = moi.x + moi.vx;
  115. //tz = moi.z + moi.vz;
  116. moi.x += moi.vx;
  117. moi.z += moi.vz;
  118. break;
  119. case 2:
  120. tt = moi.theta;
  121. moi.theta -= 90.0;
  122. moi.vx = cos(moi.theta);
  123. moi.vz = sin(moi.theta);
  124. moi.x += moi.vx;
  125. moi.z += moi.vz;
  126. moi.theta = tt;
  127. break;
  128. case 3:
  129. tt = moi.theta;
  130. moi.theta += 90.0;
  131. moi.vx = cos(moi.theta);
  132. moi.vz = sin(moi.theta);
  133. moi.x += moi.vx*1.0;
  134. moi.z += moi.vz*1.0;
  135. moi.theta = tt;
  136. break;
  137. case 4:
  138. //tx = moi.x - moi.vx;
  139. //tz = moi.z - moi.vz;
  140. moi.x -= moi.vx;
  141. moi.z -= moi.vz;
  142. break;
  143. }
  144.  
  145. pos1[0] = moi.x;
  146. pos1[1] = moi.y;
  147. pos1[2] = moi.z;
  148. glLightfv(GL_LIGHT0, GL_POSITION, pos1);
  149. }
  150. void keyboard(unsigned char c, int x, int y)
  151. {
  152. switch (c){
  153. case 27: /* escape key */
  154. exit(0);
  155. case 'w':
  156. move_viewer(1);
  157. break;
  158. case 's':
  159. move_viewer(4);
  160. break;
  161. case 'a':
  162. move_viewer(2);
  163. break;
  164. case 'd':
  165. move_viewer(3);
  166. break;
  167. case 'r':
  168. if (!automode){
  169. automode = 1;
  170. kick.currentphase = 0;
  171. }
  172. break;
  173. }
  174. update_viewer();
  175. glutPostRedisplay();
  176. }
  177. void mouseMove(int x, int y)
  178. {
  179.  
  180.  
  181. // update the angles
  182. int diffx = x - lastx;
  183. int diffy = y - lasty;
  184. lastx = x;
  185. lasty = y;
  186.  
  187.  
  188. moi.phi = ((diffy *2) - y) * 0.001f;
  189. moi.theta = ((diffx*2) - x) * 0.001f;
  190.  
  191. /*moi.phi += diffx;
  192. moi.theta += diffy;*/
  193. // update camera's direction
  194. update_viewer();
  195. glutPostRedisplay();
  196. // put new directions back into the origins
  197. yOrigin = moi.phi;
  198. xOrigin = moi.theta;
  199.  
  200. }
  201. void arrows(int c, int x, int y)
  202. {
  203. switch (c){
  204. case GLUT_KEY_LEFT:
  205. break;
  206. case GLUT_KEY_UP:
  207. moi.phi += 0.1;
  208. break;
  209. case GLUT_KEY_DOWN:
  210. moi.phi -= 0.1;
  211. break;
  212. case GLUT_KEY_RIGHT:
  213. bob.heading -= 5.0;
  214. break;
  215. }
  216. update_viewer();
  217. glutPostRedisplay();
  218. }
  219. void reshape(int w, int h)
  220. {
  221. glViewport(0, 0, w, h);
  222. glMatrixMode(GL_PROJECTION);
  223. glLoadIdentity();
  224. gluPerspective(pov, (GLdouble)w / (GLdouble)h, 0.001, 50.0);
  225. glMatrixMode(GL_MODELVIEW);
  226. }
  227. void init_action(ACTION *p)
  228. {
  229. int i;
  230.  
  231. p->startangle = 0.0;
  232. for (i = 0; i<MAXTICKS; i++)
  233. p->slope[i] = (i < MAXTICKS / 3) ? 4.0 : -2.0;
  234. p->angle[0] = p->startangle;
  235. for (i = 1; i<MAXTICKS; i++)
  236. p->angle[i] = p->angle[i - 1] + p->slope[i];
  237. p->currentphase = 0;
  238. }
  239. void init_monster(MONSTER *p)
  240. {
  241. memset(p, 0, sizeof(MONSTER));
  242. }
  243. void init()
  244. {
  245. moi.x = 0.0;
  246. moi.y = 2.5;
  247. moi.z = 5.0;
  248. pos1[0] = moi.x;
  249. pos1[1] = moi.y;
  250. pos1[2] = moi.z;
  251. moi.theta = -M_PI / 2.0;
  252. moi.phi = 0.0;
  253.  
  254. automode = 0;
  255. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  256. update_viewer();
  257.  
  258. glEnable(GL_LIGHTING);
  259. glEnable(GL_DEPTH_TEST);
  260. glEnable(GL_TEXTURE_2D);
  261. glEnable(GL_LIGHT0);
  262. glEnable(GL_LIGHT1);
  263.  
  264. glLightfv(GL_LIGHT0, GL_POSITION, pos1);
  265. glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
  266. glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
  267. glLightfv(GL_LIGHT0, GL_SPECULAR, spe);
  268.  
  269. glLightfv(GL_LIGHT0, GL_POSITION, pos2);
  270. glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
  271. glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
  272. glLightfv(GL_LIGHT0, GL_SPECULAR, spe);
  273.  
  274. body = gluNewQuadric();
  275. gluQuadricDrawStyle(body, GLU_LINE);
  276.  
  277. init_monster(&bob);
  278. init_action(&kick);
  279. }
  280. void animate()
  281. {
  282. int n;
  283.  
  284. if (!automode)
  285. return;
  286. fprintf(stderr, "made it to animate!\n");
  287. n = kick.currentphase++;
  288. if (n >= MAXTICKS){
  289. automode = 0;
  290. bob.rightleg = kick.startangle;
  291. }
  292. else {
  293. bob.rightleg = kick.angle[n];
  294. }
  295. update_viewer();
  296. glutPostRedisplay();
  297. }
  298. void update_viewer()
  299. {
  300. moi.vx = cos(moi.theta);
  301. moi.vy = sin(moi.phi);
  302. moi.vz = sin(moi.theta);
  303. }
  304. void display()
  305. {
  306. int i, j, k, u, v;
  307.  
  308.  
  309. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  310. glLineWidth(2.0);
  311. glLoadIdentity();
  312.  
  313. gluLookAt(moi.x, moi.y, moi.z,
  314. moi.x + moi.vx, moi.y + moi.vy, moi.z + moi.vz,
  315. 0.0, 1.0, 0.0);
  316.  
  317. /* blocks(x,y,z) */
  318. for (i = -10; i < 20; i++){
  319. for (k = -10; k < 20; k++){
  320. v = rand();
  321. if (v % 2 == 0){
  322. glColor3f(1.0f, 1.0f, 1.0f);
  323. }
  324. else{
  325. glColor3f(0.1f, 0.9f, 0.2f);
  326. }
  327.  
  328. blocks(i, 0, k);
  329. }
  330. }
  331.  
  332. //monster(&bob);
  333.  
  334. glutSwapBuffers();
  335. }
  336. void blocks(int x, int y, int z)
  337. {
  338. /* floor blocks */
  339.  
  340. // glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, matamb);
  341. // glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, matdif);
  342. // glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, matspe);
  343. // glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0);
  344. glBegin(GL_LINE_STRIP);
  345. /* top of block */
  346. glVertex3f(x, y+1.0, z);
  347. glVertex3f(x+1.0, y+1.0, z);
  348. glVertex3f(x+1.0, y+1.0, z+1.0);
  349. glVertex3f(x, y+1.0, z+1.0);
  350. glEnd();
  351. /* left of block */
  352. glBegin(GL_LINE_STRIP);
  353. glVertex3f(x, y+1.0, z);
  354. glVertex3f(x, y, z);
  355. glVertex3f(x, y, z+1.0);
  356. glVertex3f(x, y + 1.0, z + 1.0);
  357. glEnd();
  358. /* right of block */
  359. glBegin(GL_LINE_STRIP);
  360. glVertex3f(x+1.0, y+1.0, z);
  361. glVertex3f(x+1.0, y, z);
  362. glVertex3f(x+1.0, y, z+1.0);
  363. glVertex3f(x+1.0, y+1.0, z+1.0);
  364. glEnd();
  365. /* front of block */
  366. glBegin(GL_LINE_STRIP);
  367. glVertex3f(x, y+1.0, z);
  368. glVertex3f(x, y, z);
  369. glVertex3f(x+1.0, y, z);
  370. glVertex3f(x+1.0, y+1.0, z);
  371. glEnd();
  372. /* back of block */
  373. glBegin(GL_LINE_STRIP);
  374. glVertex3f(x, y+1.0, z+1.0);
  375. glVertex3f(x, y, z+1.0);
  376. glVertex3f(x+1.0, y, z+1.0);
  377. glVertex3f(x+1.0, y+1.0, z+1.0);
  378. glEnd();
  379. /* bottom of block */
  380. glBegin(GL_LINE_STRIP);
  381. glVertex3f(x, y, z);
  382. glVertex3f(x+1.0, y, z);
  383. glVertex3f(x+1.0, y, z+1.0);
  384. glVertex3f(x, y, z+1.0);
  385. glEnd();
  386. }
  387. void monster(MONSTER *p)
  388. {
  389. glPushMatrix();
  390.  
  391. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, objamb);
  392. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, objdif);
  393. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, objspe);
  394. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 10.0);
  395.  
  396. glRotatef(bob.heading, 0.0, 1.0, 0.0);
  397.  
  398. glRotatef(-90.0, 1.0, 0.0, 0.0);
  399.  
  400. /* torso and head*/
  401.  
  402. glPushMatrix();
  403.  
  404. glTranslatef(0.0, 0.0, 2.5);
  405. gluSphere(body, 0.5, 12, 12);
  406.  
  407. glTranslatef(0.0, 0.0, -1.5);
  408. glScalef(0.5, 1.0, 1.0);
  409. gluCylinder(body, 0.2, 0.3, 1.0, 12, 12);
  410. glPopMatrix();
  411.  
  412. /* legs */
  413. glPushMatrix();
  414. glTranslatef(0.0, 0.1, 1.0);
  415. glRotatef(p->rightleg, 0.0, 1.0, 0.0);
  416. glTranslatef(0.0, 0.0, -1.0);
  417. gluCylinder(body, 0.05, 0.05, 1.0, 12, 12);
  418. glPopMatrix();
  419.  
  420. glTranslatef(0.0, -0.1, 0.0);
  421. gluCylinder(body, 0.05, 0.05, 1.0, 12, 12);
  422.  
  423. glPopMatrix();
  424. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:11:21: fatal error: GL/glut.h: No such file or directory
 #include <GL/glut.h>
                     ^
compilation terminated.
stdout
Standard output is empty