Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.36 KB | None | 0 0
  1. #include <GL/gl.h>
  2. #include <GL/glu.h>
  3. #include <GL/glut.h>
  4. #include <stdio.h>
  5. #include <SOIL.h>
  6. #define DEGREES_PER_PIXEL 0.6f
  7. #define ZOOM_FACTOR 0.04f
  8. #define UNITS_PER_PIXEL 0.1f
  9.  
  10.  
  11. typedef struct {
  12. bool leftButton;
  13. bool rightButton;
  14. int x;
  15. int y;
  16. } MouseState;
  17.  
  18. MouseState mouseState = { false, false, 0, 0 };
  19.  
  20. GLuint pista_tex; GLuint fire_tex;
  21. GLfloat zoom = 90;
  22. GLfloat eye[] = { 0, 0, 20 };
  23. GLfloat at[] = { 0, 0, 0 };
  24. GLUquadric* pista; GLUquadric* roda; GLUquadric* fire;
  25. float angle = 0;
  26. float rotacao_nave = 10;
  27. float xRotate = 0, yRotate = 0;
  28. float movPista1 = 0;
  29. bool movimentando = false;
  30. bool gameOverBool = false;
  31. int profundidade_eixo = -20;
  32. float moveCarro = 0;
  33.  
  34. int pontos = 0;
  35. int borgSize = 1;
  36. float randX = 0;
  37. float borgSpeed = 0;
  38. float moveYBorg = 0;
  39. float roadSpeed = 0;
  40.  
  41. bool colisao = false;
  42.  
  43.  
  44. static GLuint LoadItens(char* filename ){
  45. GLuint tex;
  46. tex = SOIL_load_OGL_texture(filename, SOIL_LOAD_AUTO,
  47. SOIL_CREATE_NEW_ID,
  48. SOIL_FLAG_INVERT_Y );
  49. if( 0 == tex )
  50. {
  51. printf( "erro do SOIL: '%s'\n", SOIL_last_result() );
  52. }
  53.  
  54. // seta os parametros para a textura
  55. glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_DECAL);
  56.  
  57. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  58. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  59.  
  60. return tex;
  61. }
  62.  
  63. void MouseMove(int x, int y) {
  64. /* Calculate the displacement in movement */
  65. int xDelta = mouseState.x - x;
  66. int yDelta = mouseState.y - y;
  67.  
  68. /* Commit the mouse position */
  69. mouseState.x = x;
  70. mouseState.y = y;
  71.  
  72. /* If left button is down, rotate when mouse is moved */
  73. if (mouseState.leftButton) {
  74. xRotate -= xDelta * DEGREES_PER_PIXEL;
  75. yRotate -= yDelta * DEGREES_PER_PIXEL;
  76. }
  77.  
  78. glutPostRedisplay();
  79. }
  80.  
  81.  
  82. void draw(void)
  83. {
  84. //printf("%f\n", zoom);
  85. glClearColor(0, 0, 0, 1);
  86. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  87.  
  88. glMatrixMode(GL_MODELVIEW);
  89. glLoadIdentity();
  90.  
  91. gluLookAt(eye[0], eye[1], eye[2], at[0], at[1], at[2], 0, 1, 0);
  92.  
  93. /* Rotate the scene in the x and y directions */
  94. glRotatef(xRotate, 0, 1, 0);
  95. glRotatef(yRotate, 1, 0, 0);
  96.  
  97. //carro
  98. glPushMatrix();
  99. //glShadeModel(GL_SMOOTH);
  100. glEnable(GL_COLOR_MATERIAL);
  101. //glClearColor(0, 0, 15, 1);
  102. //glColor3ub(255, 200, 0);
  103. glColor3f(0.7,0.7,0.7);
  104. glTranslatef(moveCarro,-13,1.7);
  105. glScaled(0.8,1.5,0.4);
  106. glutSolidCube(3);
  107. //glDisable(GL_COLOR_MATERIAL);
  108. glPopMatrix();
  109. /*
  110. // parte de cima do carro
  111. glPushMatrix();
  112. glColor3f(0,0,1);
  113. glTranslatef(moveCarro,-13,2.5);
  114. glScaled(0.8,1,0.4);
  115. glutSolidCube(2);
  116. glPopMatrix();
  117.  
  118. // cilindro
  119. glPushMatrix();
  120. glColor3f(0,0,1);
  121. glTranslatef(moveCarro,-13,2.5);
  122. glScaled(0.8,1,0.4);
  123. gluCylinder(pista, 10, 10, 10, 20, 1);
  124. glPopMatrix();
  125. */
  126. glColor3f(1,1,1);
  127.  
  128. glPushMatrix();
  129. glTranslatef(-10, movPista1+23,profundidade_eixo-21);
  130. glRotatef(90, 0, 1, 0);
  131. glRotatef(45, 0,0, 1);
  132. glEnable(GL_TEXTURE_2D);
  133. glBindTexture(GL_TEXTURE_2D, pista_tex);
  134. gluQuadricDrawStyle(pista, GLU_FILL);
  135. glBindTexture(GL_TEXTURE_2D, pista_tex);
  136. gluQuadricTexture(pista, GL_TRUE);
  137. gluQuadricNormals(pista, GLU_SMOOTH);
  138. gluCylinder(pista, 60, 60, 20, 4, 1);
  139. glDisable(GL_TEXTURE_2D);
  140. glPopMatrix();
  141.  
  142. /*
  143. //pista2
  144. glPushMatrix();
  145. glTranslatef(-10, movPista1+42.5,profundidade_eixo);
  146. glRotatef(90, 0, 1, 0);
  147. glRotatef(45, 0,0, 1);
  148. //glEnable(GL_TEXTURE_2D);
  149. glBindTexture(GL_TEXTURE_2D, pista_tex);
  150. gluQuadricDrawStyle(pista, GLU_FILL);
  151. glBindTexture(GL_TEXTURE_2D, pista_tex);
  152. gluQuadricTexture(pista, GL_TRUE);
  153. gluQuadricNormals(pista, GLU_SMOOTH);
  154. gluCylinder(pista, 30, 30, 20, 4, 1);
  155. glDisable(GL_TEXTURE_2D);
  156. glPopMatrix();
  157. */
  158.  
  159. // carro inimigo?
  160. glPushMatrix();
  161. glTranslatef(randX, moveYBorg, 1.7);
  162. //glShadeModel(GL_SMOOTH);
  163. glEnable(GL_COLOR_MATERIAL);
  164. //glClearColor(0, 0, 15, 1);
  165. //glColor3ub(255, 200, 0);
  166. glColor3f(0.7,0.7,0.7);
  167. //glTranslatef(moveCarro,-13,1.7);
  168. glScaled(0.8,1.5,0.4);
  169. glutSolidCube(3);
  170. //glDisable(GL_COLOR_MATERIAL);
  171. //glRotatef(angle, 0, 1, 0);
  172. //glRotatef(angle*2, 0,0, 1);
  173. glPopMatrix();
  174.  
  175. if (gameOverBool) {
  176. // explosao1
  177. glPushMatrix();
  178. glTranslatef(moveCarro, -12, 1.7);
  179. glRotatef(-angle*10, 1, 0, 0);
  180. glRotatef(-angle*2, 0, 1, 0);
  181. glRotatef(-angle*7, 0, 0, 1);
  182. //glRotatef(45, 0,0, 1);
  183. glEnable(GL_TEXTURE_2D);
  184. glBindTexture(GL_TEXTURE_2D, fire_tex);
  185. gluQuadricDrawStyle(fire, GLU_FILL);
  186. glBindTexture(GL_TEXTURE_2D, fire_tex);
  187. gluQuadricTexture(fire, GL_TRUE);
  188. gluQuadricNormals(fire, GLU_SMOOTH);
  189. gluSphere(fire, 3, 32, 16);
  190. glDisable(GL_TEXTURE_2D);
  191. glPopMatrix();
  192. }
  193.  
  194.  
  195. glutSwapBuffers();
  196. }
  197.  
  198. void rotation(int value){
  199. if (!gameOverBool) {
  200. movPista1-= 1;
  201. borgSpeed-= (1+roadSpeed);
  202. moveYBorg = borgSpeed + 20;
  203. if (movPista1<=-43){
  204. movPista1=0;
  205. }
  206.  
  207. if (borgSpeed<=-43) {
  208. borgSpeed = 15;
  209. randX = rand() % 18 + (-8);
  210. if (roadSpeed < 3) {
  211. roadSpeed+=0.05;
  212. }
  213. }
  214.  
  215. if (moveYBorg <= -11 && moveYBorg >= -12) {
  216. if (!((randX >= moveCarro+2.5) || (randX <= moveCarro-2.5))) {
  217. printf("colidiu\n");
  218. gameOverBool = true;
  219. }
  220. }
  221. }
  222.  
  223. angle += 5;
  224. glutPostRedisplay();
  225. glutTimerFunc(50, rotation, 1);
  226.  
  227. }
  228.  
  229. void resize(int w, int h)
  230. {
  231. if (!h)
  232. h = 1;
  233. glViewport(0, 0, w, h);
  234.  
  235. glMatrixMode(GL_PROJECTION);
  236. glLoadIdentity();
  237. gluPerspective(zoom, 1.0*w/h, 0.1, 100.0);
  238. }
  239.  
  240. void init(void)
  241. {
  242.  
  243. /* Initialize the camera position */
  244. glMatrixMode(GL_MODELVIEW);
  245. glLoadIdentity();
  246. gluLookAt(eye[0], eye[1], eye[2], at[0], at[1], at[2], 0, 1, 0);
  247. glEnable(GL_DEPTH_TEST);
  248. //make_tex();
  249. pista = gluNewQuadric();
  250. roda = gluNewQuadric();
  251. fire = gluNewQuadric();
  252.  
  253. glEnable(GL_TEXTURE_2D);
  254.  
  255. pista_tex = LoadItens("D:\\street.jpg");
  256. fire_tex = LoadItens("D:\\fire.jpg");
  257. }
  258.  
  259. void mouse(int button, int state, int x, int y)
  260. {
  261.  
  262.  
  263.  
  264. if ((button == 3) || (button == 4))
  265. {
  266. // 3 pra cima, 4 pra baixo
  267. if (button == 3){
  268. eye[0] = (1 - ZOOM_FACTOR) * eye[0] + at[0] * ZOOM_FACTOR;
  269. eye[1] = (1 - ZOOM_FACTOR) * eye[1] + at[1] * ZOOM_FACTOR;
  270. eye[2] = (1 - ZOOM_FACTOR) * eye[2] + at[2] * ZOOM_FACTOR;
  271.  
  272.  
  273. }
  274.  
  275. if (button == 4){
  276. eye[0] = (1 + ZOOM_FACTOR) * eye[0] - at[0] * ZOOM_FACTOR;
  277. eye[1] = (1 + ZOOM_FACTOR) * eye[1] - at[1] * ZOOM_FACTOR;
  278. eye[2] = (1 + ZOOM_FACTOR) * eye[2] - at[2] * ZOOM_FACTOR;
  279.  
  280. }
  281.  
  282. if (state == GLUT_UP) return; // S pra evitar GLUT_UP redundante pq a roda do mouse considerada botao
  283.  
  284. }else{ // eventos normais button
  285. //printf("Botao %s em %d %d\n", (state == GLUT_DOWN) ? "Clicado" : "Largado", x, y);
  286. }
  287.  
  288. if (button == GLUT_LEFT_BUTTON) {
  289. if (state == GLUT_DOWN)
  290. mouseState.leftButton = true;
  291. else
  292. mouseState.leftButton = false;
  293. }
  294. if (button == GLUT_RIGHT_BUTTON) {
  295. if (state == GLUT_DOWN)
  296. mouseState.rightButton = true;
  297. else
  298. mouseState.rightButton = false;
  299. }
  300.  
  301. // update the mouse position, so we can track the mouse move
  302. mouseState.x = x;
  303. mouseState.y = y;
  304.  
  305. }
  306.  
  307. void ControlaTeclado(unsigned char key, int x, int y){
  308. switch(key){
  309.  
  310. //DIMINUI A VELOCIDADE
  311. case ',':
  312. eye[1] = eye[1] - UNITS_PER_PIXEL;
  313. at[1] = at[1] - UNITS_PER_PIXEL;
  314. break;
  315. case 'e':
  316. eye[1] = eye[1] + UNITS_PER_PIXEL;
  317. at[1] = at[1] + UNITS_PER_PIXEL;
  318. break;
  319. case 'i':
  320. eye[0] = eye[0] + UNITS_PER_PIXEL;
  321. at[0] = at[0] + UNITS_PER_PIXEL;
  322. break;
  323. case 'a':
  324. eye[0] = eye[0] - UNITS_PER_PIXEL;
  325. at[0] = at[0] - UNITS_PER_PIXEL;
  326. case 'A':
  327. //velocidade += 1;
  328. //printf("%f\n", velocidade);
  329. break;
  330. //AUMENTA A VELOCIDADE
  331. case 'D':
  332. //velocidade -= 1;
  333. //printf("%f\n", velocidade);
  334. break;
  335. case ' ':
  336. //bomb = true;
  337. //boom = false;
  338. //fire = false;
  339. //posxbomb = posX;
  340. //posybomb = posY;
  341. break;
  342. }
  343.  
  344. glutPostRedisplay();
  345. }
  346.  
  347. void SpecialKey(int key, int x, int y) {
  348. switch (key) {
  349. case GLUT_KEY_LEFT:
  350. if(moveCarro>=-8.0 && !gameOverBool){
  351. moveCarro--;
  352. colisao = true;
  353. }
  354.  
  355.  
  356. break;
  357. case GLUT_KEY_RIGHT:
  358. if(moveCarro<=8.0 && !gameOverBool){
  359. moveCarro++;
  360. colisao = true;
  361. };
  362.  
  363.  
  364. break;
  365. case GLUT_KEY_UP:
  366.  
  367. break;
  368. case GLUT_KEY_DOWN:
  369.  
  370. break;
  371. case GLUT_KEY_END: /* zoom out */
  372. //eye[0] = (1 + ZOOM_FACTOR) * eye[0] - at[0] * ZOOM_FACTOR;
  373. //eye[1] = (1 + ZOOM_FACTOR) * eye[1] - at[1] * ZOOM_FACTOR;
  374. //eye[2] = (1 + ZOOM_FACTOR) * eye[2] - at[2] * ZOOM_FACTOR;
  375. break;
  376. case GLUT_KEY_HOME: /* zoom in */
  377. //eye[0] = (1 - ZOOM_FACTOR) * eye[0] + at[0] * ZOOM_FACTOR;
  378. //eye[1] = (1 - ZOOM_FACTOR) * eye[1] + at[1] * ZOOM_FACTOR;
  379. //eye[2] = (1 - ZOOM_FACTOR) * eye[2] + at[2] * ZOOM_FACTOR;
  380. break;
  381. }
  382. glutPostRedisplay();
  383. }
  384.  
  385. void SpecialKeyUp(int key, int x, int y) {
  386. switch (key) {
  387. case GLUT_KEY_LEFT:
  388. //movimentando = false;
  389. break;
  390. case GLUT_KEY_RIGHT:
  391. //movimentando = false;
  392. break;
  393. case GLUT_KEY_UP:
  394. movimentando = false;
  395. break;
  396. case GLUT_KEY_DOWN:
  397. movimentando = false;
  398. break;
  399. }
  400. glutPostRedisplay();
  401. }
  402.  
  403. void VerificaRotacaoNave(int teste){
  404. if (movimentando == false){
  405. if (rotacao_nave > 10){
  406. while (rotacao_nave > 10){
  407. rotacao_nave+=1;
  408. }
  409. }else{
  410. while (rotacao_nave < 10){
  411. rotacao_nave-=1;
  412. }
  413. }
  414. }
  415. glutPostRedisplay();
  416. glutTimerFunc(10, VerificaRotacaoNave, 1);
  417.  
  418. }
  419.  
  420. int main(int argc, char **argv)
  421. {
  422. glutInit(&argc, argv);
  423.  
  424. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  425. glutInitWindowSize(640, 512);
  426.  
  427. glutCreateWindow("Test");
  428.  
  429. glutDisplayFunc(draw);
  430. glutReshapeFunc(resize);
  431. glutTimerFunc(50, rotation, 1);
  432. glutMouseFunc(mouse);
  433. glutMotionFunc(MouseMove);
  434. glutKeyboardFunc(ControlaTeclado);
  435. glutSpecialFunc(SpecialKey);
  436. glutSpecialUpFunc(SpecialKeyUp);
  437.  
  438. glutTimerFunc(10, VerificaRotacaoNave, 1);
  439.  
  440. init();
  441.  
  442. glutMainLoop();
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement