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

BambooIsland Abgabe 2

- Main Page
- Related Pages
- Classes
- Files

- File List

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

    00001 /*
    00002  * source file for SceneObject_Terrain class
    00003  * written by: christian moellinger <ch.moellinger@gmail.com>
    00004  * 01/2011
    00005  */
    00006
    00007 #include "SceneObject_Terrain.h"
    00008 #include "ShaderManager.h"
    00009 #include "TextureManager.h"
    00010 #include "Logger.h"
    00011
    00012 #include <vector>
    00013
    00014 double terrain_resolution = 256.0;
    00015
    00016 SceneObject_Terrain::SceneObject_Terrain()
    00017 {
    00018     //first create a 512x512 mesh with y=0
    00019
    00020     std::vector<GLdouble> vertices_vector;
    00021     std::vector<GLint> indices_vector;
    00022
    00023     for (int x=0; x < 2; x++)
    00024         for (int z=0; z < terrain_resolution; z++)
    00025         {
    00026             vertices_vector.push_back(x / terrain_resolution);
    00027             vertices_vector.push_back(0.0);
    00028             vertices_vector.push_back(z / terrain_resolution);
    00029         }
    00030
    00031     //now write this vector in an c-array
    00032     GLdouble *vertices3f = new GLdouble[vertices_vector.size()];
    00033
    00034     for (int a=0; a < vertices_vector.size(); a++)
    00035         vertices3f[a] = vertices_vector.at(a);
    00036
    00037
    00038     /* INDIZES */
    00039     for (int x=0; x < 1; x++)
    00040         for (int z=0; z < terrain_resolution; z++)
    00041         {
    00042             indices_vector.push_back((x+1) * terrain_resolution  + z);
    00043             indices_vector.push_back((x) * terrain_resolution  + z);
    00044         }
    00045
    00046     //now write this vector in an c-array
    00047     GLuint *indizes = new GLuint[indices_vector.size()];
    00048
    00049     for (int a=0; a < indices_vector.size(); a++)
    00050         indizes[a] = indices_vector.at(a);
    00051
    00052     m_iIndexArraySize = indices_vector.size();
    00053
    00054     //clear error status
    00055     glGetError();
    00056
    00057     // create VAO
    00058     glGenVertexArrays(1, &m_glnVertexArrayObject);
    00059     glBindVertexArray(m_glnVertexArrayObject);
    00060
    00061     //in vec3 in_Position;
    00062     glGenBuffers(1, &m_glnVertexBuffer);
    00063
    00064     glBindBuffer(GL_ARRAY_BUFFER, m_glnVertexBuffer);
    00065     glBufferData(GL_ARRAY_BUFFER, vertices_vector.size() * sizeof(GLdouble), vertices3f, GL_STATIC_DRAW);
    00066
    00067     glGenBuffers(1,&m_glnIndexBuffer);
    00068
    00069     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_glnIndexBuffer);
    00070     glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_iIndexArraySize * sizeof(GLuint), indizes, GL_STATIC_DRAW);
    00071
    00072     //delete used memory
    00073
    00074     delete[] vertices3f;
    00075     delete[] indizes;
    00076
    00077     //check for error
    00078     GLenum error = glGetError();
    00079
    00080     if (error != GL_NO_ERROR)
    00081         Logger::instance()->log(Logger::ERROR, translateGLerror(error));
    00082
    00083     Logger::instance()->log(Logger::DEBUG, "SceneObject_Terrain created");
    00084 }
    00085
    00086 SceneObject_Terrain::~SceneObject_Terrain()
    00087 {
    00088     //Delete the used buffers
    00089     glDeleteBuffers(1, &m_glnIndexBuffer);
    00090     glDeleteBuffers(1, &m_glnVertexBuffer);
    00091     glDeleteBuffers(1, &m_glnVertexArrayObject);
    00092 }
    00093
    00094 void SceneObject_Terrain::itlRender()
    00095 {
    00096     bool unlock_hack = false;
    00097
    00098     glBindVertexArray(m_glnVertexArrayObject);
    00099     glBindBuffer(GL_ARRAY_BUFFER, m_glnVertexBuffer);
    00100
    00101     ShaderManager::instance()->pushActiveShader();
    00102
    00103     if (m_tRenderState == Normal)
    00104         ShaderManager::instance()->activateShader("heightmap");
    00105     else
    00106     if (m_tRenderState == LightAsCam)
    00107     {
    00108         ShaderManager::instance()->activateShader("heightmap-simple");
    00109     }
    00110     else //m_tRenderstate == Reflected
    00111     {
    00112         /* TODO: this is dirty design. make it better.
    00113            * to work with the water shader and the used reflection algorithm (which actually locks the shader to the reflect-shader),
    00114            * we must unlock it to use our own reflection shader because heightmap-rendering is complete done in shader and can not be done by the simple phong-reflection-shader
    00115            * afterwards, we pop the active shader from the stack and lock it again.
    00116         */
    00117
    00118         unlock_hack = true;
    00119         ShaderManager::instance()->unlockActiveShader();
    00120         ShaderManager::instance()->pushActiveShader();
    00121         ShaderManager::instance()->activateShader("heightmap-reflect");
    00122     }
    00123
    00124     this->sendTransformMatrices(true);
    00125
    00126     const GLint l_in_Position(ShaderManager::instance()->getAttribute("in_Position"));
    00127
    00128     glVertexAttribPointer(l_in_Position, 3, GL_DOUBLE, GL_FALSE, 3 * sizeof(GLdouble), NULL);
    00129
    00130     glEnableVertexAttribArray(l_in_Position);
    00131
    00132     GLuint texture_unit_heightmap = TextureManager::instance()->useTexture("heightmap");            //load texture in texture unit
    00133     GLuint texture_unit_detail1 = TextureManager::instance()->useTexture("terrain_detail1");        //load texture in texture unit
    00134     GLuint texture_unit_detail2 = TextureManager::instance()->useTexture("terrain_detail2");        //load texture in texture unit
    00135     GLuint texture_unit_detail3 = TextureManager::instance()->useTexture("fbo_light_color");        //load texture in texture unit
    00136     GLuint texture_unit_detail4 = TextureManager::instance()->useTexture("terrain_detail1_normal");         //load texture in texture unit
    00137     GLuint texture_unit_detail5 = TextureManager::instance()->useTexture("terrain_detail1_bump");           //load texture in texture unit
    00138     GLuint texture_unit_detail6 = TextureManager::instance()->useTexture("terrain_detail2_normal");         //load texture in texture unit
    00139     GLuint texture_unit_detail7 = TextureManager::instance()->useTexture("terrain_detail2_bump");           //load texture in texture unit
    00140     GLuint texture_unit_detail8 = TextureManager::instance()->useTexture("terrain_detail3");        //load texture in texture unit
    00141     GLuint texture_unit_detail9 = TextureManager::instance()->useTexture("terrain_detail3_normal");         //load texture in texture unit
    00142     GLuint texture_unit_detail10 = TextureManager::instance()->useTexture("terrain_detail3_bump");          //load texture in texture unit
    00143
    00144     //tell the shader which texture unit were used
    00145     glUniform1i(ShaderManager::instance()->getUniform("heightmap"), texture_unit_heightmap);
    00146     glUniform1i(ShaderManager::instance()->getUniform("detailTexture1"), texture_unit_detail1);
    00147     glUniform1i(ShaderManager::instance()->getUniform("detailTexture2"), texture_unit_detail2);
    00148     glUniform1i(ShaderManager::instance()->getUniform("shadowmap"), texture_unit_detail3);
    00149     glUniform1i(ShaderManager::instance()->getUniform("detailTexture1_normal"), texture_unit_detail4);
    00150     glUniform1i(ShaderManager::instance()->getUniform("detailTexture1_bump"), texture_unit_detail5);
    00151     glUniform1i(ShaderManager::instance()->getUniform("detailTexture2_normal"), texture_unit_detail6);
    00152     glUniform1i(ShaderManager::instance()->getUniform("detailTexture2_bump"), texture_unit_detail7);
    00153     glUniform1i(ShaderManager::instance()->getUniform("detailTexture3"), texture_unit_detail8);
    00154     glUniform1i(ShaderManager::instance()->getUniform("detailTexture3_normal"), texture_unit_detail9);
    00155     glUniform1i(ShaderManager::instance()->getUniform("detailTexture3_bump"), texture_unit_detail10);
    00156
    00157     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_glnIndexBuffer);
    00158
    00159
    00160     glDrawElementsInstanced(GL_TRIANGLE_STRIP, terrain_resolution * 2, GL_UNSIGNED_INT, NULL, terrain_resolution);
    00161
    00162     TextureManager::instance()->unuseTexture("heightmap");                                  //tell the texturemanager to release the used texture unit
    00163     TextureManager::instance()->unuseTexture("terrain_detail1");                                    //tell the texturemanager to release the used texture unit
    00164     TextureManager::instance()->unuseTexture("terrain_detail2");                                    //tell the texturemanager to release the used texture unit
    00165     TextureManager::instance()->unuseTexture("fbo_light_color");            //load texture in texture unit
    00166     TextureManager::instance()->unuseTexture("terrain_detail1_normal");     //load texture in texture unit
    00167     TextureManager::instance()->unuseTexture("terrain_detail1_bump");       //load texture in texture unit
    00168     TextureManager::instance()->unuseTexture("terrain_detail2_normal");     //load texture in texture unit
    00169     TextureManager::instance()->unuseTexture("terrain_detail2_bump");       //load texture in texture unit
    00170     TextureManager::instance()->unuseTexture("terrain_detail3");                                    //tell the texturemanager to release the used texture unit
    00171     TextureManager::instance()->unuseTexture("terrain_detail3_normal");     //load texture in texture unit
    00172     TextureManager::instance()->unuseTexture("terrain_detail3_bump");       //load texture in texture unit
    00173
    00174     if (unlock_hack==true)
    00175     {
    00176         //pop our shader from the stack and lock the previous active one again
    00177         ShaderManager::instance()->popActiveShader();
    00178         ShaderManager::instance()->lockActiveShader();
    00179     }
    00180
    00181     ShaderManager::instance()->popActiveShader();
    00182 }

All Classes Functions Variables Enumerations Enumerator