- working VBO code for static objects in game, currently disabled in code but to enabled edit model.h and uncomment #define ENABLE_VBO_CODE

This commit is contained in:
Mark Vejvoda 2011-01-18 23:56:30 +00:00
parent 7c15f58286
commit 6b0bc35e3c
8 changed files with 195 additions and 65 deletions

View File

@ -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) {

View File

@ -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];}

View File

@ -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

View File

@ -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

View File

@ -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;
};

View File

@ -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

View File

@ -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; i<model->getMeshCount(); ++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; i<model->getMeshCount(); ++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; i<model->getMeshCount(); ++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; i<mesh->getIndexCount(); ++i){

View File

@ -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; i<meshCount; ++i){
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;
lastCycleData = cycle;
}
@ -433,6 +482,7 @@ void Model::updateInterpolationVertices(float t, bool cycle) {
for(unsigned int i=0; i<meshCount; ++i){
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;
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; i<meshCount; ++i){
meshes[i].BuildVBOs();
}
}
#endif
//#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
else if(fileHeader.version==3){
@ -573,14 +625,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; i<meshCount; ++i){
meshes[i].BuildVBOs();
}
}
#endif
//#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
else if(fileHeader.version==2) {
@ -591,14 +643,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; i<meshCount; ++i){
meshes[i].BuildVBOs();
}
}
#endif
//#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 {
throw runtime_error("Invalid model version: "+ intToStr(fileHeader.version));