diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 75013285..bb718020 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -2598,7 +2598,7 @@ void Renderer::renderMenuBackground(const MenuBackground *menuBackground){ glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.5f); modelRenderer->begin(true, true, true); - modelRenderer->render(menuBackground->getMainModel()); + modelRenderer->render(menuBackground->getMainModelPtr()); modelRenderer->end(); glDisable(GL_ALPHA_TEST); @@ -2618,7 +2618,7 @@ void Renderer::renderMenuBackground(const MenuBackground *menuBackground){ glLoadIdentity(); glTranslatef(i*2.f-4.f, -1.4f, -7.5f); menuBackground->getCharacterModelPtr(i)->updateInterpolationData(menuBackground->getAnim(), true); - modelRenderer->render(menuBackground->getCharacterModel(i)); + modelRenderer->render(menuBackground->getCharacterModelPtr(i)); glPopMatrix(); } modelRenderer->end(); @@ -3319,7 +3319,7 @@ void Renderer::renderObjectsFast() { visibleIndex < qCache.visibleObjectList.size(); ++visibleIndex) { Object *o = qCache.visibleObjectList[visibleIndex]; - const Model *objModel= o->getModel(); + Model *objModel= o->getModelPtr(); const Vec3f &v= o->getConstPos(); if(modelRenderStarted == false) { diff --git a/source/glest_game/menu/menu_background.h b/source/glest_game/menu/menu_background.h index a9fca143..ccb76e1e 100644 --- a/source/glest_game/menu/menu_background.h +++ b/source/glest_game/menu/menu_background.h @@ -87,6 +87,7 @@ public: const Model *getCharacterModel(int i) const {return characterModels[i];} Model *getCharacterModelPtr(int i) const {return characterModels[i];} const Model *getMainModel() const {return mainModel;} + Model *getMainModelPtr() const {return mainModel;} float getFade() const {return fade;} Vec2f getRaindropPos(int i) const {return raindropPos[i];} float getRaindropState(int i) const {return raindropStates[i];} diff --git a/source/glest_game/types/resource_type.cpp b/source/glest_game/types/resource_type.cpp index 1574bcbf..8467f966 100644 --- a/source/glest_game/types/resource_type.cpp +++ b/source/glest_game/types/resource_type.cpp @@ -77,6 +77,7 @@ void ResourceType::load(const string &dir, Checksum* checksum, Checksum *techtre string path=dir+"/" + modelNode->getAttribute("path")->getRestrictedValue(); model= renderer.newModel(rsGame); + model->setIsStaticModel(true); model->load(path); //default resources diff --git a/source/shared_lib/include/graphics/gl/model_renderer_gl.h b/source/shared_lib/include/graphics/gl/model_renderer_gl.h index e74f98b4..7f3acd80 100644 --- a/source/shared_lib/include/graphics/gl/model_renderer_gl.h +++ b/source/shared_lib/include/graphics/gl/model_renderer_gl.h @@ -34,16 +34,16 @@ public: ModelRendererGl(); virtual void begin(bool renderNormals, bool renderTextures, bool renderColors, MeshCallback *meshCallback); virtual void end(); - virtual void render(const Model *model); - virtual void renderNormalsOnly(const Model *model); + virtual void render(Model *model); + virtual void renderNormalsOnly(Model *model); void setDuplicateTexCoords(bool duplicateTexCoords) {this->duplicateTexCoords= duplicateTexCoords;} void setSecondaryTexCoordUnit(int secondaryTexCoordUnit) {this->secondaryTexCoordUnit= secondaryTexCoordUnit;} private: - void renderMesh(const Mesh *mesh, bool isStaticModel); - void renderMeshNormals(const Mesh *mesh, bool isStaticModel); + void renderMesh(Mesh *mesh, bool isStaticModel); + void renderMeshNormals(Mesh *mesh, bool isStaticModel); }; }}}//end namespace diff --git a/source/shared_lib/include/graphics/model.h b/source/shared_lib/include/graphics/model.h index 49495f90..ea2577eb 100644 --- a/source/shared_lib/include/graphics/model.h +++ b/source/shared_lib/include/graphics/model.h @@ -76,8 +76,11 @@ private: #if defined(ENABLE_VBO_CODE) // Vertex Buffer Object Names - uint32 m_nVBOVertices; // Vertex VBO Name - uint32 m_nVBOTexCoords; // Texture Coordinate VBO Name + bool hasBuiltVBOs; + uint32 m_nVBOVertices; // Vertex VBO Name + uint32 m_nVBOTexCoords; // Texture Coordinate VBO Name + uint32 m_nVBONormals; // Normal VBO Name + uint32 m_nVBOIndexes; // Indexes VBO Name #endif public: @@ -97,9 +100,15 @@ public: uint32 getTriangleCount() const; #if defined(ENABLE_VBO_CODE) - uint32 getVBOVertices() const { return m_nVBOVertices;} + uint32 getVBOVertices() const { return m_nVBOVertices;} uint32 getVBOTexCoords() const { return m_nVBOTexCoords;} + uint32 getVBONormals() const { return m_nVBONormals;} + uint32 getVBOIndexes() const { return m_nVBOIndexes;} + + bool hasBuiltVBOEntities() const { return hasBuiltVBOs;} + void BuildVBOs(); + void ReleaseVBOs(); #endif //data @@ -159,6 +168,7 @@ private: float lastTVertex; bool lastCycleVertex; + string fileName; bool isStaticModel; public: @@ -177,6 +187,7 @@ public: uint8 getFileVersion() const {return fileVersion;} uint32 getMeshCount() const {return meshCount;} const Mesh *getMesh(int i) const {return &meshes[i];} + Mesh *getMeshPtr(int i) const {return &meshes[i];} uint32 getTriangleCount() const; uint32 getVertexCount() const; @@ -193,6 +204,8 @@ public: bool getIsStaticModel() const { return isStaticModel; } void setIsStaticModel(bool value) { isStaticModel = value; } + string getFileName() const { return fileName; } + private: void buildInterpolationData() const; }; diff --git a/source/shared_lib/include/graphics/model_renderer.h b/source/shared_lib/include/graphics/model_renderer.h index 2d24f797..f8287704 100644 --- a/source/shared_lib/include/graphics/model_renderer.h +++ b/source/shared_lib/include/graphics/model_renderer.h @@ -50,8 +50,8 @@ public: virtual void begin(bool renderNormals, bool renderTextures, bool renderColors, MeshCallback *meshCallback= NULL)=0; virtual void end()=0; - virtual void render(const Model *model)=0; - virtual void renderNormalsOnly(const Model *model)=0; + virtual void render(Model *model)=0; + virtual void renderNormalsOnly(Model *model)=0; }; }}//end namespace diff --git a/source/shared_lib/sources/graphics/gl/model_renderer_gl.cpp b/source/shared_lib/sources/graphics/gl/model_renderer_gl.cpp index a2a70257..aff75a3d 100644 --- a/source/shared_lib/sources/graphics/gl/model_renderer_gl.cpp +++ b/source/shared_lib/sources/graphics/gl/model_renderer_gl.cpp @@ -97,29 +97,34 @@ void ModelRendererGl::end() { assertGl(); } -void ModelRendererGl::render(const Model *model) { +void ModelRendererGl::render(Model *model) { //assertions assert(rendering); assertGl(); - //render every mesh - for(uint32 i=0; igetMeshCount(); ++i) { - renderMesh(model->getMesh(i),model->getIsStaticModel()); - } + //if(model->getIsStaticModel()) printf("In [%s::%s Line: %d] filename [%s] is static about to render...\n",__FILE__,__FUNCTION__,__LINE__,model->getFileName().c_str()); + //render every mesh + //if(model->getIsStaticModel() == true) { + for(uint32 i=0; igetMeshCount(); ++i) { + renderMesh(model->getMeshPtr(i),model->getIsStaticModel()); + } + //} //assertions assertGl(); } -void ModelRendererGl::renderNormalsOnly(const Model *model) { +void ModelRendererGl::renderNormalsOnly(Model *model) { //assertions assert(rendering); assertGl(); //render every mesh + //if(model->getIsStaticModel() == true) { for(uint32 i=0; igetMeshCount(); ++i) { - renderMeshNormals(model->getMesh(i),model->getIsStaticModel()); + renderMeshNormals(model->getMeshPtr(i),model->getIsStaticModel()); } + //} //assertions assertGl(); @@ -127,7 +132,7 @@ void ModelRendererGl::renderNormalsOnly(const Model *model) { // ===================== PRIVATE ======================= -void ModelRendererGl::renderMesh(const Mesh *mesh, bool isStaticModel) { +void ModelRendererGl::renderMesh(Mesh *mesh, bool isStaticModel) { //assertions assertGl(); @@ -180,14 +185,21 @@ void ModelRendererGl::renderMesh(const Mesh *mesh, bool isStaticModel) { #if defined(ENABLE_VBO_CODE) if(isStaticModel == true) { + if(mesh->hasBuiltVBOEntities() == false) { + mesh->BuildVBOs(); + } + //vertices glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBOVertices() ); glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer + glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); //normals if(renderNormals) { + glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBONormals() ); glEnableClientState(GL_NORMAL_ARRAY); - glNormalPointer(GL_FLOAT, 0, mesh->getInterpolationData()->getNormals()); + glNormalPointer(GL_FLOAT, 0, (char *) NULL); + glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); } else{ glDisableClientState(GL_NORMAL_ARRAY); @@ -195,17 +207,55 @@ void ModelRendererGl::renderMesh(const Mesh *mesh, bool isStaticModel) { //tex coords if(renderTextures && mesh->getTexture(mtDiffuse) != NULL ) { - glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBOTexCoords() ); if(duplicateTexCoords) { glActiveTexture(GL_TEXTURE0 + secondaryTexCoordUnit); - //glEnableClientState(GL_TEXTURE_COORD_ARRAY); - //glTexCoordPointer(2, GL_FLOAT, 0, mesh->getTexCoords()); + + glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBOTexCoords() ); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer + glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); + } + + glActiveTexture(GL_TEXTURE0); + + glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBOTexCoords() ); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer + glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); + } + else { + if(duplicateTexCoords) { + glActiveTexture(GL_TEXTURE0 + secondaryTexCoordUnit); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + } + glActiveTexture(GL_TEXTURE0); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + } + +/* + //normals + if(renderNormals) { + glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBONormals() ); + glEnableClientState(GL_NORMAL_ARRAY); + glNormalPointer(GL_FLOAT, 0, (char *) NULL); + } + else{ + glDisableClientState(GL_NORMAL_ARRAY); + } + + //tex coords + if(renderTextures && mesh->getTexture(mtDiffuse) != NULL ) { + if(duplicateTexCoords) { + glActiveTexture(GL_TEXTURE0 + secondaryTexCoordUnit); + + glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBOTexCoords() ); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer } glActiveTexture(GL_TEXTURE0); - //glEnableClientState(GL_TEXTURE_COORD_ARRAY); - //glTexCoordPointer(2, GL_FLOAT, 0, mesh->getTexCoords()); + glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBOTexCoords() ); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer } else { @@ -216,6 +266,7 @@ void ModelRendererGl::renderMesh(const Mesh *mesh, bool isStaticModel) { glActiveTexture(GL_TEXTURE0); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } +*/ } else #endif @@ -254,14 +305,26 @@ void ModelRendererGl::renderMesh(const Mesh *mesh, bool isStaticModel) { } } +#if defined(ENABLE_VBO_CODE) + if(isStaticModel == true) { + glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mesh->getVBOIndexes() ); + glDrawRangeElements(GL_TRIANGLES, 0, vertexCount-1, indexCount, GL_UNSIGNED_INT, (char *)NULL); + glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 ); + + //glDrawRangeElements(GL_TRIANGLES, 0, vertexCount-1, indexCount, GL_UNSIGNED_INT, mesh->getIndices()); + } + else +#endif + { //draw model - glDrawRangeElements(GL_TRIANGLES, 0, vertexCount-1, indexCount, GL_UNSIGNED_INT, mesh->getIndices()); + glDrawRangeElements(GL_TRIANGLES, 0, vertexCount-1, indexCount, GL_UNSIGNED_INT, mesh->getIndices()); + } //assertions assertGl(); } -void ModelRendererGl::renderMeshNormals(const Mesh *mesh,bool isStaticModel) { +void ModelRendererGl::renderMeshNormals(Mesh *mesh,bool isStaticModel) { glBegin(GL_LINES); for(unsigned int i= 0; igetIndexCount(); ++i){ diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index e93402ec..fb12fae7 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -62,6 +62,16 @@ Mesh::Mesh() { twoSided= false; customColor= false; + +#if defined(ENABLE_VBO_CODE) + hasBuiltVBOs = false; + // Vertex Buffer Object Names + m_nVBOVertices = 0; + m_nVBOTexCoords = 0; + m_nVBONormals = 0; + m_nVBOIndexes = 0; +#endif + } Mesh::~Mesh() { @@ -76,6 +86,11 @@ void Mesh::init() { } void Mesh::end() { + +#if defined(ENABLE_VBO_CODE) + void ReleaseVBOs(); +#endif + delete [] vertices; delete [] normals; delete [] texCoords; @@ -113,21 +128,54 @@ void Mesh::updateInterpolationVertices(float t, bool cycle) { #if defined(ENABLE_VBO_CODE) void Mesh::BuildVBOs() { - // Generate And Bind The Vertex Buffer - glGenBuffersARB( 1, &m_nVBOVertices ); // Get A Valid Name - glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices ); // Bind The Buffer - // Load The Data - glBufferDataARB( GL_ARRAY_BUFFER_ARB, getVertexCount() * 3 * sizeof(float), vertices, GL_STATIC_DRAW_ARB ); + if(hasBuiltVBOs == false) { + //printf("In [%s::%s Line: %d] setting up a VBO...\n",__FILE__,__FUNCTION__,__LINE__); - // Generate And Bind The Texture Coordinate Buffer - glGenBuffersARB( 1, &m_nVBOTexCoords ); // Get A Valid Name - glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOTexCoords ); // Bind The Buffer - // Load The Data - glBufferDataARB( GL_ARRAY_BUFFER_ARB, getVertexCount() * 2 * sizeof(float), texCoords, GL_STATIC_DRAW_ARB ); + // Generate And Bind The Vertex Buffer + glGenBuffersARB( 1, &m_nVBOVertices ); // Get A Valid Name + glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices ); // Bind The Buffer + // Load The Data + glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f)*frameCount*vertexCount, getInterpolationData()->getVertices(), GL_STATIC_DRAW_ARB ); + glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); - // Our Copy Of The Data Is No Longer Necessary, It Is Safe In The Graphics Card - delete [] vertices; vertices = NULL; - delete [] texCoords; texCoords = NULL; + // Generate And Bind The Texture Coordinate Buffer + glGenBuffersARB( 1, &m_nVBOTexCoords ); // Get A Valid Name + glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOTexCoords ); // Bind The Buffer + // Load The Data + glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec2f)*vertexCount, texCoords, GL_STATIC_DRAW_ARB ); + glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); + + // Generate And Bind The Normal Buffer + glGenBuffersARB( 1, &m_nVBONormals ); // Get A Valid Name + glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBONormals ); // Bind The Buffer + // Load The Data + glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f)*frameCount*vertexCount, getInterpolationData()->getNormals(), GL_STATIC_DRAW_ARB ); + glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); + + // Generate And Bind The Index Buffer + glGenBuffersARB( 1, &m_nVBOIndexes ); // Get A Valid Name + glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, m_nVBOIndexes ); // Bind The Buffer + // Load The Data + glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(uint32)*indexCount, indices, GL_STATIC_DRAW_ARB ); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); + + // Our Copy Of The Data Is No Longer Necessary, It Is Safe In The Graphics Card + delete [] vertices; vertices = NULL; + delete [] texCoords; texCoords = NULL; + delete [] normals; normals = NULL; + delete [] indices; indices = NULL; + + hasBuiltVBOs = true; + } +} + +void Mesh::ReleaseVBOs() { + if(hasBuiltVBOs == true) { + glDeleteBuffersARB( 1, &m_nVBOVertices ); // Get A Valid Name + glDeleteBuffersARB( 1, &m_nVBOTexCoords ); // Get A Valid Name + glDeleteBuffersARB( 1, &m_nVBONormals ); // Get A Valid Name + glDeleteBuffersARB( 1, &m_nVBOIndexes ); // Get A Valid Name + } } #endif @@ -423,6 +471,7 @@ void Model::updateInterpolationData(float t, bool cycle) { for(unsigned int i=0; ifileName.c_str(),t,cycle,lastTData,lastCycleData); lastTData = t; lastCycleData = cycle; } @@ -433,6 +482,7 @@ void Model::updateInterpolationVertices(float t, bool cycle) { for(unsigned int i=0; ifileName.c_str(),t,cycle,lastTData,lastCycleData); lastTVertex = t; lastCycleVertex = cycle; } @@ -466,6 +516,8 @@ void Model::load(const string &path, bool deletePixMapAfterLoad) { else{ throw runtime_error("Unknown model format: " + extension); } + + this->fileName = path; } void Model::save(const string &path) { @@ -554,14 +606,14 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) { meshes[i].buildInterpolationData(); } -#if defined(ENABLE_VBO_CODE) - if(isStaticModel == true) { - this->updateInterpolationData(0.f, true); - for(uint32 i=0; iupdateInterpolationData(0.f, true); +// for(uint32 i=0; iupdateInterpolationData(0.f, true); - for(uint32 i=0; iupdateInterpolationData(0.f, true); +// for(uint32 i=0; iupdateInterpolationData(0.f, true); - for(uint32 i=0; iupdateInterpolationData(0.f, true); +// for(uint32 i=0; i