Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.01 KB | None | 0 0
  1. #ifdef __APPLE__
  2.      #include <GLUT/glut.h>
  3.      #include <OpenGL/gl.h>
  4. #else
  5.      #include <GL/glut.h>
  6.      #include <GL/gl.h>
  7. #endif
  8.  
  9. #include <vector>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <iostream>
  14. #include <ctime>
  15. #include <cmath>
  16.  
  17. using namespace std;
  18.  
  19. #define tf  3 // qtd_vertices por face
  20. #define tvn 3 // qtd_componentes de cada normal
  21. #define tvt 2 // qtd_componentes de cada textura
  22. #define tv  3 // qtd_componentes de cada vertice
  23. #define qtdObjetos 5 // qtd_objetos na cena
  24.  
  25. #define PISTA 0
  26. #define AVIAO 1
  27. #define MED_TORRE 2
  28. #define THE_CITY 3
  29. #define CITADEL 4
  30.  
  31. #define FORWARD   0
  32. #define BACKWARDS 1
  33. #define LEFT      0
  34. #define RIGHT     1
  35.  
  36. int nFaces[qtdObjetos];
  37. int nVetoresNormais[qtdObjetos];
  38. int nVetoresTextura[qtdObjetos];
  39. int nVertices[qtdObjetos];
  40.  
  41. vector <vector < vector<double> > > v(qtdObjetos);
  42. vector <vector < vector<double> > > vt(qtdObjetos);
  43. vector <vector < vector<double> > > vn(qtdObjetos);
  44. vector <vector < vector<int> > > f(qtdObjetos);
  45.  
  46.  
  47. void init(void);
  48. void display(void);
  49. void keyboard(unsigned char key, int x, int y);
  50. void mouse(int button, int state, int x, int y);
  51. void lerArquivo();
  52.  
  53.  
  54. //movimento
  55. void move_straight(int);
  56. void move_sideways(int);
  57. void move();
  58. clock_t begin = clock();
  59. clock_t end = clock();
  60. clock_t rate;
  61. double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  62. double pi = 3.141592;
  63. GLdouble speed_cam = 0;
  64. GLdouble speed = 0;
  65. GLdouble viewer_prev[3];
  66. GLdouble rot_x = 0, rot_y = 0, rot_z = 0;
  67. GLdouble rot_x_r = 0, rot_y_r = 0, rot_z_r = 0;
  68. GLdouble mouse_x = 50, mouse_y = 50;
  69. GLdouble mouse_x_delta = 0, mouse_y_delta = 0;
  70. bool to_move;
  71. bool free_cam = false;
  72.  
  73. //tamanho da janela em pixels
  74. int largurajanela = 500, alturajanela = 500;
  75.  
  76. //posicao do observador (camera)
  77. GLdouble viewer[] = {0.0, 2.0, 8.0};
  78.  
  79. void desenha_aviao(){
  80.   glColor3f(0.0, 0.0, 1.0);
  81.   for(int i = 1; i <= nFaces[1]; i++){
  82.     glBegin(GL_TRIANGLES);
  83.       glVertex3f((free_cam?viewer_prev[0]:viewer[0])+v[1][f[1][i][0]][0], (free_cam?viewer_prev[1]:viewer[1])-2.0+v[1][f[1][i][0]][1], (free_cam?viewer_prev[2]:viewer[2])-8.0+v[1][f[1][i][0]][2]); // vertice 1
  84.       glVertex3f((free_cam?viewer_prev[0]:viewer[0])+v[1][f[1][i][3]][0], (free_cam?viewer_prev[1]:viewer[1])-2.0+v[1][f[1][i][3]][1], (free_cam?viewer_prev[2]:viewer[2])-8.0+v[1][f[1][i][3]][2]); // vertice 2
  85.       glVertex3f((free_cam?viewer_prev[0]:viewer[0])+v[1][f[1][i][6]][0], (free_cam?viewer_prev[1]:viewer[1])-2.0+v[1][f[1][i][6]][1], (free_cam?viewer_prev[2]:viewer[2])-8.0+v[1][f[1][i][6]][2]); // vertice 3
  86.     glEnd();
  87.   }
  88. }
  89.  
  90. void desenha(int obj){
  91.   for(int i = 1; i <= nFaces[obj]; i++){
  92.     glBegin(GL_TRIANGLES);
  93.       glVertex3f(v[obj][f[obj][i][0]][0], v[obj][f[obj][i][0]][1], v[obj][f[obj][i][0]][2]); // vertice 1
  94.       glVertex3f(v[obj][f[obj][i][3]][0], v[obj][f[obj][i][3]][1], v[obj][f[obj][i][3]][2]); // vertice 2
  95.       glVertex3f(v[obj][f[obj][i][6]][0], v[obj][f[obj][i][6]][1], v[obj][f[obj][i][6]][2]); // vertice 3
  96.     glEnd();
  97.   }
  98. }
  99.  
  100. // glFrustum(left, right, bottom, top, near, far) -> projecao perspectiva
  101. void init(void) {
  102.     glClearColor(1.0, 1.0, 1.0, 1.0); // cor para limpeza do buffer
  103.     glMatrixMode(GL_PROJECTION);
  104.     glLoadIdentity();
  105.     glFrustum(-0.001, 0.001, -0.001, 0.001, 0.001, 500.0); // proje��o perspectiva
  106.     glMatrixMode(GL_MODELVIEW);
  107. }
  108.  
  109. void display(void) {
  110.    
  111.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //limpa a janela
  112.     glLoadIdentity();
  113.    
  114.     move();
  115.    
  116.    
  117.     gluLookAt(viewer[0],viewer[1],viewer[2], // define posicao do observador
  118.     viewer[0] + mouse_x_delta , viewer[1]-2.0 + mouse_y_delta, viewer[2]-8.0,          // ponto de interesse (foco)
  119.     0.0, 10.0, 0.0);                            // vetor de "view up"
  120.    
  121.     if(free_cam){
  122.         glRotatef(rot_y, 0.0, 1.0, 0.0);
  123.         glRotatef(rot_x, 1.0, 0.0, 0.0);
  124.     }else{
  125.         glRotatef(rot_y_r, 0.0, 1.0, 0.0);
  126.         glRotatef(rot_x_r, 1.0, 0.0, 0.0);
  127.     }
  128.     glColor3f(1.0, 0.0, 0.0);
  129.     glPushMatrix();
  130.     glScaled(0.4, 0.4, 0.4);
  131.     glTranslatef(300, -10, -850);
  132.     desenha(CITADEL);
  133.     glPopMatrix();
  134.     glColor3f(0.0, 1.0, 0.0);
  135.     glPushMatrix();
  136.     glScaled(0.3, 0.3, 0.3);
  137.     glTranslatef(0, -200, 100);
  138.     desenha(THE_CITY);
  139.     glPopMatrix();
  140.     glColor3f(0.0, 0.0, 0.0);
  141.     glPushMatrix();
  142.     glScaled(1, 1, 10);
  143.     glTranslatef(-1.2, -10, 3.5);
  144.     desenha(PISTA);
  145.     glPopMatrix();
  146.     glColor3f(1.0, 0.0, 1.0);
  147.     glPushMatrix();
  148.     glScaled(1, 1, 1);
  149.     glTranslatef(-18.0, -10, -10);
  150.     desenha(MED_TORRE);
  151.     glPopMatrix();
  152.     if(!free_cam) {
  153.         glRotatef(-rot_y_r, 0.0, 1.0, 0.0);
  154.         glRotatef(-rot_x_r, 1.0, 0.0, 0.0);
  155.     }else{
  156.         glRotatef(-rot_y_r, 0.0, 1.0, 0.0);
  157.         glRotatef(-rot_x_r, 1.0, 0.0, 0.0);
  158.     }
  159.     desenha_aviao();
  160.     glFlush();
  161.     glutSwapBuffers(); //usando double buffer (para evitar 'flicker')
  162. }
  163.  
  164. void keyboard(unsigned char key, int x, int y) {
  165.     if (key == 27) exit(0); //ESC
  166.     if (key == 'x') viewer[0] -= 0.125;
  167.     if (key == 'X') viewer[0] += 0.125;
  168.     if (key == 'y') viewer[1] -= 0.125;
  169.     if (key == 'Y') viewer[1] += 0.125;
  170.     if (key == 'z') viewer[2] -= 0.125;
  171.     if (key == 'Z') viewer[2] += 0.125;
  172.     if (key == 'w' || key == 'W') {to_move = true; move_straight(FORWARD);}
  173.     if (key == 'a' || key == 'A') move_sideways(LEFT);
  174.     if (key == 's' || key == 'S') {to_move = true; move_straight(BACKWARDS);}
  175.     if (key == 'd' || key == 'D') move_sideways(RIGHT);
  176.     if (key == 'f' || key == 'F') { if(!free_cam)for(int i = 0; i < 3; i++) viewer_prev[i] = viewer[i]; else for(int i = 0; i < 3; i++) viewer[i] = viewer_prev[i]; free_cam = 1-free_cam; mouse_x_delta = mouse_y_delta = speed_cam = 0; rot_x = rot_x_r; rot_y = rot_y_r;}
  177.     display();
  178. }
  179.  
  180. void keyboard_up(unsigned char key, int x, int y){
  181.     if (key == 'w' || key == 'W') {to_move = false; move_straight(FORWARD);}
  182.     if (key == 's' || key == 'S') {to_move = false; move_straight(BACKWARDS);}
  183.     display();
  184. }
  185.  
  186. void mouse(int x, int y)
  187. {
  188.  
  189. if(free_cam){
  190.      if(x > mouse_x) rot_y += 1; else if(mouse_x > x) rot_y -= 1;
  191.         if(mouse_y > y) rot_x -= 1; else if(mouse_y < y) rot_x += 1;
  192.         mouse_x = x;
  193.         mouse_y = y;
  194.     } glutPostRedisplay();
  195. }
  196.  
  197.  
  198. void SpecialInput(int key, int x, int y)
  199. {
  200.     switch(key){
  201.    
  202.     case GLUT_KEY_UP:
  203.             if(speed_cam < 4){ speed_cam -= 0.05;}
  204.         break;
  205.     case GLUT_KEY_DOWN:
  206.             if(speed_cam > -1){speed_cam += 0.1;}
  207.         break;
  208.     case GLUT_KEY_LEFT:
  209.         break;
  210.     case GLUT_KEY_RIGHT:
  211.  
  212.         break;
  213. }
  214.  
  215. glutPostRedisplay();
  216. }
  217.  
  218. void lerArquivo(int _i){
  219.   char c[100];
  220.   fgets(c, sizeof(c), stdin);
  221.   fgets(c, sizeof(c), stdin);
  222.   fgets(c, sizeof(c), stdin);
  223.   fgets(c, sizeof(c), stdin);
  224.   int contv = 0, contvn = 0, contvt = 0, contf = 0;
  225.   while(scanf("%s", c) == 1){
  226.     if(strcmp(c, "v") == 0){
  227.       contv++;
  228.       for(int i = 0; i < tv; i++){
  229.         double a;
  230.         scanf("%lf", &a);
  231.         if(_i == 1){
  232.           a = a*0.7;
  233.         }
  234.         v[_i][contv].push_back(a);
  235.       }
  236.     } else if(strcmp(c, "vt") == 0) {
  237.       contvt++;
  238.       for(int i = 0; i < tvt; i++){
  239.         double a;
  240.         scanf("%lf", &a);
  241.         vt[_i][contvt].push_back(a);
  242.       }
  243.     } else if(strcmp(c, "vn") == 0) {
  244.       contvn++;
  245.       for(int i = 0; i < tvn; i++){
  246.         double a;
  247.         scanf("%lf", &a);
  248.         vn[_i][contvn].push_back(a);
  249.       }
  250.     } else if(strcmp(c, "f")  == 0){
  251.       contf++;
  252.       for(int i = 0; i < tf; i++){
  253.         int a, b, c;
  254.         scanf("%d/%d/%d", &a, &b, &c);
  255.         f[_i][contf].push_back(a);
  256.         f[_i][contf].push_back(b);
  257.         f[_i][contf].push_back(c);
  258.       }
  259.     } else {
  260.       if(strcmp(c, "AVIAO") == 0 || strcmp(c, "MED_TORRE") == 0 || strcmp(c, "THE_CITY") == 0 || strcmp(c, "CITADEL") == 0){
  261.         nFaces[_i] = contf;
  262.         nVetoresNormais[_i] = contvn;
  263.         nVetoresTextura[_i] = contvt;
  264.         nVertices[_i] = contv;
  265.         if(strcmp(c, "MED_TORRE")==0) {
  266.           fgets(c, sizeof(c), stdin);
  267.         }
  268.         lerArquivo(_i+1);
  269.         return;
  270.       }
  271.       fgets(c, sizeof(c), stdin); // linha inutil
  272.     }
  273.   }
  274.   nFaces[_i] = contf;
  275.   nVetoresNormais[_i] = contvn;
  276.   nVetoresTextura[_i] = contvt;
  277.   nVertices[_i] = contv;
  278. }
  279.  
  280.  
  281. int main(int argc, char **argv) {
  282.     for(int i = 0; i < qtdObjetos; i++){
  283.        v[i].resize(500000);
  284.       vt[i].resize(500000);
  285.       vn[i].resize(500000);
  286.        f[i].resize(300000);
  287.     }
  288.     lerArquivo(0);
  289.     glutInit(&argc, argv); //inicializa a glut
  290.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //tipo de buffer/cores
  291.     glutInitWindowSize(largurajanela, alturajanela); //dimens�es da janela
  292.     glutInitWindowPosition(100, 100); //posicao da janela
  293.     glutCreateWindow("Visualizacao 3D - Movimento de camera "); //cria a janela
  294.     glutDisplayFunc(display); //determina fun��o callback de desenho
  295.     init();
  296.     glutKeyboardFunc(keyboard); //determina fun��o callback de teclado
  297.     glutKeyboardUpFunc(keyboard_up);
  298.     glutPassiveMotionFunc(mouse);
  299.     glutSpecialFunc(SpecialInput);
  300.    
  301.     glEnable(GL_CULL_FACE); //habilita remo��o de superf�cies ocultas
  302.  
  303.     glutMainLoop();
  304.     return 0;
  305. }
  306.  
  307.  
  308. void move(){
  309.     end = clock();
  310.     elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  311.     if(elapsed_secs > 0.01){
  312.         (free_cam?viewer_prev[2]:viewer[2]) += speed;
  313.         begin = clock();
  314.         if(free_cam){
  315.             viewer[2] += speed_cam;
  316.         }
  317.     }
  318.     glutPostRedisplay();
  319. }
  320.  
  321. void move_straight(int sentido){
  322.     if(sentido){
  323.         end = clock();
  324.         elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  325.         if(elapsed_secs > 0.003 && to_move){
  326.             if(speed < 4){ speed += 0.05;
  327.                 (free_cam?viewer_prev[2]:viewer[2]) += speed;
  328.             }
  329.             begin = clock();
  330.         }
  331.     }else{
  332.         end = clock();
  333.         elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  334.         if(elapsed_secs > 0.005 && to_move){
  335.             if(speed > -1){ speed -= 0.07;
  336.                 (free_cam?viewer_prev[2]:viewer[2]) += speed;
  337.             }
  338.             begin = clock();
  339.         }
  340.     }
  341. }
  342. void move_sideways(int sentido){
  343.     if(sentido){
  344.         rot_y_r += 1;
  345.     }else{
  346.         rot_y_r -= 1;
  347.     }
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement