- attempt to add "some" VBO rendering for static models (need to test if this improves performance in different environments)

This commit is contained in:
Mark Vejvoda 2011-02-04 01:24:26 +00:00
parent 2dc0d97f55
commit dab57fcb34
9 changed files with 177 additions and 201 deletions

View File

@ -98,6 +98,7 @@ const char *GAME_ARGS[] = {
"--log-path", "--log-path",
"--show-ini-settings", "--show-ini-settings",
"--disable-backtrace", "--disable-backtrace",
"--disable-vbo",
"--verbose" "--verbose"
}; };
@ -120,6 +121,7 @@ enum GAME_ARG_TYPE {
GAME_ARG_LOG_PATH, GAME_ARG_LOG_PATH,
GAME_ARG_SHOW_INI_SETTINGS, GAME_ARG_SHOW_INI_SETTINGS,
GAME_ARG_DISABLE_BACKTRACE, GAME_ARG_DISABLE_BACKTRACE,
GAME_ARG_DISABLE_VBO,
GAME_ARG_VERBOSE_MODE GAME_ARG_VERBOSE_MODE
}; };
@ -1576,6 +1578,12 @@ int glestMain(int argc, char** argv) {
return -1; return -1;
} }
// Explicitly disable VBO's
if(config.getBool("DisableVBO","false") == true || hasCommandArgument(argc, argv,GAME_ARGS[GAME_ARG_DISABLE_VBO]) == true) {
setVBOSupported(false);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("**WARNING** Disabling VBO's\n");
}
// Setup the file crc thread // Setup the file crc thread
std::auto_ptr<FileCRCPreCacheThread> preCacheThread; std::auto_ptr<FileCRCPreCacheThread> preCacheThread;

View File

