Tech ENABLE_GL_SHADERS_ATTRIBUTES - Use shader's vertex attributes and matrices in GLMmSegmentationGizmo3DScene

(cherry picked from commit prusa3d/PrusaSlicer@4eee0d05f3)
This commit is contained in:
enricoturri1966 2023-10-27 09:30:17 +08:00 committed by Noisyfox
parent 15bad7fc19
commit 63272a9df8
3 changed files with 32 additions and 55 deletions

View file

@ -918,19 +918,27 @@ void GLMmSegmentationGizmo3DScene::render(size_t triangle_indices_idx) const
assert(this->vertices_VBO_id != 0); assert(this->vertices_VBO_id != 0);
assert(this->triangle_indices_VBO_ids[triangle_indices_idx] != 0); assert(this->triangle_indices_VBO_ids[triangle_indices_idx] != 0);
GLShaderProgram* shader = wxGetApp().get_current_shader();
if (shader == nullptr)
return;
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, this->vertices_VBO_id)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, this->vertices_VBO_id));
glsafe(::glVertexPointer(3, GL_FLOAT, 3 * sizeof(float), (const void*)(0 * sizeof(float)))); const GLint position_id = shader->get_attrib_location("v_position");
if (position_id != -1) {
glsafe(::glEnableClientState(GL_VERTEX_ARRAY)); glsafe(::glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (GLvoid*)0));
glsafe(::glEnableVertexAttribArray(position_id));
// Render using the Vertex Buffer Objects.
if (this->triangle_indices_sizes[triangle_indices_idx] > 0) {
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->triangle_indices_VBO_ids[triangle_indices_idx]));
glsafe(::glDrawElements(GL_TRIANGLES, GLsizei(this->triangle_indices_sizes[triangle_indices_idx]), GL_UNSIGNED_INT, nullptr));
glsafe(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
} }
glsafe(::glDisableClientState(GL_VERTEX_ARRAY)); // Render using the Vertex Buffer Objects.
if (this->triangle_indices_VBO_ids[triangle_indices_idx] != 0 &&
this->triangle_indices_sizes[triangle_indices_idx] > 0) {
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->triangle_indices_VBO_ids[triangle_indices_idx]));
glsafe(::glDrawElements(GL_TRIANGLES, GLsizei(this->triangle_indices_sizes[triangle_indices_idx]), GL_UNSIGNED_INT, nullptr));
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
}
if (position_id != -1)
glsafe(::glDisableVertexAttribArray(position_id));
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
} }

View file

@ -1179,20 +1179,6 @@ void TriangleSelectorPatch::render(ImGuiWrapper* imgui, const Transform3d& matri
if (!shader) if (!shader)
return; return;
assert(shader->get_name() == "gouraud_attr" || shader->get_name() == "mm_gouraud_attr"); assert(shader->get_name() == "gouraud_attr" || shader->get_name() == "mm_gouraud_attr");
GLint position_id = -1;
GLint barycentric_id = -1;
if (wxGetApp().plater()->is_wireframe_enabled()) {
position_id = shader->get_attrib_location("v_position");
barycentric_id = shader->get_attrib_location("v_barycentric");
if (m_need_wireframe && wxGetApp().plater()->is_show_wireframe()) {
//BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", show_wireframe on");
shader->set_uniform("show_wireframe", true);
}
else {
//BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", show_wireframe off");
shader->set_uniform("show_wireframe", false);
}
}
for (size_t buffer_idx = 0; buffer_idx < m_triangle_patches.size(); ++buffer_idx) { for (size_t buffer_idx = 0; buffer_idx < m_triangle_patches.size(); ++buffer_idx) {
if (this->has_VBOs(buffer_idx)) { if (this->has_VBOs(buffer_idx)) {
@ -1210,9 +1196,7 @@ void TriangleSelectorPatch::render(ImGuiWrapper* imgui, const Transform3d& matri
//to make black not too hard too see //to make black not too hard too see
ColorRGBA new_color = adjust_color_for_rendering(color); ColorRGBA new_color = adjust_color_for_rendering(color);
shader->set_uniform("uniform_color", new_color); shader->set_uniform("uniform_color", new_color);
//shader->set_uniform("uniform_color", color); this->render(buffer_idx);
//BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", buffer_idx %1%: new_color[%2%, %3%, %4%, %5%]")%buffer_idx%new_color[0]%new_color[1]%new_color[2]%new_color[3];
this->render(buffer_idx, (int)position_id, (int)barycentric_id);
} }
} }
@ -1443,7 +1427,7 @@ void TriangleSelectorPatch::update_render_data()
//BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", exit"); //BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", exit");
} }
void TriangleSelectorPatch::render(int triangle_indices_idx, int position_id, int barycentric_id) void TriangleSelectorPatch::render(int triangle_indices_idx)
{ {
assert(triangle_indices_idx < this->m_triangle_indices_VBO_ids.size()); assert(triangle_indices_idx < this->m_triangle_indices_VBO_ids.size());
assert(this->m_triangle_patches.size() == this->m_triangle_indices_VBO_ids.size()); assert(this->m_triangle_patches.size() == this->m_triangle_indices_VBO_ids.size());
@ -1452,28 +1436,17 @@ void TriangleSelectorPatch::render(int triangle_indices_idx, int position_id, in
assert(this->m_vertices_VBO_ids[triangle_indices_idx] != 0); assert(this->m_vertices_VBO_ids[triangle_indices_idx] != 0);
assert(this->m_triangle_indices_VBO_ids[triangle_indices_idx] != 0); assert(this->m_triangle_indices_VBO_ids[triangle_indices_idx] != 0);
//glsafe(::glBindBuffer(GL_ARRAY_BUFFER, this->m_vertices_VBO_id)); GLShaderProgram *shader = wxGetApp().get_current_shader();
//glsafe(::glVertexPointer(3, GL_FLOAT, 3 * sizeof(float), (const void*)(0 * sizeof(float)))); if (shader == nullptr)
if (this->m_triangle_indices_sizes[triangle_indices_idx] > 0) { return;
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, this->m_vertices_VBO_ids[triangle_indices_idx]));
if (position_id != -1) {
glsafe(::glEnableVertexAttribArray((GLint)position_id));
glsafe(::glVertexAttribPointer((GLint)position_id, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), nullptr));
}
else {
glsafe(::glVertexPointer(3, GL_FLOAT, 3 * sizeof(float), nullptr));
}
if (barycentric_id != -1) { glsafe(::glBindBuffer(GL_ARRAY_BUFFER, this->m_vertices_VBO_ids[triangle_indices_idx]));
glsafe(::glEnableVertexAttribArray((GLint)barycentric_id)); const GLint position_id = shader->get_attrib_location("v_position");
glsafe(::glVertexAttribPointer((GLint)barycentric_id, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (GLvoid*)(intptr_t)(3 * sizeof(float)))); if (position_id != -1) {
} glsafe(::glVertexAttribPointer((GLint) position_id, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr));
//glsafe(::glVertexPointer(3, GL_FLOAT, 3 * sizeof(float), nullptr)); glsafe(::glEnableVertexAttribArray((GLint)position_id));
//BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", Line %1%: triangle_indices_idx %2%, bind vertex vbo, buffer id %3%")%__LINE__%triangle_indices_idx%this->m_vertices_VBO_ids[triangle_indices_idx];
} }
glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
// Render using the Vertex Buffer Objects. // Render using the Vertex Buffer Objects.
if (this->m_triangle_indices_sizes[triangle_indices_idx] > 0) { if (this->m_triangle_indices_sizes[triangle_indices_idx] > 0) {
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->m_triangle_indices_VBO_ids[triangle_indices_idx])); glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->m_triangle_indices_VBO_ids[triangle_indices_idx]));
@ -1482,14 +1455,10 @@ void TriangleSelectorPatch::render(int triangle_indices_idx, int position_id, in
//BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", Line %1%: triangle_indices_idx %2%, bind indices vbo, buffer id %3%")%__LINE__%triangle_indices_idx%this->m_triangle_indices_VBO_ids[triangle_indices_idx]; //BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", Line %1%: triangle_indices_idx %2%, bind indices vbo, buffer id %3%")%__LINE__%triangle_indices_idx%this->m_triangle_indices_VBO_ids[triangle_indices_idx];
} }
glsafe(::glDisableClientState(GL_VERTEX_ARRAY)); if (position_id != -1)
if ((this->m_triangle_indices_sizes[triangle_indices_idx] > 0)&&(position_id != -1))
glsafe(::glDisableVertexAttribArray(position_id)); glsafe(::glDisableVertexAttribArray(position_id));
if ((this->m_triangle_indices_sizes[triangle_indices_idx] > 0)&&(barycentric_id != -1))
glsafe(::glDisableVertexAttribArray((GLint)barycentric_id));
if (this->m_triangle_indices_sizes[triangle_indices_idx] > 0) glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0));
} }
void TriangleSelectorPatch::release_geometry() void TriangleSelectorPatch::release_geometry()

View file

@ -35,7 +35,7 @@ public:
virtual ~TriangleSelectorGUI() = default; virtual ~TriangleSelectorGUI() = default;
virtual void render(ImGuiWrapper* imgui, const Transform3d& matrix); virtual void render(ImGuiWrapper* imgui, const Transform3d& matrix);
void render(const Transform3d& matrix) { this->render(nullptr, matrix); } //void render(const Transform3d& matrix) { this->render(nullptr, matrix); }
void set_wireframe_needed(bool need_wireframe) { m_need_wireframe = need_wireframe; } void set_wireframe_needed(bool need_wireframe) { m_need_wireframe = need_wireframe; }
bool get_wireframe_needed() { return m_need_wireframe; } bool get_wireframe_needed() { return m_need_wireframe; }
@ -170,7 +170,7 @@ protected:
private: private:
void update_render_data(); void update_render_data();
void render(int buffer_idx, int position_id = -1, int barycentric_id = -1); void render(int buffer_idx);
}; };