BambooIsland: C:/Users/easterbunny/Desktop/Echtzeitgraphik-LU/trunk/src/SceneObject_Sphere.cpp Source File

BambooIsland Abgabe 2

- Main Page
- Related Pages
- Classes
- Files

- File List

# C:/Users/easterbunny/Desktop/Echtzeitgraphik-LU/trunk/src/SceneObject_Sphere.cpp

    00001 /*
    00002  * source file for SceneObject_Sphere class
    00003  * written by: christian moellinger <ch.moellinger@gmail.com>
    00004  * 10/2010
    00005  */
    00006 #include <string>
    00007 #include <vector>
    00008
    00009 #include <glm/gtc/matrix_projection.hpp>
    00010 #include <glm/gtc/matrix_transform.hpp>
    00011 #include <glm/gtx/transform.hpp>
    00012 #include <glm/gtx/transform2.hpp>
    00013 #include <glm/gtc/type_ptr.hpp>
    00014 #include <glm/glm.hpp>
    00015
    00016 #include "TextureManager.h"
    00017 #include "Logger.h"
    00018 #include "ShaderManager.h"
    00019 #include "SceneObject_Sphere.h"
    00020
    00021 using namespace std;
    00022
    00023 const double PI = 3.141516;
    00024
    00025 SceneObject_Sphere::SceneObject_Sphere() : SceneObject()
    00026 {
    00027     int i, j;
    00028     int t=0;    //for index array
    00029     int a, b;
    00030     const int lats=24;
    00031     const int longs=24;
    00032     vector<GLdouble> puffer;
    00033     vector<GLint> pufferInd;
    00034     GLdouble *vertexArray;
    00035     GLuint *indexArray;
    00036
    00037     for(i = 1; i <= lats; i++)
    00038     {
    00039             double lat_n1 = PI * (-0.5 + (double) (i - 2) / lats);
    00040             double z_n1  = sin(lat_n1);
    00041             double zr_n1 =  cos(lat_n1);
    00042
    00043             double lat0 = PI * (-0.5 + (double) (i - 1) / lats);
    00044             double z0  = sin(lat0);
    00045             double zr0 =  cos(lat0);
    00046
    00047             double lat1 = PI * (-0.5 + (double) i / lats);
    00048             double z1 = sin(lat1);
    00049             double zr1 = cos(lat1);
    00050
    00051             double lat2 = PI * (-0.5 + (double) (i+1) / lats);
    00052             double z2 = sin(lat2);
    00053             double zr2 = cos(lat2);
    00054
    00055             for(j = 0; j <= longs; j++)
    00056             {
    00057                     double lng = 2 * PI * (double) (j - 1) / longs;
    00058                     double x = cos(lng);
    00059                     double y = sin(lng);
    00060
    00061                     puffer.push_back(x*zr0);        //x
    00062                     puffer.push_back(y*zr0);        //y
    00063                     puffer.push_back(-z0);          //z
    00064
    00065                     //calculate tangent
    00066                     glm::vec3 tangent;
    00067                     tangent = glm::normalize(glm::vec3((x*zr1 - x*zr_n1), y*zr1 - y*zr_n1, (-z1) - (-z_n1)));
    00068
    00069                     puffer.push_back(tangent.x);
    00070                     puffer.push_back(tangent.y);
    00071                     puffer.push_back(tangent.z);
    00072
    00073                     //now add texture coordinates
    00074
    00075                     puffer.push_back((float)j / longs);     //z
    00076                     puffer.push_back((float)(i-1) / lats);          //u
    00077                     pufferInd.push_back(t++);       //add index
    00078
    00079                     /* next longitude */
    00080                     puffer.push_back(x*zr1);
    00081                     puffer.push_back(y*zr1);
    00082                     puffer.push_back(-z1);
    00083
    00084                     //calculate tangent
    00085                     tangent = glm::normalize(glm::vec3(x*zr2 - x*zr0, y*zr2 - y*zr0, (-z2) - (-z0)));
    00086
    00087                     puffer.push_back(tangent.x);
    00088                     puffer.push_back(tangent.y);
    00089                     puffer.push_back(tangent.z);
    00090
    00091                     puffer.push_back((float)(j) / longs);           //u
    00092                     puffer.push_back((float)(i) / lats);            //z
    00093                     pufferInd.push_back(t++);
    00094             }
    00095     }
    00096     vertexArray = new GLdouble[puffer.size()];
    00097     indexArray = new GLuint[puffer.size()];
    00098     /* den Index-Array befüllen. Es wird ein TRIANGLE_STRIP erzeugt, daher im zick-zack verfahren */
    00099     for (a=0,b=0; a < pufferInd.size(); a+=1)
    00100     {
    00101             indexArray[b++] = pufferInd[a];
    00102     }
    00103     /* nun den Vertex-Array befüllen - interleaved mit den Color-Werten und Normalvektoren(x1,y1,z1,r1,g1,b1,Nx1,Ny1,Nz1,x2,y2,z2,r2,g2,b2,Nx2,Ny2,Nz2,...) */
    00104     for (a=0,b=0; a < puffer.size(); a++)
    00105     {
    00106             vertexArray[b++] = puffer[a];
    00107     }
    00108
    00109     glGenVertexArrays(1, &this->vao);
    00110     glBindVertexArray(this->vao);
    00111
    00112     /* Vertexbuffer generieren */
    00113     glGenBuffers(1,&this->vertex_buffer);
    00114     glGenBuffers(1,&this->index_buffer);
    00115     /* zum befüllen mal aktivieren ...*/
    00116     glBindBuffer(GL_ARRAY_BUFFER, this->vertex_buffer);
    00117     /* befüllen ... */
    00118     glBufferData(GL_ARRAY_BUFFER,puffer.size()*sizeof(vertexArray[0]),vertexArray,GL_STATIC_DRAW);
    00119
    00120     /* nun nur noch den Index-Array in den Bufferbereich schreiben */
    00121     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,this->index_buffer);
    00122     glBufferData(GL_ELEMENT_ARRAY_BUFFER, pufferInd.size()*sizeof(indexArray[0]), indexArray, GL_STATIC_DRAW);
    00123
    00124     this->indexArray_size = pufferInd.size();
    00125
    00126     delete[] vertexArray;
    00127     delete[] indexArray;
    00128
    00129     //check for error
    00130     GLenum error = glGetError();
    00131
    00132     if (error != GL_NO_ERROR)
    00133         Logger::instance()->log(Logger::ERROR, translateGLerror(error));
    00134
    00135     Logger::instance()->log(Logger::DEBUG, "SceneObject_Sphere created");
    00136 }
    00137
    00138 SceneObject_Sphere::~SceneObject_Sphere()
    00139 {
    00140
    00141 }
    00142
    00143 void SceneObject_Sphere::itlRender()
    00144 {
    00145     bool bShaderActivated;
    00146
    00147     glBindVertexArray(vao);
    00148     glBindBuffer(GL_ARRAY_BUFFER, this->vertex_buffer);
    00149
    00150     ShaderManager::instance()->pushActiveShader();
    00151     bShaderActivated = ShaderManager::instance()->activateShader("bump_shader");
    00152
    00153     this->sendTransformMatrices(true);
    00154
    00155     const GLint l_in_Position(ShaderManager::instance()->getAttribute("in_Position"));
    00156     const GLint l_normals(ShaderManager::instance()->getAttribute("in_Normal"));
    00157     const GLint l_texcoords(ShaderManager::instance()->getAttribute("in_Texcoord"));
    00158     const GLint l_tangents(ShaderManager::instance()->getAttribute("in_Tangent"));
    00159
    00160     if (l_in_Position != -1)
    00161     {
    00162         glVertexAttribPointer(l_in_Position, 3, GL_DOUBLE, GL_FALSE, 8 * sizeof(GLdouble), NULL);
    00163         glEnableVertexAttribArray(l_in_Position);
    00164     }
    00165
    00166     if (l_normals != -1)
    00167     {
    00168         glVertexAttribPointer(l_normals, 3, GL_DOUBLE, GL_FALSE, 8 * sizeof(GLdouble), NULL);
    00169         glEnableVertexAttribArray(l_normals);
    00170     }
    00171
    00172     if (l_texcoords != -1)
    00173     {
    00174         glVertexAttribPointer(l_texcoords, 2, GL_DOUBLE, GL_FALSE, 8 * sizeof(GLdouble), (GLvoid *) (6 * sizeof(GLdouble)));
    00175         glEnableVertexAttribArray(l_texcoords);
    00176     }
    00177
    00178     if (l_tangents != -1)
    00179     {
    00180         glVertexAttribPointer(l_tangents, 3, GL_DOUBLE, GL_FALSE, 8 * sizeof(GLdouble), (GLvoid *) (3 * sizeof(GLdouble)));
    00181         glEnableVertexAttribArray(l_tangents);
    00182     }
    00183
    00184     GLint l_color_texture = ShaderManager::instance()->getUniform("color_texture");
    00185     GLint l_normal_texture = ShaderManager::instance()->getUniform("normal_texture");
    00186
    00187     if (l_color_texture != -1)
    00188     {
    00189         GLuint used_texture_unit1 = TextureManager::instance()->useTexture("sphere14");     //load texture in texture unit
    00190         glUniform1i(l_color_texture,used_texture_unit1);   //tell the shader which texture unit was used
    00191     }
    00192
    00193     if (l_normal_texture != -1)
    00194     {
    00195         GLuint used_texture_unit2 = TextureManager::instance()->useTexture("sphere14_bump");        //load texture in texture unit
    00196         glUniform1i(ShaderManager::instance()->getUniform("normal_texture"),used_texture_unit2);   //tell the shader which texture unit was used
    00197     }
    00198
    00199     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,this->index_buffer);
    00200     glDrawElements(GL_TRIANGLE_STRIP, this->indexArray_size, GL_UNSIGNED_INT, NULL);
    00201
    00202     if (l_color_texture != -1)
    00203         TextureManager::instance()->unuseTexture("sphere14");                               //tell the texturemanager to release the used texture unit
    00204
    00205     if (l_normal_texture != -1)
    00206         TextureManager::instance()->unuseTexture("sphere14_bump");                                  //tell the texturemanager to release the used texture unit
    00207
    00208     ShaderManager::instance()->popActiveShader();
    00209 }

All Classes Functions Variables Enumerations Enumerator