@ -29,7 +29,6 @@ void ObjectType::init(int modelCount, int objectClass, bool walkable, int height
void ObjectType::loadModel(const string &path){ void ObjectType::loadModel(const string &path){
Model *model= Renderer::getInstance().newModel(rsGame); Model *model= Renderer::getInstance().newModel(rsGame);
model->setIsStaticModel(true);
model->load(path); model->load(path);
color= Vec3f(0.f); color= Vec3f(0.f);
if(model->getMeshCount()>0 && model->getMesh(0)->getTexture(0) != NULL) { if(model->getMeshCount()>0 && model->getMesh(0)->getTexture(0) != NULL) {

View File

@ -77,7 +77,6 @@ void ResourceType::load(const string &dir, Checksum* checksum, Checksum *techtre
string path=dir+"/" + modelNode->getAttribute("path")->getRestrictedValue(); string path=dir+"/" + modelNode->getAttribute("path")->getRestrictedValue();
model= renderer.newModel(rsGame); model= renderer.newModel(rsGame);
model->setIsStaticModel(true);
model->load(path); model->load(path);
//default resources //default resources

View File

@ -17,13 +17,13 @@
#include "opengl.h" #include "opengl.h"
#include "leak_dumper.h" #include "leak_dumper.h"
namespace Shared{ namespace Graphics{ namespace Gl{ namespace Shared { namespace Graphics { namespace Gl {
// ===================================================== // =====================================================
// class ModelRendererGl // class ModelRendererGl
// ===================================================== // =====================================================
class ModelRendererGl: public ModelRenderer{ class ModelRendererGl: public ModelRenderer {
private: private:
bool rendering; bool rendering;
bool duplicateTexCoords; bool duplicateTexCoords;
@ -42,8 +42,8 @@ public:
private: private:
void renderMesh(Mesh *mesh, bool isStaticModel); void renderMesh(Mesh *mesh);
void renderMeshNormals(Mesh *mesh, bool isStaticModel); void renderMeshNormals(Mesh *mesh);
}; };
}}}//end namespace }}}//end namespace

View File

@ -30,6 +30,10 @@ using Util::intToStr;
// Globals // Globals
// ===================================================== // =====================================================
bool getVBOSupported();
void setVBOSupported(bool value);
void overrideGlExtensionSupport(const char *extensionName,bool value);
bool isGlExtensionSupported(const char *extensionName); bool isGlExtensionSupported(const char *extensionName);
bool isGlVersionSupported(int major, int minor, int release); bool isGlVersionSupported(int major, int minor, int release);
const char *getGlVersion(); const char *getGlVersion();

View File

@ -25,9 +25,7 @@ using std::string;
using std::map; using std::map;
using std::pair; using std::pair;
//#define ENABLE_VBO_CODE namespace Shared { namespace Graphics {
namespace Shared{ namespace Graphics{
class Model; class Model;
class Mesh; class Mesh;
@ -74,14 +72,12 @@ private:
InterpolationData *interpolationData; InterpolationData *interpolationData;
TextureManager *textureManager; TextureManager *textureManager;
#if defined(ENABLE_VBO_CODE)
// Vertex Buffer Object Names // Vertex Buffer Object Names
bool hasBuiltVBOs; bool hasBuiltVBOs;
uint32 m_nVBOVertices; // Vertex VBO Name uint32 m_nVBOVertices; // Vertex VBO Name
uint32 m_nVBOTexCoords; // Texture Coordinate VBO Name uint32 m_nVBOTexCoords; // Texture Coordinate VBO Name
uint32 m_nVBONormals; // Normal VBO Name uint32 m_nVBONormals; // Normal VBO Name
uint32 m_nVBOIndexes; // Indexes VBO Name uint32 m_nVBOIndexes; // Indexes VBO Name
#endif
public: public:
//init & end //init & end
@ -99,17 +95,13 @@ public:
uint32 getIndexCount() const {return indexCount;} uint32 getIndexCount() const {return indexCount;}
uint32 getTriangleCount() const; 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 getVBOTexCoords() const { return m_nVBOTexCoords;}
uint32 getVBONormals() const { return m_nVBONormals;} uint32 getVBONormals() const { return m_nVBONormals;}
uint32 getVBOIndexes() const { return m_nVBOIndexes;} uint32 getVBOIndexes() const { return m_nVBOIndexes;}
bool hasBuiltVBOEntities() const { return hasBuiltVBOs;} bool hasBuiltVBOEntities() const { return hasBuiltVBOs;}
void BuildVBOs(); void BuildVBOs();
void ReleaseVBOs(); void ReleaseVBOs();
#endif
//data //data
const Vec3f *getVertices() const {return vertices;} const Vec3f *getVertices() const {return vertices;}
@ -169,7 +161,6 @@ private:
bool lastCycleVertex; bool lastCycleVertex;
string fileName; string fileName;
bool isStaticModel;
public: public:
//constructor & destructor //constructor & destructor
@ -201,9 +192,6 @@ public:
void setTextureManager(TextureManager *textureManager) {this->textureManager= textureManager;} void setTextureManager(TextureManager *textureManager) {this->textureManager= textureManager;}
void deletePixels(); void deletePixels();
bool getIsStaticModel() const { return isStaticModel; }
void setIsStaticModel(bool value) { isStaticModel = value; }
string getFileName() const { return fileName; } string getFileName() const { return fileName; }
private: private:

View File

@ -106,8 +106,8 @@ void ModelRendererGl::render(Model *model) {
//render every mesh //render every mesh
//if(model->getIsStaticModel() == true) { //if(model->getIsStaticModel() == true) {
for(uint32 i=0; i<model->getMeshCount(); ++i) { for(uint32 i = 0; i < model->getMeshCount(); ++i) {
renderMesh(model->getMeshPtr(i),model->getIsStaticModel()); renderMesh(model->getMeshPtr(i));
} }
//} //}
//assertions //assertions
@ -122,7 +122,7 @@ void ModelRendererGl::renderNormalsOnly(Model *model) {
//render every mesh //render every mesh
//if(model->getIsStaticModel() == true) { //if(model->getIsStaticModel() == true) {
for(uint32 i=0; i<model->getMeshCount(); ++i) { for(uint32 i=0; i<model->getMeshCount(); ++i) {
renderMeshNormals(model->getMeshPtr(i),model->getIsStaticModel()); renderMeshNormals(model->getMeshPtr(i));
} }
//} //}
@ -132,7 +132,7 @@ void ModelRendererGl::renderNormalsOnly(Model *model) {
// ===================== PRIVATE ======================= // ===================== PRIVATE =======================
void ModelRendererGl::renderMesh(Mesh *mesh, bool isStaticModel) { void ModelRendererGl::renderMesh(Mesh *mesh) {
//assertions //assertions
assertGl(); assertGl();
@ -183,11 +183,11 @@ void ModelRendererGl::renderMesh(Mesh *mesh, bool isStaticModel) {
//assertions //assertions
assertGl(); assertGl();
#if defined(ENABLE_VBO_CODE) if(getVBOSupported() == true && mesh->getFrameCount() == 1) {
if(isStaticModel == true) {
if(mesh->hasBuiltVBOEntities() == false) { if(mesh->hasBuiltVBOEntities() == false) {
mesh->BuildVBOs(); mesh->BuildVBOs();
} }
//printf("Rendering Mesh with VBO's\n");
//vertices //vertices
glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBOVertices() ); glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBOVertices() );
@ -231,46 +231,10 @@ void ModelRendererGl::renderMesh(Mesh *mesh, bool isStaticModel) {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY); 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);
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 {
if(duplicateTexCoords) {
glActiveTexture(GL_TEXTURE0 + secondaryTexCoordUnit);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
glActiveTexture(GL_TEXTURE0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
*/
} }
else else {
#endif //printf("Rendering Mesh WITHOUT VBO's\n");
{
//vertices //vertices
glVertexPointer(3, GL_FLOAT, 0, mesh->getInterpolationData()->getVertices()); glVertexPointer(3, GL_FLOAT, 0, mesh->getInterpolationData()->getVertices());
@ -305,18 +269,15 @@ void ModelRendererGl::renderMesh(Mesh *mesh, bool isStaticModel) {
} }
} }
#if defined(ENABLE_VBO_CODE) if(getVBOSupported() == true && mesh->getFrameCount() == 1) {
if(isStaticModel == true) {
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mesh->getVBOIndexes() ); glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mesh->getVBOIndexes() );
glDrawRangeElements(GL_TRIANGLES, 0, vertexCount-1, indexCount, GL_UNSIGNED_INT, (char *)NULL); glDrawRangeElements(GL_TRIANGLES, 0, vertexCount-1, indexCount, GL_UNSIGNED_INT, (char *)NULL);
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 ); glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
//glDrawRangeElements(GL_TRIANGLES, 0, vertexCount-1, indexCount, GL_UNSIGNED_INT, mesh->getIndices()); //glDrawRangeElements(GL_TRIANGLES, 0, vertexCount-1, indexCount, GL_UNSIGNED_INT, mesh->getIndices());
} }
else else {
#endif //draw model
{
//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());
} }
@ -324,17 +285,47 @@ void ModelRendererGl::renderMesh(Mesh *mesh, bool isStaticModel) {
assertGl(); assertGl();
} }
void ModelRendererGl::renderMeshNormals(Mesh *mesh,bool isStaticModel) { void ModelRendererGl::renderMeshNormals(Mesh *mesh) {
if(getVBOSupported() == true && mesh->getFrameCount() == 1) {
if(mesh->hasBuiltVBOEntities() == false) {
mesh->BuildVBOs();
}
glBegin(GL_LINES); //printf("Rendering Mesh Normals with VBO's\n");
for(unsigned int i= 0; i<mesh->getIndexCount(); ++i){
const Vec3f &vertex= mesh->getInterpolationData()->getVertices()[mesh->getIndices()[i]];
const Vec3f &normal= vertex + mesh->getInterpolationData()->getNormals()[mesh->getIndices()[i]];
glVertex3fv(vertex.ptr()); //vertices
glVertex3fv(normal.ptr()); glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBOVertices() );
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
//glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
//normals
glBindBufferARB( GL_ARRAY_BUFFER_ARB, mesh->getVBONormals() );
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, (char *) NULL);
//glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
//misc vars
uint32 vertexCount= mesh->getVertexCount();
uint32 indexCount= mesh->getIndexCount();
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 );
}
else {
//printf("Rendering Mesh Normals WITHOUT VBO's\n");
glBegin(GL_LINES);
for(unsigned int i = 0; i < mesh->getIndexCount(); ++i) {
const Vec3f &vertex= mesh->getInterpolationData()->getVertices()[mesh->getIndices()[i]];
const Vec3f &normal= vertex + mesh->getInterpolationData()->getNormals()[mesh->getIndices()[i]];
glVertex3fv(vertex.ptr());
glVertex3fv(normal.ptr());
}
glEnd();
} }
glEnd();
} }
}}}//end namespace }}}//end namespace

View File

@ -1,7 +1,7 @@
// ============================================================== // ==============================================================
// This file is part of Glest Shared Library (www.glest.org) // This file is part of Glest Shared Library (www.glest.org)
// //
// Copyright (C) 2001-2008 Martiño Figueroa // Copyright (C) 2001-2008 Marti<EFBFBD>o Figueroa
// //
// You can redistribute this code and/or modify it under // You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published // the terms of the GNU General Public License as published
@ -16,35 +16,63 @@
#include "graphics_interface.h" #include "graphics_interface.h"
#include "context_gl.h" #include "context_gl.h"
#include "gl_wrap.h" #include "gl_wrap.h"
#include <map>
#include "util.h"
#include "leak_dumper.h" #include "leak_dumper.h"
using namespace Shared::Platform; using namespace Shared::Platform;
using namespace Shared::Util;
using namespace std; using namespace std;
namespace Shared{ namespace Graphics{ namespace Gl{ namespace Shared { namespace Graphics { namespace Gl {
std::map<string,bool> cacheExtensionCheckList;
static int vboEnabled = 0;
// ===================================================== // =====================================================
// class Globals // class Globals
// ===================================================== // =====================================================
bool isGlExtensionSupported(const char *extensionName){ bool getVBOSupported() {
if(vboEnabled == 0) {
bool value = isGlExtensionSupported("GL_ARB_vertex_buffer_object");
vboEnabled = (value == true ? 1 : -1);
}
return (vboEnabled == 1);
}
void setVBOSupported(bool value) {
vboEnabled = (value == true ? 1 : -1);
};
void overrideGlExtensionSupport(const char *extensionName,bool value) {
cacheExtensionCheckList[extensionName]=value;
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("OpenGL Extension [%s] supported status FORCED TO = %d\n",extensionName,cacheExtensionCheckList[extensionName]);
}
bool isGlExtensionSupported(const char *extensionName) {
if(cacheExtensionCheckList.find(extensionName) != cacheExtensionCheckList.end()) {
return cacheExtensionCheckList[extensionName];
}
const GLubyte *extensionStr= glGetString(GL_EXTENSIONS); const GLubyte *extensionStr= glGetString(GL_EXTENSIONS);
const char *s= reinterpret_cast<const char *>(extensionStr); const char *s= reinterpret_cast<const char *>(extensionStr);
size_t len= strlen(extensionName); size_t len= strlen(extensionName);
cacheExtensionCheckList[extensionName]=false;
if(s != NULL) { if(s != NULL) {
while ((s = strstr (s, extensionName)) != NULL) { while ((s = strstr (s, extensionName)) != NULL) {
s+= len; s+= len;
if((*s == ' ') || (*s == '\0')) { if((*s == ' ') || (*s == '\0')) {
return true; cacheExtensionCheckList[extensionName] = true;
break;
} }
} }
} }
return false; if(SystemFlags::VERBOSE_MODE_ENABLED) printf("OpenGL Extension [%s] supported status = %d\n",extensionName,cacheExtensionCheckList[extensionName]);
return cacheExtensionCheckList[extensionName];
} }
bool isGlVersionSupported(int major, int minor, int release){ bool isGlVersionSupported(int major, int minor, int release) {
const char *strVersion= getGlVersion(); const char *strVersion= getGlVersion();
@ -52,94 +80,94 @@ bool isGlVersionSupported(int major, int minor, int release){
const char *majorTok= strVersion; const char *majorTok= strVersion;
int majorSupported= atoi(majorTok); int majorSupported= atoi(majorTok);
if(majorSupported<major){ if(majorSupported<major) {
return false; return false;
} }
else if(majorSupported>major){ else if(majorSupported>major) {
return true; return true;
} }
//minor //minor
int i=0; int i=0;
while(strVersion[i]!='.'){ while(strVersion[i]!='.') {
++i; ++i;
} }
const char *minorTok= &strVersion[i]+1; const char *minorTok= &strVersion[i]+1;
int minorSupported= atoi(minorTok); int minorSupported= atoi(minorTok);
if(minorSupported<minor){ if(minorSupported<minor) {
return false; return false;
} }
else if(minorSupported>minor){ else if(minorSupported>minor) {
return true; return true;
} }
//release //release
++i; ++i;
while(strVersion[i]!='.'){ while(strVersion[i]!='.') {
++i; ++i;
} }
const char *releaseTok= &strVersion[i]+1; const char *releaseTok= &strVersion[i]+1;
if(atoi(releaseTok)<release){ if(atoi(releaseTok) < release) {
return false; return false;
} }
return true; return true;
} }
const char *getGlVersion(){ const char *getGlVersion() {
return reinterpret_cast<const char *>(glGetString(GL_VERSION)); return reinterpret_cast<const char *>(glGetString(GL_VERSION));
} }
const char *getGlRenderer(){ const char *getGlRenderer() {
return reinterpret_cast<const char *>(glGetString(GL_RENDERER)); return reinterpret_cast<const char *>(glGetString(GL_RENDERER));
} }
const char *getGlVendor(){ const char *getGlVendor() {
return reinterpret_cast<const char *>(glGetString(GL_VENDOR)); return reinterpret_cast<const char *>(glGetString(GL_VENDOR));
} }
const char *getGlExtensions(){ const char *getGlExtensions() {
return reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)); return reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS));
} }
const char *getGlPlatformExtensions(){ const char *getGlPlatformExtensions() {
Context *c= GraphicsInterface::getInstance().getCurrentContext(); Context *c= GraphicsInterface::getInstance().getCurrentContext();
return getPlatformExtensions(static_cast<ContextGl*>(c)->getPlatformContextGl()); return getPlatformExtensions(static_cast<ContextGl*>(c)->getPlatformContextGl());
} }
int getGlMaxLights(){ int getGlMaxLights() {
int i; int i;
glGetIntegerv(GL_MAX_LIGHTS, &i); glGetIntegerv(GL_MAX_LIGHTS, &i);
return i; return i;
} }
int getGlMaxTextureSize(){ int getGlMaxTextureSize() {
int i; int i;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &i); glGetIntegerv(GL_MAX_TEXTURE_SIZE, &i);
return i; return i;
} }
int getGlMaxTextureUnits(){ int getGlMaxTextureUnits() {
int i; int i;
glGetIntegerv(GL_MAX_TEXTURE_UNITS, &i); glGetIntegerv(GL_MAX_TEXTURE_UNITS, &i);
return i; return i;
} }
int getGlModelviewMatrixStackDepth(){ int getGlModelviewMatrixStackDepth() {
int i; int i;
glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &i); glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &i);
return i; return i;
} }
int getGlProjectionMatrixStackDepth(){ int getGlProjectionMatrixStackDepth() {
int i; int i;
glGetIntegerv(GL_MAX_PROJECTION_STACK_DEPTH, &i); glGetIntegerv(GL_MAX_PROJECTION_STACK_DEPTH, &i);
return i; return i;
} }
void checkGlExtension(const char *extensionName){ void checkGlExtension(const char *extensionName) {
if(!isGlExtensionSupported(extensionName)){ if(!isGlExtensionSupported(extensionName)){
throw runtime_error("OpenGL extension not supported: " + string(extensionName)); throw runtime_error("OpenGL extension not supported: " + string(extensionName));
} }

View File

@ -19,17 +19,12 @@
#include "conversion.h" #include "conversion.h"
#include "util.h" #include "util.h"
#include "platform_common.h" #include "platform_common.h"
#if defined(ENABLE_VBO_CODE)
#include "opengl.h" #include "opengl.h"
#endif
#include "leak_dumper.h" #include "leak_dumper.h"
using namespace Shared::Platform; using namespace Shared::Platform;
using namespace Shared::PlatformCommon; using namespace Shared::PlatformCommon;
using namespace Shared::Graphics::Gl;
using namespace std; using namespace std;
@ -65,15 +60,12 @@ Mesh::Mesh() {
twoSided= false; twoSided= false;
customColor= false; customColor= false;
#if defined(ENABLE_VBO_CODE)
hasBuiltVBOs = false; hasBuiltVBOs = false;
// Vertex Buffer Object Names // Vertex Buffer Object Names
m_nVBOVertices = 0; m_nVBOVertices = 0;
m_nVBOTexCoords = 0; m_nVBOTexCoords = 0;
m_nVBONormals = 0; m_nVBONormals = 0;
m_nVBOIndexes = 0; m_nVBOIndexes = 0;
#endif
} }
Mesh::~Mesh() { Mesh::~Mesh() {
@ -88,10 +80,7 @@ void Mesh::init() {
} }
void Mesh::end() { void Mesh::end() {
ReleaseVBOs();
#if defined(ENABLE_VBO_CODE)
void ReleaseVBOs();
#endif
delete [] vertices; delete [] vertices;
delete [] normals; delete [] normals;
@ -127,61 +116,61 @@ void Mesh::updateInterpolationVertices(float t, bool cycle) {
interpolationData->updateVertices(t, cycle); interpolationData->updateVertices(t, cycle);
} }
#if defined(ENABLE_VBO_CODE)
void Mesh::BuildVBOs() { void Mesh::BuildVBOs() {
if(hasBuiltVBOs == false) { if(getVBOSupported() == true) {
//printf("In [%s::%s Line: %d] setting up a VBO...\n",__FILE__,__FUNCTION__,__LINE__); if(hasBuiltVBOs == false) {
//printf("In [%s::%s Line: %d] setting up a VBO...\n",__FILE__,__FUNCTION__,__LINE__);
// Generate And Bind The Vertex Buffer // Generate And Bind The Vertex Buffer
glGenBuffersARB( 1, &m_nVBOVertices ); // Get A Valid Name glGenBuffersARB( 1, &m_nVBOVertices ); // Get A Valid Name
glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices ); // Bind The Buffer glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices ); // Bind The Buffer
// Load The Data // Load The Data
glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f)*frameCount*vertexCount, getInterpolationData()->getVertices(), GL_STATIC_DRAW_ARB ); glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f)*frameCount*vertexCount, getInterpolationData()->getVertices(), GL_STATIC_DRAW_ARB );
glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
// Generate And Bind The Texture Coordinate Buffer // Generate And Bind The Texture Coordinate Buffer
glGenBuffersARB( 1, &m_nVBOTexCoords ); // Get A Valid Name glGenBuffersARB( 1, &m_nVBOTexCoords ); // Get A Valid Name
glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOTexCoords ); // Bind The Buffer glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOTexCoords ); // Bind The Buffer
// Load The Data // Load The Data
glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec2f)*vertexCount, texCoords, GL_STATIC_DRAW_ARB ); glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec2f)*vertexCount, texCoords, GL_STATIC_DRAW_ARB );
glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
// Generate And Bind The Normal Buffer // Generate And Bind The Normal Buffer
glGenBuffersARB( 1, &m_nVBONormals ); // Get A Valid Name glGenBuffersARB( 1, &m_nVBONormals ); // Get A Valid Name
glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBONormals ); // Bind The Buffer glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBONormals ); // Bind The Buffer
// Load The Data // Load The Data
glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f)*frameCount*vertexCount, getInterpolationData()->getNormals(), GL_STATIC_DRAW_ARB ); glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f)*frameCount*vertexCount, getInterpolationData()->getNormals(), GL_STATIC_DRAW_ARB );
glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
// Generate And Bind The Index Buffer // Generate And Bind The Index Buffer
glGenBuffersARB( 1, &m_nVBOIndexes ); // Get A Valid Name glGenBuffersARB( 1, &m_nVBOIndexes ); // Get A Valid Name
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, m_nVBOIndexes ); // Bind The Buffer glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, m_nVBOIndexes ); // Bind The Buffer
// Load The Data // Load The Data
glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(uint32)*indexCount, indices, GL_STATIC_DRAW_ARB ); glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(uint32)*indexCount, indices, GL_STATIC_DRAW_ARB );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
// Our Copy Of The Data Is No Longer Necessary, It Is Safe In The Graphics Card // Our Copy Of The Data Is No Longer Necessary, It Is Safe In The Graphics Card
delete [] vertices; vertices = NULL; delete [] vertices; vertices = NULL;
delete [] texCoords; texCoords = NULL; delete [] texCoords; texCoords = NULL;
delete [] normals; normals = NULL; delete [] normals; normals = NULL;
delete [] indices; indices = NULL; delete [] indices; indices = NULL;
hasBuiltVBOs = true; hasBuiltVBOs = true;
}
} }
} }
void Mesh::ReleaseVBOs() { void Mesh::ReleaseVBOs() {
if(hasBuiltVBOs == true) { if(getVBOSupported() == true) {
glDeleteBuffersARB( 1, &m_nVBOVertices ); // Get A Valid Name if(hasBuiltVBOs == true) {
glDeleteBuffersARB( 1, &m_nVBOTexCoords ); // Get A Valid Name glDeleteBuffersARB( 1, &m_nVBOVertices ); // Get A Valid Name
glDeleteBuffersARB( 1, &m_nVBONormals ); // Get A Valid Name glDeleteBuffersARB( 1, &m_nVBOTexCoords ); // Get A Valid Name
glDeleteBuffersARB( 1, &m_nVBOIndexes ); // Get A Valid Name glDeleteBuffersARB( 1, &m_nVBONormals ); // Get A Valid Name
glDeleteBuffersARB( 1, &m_nVBOIndexes ); // Get A Valid Name
}
} }
} }
#endif
// ==================== load ==================== // ==================== load ====================
void Mesh::loadV2(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad) { void Mesh::loadV2(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad) {
@ -457,18 +446,17 @@ void Mesh::deletePixels() {
// ==================== constructor & destructor ==================== // ==================== constructor & destructor ====================
Model::Model(){ Model::Model() {
meshCount= 0; meshCount = 0;
meshes= NULL; meshes = NULL;
textureManager= NULL; textureManager = NULL;
lastTData = -1; lastTData = -1;
lastCycleData = false; lastCycleData = false;
lastTVertex = -1; lastTVertex = -1;
lastCycleVertex = false; lastCycleVertex = false;
isStaticModel = false;
} }
Model::~Model(){ Model::~Model() {
delete [] meshes; delete [] meshes;
meshes = NULL; meshes = NULL;
} }
@ -483,40 +471,38 @@ void Model::buildInterpolationData() const{
void Model::updateInterpolationData(float t, bool cycle) { void Model::updateInterpolationData(float t, bool cycle) {
if(lastTData != t || lastCycleData != cycle) { if(lastTData != t || lastCycleData != cycle) {
for(unsigned int i=0; i<meshCount; ++i){ for(unsigned int i = 0; i < meshCount; ++i) {
meshes[i].updateInterpolationData(t, cycle); meshes[i].updateInterpolationData(t, cycle);
} }
//if(isStaticModel) printf("In [%s::%s Line: %d] filename [%s] t = [%f] cycle = [%d] lastTData = [%f] lastCycleData [%d]\n",__FILE__,__FUNCTION__,__LINE__,this->fileName.c_str(),t,cycle,lastTData,lastCycleData); lastTData = t;
lastTData = t; lastCycleData = cycle;
lastCycleData = cycle;
} }
} }
void Model::updateInterpolationVertices(float t, bool cycle) { void Model::updateInterpolationVertices(float t, bool cycle) {
if(lastTVertex != t || lastCycleVertex != cycle) { if(lastTVertex != t || lastCycleVertex != cycle) {
for(unsigned int i=0; i<meshCount; ++i){ for(unsigned int i = 0; i < meshCount; ++i) {
meshes[i].updateInterpolationVertices(t, cycle); meshes[i].updateInterpolationVertices(t, cycle);
} }
//if(isStaticModel) printf("In [%s::%s Line: %d] filename [%s] t = [%f] cycle = [%d] lastTData = [%f] lastCycleData [%d]\n",__FILE__,__FUNCTION__,__LINE__,this->fileName.c_str(),t,cycle,lastTData,lastCycleData); lastTVertex = t;
lastTVertex = t;
lastCycleVertex = cycle; lastCycleVertex = cycle;
} }
} }
// ==================== get ==================== // ==================== get ====================
uint32 Model::getTriangleCount() const{ uint32 Model::getTriangleCount() const {
uint32 triangleCount= 0; uint32 triangleCount= 0;
for(uint32 i=0; i<meshCount; ++i){ for(uint32 i = 0; i < meshCount; ++i) {
triangleCount+= meshes[i].getIndexCount()/3; triangleCount += meshes[i].getIndexCount()/3;
} }
return triangleCount; return triangleCount;
} }
uint32 Model::getVertexCount() const { uint32 Model::getVertexCount() const {
uint32 vertexCount= 0; uint32 vertexCount= 0;
for(uint32 i=0; i<meshCount; ++i){ for(uint32 i = 0; i < meshCount; ++i) {
vertexCount+= meshes[i].getVertexCount(); vertexCount += meshes[i].getVertexCount();
} }
return vertexCount; return vertexCount;
} }
@ -620,15 +606,6 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) {
meshes[i].load(dir, f, textureManager,deletePixMapAfterLoad); meshes[i].load(dir, f, textureManager,deletePixMapAfterLoad);
meshes[i].buildInterpolationData(); meshes[i].buildInterpolationData();
} }
//#if defined(ENABLE_VBO_CODE)
// if(isStaticModel == true) {
// this->updateInterpolationData(0.f, true);
// for(uint32 i=0; i<meshCount; ++i){
// meshes[i].BuildVBOs();
// }
// }
//#endif
} }
//version 3 //version 3
else if(fileHeader.version==3){ else if(fileHeader.version==3){
@ -639,15 +616,6 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) {
meshes[i].loadV3(dir, f, textureManager,deletePixMapAfterLoad); meshes[i].loadV3(dir, f, textureManager,deletePixMapAfterLoad);
meshes[i].buildInterpolationData(); meshes[i].buildInterpolationData();
} }
//#if defined(ENABLE_VBO_CODE)
// if(isStaticModel == true) {
// this->updateInterpolationData(0.f, true);
// for(uint32 i=0; i<meshCount; ++i){
// meshes[i].BuildVBOs();
// }
// }
//#endif
} }
//version 2 //version 2
else if(fileHeader.version==2) { else if(fileHeader.version==2) {
@ -657,15 +625,6 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) {
meshes[i].loadV2(dir, f, textureManager,deletePixMapAfterLoad); meshes[i].loadV2(dir, f, textureManager,deletePixMapAfterLoad);
meshes[i].buildInterpolationData(); meshes[i].buildInterpolationData();
} }
//#if defined(ENABLE_VBO_CODE)
// if(isStaticModel == true) {
// this->updateInterpolationData(0.f, true);
// for(uint32 i=0; i<meshCount; ++i){
// meshes[i].BuildVBOs();
// }
// }
//#endif
} }
else { else {
throw runtime_error("Invalid model version: "+ intToStr(fileHeader.version)); throw runtime_error("Invalid model version: "+ intToStr(fileHeader.version));