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

BambooIsland Abgabe 2

- Main Page
- Related Pages
- Classes
- Files

- File List

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

    00001 /*
    00002  * source file for SceneObject_Sphere class
    00003  * written by: christian moellinger <ch.moellinger@gmail.com>
    00004  * 10/2010
    00005  */
    00006
    00007 #include <glm/gtc/matrix_projection.hpp>
    00008 #include <glm/gtx/inverse_transpose.hpp>
    00009 #include <glm/gtc/matrix_transform.hpp>
    00010 #include <glm/gtx/transform.hpp>
    00011 #include <glm/gtx/transform2.hpp>
    00012 #include <glm/gtc/type_ptr.hpp>
    00013
    00014 #include <string>
    00015 #include <vector>
    00016
    00017 #include "TextureManager.h"
    00018 #include "Camera.h"
    00019 #include "Logger.h"
    00020 #include "ShaderManager.h"
    00021 #include "SceneObject_Skysphere.h"
    00022
    00023 using namespace std;
    00024
    00025 const double PI = 3.1415926;
    00026
    00027 SceneObject_Skysphere::SceneObject_Skysphere() : SceneObject()
    00028 {
    00029     int i, j;
    00030     int t=0;    //for index array
    00031     int a, b;
    00032     const int lats=28;
    00033     const int longs=40;
    00034     vector<GLdouble> puffer;
    00035     vector<GLint> pufferInd;
    00036     GLdouble *vertexArray;
    00037     GLuint *indexArray;
    00038
    00039     for(i = 1; i <= lats; i++)
    00040     {
    00041             double lat0 = PI * (-0.5 + (double) (i - 1) / lats);
    00042             double z0  = sin(lat0);
    00043             double zr0 =  cos(lat0);
    00044             double lat1 = PI * (-0.5 + (double) i / lats);
    00045             double z1 = sin(lat1);
    00046             double zr1 = cos(lat1);
    00047             for(j = 0; j <= longs; j++)
    00048             {
    00049                     double lng = 2 * PI * (double) (j - 1) / longs;
    00050                     double x = cos(lng);
    00051                     double y = sin(lng);
    00052                     puffer.push_back(x*zr1);        //x
    00053                     puffer.push_back(y*zr1);        //y
    00054                     puffer.push_back(-z1);          //z
    00055                     puffer.push_back((float)j / longs);     //z
    00056                     puffer.push_back((float)(i) / lats);            //u
    00057                     pufferInd.push_back(t++);       //add index
    00058                     puffer.push_back(x*zr0);
    00059                     puffer.push_back(y*zr0);
    00060                     puffer.push_back(-z0);
    00061                     puffer.push_back((float)(j) / longs);           //u
    00062                     puffer.push_back((float)(i-1) / lats);          //z
    00063                     pufferInd.push_back(t++);
    00064             }
    00065     }
    00066     vertexArray = new GLdouble[puffer.size()];
    00067     indexArray = new GLuint[puffer.size()];
    00068     /* den Index-Array befüllen. Es wird ein TRIANGLE_STRIP erzeugt, daher im zick-zack verfahren */
    00069     for (a=0,b=0; a < pufferInd.size(); a+=1)
    00070     {
    00071             indexArray[b++] = pufferInd[a];
    00072     }
    00073     /* 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,...) */
    00074     for (a=0,b=0; a < puffer.size(); a++)
    00075     {
    00076             vertexArray[b++] = puffer[a];
    00077     }
    00078
    00079     glGenVertexArrays(1, &this->vao);
    00080     glBindVertexArray(this->vao);
    00081
    00082     /* Vertexbuffer generieren */
    00083     glGenBuffers(1,&this->vertex_buffer);
    00084     glGenBuffers(1,&this->index_buffer);
    00085     /* zum befüllen mal aktivieren ...*/
    00086     glBindBuffer(GL_ARRAY_BUFFER, this->vertex_buffer);
    00087     /* befüllen ... */
    00088     glBufferData(GL_ARRAY_BUFFER,puffer.size()*sizeof(vertexArray[0]),vertexArray,GL_STATIC_DRAW);
    00089
    00090     /* nun nur noch den Index-Array in den Bufferbereich schreiben */
    00091     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,this->index_buffer);
    00092     glBufferData(GL_ELEMENT_ARRAY_BUFFER, pufferInd.size()*sizeof(indexArray[0]), indexArray, GL_STATIC_DRAW);
    00093
    00094     this->indexArray_size = pufferInd.size();
    00095
    00096     /* WICHTIG: Buffer wieder deaktivieren, um keine Nebenwirkungen auf andere Calls zu haben. Bei Bedarf wieder aktivieren */
    00097     glBindBuffer(GL_ARRAY_BUFFER,0);
    00098     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
    00099
    00100     delete[] vertexArray;
    00101     delete[] indexArray;
    00102
    00103     //check for error
    00104     GLenum error = glGetError();
    00105
    00106     if (error != GL_NO_ERROR)
    00107         Logger::instance()->log(Logger::ERROR, translateGLerror(error));
    00108
    00109     Logger::instance()->log(Logger::DEBUG, "SceneObject_SkySphere created");
    00110 }
    00111
    00112 SceneObject_Skysphere::~SceneObject_Skysphere()
    00113 {
    00114
    00115 }
    00116
    00117 void SceneObject_Skysphere::itlRender()
    00118 {
    00119     bool bWeUnlockedActiveShader = false;
    00120
    00121     glBindVertexArray(vao);
    00122     glBindBuffer(GL_ARRAY_BUFFER, this->vertex_buffer);
    00123     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,this->index_buffer);
    00124
    00125     //if the current shader is locked, unlock it to force the use of skysphere-shader
    00126     if (ShaderManager::instance()->isActiveShaderLocked())
    00127     {
    00128         bWeUnlockedActiveShader = true;
    00129         ShaderManager::instance()->unlockActiveShader();
    00130     }
    00131
    00132     ShaderManager::instance()->pushActiveShader();
    00133     ShaderManager::instance()->activateShader("skysphere");
    00134
    00135     this->sendTransformMatrices(false);
    00136     glUniformMatrix4fv(ShaderManager::instance()->getUniform("Camera_InverseTranslationMatrix"), 1, GL_FALSE, &Camera::instance()->getInverseTranslationMatrix()[0][0]);
    00137
    00138     const GLint l_in_Position(ShaderManager::instance()->getAttribute("in_Position"));
    00139     const GLint l_texcoords(ShaderManager::instance()->getAttribute("in_Texcoord"));
    00140
    00141     glVertexAttribPointer(l_in_Position, 3, GL_DOUBLE, GL_FALSE, 5 * sizeof(GLdouble), NULL);
    00142     glVertexAttribPointer(l_texcoords, 2, GL_DOUBLE, GL_FALSE, 5 * sizeof(GLdouble), (GLvoid *) (3 * sizeof(GLdouble)));
    00143
    00144     glEnableVertexAttribArray(l_in_Position);
    00145     glEnableVertexAttribArray(l_texcoords);
    00146
    00147     GLuint used_texture_unit = TextureManager::instance()->useTexture("skysphere");         //load texture in texture unit
    00148     glUniform1i(ShaderManager::instance()->getUniform("color_texture"),used_texture_unit);   //tell the shader which texture unit was used
    00149
    00150     glDrawElements(GL_TRIANGLE_STRIP, this->indexArray_size, GL_UNSIGNED_INT, NULL);
    00151
    00152     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    00153     glBindBuffer(GL_ARRAY_BUFFER, 0);
    00154     glBindVertexArray(0);
    00155
    00156     TextureManager::instance()->unuseTexture("skysphere");                                  //tell the texturemanager to release the used texture units
    00157     ShaderManager::instance()->popActiveShader();
    00158
    00159     if (bWeUnlockedActiveShader)        //if we unlocked the active shader, lock it again
    00160         ShaderManager::instance()->lockActiveShader();
    00161
    00162 }

All Classes Functions Variables Enumerations Enumerator