Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #include <OpenGL/gl.h>
- #else
- #include <GL/glut.h>
- #include <GL/gl.h>
- #endif
- #include <vector>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <iostream>
- #include <ctime>
- #include <cmath>
- using namespace std;
- #define tf 3 // qtd_vertices por face
- #define tvn 3 // qtd_componentes de cada normal
- #define tvt 2 // qtd_componentes de cada textura
- #define tv 3 // qtd_componentes de cada vertice
- #define qtdObjetos 5 // qtd_objetos na cena
- #define PISTA 0
- #define AVIAO 1
- #define MED_TORRE 2
- #define THE_CITY 3
- #define CITADEL 4
- #define FORWARD 0
- #define BACKWARDS 1
- #define LEFT 0
- #define RIGHT 1
- int nFaces[qtdObjetos];
- int nVetoresNormais[qtdObjetos];
- int nVetoresTextura[qtdObjetos];
- int nVertices[qtdObjetos];
- vector <vector < vector<double> > > v(qtdObjetos);
- vector <vector < vector<double> > > vt(qtdObjetos);
- vector <vector < vector<double> > > vn(qtdObjetos);
- vector <vector < vector<int> > > f(qtdObjetos);
- void init(void);
- void display(void);
- void keyboard(unsigned char key, int x, int y);
- void mouse(int button, int state, int x, int y);
- void lerArquivo();
- //movimento
- void move_straight(int);
- void move_sideways(int);
- void move();
- clock_t begin = clock();
- clock_t end = clock();
- clock_t rate;
- double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
- double pi = 3.141592;
- GLdouble speed_cam = 0;
- GLdouble speed = 0;
- GLdouble viewer_prev[3];
- GLdouble rot_x = 0, rot_y = 0, rot_z = 0;
- GLdouble rot_x_r = 0, rot_y_r = 0, rot_z_r = 0;
- GLdouble mouse_x = 50, mouse_y = 50;
- GLdouble mouse_x_delta = 0, mouse_y_delta = 0;
- bool to_move;
- bool free_cam = false;
- //tamanho da janela em pixels
- int largurajanela = 500, alturajanela = 500;
- //posicao do observador (camera)
- GLdouble viewer[] = {0.0, 2.0, 8.0};
- void desenha_aviao(){
- glColor3f(0.0, 0.0, 1.0);
- for(int i = 1; i <= nFaces[1]; i++){
- glBegin(GL_TRIANGLES);
- 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
- 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
- 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
- glEnd();
- }
- }
- void desenha(int obj){
- for(int i = 1; i <= nFaces[obj]; i++){
- glBegin(GL_TRIANGLES);
- glVertex3f(v[obj][f[obj][i][0]][0], v[obj][f[obj][i][0]][1], v[obj][f[obj][i][0]][2]); // vertice 1
- glVertex3f(v[obj][f[obj][i][3]][0], v[obj][f[obj][i][3]][1], v[obj][f[obj][i][3]][2]); // vertice 2
- glVertex3f(v[obj][f[obj][i][6]][0], v[obj][f[obj][i][6]][1], v[obj][f[obj][i][6]][2]); // vertice 3
- glEnd();
- }
- }
- // glFrustum(left, right, bottom, top, near, far) -> projecao perspectiva
- void init(void) {
- glClearColor(1.0, 1.0, 1.0, 1.0); // cor para limpeza do buffer
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(-0.001, 0.001, -0.001, 0.001, 0.001, 500.0); // proje��o perspectiva
- glMatrixMode(GL_MODELVIEW);
- }
- void display(void) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //limpa a janela
- glLoadIdentity();
- move();
- gluLookAt(viewer[0],viewer[1],viewer[2], // define posicao do observador
- viewer[0] + mouse_x_delta , viewer[1]-2.0 + mouse_y_delta, viewer[2]-8.0, // ponto de interesse (foco)
- 0.0, 10.0, 0.0); // vetor de "view up"
- if(free_cam){
- glRotatef(rot_y, 0.0, 1.0, 0.0);
- glRotatef(rot_x, 1.0, 0.0, 0.0);
- }else{
- glRotatef(rot_y_r, 0.0, 1.0, 0.0);
- glRotatef(rot_x_r, 1.0, 0.0, 0.0);
- }
- glColor3f(1.0, 0.0, 0.0);
- glPushMatrix();
- glScaled(0.4, 0.4, 0.4);
- glTranslatef(300, -10, -850);
- desenha(CITADEL);
- glPopMatrix();
- glColor3f(0.0, 1.0, 0.0);
- glPushMatrix();
- glScaled(0.3, 0.3, 0.3);
- glTranslatef(0, -200, 100);
- desenha(THE_CITY);
- glPopMatrix();
- glColor3f(0.0, 0.0, 0.0);
- glPushMatrix();
- glScaled(1, 1, 10);
- glTranslatef(-1.2, -10, 3.5);
- desenha(PISTA);
- glPopMatrix();
- glColor3f(1.0, 0.0, 1.0);
- glPushMatrix();
- glScaled(1, 1, 1);
- glTranslatef(-18.0, -10, -10);
- desenha(MED_TORRE);
- glPopMatrix();
- if(!free_cam) {
- glRotatef(-rot_y_r, 0.0, 1.0, 0.0);
- glRotatef(-rot_x_r, 1.0, 0.0, 0.0);
- }else{
- glRotatef(-rot_y_r, 0.0, 1.0, 0.0);
- glRotatef(-rot_x_r, 1.0, 0.0, 0.0);
- }
- desenha_aviao();
- glFlush();
- glutSwapBuffers(); //usando double buffer (para evitar 'flicker')
- }
- void keyboard(unsigned char key, int x, int y) {
- if (key == 27) exit(0); //ESC
- if (key == 'x') viewer[0] -= 0.125;
- if (key == 'X') viewer[0] += 0.125;
- if (key == 'y') viewer[1] -= 0.125;
- if (key == 'Y') viewer[1] += 0.125;
- if (key == 'z') viewer[2] -= 0.125;
- if (key == 'Z') viewer[2] += 0.125;
- if (key == 'w' || key == 'W') {to_move = true; move_straight(FORWARD);}
- if (key == 'a' || key == 'A') move_sideways(LEFT);
- if (key == 's' || key == 'S') {to_move = true; move_straight(BACKWARDS);}
- if (key == 'd' || key == 'D') move_sideways(RIGHT);
- 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;}
- display();
- }
- void keyboard_up(unsigned char key, int x, int y){
- if (key == 'w' || key == 'W') {to_move = false; move_straight(FORWARD);}
- if (key == 's' || key == 'S') {to_move = false; move_straight(BACKWARDS);}
- display();
- }
- void mouse(int x, int y)
- {
- if(free_cam){
- if(x > mouse_x) rot_y += 1; else if(mouse_x > x) rot_y -= 1;
- if(mouse_y > y) rot_x -= 1; else if(mouse_y < y) rot_x += 1;
- mouse_x = x;
- mouse_y = y;
- } glutPostRedisplay();
- }
- void SpecialInput(int key, int x, int y)
- {
- switch(key){
- case GLUT_KEY_UP:
- if(speed_cam < 4){ speed_cam -= 0.05;}
- break;
- case GLUT_KEY_DOWN:
- if(speed_cam > -1){speed_cam += 0.1;}
- break;
- case GLUT_KEY_LEFT:
- break;
- case GLUT_KEY_RIGHT:
- break;
- }
- glutPostRedisplay();
- }
- void lerArquivo(int _i){
- char c[100];
- fgets(c, sizeof(c), stdin);
- fgets(c, sizeof(c), stdin);
- fgets(c, sizeof(c), stdin);
- fgets(c, sizeof(c), stdin);
- int contv = 0, contvn = 0, contvt = 0, contf = 0;
- while(scanf("%s", c) == 1){
- if(strcmp(c, "v") == 0){
- contv++;
- for(int i = 0; i < tv; i++){
- double a;
- scanf("%lf", &a);
- if(_i == 1){
- a = a*0.7;
- }
- v[_i][contv].push_back(a);
- }
- } else if(strcmp(c, "vt") == 0) {
- contvt++;
- for(int i = 0; i < tvt; i++){
- double a;
- scanf("%lf", &a);
- vt[_i][contvt].push_back(a);
- }
- } else if(strcmp(c, "vn") == 0) {
- contvn++;
- for(int i = 0; i < tvn; i++){
- double a;
- scanf("%lf", &a);
- vn[_i][contvn].push_back(a);
- }
- } else if(strcmp(c, "f") == 0){
- contf++;
- for(int i = 0; i < tf; i++){
- int a, b, c;
- scanf("%d/%d/%d", &a, &b, &c);
- f[_i][contf].push_back(a);
- f[_i][contf].push_back(b);
- f[_i][contf].push_back(c);
- }
- } else {
- if(strcmp(c, "AVIAO") == 0 || strcmp(c, "MED_TORRE") == 0 || strcmp(c, "THE_CITY") == 0 || strcmp(c, "CITADEL") == 0){
- nFaces[_i] = contf;
- nVetoresNormais[_i] = contvn;
- nVetoresTextura[_i] = contvt;
- nVertices[_i] = contv;
- if(strcmp(c, "MED_TORRE")==0) {
- fgets(c, sizeof(c), stdin);
- }
- lerArquivo(_i+1);
- return;
- }
- fgets(c, sizeof(c), stdin); // linha inutil
- }
- }
- nFaces[_i] = contf;
- nVetoresNormais[_i] = contvn;
- nVetoresTextura[_i] = contvt;
- nVertices[_i] = contv;
- }
- int main(int argc, char **argv) {
- for(int i = 0; i < qtdObjetos; i++){
- v[i].resize(500000);
- vt[i].resize(500000);
- vn[i].resize(500000);
- f[i].resize(300000);
- }
- lerArquivo(0);
- glutInit(&argc, argv); //inicializa a glut
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //tipo de buffer/cores
- glutInitWindowSize(largurajanela, alturajanela); //dimens�es da janela
- glutInitWindowPosition(100, 100); //posicao da janela
- glutCreateWindow("Visualizacao 3D - Movimento de camera "); //cria a janela
- glutDisplayFunc(display); //determina fun��o callback de desenho
- init();
- glutKeyboardFunc(keyboard); //determina fun��o callback de teclado
- glutKeyboardUpFunc(keyboard_up);
- glutPassiveMotionFunc(mouse);
- glutSpecialFunc(SpecialInput);
- glEnable(GL_CULL_FACE); //habilita remo��o de superf�cies ocultas
- glutMainLoop();
- return 0;
- }
- void move(){
- end = clock();
- elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
- if(elapsed_secs > 0.01){
- (free_cam?viewer_prev[2]:viewer[2]) += speed;
- begin = clock();
- if(free_cam){
- viewer[2] += speed_cam;
- }
- }
- glutPostRedisplay();
- }
- void move_straight(int sentido){
- if(sentido){
- end = clock();
- elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
- if(elapsed_secs > 0.003 && to_move){
- if(speed < 4){ speed += 0.05;
- (free_cam?viewer_prev[2]:viewer[2]) += speed;
- }
- begin = clock();
- }
- }else{
- end = clock();
- elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
- if(elapsed_secs > 0.005 && to_move){
- if(speed > -1){ speed -= 0.07;
- (free_cam?viewer_prev[2]:viewer[2]) += speed;
- }
- begin = clock();
- }
- }
- }
- void move_sideways(int sentido){
- if(sentido){
- rot_y_r += 1;
- }else{
- rot_y_r -= 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement