Commit 6bd3d3f6 authored by Romain Pacanowski's avatar Romain Pacanowski

Splitting geometry draw calls into multiple calls by using multiple indexed buffer

parent 6f5495f6
......@@ -97,7 +97,7 @@ Viewer::Viewer(std::string const &full_path_to_mesh)
}
catch (const std::exception &e)
{
std::cerr << " Problem while loading PLY file. Aborting" << std::endl;
std::cerr << " Problem while loading PLY file. Aborting" << e.what()<< std::endl;
}
//Clean the data that do not go on GPU
......@@ -126,6 +126,11 @@ void Viewer::init(int w, int h){
//_mesh.initVBA();
//Retrieve some OpenGL Constants for Geometry Drawing
// glGetIntegerv( GL_MAX_ELEMENTS_VERTICES, & _max_elem_verts );
// glGetIntegerv( GL_MAX_ELEMENTS_INDICES, & _max_elem_indices);
// std::cout << " max_elem_verts = " << _max_elem_verts << " max_elem_indices = " << _max_elem_indices << std::endl;
initVBA();
reshape(w,h);
......@@ -297,11 +302,46 @@ void Viewer::initVBA()
// std::vector<mrf::math::Vec3f> const & vertices = _mesh->vertices();
glBufferData(GL_ARRAY_BUFFER, sizeof(mrf::math::Vec3f) * _vboCPU.size(), _vboCPU[0].ptr(), GL_STATIC_DRAW);
glGenBuffers(1, &mIndexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId);
// Index Buffer
// glGenBuffers(1, &mIndexBufferId);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId);
// // std::vector<mrf::uint> const & faces = _mesh->faces();
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * _index_faces.size(), _index_faces.data(), GL_STATIC_DRAW);
unsigned int const MAX_TRIANGLES_PER_BATCH = 10000000;
unsigned int const nb_triangles = (_index_faces.size() / 3 ) ;
unsigned int const nb_index_buffer = nb_triangles / MAX_TRIANGLES_PER_BATCH;
unsigned int const nb_triangles_remaining = nb_triangles - nb_index_buffer*MAX_TRIANGLES_PER_BATCH;
// std::vector<mrf::uint> const & faces = _mesh->faces();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * _index_faces.size(), _index_faces.data(), GL_STATIC_DRAW);
std::cout << "MAX_TRIANGLES_PER_BATCH" << MAX_TRIANGLES_PER_BATCH << " nb_triangles = " << nb_triangles << " nb_index_buffer = " << nb_index_buffer << " nb_triangles_remaining " << nb_triangles_remaining << std::endl;
GLuint* index_faces_ptr = (GLuint*) _index_faces.data();
for(unsigned int i=0; i< nb_index_buffer; i++)
{
// Index Buffer
glGenBuffers(1, &mIndexBufferId);
std::cout << " mIndexBufferID = " << mIndexBufferId << std::endl;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId);
_indexBufferDataIDs.push_back( std::make_pair<>(mIndexBufferId, MAX_TRIANGLES_PER_BATCH*3) );
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * MAX_TRIANGLES_PER_BATCH*3, index_faces_ptr, GL_STATIC_DRAW);
index_faces_ptr += MAX_TRIANGLES_PER_BATCH*3;
checkError();
}
if( nb_triangles_remaining > 0 )
{
glGenBuffers(1, &mIndexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId);
_indexBufferDataIDs.push_back( std::make_pair<>(mIndexBufferId, nb_triangles_remaining*3) );
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * nb_triangles_remaining*3, &(_index_faces[nb_index_buffer*MAX_TRIANGLES_PER_BATCH*3]), GL_STATIC_DRAW);
checkError();
}
glGenVertexArrays(1, &mVertexArrayId);
......@@ -322,7 +362,7 @@ Viewer::draw(Shader const & shd)
// Activate the VBO of the current mesh:
glBindVertexArray(mVertexArrayId);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId);
// Specify vertex data
......@@ -377,18 +417,22 @@ Viewer::draw(Shader const & shd)
}
// }
// send the geometry
// glDrawElements(GL_TRIANGLES, 3*mFaces.size(), GL_UNSIGNED_INT, 0);
// if( _index_faces.size() > MAX_FACES_PER_DRAW_CALL )
// {
// for (unsigned int i = 0; i < NB_DRAW_CALL_REQUIRED; i++)
// {
// }
// }
// Send the geometry. Original Code
// glDrawElements(GL_TRIANGLES, 3*mFaces.size(), GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, _index_faces.size(), GL_UNSIGNED_INT, 0);
// One Draw Call
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId);
//glDrawElements(GL_TRIANGLES, _index_faces.size(), GL_UNSIGNED_INT, 0);
// at this point the mesh has been drawn and raserized into the framebuffer!
for( unsigned int i=0; i < _indexBufferDataIDs.size(); i++)
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferDataIDs[i].first );
glDrawElements(GL_TRIANGLES, _indexBufferDataIDs[i].second, GL_UNSIGNED_INT, 0);
}
// at this point the mesh has been drawn and raserized into the framebuffer!
glDisableVertexAttribArray(vertex_loc);
......@@ -396,18 +440,11 @@ Viewer::draw(Shader const & shd)
if (tangent_loc >= 0) glDisableVertexAttribArray(tangent_loc);
// if (_color_per_vertex.size() == _vertices.size())
// {
if (color_loc >= 0)
glDisableVertexAttribArray(color_loc);
//}
if (color_loc >= 0)
glDisableVertexAttribArray(color_loc);
// if (_uvs.size() == _vertices.size())
// {
if (texcoord_loc >= 0)
glDisableVertexAttribArray(texcoord_loc);
//}
if (texcoord_loc >= 0)
glDisableVertexAttribArray(texcoord_loc);
checkError();
checkError();
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment