Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************
- ########## Project 1 Basis ##########
- Graphics Processing - PG (if680)
- Center of Informatics - CIn
- Federal University of Pernambuco - UFPE
- @authors
- {
- Caio Lins (csnrl at cin.ufpe.br),
- Geovane Pereira (geeosp at cin.ufpe.br),
- Vinicius Emanuel (vems at cin.ufpe.br)
- }
- Reference for OpenGL commands: https://www.opengl.org/sdk/docs/man2/xhtml/
- *****************************************************/
- #include "Main.h"
- float r = 3;
- // ##### Params START #####
- int old_x = 0;
- int old_y = 0;
- int valid = 0;
- float tx = 0, ty = -0.5, tz = -6; // valores da matriz Rt
- float anguloRotacaoX = 0;
- float anguloRotacaoY = 0;
- float anguloRotacaoZ = 0;
- float fatorTranslacao = 0.1;
- float fatorRotacao = 1.0;
- double znear = 0.1;
- typedef float matriz4x4[16];
- typedef float vetor3[3];
- matriz4x4 translacao = { 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- tx, ty, tz, 1 };
- matriz4x4 rotacao = { 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1 };
- matriz4x4 ID = { 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1 };
- matriz4x4 rotacaoX = { 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1 };
- matriz4x4 rotacaoY = { 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1 };
- GLfloat extrinsic[16] =
- {
- 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- tx, ty, tz, 1
- };
- vetor3 CameraPos = { tx, ty, tz };
- vetor3 CameraLook = { 0, 0, 1 };
- void CameraTranslateX(float t){
- extrinsic[12] += t;
- translacao[12] += t;
- }
- void CameraTranslateZ(float t){
- extrinsic[14] += t;
- translacao[14] += t;
- }
- void MultMat(matriz4x4 m1, matriz4x4 m2, matriz4x4 resultado){
- // Fisrt Column
- resultado[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3];
- resultado[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3];
- resultado[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3];
- resultado[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3];
- // Second Column
- resultado[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7];
- resultado[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7];
- resultado[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7];
- resultado[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7];
- // Third Column
- resultado[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11];
- resultado[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11];
- resultado[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11];
- resultado[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11];
- // Fourth Column
- resultado[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15];
- resultado[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15];
- resultado[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15];
- resultado[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15];
- }
- void tornarIdentidade(matriz4x4 mi){
- mi[0] = mi[5] = mi[10] = mi[15] = 1.0;
- mi[1] = mi[2] = mi[3] = mi[4] = 0.0;
- mi[6] = mi[7] = mi[8] = mi[9] = 0.0;
- mi[11] = mi[12] = mi[13] = mi[14] = 0.0;
- }
- void CameraRotateX(matriz4x4 matriz, float ang){
- float rad = ang * cento80PI;
- tornarIdentidade(matriz);
- matriz[5] = cosf(rad);
- matriz[6] = -sinf(rad);
- matriz[9] = -matriz[6];
- matriz[10] = matriz[5];
- }
- void CameraRotateY(matriz4x4 matriz, float ang){
- float rad = ang * cento80PI;
- tornarIdentidade(matriz);
- matriz[0] = cosf(rad);
- matriz[2] = sinf(rad);
- matriz[8] = -matriz[2];
- matriz[10] = matriz[0];
- }
- bool esconde = false;
- bool dolly = false;
- // ##### Params END #####
- // Screen params
- GLfloat wWidth = 1366.0;
- GLfloat wHeight = 768.0;
- // Constants for object translation and rotation and camera translation
- const double translateConstant = 0.01;
- const double rotateConst = 1.5;
- const double translateCameraConst = 0.005;
- bool buffer[250];
- ///////////////////////////////////////////
- // PARTE DE CARREGAR OS OBJETOS
- ///////////////////////////////////////////
- typedef struct coord {
- float x, y, z;
- coord(float a, float b, float c) : x(a), y(b), z(c) {};
- };
- typedef struct face {
- int num;
- bool quad = false;
- bool penta = false;
- bool hexa = false;
- int faces[6];
- face(int n, int f1, int f2, int f3) : num(n) {
- faces[0] = f1;
- faces[1] = f2;
- faces[2] = f3;
- quad = false;
- }
- face(int n, int f1, int f2, int f3, int f4) : num(n) {
- faces[0] = f1;
- faces[1] = f2;
- faces[2] = f3;
- faces[3] = f4;
- quad = true;
- penta = false;
- hexa = false;
- }
- face(int n, int f1, int f2, int f3, int f4, int f5) : num(n) {
- faces[0] = f1;
- faces[1] = f2;
- faces[2] = f3;
- faces[3] = f4;
- faces[4] = f5;
- quad = false;
- penta = true;
- hexa = false;
- }
- face(int n, int f1, int f2, int f3, int f4, int f5, int f6) : num(n) {
- faces[0] = f1;
- faces[1] = f2;
- faces[2] = f3;
- faces[3] = f4;
- faces[4] = f5;
- faces[5] = f6;
- quad = false;
- penta = false;
- hexa = true;
- }
- };
- typedef struct objPos {
- float x, y, z;
- float scale = 1;
- float rotationX = 0, rotationY = 0, rotationZ = 0;
- objPos(float a, float b, float c) : x(a), y(b), z(c) {};
- };
- int num, select = 0;
- float lx = 0, ly = 0, lz = 0;
- float lx1 = 0, ly1 = 0, lz1 = 0;
- float d = 0.1;
- bool temNormal, ehSimples;
- //float rotationX, rotationY, rotationZ;
- vector<int>listaObj;
- vector<objPos*>listaPosObj;
- vector<string*>listaArquivos;
- // vetor das normais
- vector<coord*> normais;
- void listar(){
- listaArquivos.push_back(new string("./Objetos/chimp.obj"));
- //listaArquivos.push_back(new string("./Objetos/yoda.obj"));
- listaArquivos.push_back(new string("./Objetos/whale.obj"));
- listaArquivos.push_back(new string("./Objetos/chimp.obj"));
- listaArquivos.push_back(new string("./Objetos/spheretri.obj"));
- /*listaArquivos.push_back(new string("./Objetos/venus.obj"));
- listaArquivos.push_back(new string("./Objetos/lion.obj"));
- listaArquivos.push_back(new string("./Objetos/eagle.obj"));
- listaArquivos.push_back(new string("./Objetos/Dog.obj"));
- listaArquivos.push_back(new string("./Objetos/cubo1.obj"));
- listaArquivos.push_back(new string("./Objetos/cube.obj"));
- listaArquivos.push_back(new string("./Objetos/Cow.obj"));
- listaArquivos.push_back(new string("./Objetos/camel.obj"));
- listaArquivos.push_back(new string("./Objetos/apple.obj"));*/
- for (int i = 0; i < listaArquivos.size(); i++) {
- listaPosObj.push_back(new objPos(0, 0, 0));
- }
- }
- void compVetorNormalTriangulo(coord* p1, coord* p2, coord* p3)
- {
- coord* v1 = new coord(0, 0, 0);
- coord* v2 = new coord(0, 0, 0);
- double x, y, z;
- double len;
- /* Encontra vetor v1 */
- v1->x = p2->x - p1->x;
- v1->y = p2->y - p1->y;
- v1->z = p2->z - p1->z;
- /* Encontra vetor v2 */
- v2->x = p3->x - p1->x;
- v2->y = p3->y - p1->y;
- v2->z = p3->z - p1->z;
- /* Calculo do produto vetorial de v1 e v2 */
- x = (v1->y * v2->z) - (v1->z * v2->y);
- y = (v1->z * v2->x) - (v1->x * v2->z);
- z = (v1->x * v2->y) - (v1->y * v2->x);
- /* normalizacao de n */
- len = sqrt(x*x + y*y + z*z);
- x /= len;
- y /= len;
- z /= len;
- //glNormal3f(x, y, z);
- normais.push_back(new coord(x, y, z));
- }
- int loadObj(const char* arquivo) {
- // vetor que armazena cada linha de coordenadas como uma string
- vector<string*> vcoord;
- // vetor que armazena as coordenadas (struct coord)
- vector<coord*> vertices;
- // vetor das faces
- vector<face*> faces;
- int n = 1;
- vector<coord*> novo;
- normais = novo;
- temNormal = false;
- // carrega o arquivo
- ifstream in(arquivo);
- if (!in.is_open()) {
- cout << "Erro, arquivo nao abriu" << endl;
- exit(1);
- }
- char buffer[256];
- // coloca cada linha de coordenadas no vetor vcoord
- while (!in.eof()) {
- in.getline(buffer, 256);
- vcoord.push_back(new string(buffer));
- }
- for (int i = 0; i < vcoord.size(); i++) {
- if (vcoord[i]->c_str()[0] == '#') {
- continue;
- }
- else if (vcoord[i]->c_str()[0] == 'v' && vcoord[i]->c_str()[1] == ' ') {
- float tempx, tempy, tempz;
- sscanf(vcoord[i]->c_str(), "v %f %f %f", &tempx, &tempy, &tempz);
- vertices.push_back(new coord(tempx, tempy, tempz));
- }
- else if (vcoord[i]->c_str()[0] == 'v' && vcoord[i]->c_str()[1] == 'n') {
- float tempx, tempy, tempz;
- sscanf(vcoord[i]->c_str(), "vn %f %f %f", &tempx, &tempy, &tempz);
- normais.push_back(new coord(tempx, tempy, tempz));
- temNormal = true;
- }
- else if (vcoord[i]->c_str()[0] == 'f') {
- int nv1 = 0, nv2 = 0, nv3 = 0, nv4 = 0, nv5 = 0, nv6 = 0, fn = 0, tx1 = 0, tx2 = 0, tx3 = 0, tx4 = 0, tx5 = 0, tx6 = 0;
- /// retira os ultimos characteres caso eles sejam " "
- char lastc = *vcoord[i]->rbegin();
- while (lastc == ' ') {
- *vcoord[i] = vcoord[i]->substr(0, vcoord[i]->size() - 1);
- lastc = *vcoord[i]->rbegin();
- }
- ////////
- if (count(vcoord[i]->begin(), vcoord[i]->end(), ' ') == 6) //hexa
- {
- if (vcoord[i]->find("//") != string::npos)
- {
- sscanf(vcoord[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d %d//%d %d//%d", &nv1, &fn, &nv2, &fn, &nv3, &fn, &nv4, &fn, &nv5, &fn, &nv6, &fn);
- //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4), abs(nv5), abs(nv6)));
- faces.push_back(new face(fn, nv1, nv2, nv3, nv4, nv5, nv6));
- }
- else if (vcoord[i]->find("/") != string::npos)
- {
- sscanf(vcoord[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &nv1, &tx1, &fn, &nv2, &tx2, &fn, &nv3, &tx3, &fn, &nv4, &tx4, &fn, &nv5, &tx5, &fn, &nv6, &tx6, &fn);
- //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4), abs(nv5), abs(nv6)));
- faces.push_back(new face(fn, nv1, nv2, nv3, nv4, nv5, nv6));
- }
- else{
- ehSimples = true;
- sscanf(vcoord[i]->c_str(), "f %d %d %d %d %d %d", &nv1, &nv2, &nv3, &nv4, &nv5, &nv6);
- faces.push_back(new face(-1, nv1, nv2, nv3, nv4, nv5, nv6));
- }
- }
- else if (count(vcoord[i]->begin(), vcoord[i]->end(), ' ') == 5) //penta
- {
- if (vcoord[i]->find("//") != string::npos)
- {
- sscanf(vcoord[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d %d//%d", &nv1, &fn, &nv2, &fn, &nv3, &fn, &nv4, &fn, &nv5, &fn);
- //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4), abs(nv5)));
- faces.push_back(new face(fn, nv1, nv2, nv3, nv4, nv5));
- }
- else if (vcoord[i]->find("/") != string::npos)
- {
- sscanf(vcoord[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &nv1, &tx1, &fn, &nv2, &tx2, &fn, &nv3, &tx3, &fn, &nv4, &tx4, &fn, &nv5, &tx5, &fn);
- //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4), abs(nv5)));
- faces.push_back(new face(fn, nv1, nv2, nv3, nv4, nv5));
- }
- else{
- ehSimples = true;
- sscanf(vcoord[i]->c_str(), "f %d %d %d %d %d", &nv1, &nv2, &nv3, &nv4, &nv5);
- faces.push_back(new face(-1, nv1, nv2, nv3, nv4, nv5));
- }
- }
- else if (count(vcoord[i]->begin(), vcoord[i]->end(), ' ') == 4) //quad
- {
- if (vcoord[i]->find("//") != string::npos)
- {
- sscanf(vcoord[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d", &nv1, &fn, &nv2, &fn, &nv3, &fn, &nv4, &fn);
- //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4)));
- faces.push_back(new face(fn, nv1, nv2, nv3, nv4));
- }
- else if (vcoord[i]->find("/") != string::npos)
- {
- sscanf(vcoord[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &nv1, &tx1, &fn, &nv2, &tx2, &fn, &nv3, &tx3, &fn, &nv4, &tx4, &fn);
- //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4)));
- faces.push_back(new face(fn, nv1, nv2, nv3, nv4));
- }
- else{
- ehSimples = true;
- sscanf(vcoord[i]->c_str(), "f %d %d %d %d", &nv1, &nv2, &nv3, &nv4);
- faces.push_back(new face(-1, nv1, nv2, nv3, nv4));
- }
- }
- else { //tri
- if (vcoord[i]->find("//") != string::npos)
- {
- sscanf(vcoord[i]->c_str(), "f %d//%d %d//%d %d//%d", &nv1, &fn, &nv2, &fn, &nv3, &fn);
- //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3)));
- faces.push_back(new face(fn, nv1, nv2, nv3));
- }
- else if (vcoord[i]->find("/") != string::npos)
- {
- sscanf(vcoord[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &nv1, &tx1, &fn, &nv2, &tx2, &fn, &nv3, &tx3, &fn);
- //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3)));
- faces.push_back(new face(fn, nv1, nv2, nv3));
- }
- else{
- ehSimples = true;
- sscanf(vcoord[i]->c_str(), "f %d %d %d", &nv1, &nv2, &nv3);
- faces.push_back(new face(n, nv1, nv2, nv3));
- n++;
- }
- }
- }
- }
- if (!temNormal) {
- for (int i = 0; i < faces.size(); i++) {
- if (!faces[i]->quad && !(faces[i]->penta) && !(faces[i]->hexa)) {
- compVetorNormalTriangulo(vertices[faces[i]->faces[0] - 1], vertices[faces[i]->faces[1] - 1], vertices[faces[i]->faces[2] - 1]);
- }
- }
- }
- // desenha
- num = glGenLists(1);
- glNewList(num, GL_COMPILE);
- {
- glPointSize(2.0);
- glPushMatrix();
- printf("Faces %d, Normais %d \n", faces.size(), normais.size());
- }
- for (int i = 0; i < faces.size(); i++) {
- if (faces[i]->hexa) {
- if (temNormal) {
- glNormal3f(normais[faces[i]->num - 1]->x, normais[faces[i]->num - 1]->y, normais[faces[i]->num - 1]->z);
- }
- glBegin(GL_POLYGON);
- glVertex3f(vertices[faces[i]->faces[0] - 1]->x, vertices[faces[i]->faces[0] - 1]->y, vertices[faces[i]->faces[0] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[1] - 1]->x, vertices[faces[i]->faces[1] - 1]->y, vertices[faces[i]->faces[1] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[2] - 1]->x, vertices[faces[i]->faces[2] - 1]->y, vertices[faces[i]->faces[2] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[3] - 1]->x, vertices[faces[i]->faces[3] - 1]->y, vertices[faces[i]->faces[3] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[4] - 1]->x, vertices[faces[i]->faces[4] - 1]->y, vertices[faces[i]->faces[4] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[5] - 1]->x, vertices[faces[i]->faces[5] - 1]->y, vertices[faces[i]->faces[5] - 1]->z);
- glEnd();
- }
- else if (faces[i]->penta) {
- if (temNormal) {
- glNormal3f(normais[faces[i]->num - 1]->x, normais[faces[i]->num - 1]->y, normais[faces[i]->num - 1]->z);
- }
- glBegin(GL_POLYGON);
- glVertex3f(vertices[faces[i]->faces[0] - 1]->x, vertices[faces[i]->faces[0] - 1]->y, vertices[faces[i]->faces[0] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[1] - 1]->x, vertices[faces[i]->faces[1] - 1]->y, vertices[faces[i]->faces[1] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[2] - 1]->x, vertices[faces[i]->faces[2] - 1]->y, vertices[faces[i]->faces[2] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[3] - 1]->x, vertices[faces[i]->faces[3] - 1]->y, vertices[faces[i]->faces[3] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[4] - 1]->x, vertices[faces[i]->faces[4] - 1]->y, vertices[faces[i]->faces[4] - 1]->z);
- glEnd();
- }
- else if (faces[i]->quad) {
- if (temNormal) {
- glNormal3f(normais[faces[i]->num - 1]->x, normais[faces[i]->num - 1]->y, normais[faces[i]->num - 1]->z);
- }
- glBegin(GL_QUADS);
- glVertex3f(vertices[faces[i]->faces[0] - 1]->x, vertices[faces[i]->faces[0] - 1]->y, vertices[faces[i]->faces[0] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[1] - 1]->x, vertices[faces[i]->faces[1] - 1]->y, vertices[faces[i]->faces[1] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[2] - 1]->x, vertices[faces[i]->faces[2] - 1]->y, vertices[faces[i]->faces[2] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[3] - 1]->x, vertices[faces[i]->faces[3] - 1]->y, vertices[faces[i]->faces[3] - 1]->z);
- glEnd();
- }
- else if (!(faces[i]->quad) && !(faces[i]->penta) && !(faces[i]->hexa)) {
- if (!temNormal) {
- //compVetorNormalTriangulo(vertices[faces[i]->faces[0] - 1], vertices[faces[i]->faces[1] - 1], vertices[faces[i]->faces[2] - 1]);
- glNormal3f(normais[faces[i]->num - 1]->x, normais[faces[i]->num - 1]->y, normais[faces[i]->num - 1]->z);
- }
- else if (temNormal && !ehSimples) {
- glNormal3f(normais[faces[i]->num - 1]->x, normais[faces[i]->num - 1]->y, normais[faces[i]->num - 1]->z);
- }
- if (temNormal && ehSimples) {
- glBegin(GL_TRIANGLES);
- glVertex3f(vertices[faces[i]->faces[0] - 1]->x, vertices[faces[i]->faces[0] - 1]->y, vertices[faces[i]->faces[0] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[1] - 1]->x, vertices[faces[i]->faces[1] - 1]->y, vertices[faces[i]->faces[1] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[2] - 1]->x, vertices[faces[i]->faces[2] - 1]->y, vertices[faces[i]->faces[2] - 1]->z);
- glEnd();
- }
- else {
- glBegin(GL_TRIANGLES);
- glVertex3f(vertices[faces[i]->faces[0] - 1]->x, vertices[faces[i]->faces[0] - 1]->y, vertices[faces[i]->faces[0] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[1] - 1]->x, vertices[faces[i]->faces[1] - 1]->y, vertices[faces[i]->faces[1] - 1]->z);
- glVertex3f(vertices[faces[i]->faces[2] - 1]->x, vertices[faces[i]->faces[2] - 1]->y, vertices[faces[i]->faces[2] - 1]->z);
- glEnd();
- }
- }
- }
- glPopMatrix();
- glEndList();
- listaObj.push_back(num);
- // deleta o que foi alocado para evitar memory leak
- for (int i = 0; i < vcoord.size(); i++) {
- delete vcoord[i];
- }
- for (int i = 0; i < faces.size(); i++) {
- delete faces[i];
- }
- for (int i = 0; i < vertices.size(); i++) {
- delete vertices[i];
- }
- for (int i = 0; i < normais.size(); i++) {
- delete normais[i];
- }
- }
- void initialize() // Initialize opengl params
- {
- glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
- glClearDepth(1.0f);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glShadeModel(GL_SMOOTH);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- }
- void initLight(void) {
- GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat mat_shininess[] = { 50.0 };
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glShadeModel(GL_SMOOTH);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- GLfloat light1_ambient[] = { 0.0, 1.0, 0.0, 1.0 };
- GLfloat light1_diffuse[] = { 0.0, 1.0, 0.0, 1.0 };
- GLfloat light1_specular[] = { 0.0, 1.0, 0.0, 1.0 };
- GLfloat light1_position[] = { -2.0, 0.0, 0.0, 0.0 };
- GLfloat spot_direction[] = { 0.0, 0.0, 0.0 };
- glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
- glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
- glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
- glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHT1);
- glEnable(GL_DEPTH_TEST);
- }
- void myreshape(GLsizei w, GLsizei h) // Called at startup and when you move the window
- {
- float aspect = (float)wWidth / (float)wHeight;
- float top = tan(45 * 0.5) * znear;
- float bottom = -top;
- float left = aspect * bottom;
- float right = aspect * top;
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- wWidth = (GLfloat)w;
- wHeight = (GLfloat)h;
- if (!dolly){
- gluPerspective(45, wWidth / wHeight, 0.1f, 1000.0f);
- }
- else {
- glFrustum(-1.6, 0.9, -1.0, 1.0, znear, 1000.0f);
- //gluPerspective(45, wWidth / wHeight, znear, 3000.0f);
- }
- }
- void draw()
- {
- glLoadMatrixf(extrinsic);
- double x = 0;
- double y = 0;
- double size = 0.3;
- // carrega grid
- if (!esconde){ //desenhando o grid
- int HALF_GRID_SIZE = 20;
- glBegin(GL_LINES);
- for (int i = -HALF_GRID_SIZE; i <= HALF_GRID_SIZE; i++)
- {
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f((float)i, 0, (float)-HALF_GRID_SIZE);
- glVertex3f((float)i, 0, (float)HALF_GRID_SIZE);
- glColor3f(0.0f, 0.1f, 0.0f);
- glVertex3f((float)-HALF_GRID_SIZE, 0, (float)i);
- glVertex3f((float)HALF_GRID_SIZE, 0, (float)i);
- }
- for (int i = -HALF_GRID_SIZE; i <= HALF_GRID_SIZE; i++)
- {
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f((float)i, (float)-HALF_GRID_SIZE, 0);
- glVertex3f((float)i, (float)HALF_GRID_SIZE, 0);
- glColor3f(0.0f, 0.1f, 0.0f);
- glVertex3f((float)-HALF_GRID_SIZE, (float)i, 0);
- glVertex3f((float)HALF_GRID_SIZE, (float)i, 0);
- }
- glEnd();
- }
- // carrega objetos
- for (int i = 0; i < listaObj.size(); i++) {
- if (i != (select)) {
- glPushMatrix();
- glScalef(listaPosObj[i]->scale, listaPosObj[i]->scale, listaPosObj[i]->scale);
- glTranslatef(listaPosObj[i]->x, listaPosObj[i]->y, listaPosObj[i]->z);
- glRotatef(listaPosObj[i]->rotationX, 1, 0, 0);
- glRotatef(listaPosObj[i]->rotationY, 0, 1, 0);
- glRotatef(listaPosObj[i]->rotationZ, 0, 0, 1);
- glCallList(listaObj.at(i));
- glPopMatrix();
- }
- }
- glPushMatrix();
- glScalef(listaPosObj[select]->scale, listaPosObj[select]->scale, listaPosObj[select]->scale);
- glTranslatef(listaPosObj[select]->x, listaPosObj[select]->y, listaPosObj[select]->z);
- glRotatef(listaPosObj[select]->rotationX, 1, 0, 0);
- glRotatef(listaPosObj[select]->rotationY, 0, 1, 0);
- glRotatef(listaPosObj[select]->rotationZ, 0, 0, 1);
- glCallList(listaObj.at(select));
- glPopMatrix();
- }
- void mydisplay()
- {
- cout << "Don't remove me, I am running." << endl;
- glMatrixMode(GL_MODELVIEW);
- glClearColor(0, 0, 0, 0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- draw();
- glutSwapBuffers();
- }
- bool luz1 = false, luz2 = false;
- bool apertou = false;
- void handleKeyboardPressed(unsigned char key, int x, int y)
- {
- buffer[(int)key] = true;
- if (key == '>' || key == '.') {
- if ((select + 1) == listaObj.size()) {
- luz1 = false;
- luz2 = true;
- }
- else {
- luz1 = false;
- luz2 = false;
- select++;
- }
- apertou = true;
- }
- if (key == '<' || key == ',') {
- if ((select - 1) == -1) {
- luz1 = true;
- luz2 = false;
- }
- else {
- luz1 = false;
- luz2 = false;
- select--;
- }
- apertou = true;
- }
- if (key == 'e') {
- dolly = !dolly;
- extrinsic[14] = -300.0;
- znear = 30;
- printf("%d dolly /n", dolly);
- }
- if (key == 'm') {
- printf("%f dollyz /n", extrinsic[14]);
- printf("%f dollyznear /n", znear);
- }
- }
- void handleKeyboardUp(unsigned char key, int x, int y)
- {
- apertou = false;
- buffer[(int)key] = false;
- }
- void idleFunction() // Processa entradas no teclado
- {
- // #### Resumo ####
- // Mover objeto: 1, 2, 3, 4, 5, 6
- // Rotacionar objeto: 7, 8, 9
- // Redimensionar objeto: -\_, =\+
- // Mover camera:
- // Exit: ESC
- // #### Comandos dos Objetos ####
- if (buffer['7'] == true) {
- listaPosObj[select]->rotationX += r;
- if (listaPosObj[select]->rotationX > 360) {
- listaPosObj[select]->rotationX -= 360;
- }
- apertou = true;
- }
- if (buffer['8'] == true) {
- listaPosObj[select]->rotationY += r;
- if (listaPosObj[select]->rotationY > 360) {
- listaPosObj[select]->rotationY -= 360;
- }
- apertou = true;
- }
- if (buffer['9'] == true) {
- listaPosObj[select]->rotationZ += r;
- if (listaPosObj[select]->rotationZ > 360) {
- listaPosObj[select]->rotationZ -= 360;
- }
- apertou = true;
- }
- if (buffer['1'] == true) {
- if (luz1) {
- lx -= 0.01;
- GLfloat light_position[] = { lx, ly, lz, 0.0 };
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- }
- else if (luz2) {
- lx1 -= 0.01;
- GLfloat light1_position[] = { lx1, ly1, lz1, 0.0 };
- glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
- }
- else {
- listaPosObj[select]->x -= d;
- }
- apertou = true;
- }
- if (buffer['2'] == true) {
- if (luz1) {
- lx += 0.01;
- GLfloat light_position[] = { lx, ly, lz, 0.0 };
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- }
- else if (luz2) {
- lx1 += 0.01;
- GLfloat light1_position[] = { lx1, ly1, lz1, 0.0 };
- glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
- }
- else {
- listaPosObj[select]->x += d;
- }
- apertou = true;
- }
- if (buffer['3'] == true) {
- if (luz1) {
- GLfloat light_position[] = { lx, ly - 1, lz, 0.0 };
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- }
- else if (luz2) {
- GLfloat light1_position[] = { lx1, ly1 - 1, lz1, 0.0 };
- glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
- }
- else {
- listaPosObj[select]->y -= d;
- }
- apertou = true;
- }
- if (buffer['4'] == true) {
- if (luz1) {
- GLfloat light_position[] = { lx, ly + 1, lz, 0.0 };
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- }
- else if (luz2) {
- GLfloat light1_position[] = { lx1, ly1 + 1, lz1, 0.0 };
- glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
- }
- else {
- listaPosObj[select]->y += d;
- }
- apertou = true;
- }
- if (buffer['5'] == true) {
- if (luz1) {
- GLfloat light_position[] = { lx, ly, lz - 1, 0.0 };
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- }
- else if (luz2) {
- GLfloat light1_position[] = { lx1, ly1, lz1 - 1, 0.0 };
- glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
- }
- else {
- listaPosObj[select]->z -= d;
- }
- apertou = true;
- }
- if (buffer['6'] == true) {
- if (luz1) {
- lz += 0.01;
- GLfloat light_position[] = { lx, ly, lz, 0.0 };
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- }
- else if (luz2) {
- lz1 += 0.01;
- GLfloat light1_position[] = { lx1, ly1, lz1 + 1, 0.0 };
- glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
- }
- else {
- listaPosObj[select]->z += d;
- }
- apertou = true;
- }
- if (buffer['-'] == true || buffer['_'] == true && !luz1 && !luz2) {
- listaPosObj[select]->scale -= 0.01;
- apertou = true;
- }
- if (buffer['='] == true || buffer['+'] == true && !luz1 && !luz2) {
- listaPosObj[select]->scale += 0.01;
- apertou = true;
- }
- // #### Comandos de câmera ####
- if ((buffer['w'] && buffer['s']) || (buffer['a'] && buffer['d'])) {
- // nada acontece
- }
- else {
- if (buffer['w']) {
- CameraTranslateZ(fatorTranslacao);
- CameraPos[0] = extrinsic[12];
- CameraPos[1] = extrinsic[13];
- CameraPos[2] = extrinsic[14];
- apertou = true;
- }
- if (buffer['s']) {
- CameraTranslateZ(-fatorTranslacao);
- CameraPos[0] = extrinsic[12];
- CameraPos[1] = extrinsic[13];
- CameraPos[2] = extrinsic[14];
- apertou = true;
- }
- if (buffer['d']) {
- CameraTranslateX(-fatorTranslacao);
- CameraPos[0] = extrinsic[12];
- CameraPos[1] = extrinsic[13];
- CameraPos[2] = extrinsic[14];
- apertou = true;
- }
- if (buffer['a']) {
- CameraTranslateX(fatorTranslacao);
- CameraPos[0] = extrinsic[12];
- CameraPos[1] = extrinsic[13];
- CameraPos[2] = extrinsic[14];
- apertou = true;
- }
- }
- if (buffer['i']){
- anguloRotacaoX = anguloRotacaoX + 0.0007;
- CameraRotateX(rotacaoX, anguloRotacaoX);
- MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
- //MultMat(translacao, rotacao, extrinsic);
- MultMat(rotacao, translacao, extrinsic);
- CameraLook[0] = extrinsic[2];
- CameraLook[1] = extrinsic[6];
- CameraLook[2] = extrinsic[10];
- apertou = true;
- }
- if (buffer['k']){
- anguloRotacaoX = anguloRotacaoX - 0.0007;
- CameraRotateX(rotacaoX, anguloRotacaoX);
- MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
- //MultMat(translacao, rotacao, extrinsic);
- MultMat(rotacao, translacao, extrinsic);
- CameraLook[0] = extrinsic[2];
- CameraLook[1] = extrinsic[6];
- CameraLook[2] = extrinsic[10];
- apertou = true;
- }
- if (buffer['j']){
- anguloRotacaoY = anguloRotacaoY + 0.0007;
- CameraRotateY(rotacaoY, anguloRotacaoY);
- MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
- //MultMat(translacao, rotacao, extrinsic);
- MultMat(rotacao, translacao, extrinsic);
- CameraLook[0] = extrinsic[2];
- CameraLook[1] = extrinsic[6];
- CameraLook[2] = extrinsic[10];
- apertou = true;
- }
- if (buffer['l']){
- anguloRotacaoY = anguloRotacaoY - 0.0007;
- CameraRotateY(rotacaoY, anguloRotacaoY);
- MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
- //MultMat(translacao, rotacao, extrinsic);
- MultMat(rotacao, translacao, extrinsic);
- CameraLook[0] = extrinsic[2];
- CameraLook[1] = extrinsic[6];
- CameraLook[2] = extrinsic[10];
- apertou = true;
- }
- // comandos dolly
- if (buffer['r']) {
- if (dolly){
- znear += 0.5;
- CameraTranslateZ(-fatorTranslacao * 50);
- myreshape(wWidth, wHeight);
- apertou = true;
- }
- }
- if (buffer['f']) {
- if (dolly){
- znear -= 0.5;
- CameraTranslateZ(fatorTranslacao * 50);
- myreshape(wWidth, wHeight);
- apertou = true;
- }
- }
- // #### Outros comandos ####
- if (apertou) {
- mydisplay();
- }
- else {
- apertou = false;
- }
- if (buffer[27] == true) // ESC
- exit(0);
- }
- void mouse_func(int button, int state, int x, int y) {
- old_x = x;
- old_y = y;
- valid = state == GLUT_DOWN;
- }
- void motion_func(int x, int y) {
- if (valid) {
- int dx = old_x - x;
- int dy = old_y - y;
- CameraRotateY(rotacaoY, dx * 0.001);
- MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
- //MultMat(translacao, rotacao, extrinsic);
- MultMat(rotacao, translacao, extrinsic);
- CameraRotateX(rotacaoX, dy * 0.0001);
- MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
- //MultMat(translacao, rotacao, extrinsic);
- MultMat(rotacao, translacao, extrinsic);
- }
- old_x = x;
- old_y = y;
- mydisplay();
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
- glutInitWindowSize(wWidth, wHeight);
- glutInitWindowPosition(0, 0);
- glutCreateWindow("OpenGL");
- glutFullScreen();
- initLight();
- glutDisplayFunc(mydisplay);
- glutReshapeFunc(myreshape);
- glutMouseFunc(mouse_func);
- glutMotionFunc(motion_func);
- glutKeyboardUpFunc(handleKeyboardUp);
- glutKeyboardFunc(handleKeyboardPressed);
- glutIdleFunc(idleFunction);
- listar();
- for (int i = 0; i < listaArquivos.size(); i++) {
- loadObj(listaArquivos[i]->c_str());
- }
- //glTranslatef(0, 0, -50);
- initialize();
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement