- 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

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