From de58fbbc68642f9b27837f08b59cb7b8d272d05e Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Fri, 20 Feb 2015 17:41:00 +0100 Subject: [PATCH] Multiple objects rendering fix The buffers was only bound once at the creation of the object, which means that only the last object created had its buffers right. --- .../Polyhedron/Scene_polygon_soup_item.cpp | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/Scene_polygon_soup_item.cpp b/Polyhedron/demo/Polyhedron/Scene_polygon_soup_item.cpp index 2f53b13497a..d2cc08b26f9 100644 --- a/Polyhedron/demo/Polyhedron/Scene_polygon_soup_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_polygon_soup_item.cpp @@ -357,8 +357,8 @@ Scene_polygon_soup_item::Scene_polygon_soup_item() Scene_polygon_soup_item::~Scene_polygon_soup_item() { - //glDeleteBuffers(2, buffer); - //glDeleteVertexArrays(1, &vao); + glDeleteBuffers(2, buffer); + glDeleteVertexArrays(1, &vao); glDeleteProgram(rendering_program); delete soup; @@ -529,6 +529,16 @@ Scene_polygon_soup_item::toolTip() const void Scene_polygon_soup_item::draw() const { + //Calls the buffer info again so that it's the right one used even if there are several objects drawn + + glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); + glBufferData(GL_ARRAY_BUFFER, (positions_poly.size())*sizeof(positions_poly.data()), positions_poly.data(), GL_DYNAMIC_DRAW); + glVertexAttribPointer(0,4,GL_FLOAT,GL_FALSE,0, NULL); + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, buffer[1]); + glBufferData(GL_ARRAY_BUFFER, (normals.size())*sizeof(normals.data()), normals.data(), GL_DYNAMIC_DRAW); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); + glEnableVertexAttribArray(1); // tells the GPU to use the program just created glUseProgram(rendering_program); @@ -537,7 +547,11 @@ Scene_polygon_soup_item::draw() const { // the third argument is the number of vec4 that will be entered glDrawArrays(GL_TRIANGLES, 0, positions_poly.size()/4); //Tells OpenGL not to use the program anymore + glUseProgram(0); + + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); } void @@ -566,6 +580,17 @@ Scene_polygon_soup_item::draw_points() const { if(soup == 0) return; + glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); + + glBufferData(GL_ARRAY_BUFFER, (positions_poly.size())*sizeof(positions_poly.data()), positions_poly.data(), GL_DYNAMIC_DRAW); + glVertexAttribPointer(0,4,GL_FLOAT, GL_FALSE, 0, NULL); + glEnableVertexAttribArray(0); + //Bind the second and initialize it + glBindBuffer(GL_ARRAY_BUFFER, buffer[1]); + glBufferData(GL_ARRAY_BUFFER, (normals.size())*sizeof(normals.data()), normals.data(), GL_DYNAMIC_DRAW); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); + glEnableVertexAttribArray(1); + // tells the GPU to use the program just created glUseProgram(rendering_program);