Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GLEW_STATIC
- #include <GL/glew.h>
- //#include "../common/Angel_util.h"
- #include "LetraT.h"
- LetraT::LetraT() { }
- LetraT::~LetraT() { }
- void LetraT::desenhar()
- {
- glBindVertexArray( vao );
- int inicioPrisma;
- glm::mat4 M, espelhoX;
- espelhoX = glm::scale(espelhoX, glm::vec3(-1.0, 1.0, 1.0));
- for (int i=0; i<nPrismas; i++) {
- M = mModel[i];
- inicioPrisma = i*24;
- glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
- glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
- M = espelhoX * M;
- glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
- glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
- }
- }
- void LetraT::desenharCompleto()
- {
- glBindVertexArray( vao );
- int inicioPrisma;
- glm::mat4 M, espelho;
- espelho = glm::rotate(espelho,glm::radians(180.0f),glm::vec3(-1.0,0.0,0.0));
- espelho = glm::scale(espelho, glm::vec3(-1.0,1.0, 1.0));
- espelho = glm::scale(espelho, glm::vec3(1.0,1.0,-1.0));
- for (int i=0; i<nPrismas; i++) {
- M = mModel[i];
- inicioPrisma = i*24;
- glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
- glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
- M = espelho * M;
- M = glm::rotate(M,glm::radians(180.0f),glm::vec3(1.0,0.0,0.0));
- M = glm::scale(M, glm::vec3(1.0,1.0,-1.0));
- glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
- glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
- }
- }
- void LetraT::setmModel_loc(GLuint Model)
- {
- mModel_loc = Model;
- }
- void LetraT::criaLetraT(GLuint program, const GLchar *position,
- const GLchar *normal)
- {
- // Create and initialize a buffer object
- glGenVertexArrays( 1, &vao );
- glBindVertexArray( vao );
- GLuint vertexbuf;
- glGenBuffers( 1, &vertexbuf);
- glBindBuffer( GL_ARRAY_BUFFER, vertexbuf);
- criaPrismas();
- glBufferData( GL_ARRAY_BUFFER,
- sizeof(points) + sizeof(normals) + sizeof(tex_coord),
- NULL, GL_STATIC_DRAW );
- glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), &points );
- glBufferSubData( GL_ARRAY_BUFFER, sizeof(points), sizeof(normals), &normals );
- glBufferSubData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(normals),
- sizeof(tex_coord), &tex_coord );
- // set up vertex arrays
- GLuint vPosition = glGetAttribLocation( program, position );
- glEnableVertexAttribArray( vPosition );
- glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
- BUFFER_OFFSET(0) );
- GLuint vNormal = glGetAttribLocation( program, normal );
- glEnableVertexAttribArray( vNormal );
- glVertexAttribPointer( vNormal, 4, GL_FLOAT, GL_FALSE, 0,
- BUFFER_OFFSET(sizeof(points)) );
- }
- void LetraT::criaLetraT(GLuint program, const GLchar *position,
- const GLchar *normal, const GLchar *tCoords)
- {
- criaLetraT(program, position, normal);
- GLuint vTCoords = glGetAttribLocation( program, tCoords );
- glEnableVertexAttribArray( vTCoords );
- glVertexAttribPointer( vTCoords, 2, GL_FLOAT, GL_FALSE, 0,
- BUFFER_OFFSET(sizeof(points)+sizeof(normals)) );
- }
- glm::vec4 LetraT::normalDaFace(int a, int b, int c)
- {
- glm::vec3 u; // = vertices[b].xyz - vertices[a].xyz;
- u.x = vertices[b].x - vertices[a].x;
- u.y = vertices[b].y - vertices[a].y;
- u.z = vertices[b].z - vertices[a].z;
- glm::vec3 v; // = vertices[c].xyz - vertices[b].xyz;
- v.x = vertices[c].x - vertices[b].x;
- v.y = vertices[c].y - vertices[b].y;
- v.z = vertices[c].z - vertices[b].z;
- glm::vec3 normal = glm::normalize( glm::cross(u, v) );
- glm::vec4 normal4 = glm::vec4(normal,0.0);
- return normal4;
- }
- void LetraT::triang( int a, int b, int c)
- {
- glm::vec4 normal4 = normalDaFace(a,b,c);
- points[indexVert] = vertices[a];
- normals[indexVert] = normal4;
- tex_coord[indexVert] = glm::vec2((maxX+vertices[a].x)/maxX2, vertices[a].y/maxY);
- indexVert++;
- points[indexVert] = vertices[b];
- normals[indexVert] = normal4;
- tex_coord[indexVert] = glm::vec2((maxX+vertices[b].x)/maxX2, vertices[b].y/maxY);
- indexVert++;
- points[indexVert] = vertices[c];
- normals[indexVert] = normal4;
- tex_coord[indexVert] = glm::vec2((maxX+vertices[c].x)/maxX2, vertices[c].y/maxY);
- indexVert++;
- }
- void LetraT::quad( int a, int b, int c, int d )
- {
- glm::vec4 normal4 = normalDaFace(a,b,c);
- // triangulo 1
- points[indexVert] = vertices[a];
- normals[indexVert] = normal4;
- tex_coord[indexVert] = glm::vec2(0.0f,0.0f);
- indexVert++;
- points[indexVert] = vertices[b];
- normals[indexVert] = normal4;
- tex_coord[indexVert] = glm::vec2(2.0f,0.0f);
- indexVert++;
- points[indexVert] = vertices[c];
- normals[indexVert] = normal4;
- tex_coord[indexVert] = glm::vec2(2.0f,1.0f);
- indexVert++;
- // triangulo 2
- points[indexVert] = vertices[a];
- normals[indexVert] = normal4;
- tex_coord[indexVert] = glm::vec2(0.0f,0.0f);
- indexVert++;
- points[indexVert] = vertices[c];
- normals[indexVert] = normal4;
- tex_coord[indexVert] = glm::vec2(2.0f,1.0f);
- indexVert++;
- points[indexVert] = vertices[d];
- normals[indexVert] = normal4;
- tex_coord[indexVert] = glm::vec2(0.0f,1.0f);
- indexVert++;
- }
- // gerar 8 triângulos: 24 vertices
- void LetraT::prisma(int aF, int bF, int cF, int aT, int bT, int cT)
- {
- // prisma de bases triangulares e laterais retangulares
- triang(aF, bF, cF); // bases
- triang(aT, cT, bT);
- quad(aF,aT,bT,bF); // laterais
- quad(bF,bT,cT,cF);
- quad(cF,cT,aT,aF);
- }
- void LetraT::criaPrismas()
- {
- int aF, bF, cF, aT, bT, cT, k;
- indexVert = 0;
- // centralizar modelo (letra T) na origem
- for (int i=0; i<nVertices; i++)
- vertices[i].y -= maxY/2.0;
- for (int i=0; i<nPrismas; i++) {
- k = i*6;
- aF = prismas[k]; bF = prismas[k+1]; cF = prismas[k+2];
- aT = prismas[k+3]; bT = prismas[k+4]; cT = prismas[k+5];
- prisma(aF, bF, cF, aT, bT, cT);
- centroPrisma[i].x = (vertices[aF].x + vertices[bF].x + vertices[cF].x +
- vertices[aF].x + vertices[bF].x + vertices[cF].x )/6.0;
- centroPrisma[i].y = (vertices[aF].y + vertices[bF].y + vertices[cF].y +
- vertices[aF].y + vertices[bF].y + vertices[cF].y )/6.0;
- centroPrisma[i].z = (vertices[aF].z + vertices[bF].z + vertices[cF].z +
- vertices[aF].z + vertices[bF].z + vertices[cF].z )/6.0;
- }
- }
- //----------------------------------------------------------------------------
- void LetraT::explosao()
- {
- glm::vec3 vetExpl[nPrismas], //vetor de deslocamento do prisma
- eixoRotExpl[nPrismas], //vetor direção do eixo de rotação do prisma
- direcao[nPrismas];
- int k;
- // definir dados da explosão para cada prisma
- // definir aleatoriamente vetor de translação para cada prisma
- for (int i=0; i<nPrismas; i++) {
- vetExpl[i].x = -rand(); // x sempre negativo, porque o meioT setá espelhado
- // para a porção positiva do eixo x
- k = rand();
- vetExpl[i].y = (k%2==0)? k : -k;
- k = rand();
- vetExpl[i].z = (k%2==0)? k : -k;
- // diminuir módulo do vetor
- vetExpl[i] *= 1.0e-8;
- }
- // definir aleatoriamente um vetor de direção para o eixo
- // de rotação de cada prisma. Será considerado que esse eixo
- // passa pelo centro do prisma
- for (int i=0; i<nPrismas; i++) {
- k = rand();
- eixoRotExpl[i].x = (k%2==0)? k : -k;
- k = rand();
- eixoRotExpl[i].y = (k%2==0)? k : -k;
- k = rand();
- eixoRotExpl[i].z = (k%2==0)? k : -k;
- }
- //gira em torno de si
- for (int j = 0; j < 120.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i)
- mModel[i] = glm::rotate(mModel[i],glm::radians(1.0f),glm::vec3(-centroPrisma[i].x,-centroPrisma[i].y,-centroPrisma[i].z));
- // mModel[i] = glm::translate(mModel[i],glm::vec3(0.0f,0.0f,direcao[i].z));
- desenhar();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<999990; k++);
- }
- //translada ida
- for (int j = 0; j < 180.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i){
- mModel[i] = glm::translate(mModel[i],glm::vec3(vetExpl[i].x,vetExpl[i].y,vetExpl[i].z));
- mModel[i] = glm::rotate(mModel[i], glm::radians(1.0f),glm::vec3(eixoRotExpl[i].x,eixoRotExpl[i].y,eixoRotExpl[i].z));
- }
- desenhar();
- glutSwapBuffers();
- }
- for (int j = 0; j < 180.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i)
- mModel[i] = glm::translate(mModel[i],glm::vec3(0.0025*centroPrisma[i].x,0.0025*centroPrisma[i].y,0.0025*centroPrisma[i].z));
- desenhar();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<999991; k++);
- }
- //volta para a origem
- for (int j = 0; j < 180.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i)
- mModel[i] = glm::translate(mModel[i],glm::vec3(-0.0025*centroPrisma[i].x,-0.0025*centroPrisma[i].y,-0.0025*centroPrisma[i].z));
- desenhar();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<999991; k++);
- }
- for (int j = 0; j < 180.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i){
- mModel[i] = glm::translate(mModel[i],glm::vec3(-vetExpl[i].x,-vetExpl[i].y,-vetExpl[i].z));
- mModel[i] = glm::rotate(mModel[i], glm::radians(1.0f),glm::vec3(-eixoRotExpl[i].x,-eixoRotExpl[i].y,-eixoRotExpl[i].z));
- }
- desenhar();
- glutSwapBuffers();
- }
- //rotaciona de volta
- for (int j = 0; j < 120.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i)
- mModel[i] = glm::rotate(mModel[i],glm::radians(1.0f),glm::vec3(centroPrisma[i].x,centroPrisma[i].y,centroPrisma[i].z));
- desenhar();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<99999; k++);
- }
- }
- void LetraT::explosaoInvertida(){
- glm::vec3 vetExpl[nPrismas], //vetor de deslocamento do prisma
- eixoRotExpl[nPrismas], //vetor direção do eixo de rotação do prisma
- direcao[nPrismas];
- int k;
- // definir dados da explosão para cada prisma
- // definir aleatoriamente vetor de translação para cada prisma
- for (int i=0; i<nPrismas; i++) {
- vetExpl[i].x = -rand(); // x sempre negativo, porque o meioT setá espelhado
- // para a porção positiva do eixo x
- k = rand();
- vetExpl[i].y = (k%2==0)? k : -k;
- k = rand();
- vetExpl[i].z = (k%2==0)? k : -k;
- // diminuir módulo do vetor
- vetExpl[i] *= 1.0e-8;
- }
- // definir aleatoriamente um vetor de direção para o eixo
- // de rotação de cada prisma. Será considerado que esse eixo
- // passa pelo centro do prisma
- for (int i=0; i<nPrismas; i++) {
- k = rand();
- eixoRotExpl[i].x = (k%2==0)? k : -k;
- k = rand();
- eixoRotExpl[i].y = (k%2==0)? k : -k;
- k = rand();
- eixoRotExpl[i].z = (k%2==0)? k : -k;
- }
- //gira em torno de si
- for (int j = 0; j < 120.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i)
- mModel[i] = glm::rotate(mModel[i],glm::radians(1.0f),glm::vec3(-centroPrisma[i].x,-centroPrisma[i].y,-centroPrisma[i].z));
- // mModel[i] = glm::translate(mModel[i],glm::vec3(0.0f,0.0f,direcao[i].z));
- desenhar();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<999990; k++);
- }
- //translada ida
- for (int j = 0; j < 180.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i){
- mModel[i] = glm::translate(mModel[i],glm::vec3(vetExpl[i].x,vetExpl[i].y,vetExpl[i].z));
- mModel[i] = glm::rotate(mModel[i], glm::radians(1.0f),glm::vec3(eixoRotExpl[i].x,eixoRotExpl[i].y,eixoRotExpl[i].z));
- }
- desenhar();
- glutSwapBuffers();
- }
- for (int j = 0; j < 180.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i)
- mModel[i] = glm::translate(mModel[i],glm::vec3(0.0025*centroPrisma[i].x,0.0025*centroPrisma[i].y,0.0025*centroPrisma[i].z));
- desenhar();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<999991; k++);
- }
- glm::mat4 M;
- M = glm::rotate(M, glm::radians(1.0f), glm::vec3(0.0,0.0,1.0));
- for (int i=0; i<180.0; i++) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int j=0; j<nPrismas; j++)
- mModel[j] = M*mModel[j];
- desenhar();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<999999; k++);
- }
- //volta para a origem
- for (int j = 0; j < 180.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i)
- mModel[i] = glm::translate(mModel[i],glm::vec3(-0.0025*centroPrisma[i].x,-0.0025*centroPrisma[i].y,-0.0025*centroPrisma[i].z));
- desenhar();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<999991; k++);
- }
- for (int j = 0; j < 180.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i){
- mModel[i] = glm::translate(mModel[i],glm::vec3(-vetExpl[i].x,-vetExpl[i].y,-vetExpl[i].z));
- mModel[i] = glm::rotate(mModel[i], glm::radians(1.0f),glm::vec3(-eixoRotExpl[i].x,-eixoRotExpl[i].y,-eixoRotExpl[i].z));
- }
- desenhar();
- glutSwapBuffers();
- }
- //rotaciona de volta
- for (int j = 0; j < 120.0; ++j)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int i = 0; i < nPrismas; ++i)
- mModel[i] = glm::rotate(mModel[i],glm::radians(1.0f),glm::vec3(centroPrisma[i].x,centroPrisma[i].y,centroPrisma[i].z));
- desenhar();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<99999; k++);
- }
- }
- //----------------------------------------------------------------------------
- void LetraT::girar()
- {
- glm::mat4 M;
- M = glm::rotate(M, glm::radians(1.0f), glm::vec3(0.0,0.0,1.0));
- for (int i=0; i<180.0; i++) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int j=0; j<nPrismas; j++)
- mModel[j] = M*mModel[j];
- desenhar();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<999999; k++);
- }
- }
- void LetraT::girarCompleto(){
- glm::mat4 M;
- M = glm::rotate(M, glm::radians(1.0f), glm::vec3(0.0,0.0,1.0));
- for (int i=0; i<180.0; i++) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int j=0; j<nPrismas; j++)
- mModel[j] = M*mModel[j];
- desenharCompleto();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<999999; k++);
- }
- }
- void LetraT::curva(){
- glm::mat4 M;
- M = glm::rotate(M, glm::radians(1.0f), glm::vec3(0.0,0.0,1.0));
- //M = glm::translate(M, glm::vec3(0.01,0.0,0.0));
- for (int i=0; i<180.0; i++) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for (int j=0; j<nPrismas; j++)
- mModel[j] = M*mModel[j];
- curvaParametrica();
- glutSwapBuffers();
- //espere um pouco
- for (int k=0; k<999999; k++);
- }
- }
- void LetraT::curvaParametrica(){
- glBindVertexArray( vao );
- int inicioPrisma;
- static float x = 0.0;
- glm::mat4 M, espelho;
- espelho = glm::rotate(espelho,glm::radians(180.0f),glm::vec3(-1.0,0.0,0.0));
- espelho = glm::scale(espelho, glm::vec3(-1.0,1.0, 1.0));
- espelho = glm::scale(espelho, glm::vec3(1.0,1.0,-1.0));
- //espelho = glm::translate(M, glm::vec3(-0.0001,0.0,0.0));
- for (int i=0; i<nPrismas; i++) {
- x += sqrt(0.01);
- M = mModel[i];
- M = glm::translate(M, glm::vec3(-1.0,0.0,0.0));
- inicioPrisma = i*24;
- glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
- glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
- M = espelho * M;
- M = glm::translate(M, glm::vec3(-2.0,0.0,0.0));
- M = glm::rotate(M,glm::radians(180.0f),glm::vec3(1.0,0.0,0.0));
- M = glm::scale(M, glm::vec3(1.0,1.0,-1.0));
- //M = glm::translate(M, glm::vec3(0.0001,0.0,0.0));
- glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
- glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement