From d1b3937a888a8fc15835ff4e998703e2af965987 Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Mon, 14 Dec 2015 10:47:34 +0100 Subject: [PATCH] Edges management - The shader of the edges of the base item discards the edges when it should - The facets of the cutplane have edges - The grid is displayed in all the rendering modes --- .../internal/Mesh_3/Graph_manipulations.h | 2 +- Mesh_3/include/CGAL/polylines_to_protect.h | 6 +- .../Mesh_3_plugin/Mesh_3_plugin_cgal_code.cpp | 4 +- Polyhedron/demo/Polyhedron/Polyhedron_3.qrc | 3 + .../demo/Polyhedron/Scene_c3t3_item.cpp | 112 ++++++++++++------ Polyhedron/demo/Polyhedron/Scene_c3t3_item.h | 3 + Polyhedron/demo/Polyhedron/Viewer.cpp | 28 ++++- .../demo/Polyhedron/resources/shader_c3t3.f | 41 +++++++ .../Polyhedron/resources/shader_c3t3_edges.f | 9 ++ .../Polyhedron/resources/shader_c3t3_edges.v | 19 +++ .../Polyhedron/resources/shader_with_light.f | 4 - Three/include/CGAL/Three/Scene_item.h | 1 + Three/include/CGAL/Three/Viewer_interface.h | 1 + 13 files changed, 189 insertions(+), 44 deletions(-) create mode 100644 Polyhedron/demo/Polyhedron/resources/shader_c3t3.f create mode 100644 Polyhedron/demo/Polyhedron/resources/shader_c3t3_edges.f create mode 100644 Polyhedron/demo/Polyhedron/resources/shader_c3t3_edges.v diff --git a/Mesh_3/include/CGAL/internal/Mesh_3/Graph_manipulations.h b/Mesh_3/include/CGAL/internal/Mesh_3/Graph_manipulations.h index 4e1b31788d2..dc07c34b541 100644 --- a/Mesh_3/include/CGAL/internal/Mesh_3/Graph_manipulations.h +++ b/Mesh_3/include/CGAL/internal/Mesh_3/Graph_manipulations.h @@ -95,4 +95,4 @@ struct Graph_manipulations } // namespace internal } // namespace CGAL -#endif CGAL_INTERNAL_MESH_3_INTERNAL_GRAPH_MANIPULATIONS +#endif //CGAL_INTERNAL_MESH_3_INTERNAL_GRAPH_MANIPULATIONS diff --git a/Mesh_3/include/CGAL/polylines_to_protect.h b/Mesh_3/include/CGAL/polylines_to_protect.h index 920e8d3eebe..7cb92cfd80c 100644 --- a/Mesh_3/include/CGAL/polylines_to_protect.h +++ b/Mesh_3/include/CGAL/polylines_to_protect.h @@ -34,14 +34,14 @@ namespace CGAL { template void polylines_to_protect(const CGAL::Image_3& cgal_image, - const double vx, const double vy, const double vz, + const double, const double, const double, std::vector >& polylines) { typedef typename Kernel_traits

::Kernel K; typedef P Point_3; typedef boost::adjacency_list Graph; - typedef boost::graph_traits::vertex_descriptor vertex_descriptor; - typedef boost::graph_traits::edge_iterator edge_iterator; + typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; + // typedef typename boost::graph_traits::edge_iterator edge_iterator; const int xdim = static_cast(cgal_image.xdim()); const int ydim = static_cast(cgal_image.ydim()); diff --git a/Polyhedron/demo/Polyhedron/Plugins/Mesh_3_plugin/Mesh_3_plugin_cgal_code.cpp b/Polyhedron/demo/Polyhedron/Plugins/Mesh_3_plugin/Mesh_3_plugin_cgal_code.cpp index 9e46f202cc2..8ff2cdf8615 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/Mesh_3_plugin/Mesh_3_plugin_cgal_code.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/Mesh_3_plugin/Mesh_3_plugin_cgal_code.cpp @@ -178,10 +178,10 @@ Scene_item* cgal_code_mesh_3(const Image* pImage, - typedef Facet_extra_criterion - Extra_criterion; + Extra_criterion;*/ // facet_criteria.add(new Extra_criterion(*p_domain)); diff --git a/Polyhedron/demo/Polyhedron/Polyhedron_3.qrc b/Polyhedron/demo/Polyhedron/Polyhedron_3.qrc index b07939a8172..04e25098217 100644 --- a/Polyhedron/demo/Polyhedron/Polyhedron_3.qrc +++ b/Polyhedron/demo/Polyhedron/Polyhedron_3.qrc @@ -34,5 +34,8 @@ resources/shader_with_texture.v resources/shader_instanced.v resources/shader_no_light_no_selection.f + resources/shader_c3t3_edges.f + resources/shader_c3t3.f + resources/shader_c3t3_edges.v diff --git a/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp b/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp index b2921353fd0..9f9436a680e 100644 --- a/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp @@ -477,6 +477,20 @@ void Scene_c3t3_item::draw(CGAL::Three::Viewer_interface* viewer) const { ncthis->compute_elements(); ncthis->initialize_buffers(viewer); } + + vaos[Grid]->bind(); + program = getShaderProgram(PROGRAM_WITHOUT_LIGHT); + attrib_buffers(viewer, PROGRAM_WITHOUT_LIGHT); + program->bind(); + program->setAttributeValue("colors", QColor(Qt::black)); + QMatrix4x4 f_mat; + for (int i = 0; i<16; i++) + f_mat.data()[i] = frame->matrix()[i]; + program->setUniformValue("f_matrix", f_mat); + viewer->glDrawArrays(GL_LINES, 0, static_cast(positions_grid.size() / 3)); + program->release(); + vaos[Grid]->release(); + vaos[Facets]->bind(); program = getShaderProgram(PROGRAM_C3T3); attrib_buffers(viewer, PROGRAM_C3T3); @@ -558,27 +572,41 @@ void Scene_c3t3_item::draw_edges(CGAL::Three::Viewer_interface* viewer) const { ncthis->compute_elements(); ncthis->initialize_buffers(viewer); } - vaos[2]->bind(); - program = getShaderProgram(PROGRAM_WITHOUT_LIGHT); - attrib_buffers(viewer, PROGRAM_WITHOUT_LIGHT); - program->bind(); - program->setAttributeValue("colors", QColor(Qt::black)); - QMatrix4x4 f_mat; - for (int i = 0; i<16; i++) - f_mat.data()[i] = frame->matrix()[i]; - program->setUniformValue("f_matrix", f_mat); - viewer->glDrawArrays(GL_LINES, 0, static_cast(positions_grid.size() / 3)); - program->release(); - vaos[2]->release(); + if(renderingMode() == Wireframe) + { + vaos[Grid]->bind(); + program = getShaderProgram(PROGRAM_WITHOUT_LIGHT); + attrib_buffers(viewer, PROGRAM_WITHOUT_LIGHT); + program->bind(); + program->setAttributeValue("colors", QColor(Qt::black)); + QMatrix4x4 f_mat; + for (int i = 0; i<16; i++) + f_mat.data()[i] = frame->matrix()[i]; + program->setUniformValue("f_matrix", f_mat); + viewer->glDrawArrays(GL_LINES, 0, static_cast(positions_grid.size() / 3)); + program->release(); + vaos[Grid]->release(); + } vaos[Edges]->bind(); - program = getShaderProgram(PROGRAM_WITHOUT_LIGHT); - attrib_buffers(viewer, PROGRAM_WITHOUT_LIGHT); + program = getShaderProgram(PROGRAM_C3T3_EDGES); + attrib_buffers(viewer, PROGRAM_C3T3_EDGES); + program->bind(); + QVector4D cp(this->plane().a(),this->plane().b(),this->plane().c(),this->plane().d()); + program->setUniformValue("cutplane", cp); + program->setAttributeValue("colors", QColor(Qt::black)); + viewer->glDrawArrays(GL_LINES, 0, static_cast(positions_lines_size / 3)); + program->release(); + vaos[Edges]->release(); + + vaos[iEdges]->bind(); + program = getShaderProgram(PROGRAM_NO_SELECTION); + attrib_buffers(viewer, PROGRAM_NO_SELECTION); program->bind(); program->setAttributeValue("colors", QColor(Qt::black)); viewer->glDrawArrays(GL_LINES, 0, static_cast(positions_lines.size() / 3)); program->release(); - vaos[Edges]->release(); + vaos[iEdges]->release(); if(spheres_are_shown) { @@ -633,15 +661,17 @@ void Scene_c3t3_item::draw_points(CGAL::Three::Viewer_interface * viewer) const ncthis-> initialize_buffers(viewer); } vaos[Edges]->bind(); - program = getShaderProgram(PROGRAM_WITHOUT_LIGHT); - attrib_buffers(viewer, PROGRAM_WITHOUT_LIGHT); + program = getShaderProgram(PROGRAM_C3T3_EDGES); + attrib_buffers(viewer, PROGRAM_C3T3_EDGES); program->bind(); + QVector4D cp(this->plane().a(),this->plane().b(),this->plane().c(),this->plane().d()); + program->setUniformValue("cutplane", cp); program->setAttributeValue("colors", this->color()); viewer->glDrawArrays(GL_POINTS, 0, static_cast(positions_lines.size() / 3)); vaos[Edges]->release(); program->release(); - vaos[2]->bind(); + vaos[Grid]->bind(); program = getShaderProgram(PROGRAM_WITHOUT_LIGHT); attrib_buffers(viewer, PROGRAM_WITHOUT_LIGHT); program->bind(); @@ -652,7 +682,7 @@ void Scene_c3t3_item::draw_points(CGAL::Three::Viewer_interface * viewer) const program->setUniformValue("f_matrix", f_mat); viewer->glDrawArrays(GL_LINES, 0, static_cast(positions_grid.size() / 3)); program->release(); - vaos[2]->release(); + vaos[Grid]->release(); } void Scene_c3t3_item::draw_triangle(const Kernel::Point_3& pa, @@ -820,7 +850,23 @@ void Scene_c3t3_item::initialize_intersection_buffers(CGAL::Three::Viewer_interf program->release(); } - are_intersection_buffers_filled = true; + //vao containing the data for the lines + { + program = getShaderProgram(PROGRAM_NO_SELECTION, viewer); + program->bind(); + + vaos[iEdges]->bind(); + buffers[iEdges_vertices].bind(); + buffers[iEdges_vertices].allocate(positions_lines.data(), + static_cast(positions_lines.size()*sizeof(float))); + program->enableAttributeArray("vertex"); + program->setAttributeBuffer("vertex", GL_FLOAT, 0, 3); + buffers[iEdges_vertices].release(); + + vaos[iEdges]->release(); + program->release(); + } + are_intersection_buffers_filled = true; } @@ -858,16 +904,16 @@ void Scene_c3t3_item::initialize_buffers(CGAL::Three::Viewer_interface *viewer) positions_poly_size = positions_poly.size(); positions_poly.clear(); - positions_poly.swap(std::vector()); + positions_poly.swap(positions_poly); normals.clear(); - normals.swap(std::vector()); + normals.swap(normals); f_colors.clear(); - f_colors.swap(std::vector()); + f_colors.swap(f_colors); } //vao containing the data for the lines { - program = getShaderProgram(PROGRAM_WITHOUT_LIGHT, viewer); + program = getShaderProgram(PROGRAM_C3T3_EDGES, viewer); program->bind(); vaos[Edges]->bind(); @@ -881,6 +927,10 @@ void Scene_c3t3_item::initialize_buffers(CGAL::Three::Viewer_interface *viewer) vaos[Edges]->release(); program->release(); + positions_lines_size = positions_lines.size(); + positions_lines.clear(); + positions_lines.swap(positions_lines); + } //vao containing the data for the grid @@ -888,14 +938,14 @@ void Scene_c3t3_item::initialize_buffers(CGAL::Three::Viewer_interface *viewer) program = getShaderProgram(PROGRAM_WITHOUT_LIGHT, viewer); program->bind(); - vaos[2]->bind(); + vaos[Grid]->bind(); buffers[Grid_vertices].bind(); buffers[Grid_vertices].allocate(positions_grid.data(), static_cast(positions_grid.size()*sizeof(float))); program->enableAttributeArray("vertex"); program->setAttributeBuffer("vertex", GL_FLOAT, 0, 3); buffers[Grid_vertices].release(); - vaos[2]->release(); + vaos[Grid]->release(); program->release(); } @@ -1012,6 +1062,8 @@ void Scene_c3t3_item::compute_intersection(const Primitive& facet) draw_triangle_edges(pa, pb, pd); draw_triangle_edges(pa, pc, pd); draw_triangle_edges(pb, pc, pd); + + } void Scene_c3t3_item::compute_intersections() @@ -1019,6 +1071,7 @@ void Scene_c3t3_item::compute_intersections() positions_poly.clear(); normals.clear(); f_colors.clear(); + positions_lines.clear(); const Kernel::Plane_3& plane = this->plane(); tree.all_intersected_primitives(plane, boost::make_function_output_iterator(Compute_intersection(*this))); } @@ -1087,13 +1140,6 @@ void Scene_c3t3_item::compute_elements() //The facets { - const Kernel::Plane_3& plane = this->plane(); - GLdouble clip_plane[4]; - clip_plane[0] = plane.a(); - clip_plane[1] = plane.b(); - clip_plane[2] = plane.c(); - clip_plane[3] = plane.d(); - for (C3t3::Facet_iterator fit = c3t3().facets_begin(), end = c3t3().facets_end(); diff --git a/Polyhedron/demo/Polyhedron/Scene_c3t3_item.h b/Polyhedron/demo/Polyhedron/Scene_c3t3_item.h index f41856ce6f7..f75539862c7 100644 --- a/Polyhedron/demo/Polyhedron/Scene_c3t3_item.h +++ b/Polyhedron/demo/Polyhedron/Scene_c3t3_item.h @@ -183,6 +183,7 @@ private: Sphere_radius, Sphere_center, Wired_spheres_vertices, + iEdges_vertices, iFacet_vertices, iFacet_normals, iFacet_colors, @@ -195,6 +196,7 @@ private: Grid, Spheres, Wired_spheres, + iEdges, iFacets, NumberOfVaos }; @@ -218,6 +220,7 @@ private: PFNGLVERTEXATTRIBDIVISORARBPROC glVertexAttribDivisor; mutable std::size_t positions_poly_size; + mutable std::size_t positions_lines_size; mutable std::vector positions_lines; mutable std::vector positions_grid; mutable std::vector positions_poly; diff --git a/Polyhedron/demo/Polyhedron/Viewer.cpp b/Polyhedron/demo/Polyhedron/Viewer.cpp index 924fef1f6c1..c5802616f85 100644 --- a/Polyhedron/demo/Polyhedron/Viewer.cpp +++ b/Polyhedron/demo/Polyhedron/Viewer.cpp @@ -474,6 +474,9 @@ void Viewer::attrib_buffers(int program_name) const { program->setUniformValue("spec_power", 51.8f); program->setUniformValue("is_two_side", is_both_sides); break; + case PROGRAM_C3T3_EDGES: + program->setUniformValue("mvp_matrix", mvp_mat); + break; case PROGRAM_WITHOUT_LIGHT: program->setUniformValue("mvp_matrix", mvp_mat); program->setUniformValue("mv_matrix", mv_mat); @@ -908,7 +911,7 @@ QOpenGLShaderProgram* Viewer::getShaderProgram(int name) const { std::cerr<<"adding vertex shader FAILED"<addShaderFromSourceFile(QOpenGLShader::Fragment,":/cgal/Polyhedron_3/resources/shader_with_light.f")) + if(!program->addShaderFromSourceFile(QOpenGLShader::Fragment,":/cgal/Polyhedron_3/resources/shader_c3t3.f")) { std::cerr<<"adding fragment shader FAILED"<shader_programs[PROGRAM_C3T3_EDGES]) + { + return d->shader_programs[PROGRAM_C3T3_EDGES]; + } + + else + { + + QOpenGLShaderProgram *program = new QOpenGLShaderProgram(viewer); + if(!program->addShaderFromSourceFile(QOpenGLShader::Vertex,":/cgal/Polyhedron_3/resources/shader_c3t3_edges.v")) + { + std::cerr<<"adding vertex shader FAILED"<addShaderFromSourceFile(QOpenGLShader::Fragment,":/cgal/Polyhedron_3/resources/shader_c3t3_edges.f")) + { + std::cerr<<"adding fragment shader FAILED"<link(); + d->shader_programs[PROGRAM_C3T3_EDGES] = program; + return program; + } + break; case PROGRAM_WITH_LIGHT: if(d->shader_programs[PROGRAM_WITH_LIGHT]) { diff --git a/Polyhedron/demo/Polyhedron/resources/shader_c3t3.f b/Polyhedron/demo/Polyhedron/resources/shader_c3t3.f new file mode 100644 index 00000000000..5e3ca3a257b --- /dev/null +++ b/Polyhedron/demo/Polyhedron/resources/shader_c3t3.f @@ -0,0 +1,41 @@ +#version 120 +varying highp vec4 color; +varying highp vec4 fP; +varying highp vec3 fN; +uniform highp vec4 light_pos; +uniform highp vec4 light_diff; +uniform highp vec4 light_spec; +uniform highp vec4 light_amb; +uniform highp float spec_power ; +uniform int is_two_side; +uniform bool is_selected; +void main(void) { + if(color.x>0) + { + highp vec3 L = light_pos.xyz - fP.xyz; + highp vec3 V = -fP.xyz; + highp vec3 N; + if(fN == highp vec3(0.0,0.0,0.0)) + N = highp vec3(0.0,0.0,0.0); + else + N = normalize(fN); + L = normalize(L); + V = normalize(V); + highp vec3 R = reflect(-L, N); + vec4 diffuse; + if(is_two_side == 1) + diffuse = abs(dot(N,L)) * light_diff * color; + else + diffuse = max(dot(N,L), 0.0) * light_diff * color; + highp vec4 specular = pow(max(dot(R,V), 0.0), spec_power) * light_spec; + vec4 ret_color = vec4((color*light_amb).xyz + diffuse.xyz + specular.xyz,1); + if(is_selected) + gl_FragColor = vec4(ret_color.r+70.0/255.0, ret_color.g+70.0/255.0, ret_color.b+70.0/255.0, 1.0); + else + gl_FragColor = ret_color; + } + else + discard; +} + + diff --git a/Polyhedron/demo/Polyhedron/resources/shader_c3t3_edges.f b/Polyhedron/demo/Polyhedron/resources/shader_c3t3_edges.f new file mode 100644 index 00000000000..8a19a0a5e46 --- /dev/null +++ b/Polyhedron/demo/Polyhedron/resources/shader_c3t3_edges.f @@ -0,0 +1,9 @@ +#version 120 +varying highp vec4 color; +void main(void) +{ + if(color.w>0) + gl_FragColor = vec4(0,0,0,1.0); + else + discard; +} diff --git a/Polyhedron/demo/Polyhedron/resources/shader_c3t3_edges.v b/Polyhedron/demo/Polyhedron/resources/shader_c3t3_edges.v new file mode 100644 index 00000000000..6823cd5f30d --- /dev/null +++ b/Polyhedron/demo/Polyhedron/resources/shader_c3t3_edges.v @@ -0,0 +1,19 @@ +#version 120 +attribute highp vec4 vertex; +attribute highp vec3 colors; +uniform highp mat4 mvp_matrix; +uniform highp vec4 cutplane; +varying highp vec4 color; +void main(void) +{ + if(vertex.x * cutplane.x + vertex.y * cutplane.y + vertex.z * cutplane.z + cutplane.w > 0) + { + color = vec4(0.0, 1.0, 1.0, 0.0); + } + else + { + color = vec4(colors, 1.0); + } + gl_Position = mvp_matrix * vertex; +} + diff --git a/Polyhedron/demo/Polyhedron/resources/shader_with_light.f b/Polyhedron/demo/Polyhedron/resources/shader_with_light.f index 2da0cac3e31..badf68a2b3e 100644 --- a/Polyhedron/demo/Polyhedron/resources/shader_with_light.f +++ b/Polyhedron/demo/Polyhedron/resources/shader_with_light.f @@ -10,7 +10,6 @@ uniform highp float spec_power ; uniform int is_two_side; uniform bool is_selected; void main(void) { - if(color.w > 0.0){ highp vec3 L = light_pos.xyz - fP.xyz; highp vec3 V = -fP.xyz; highp vec3 N; @@ -32,9 +31,6 @@ void main(void) { gl_FragColor = vec4(ret_color.r+70.0/255.0, ret_color.g+70.0/255.0, ret_color.b+70.0/255.0, 1.0); else gl_FragColor = ret_color; -}else{ - discard; -} } diff --git a/Three/include/CGAL/Three/Scene_item.h b/Three/include/CGAL/Three/Scene_item.h index 03a0da692dc..d8159c78594 100644 --- a/Three/include/CGAL/Three/Scene_item.h +++ b/Three/include/CGAL/Three/Scene_item.h @@ -61,6 +61,7 @@ public: PROGRAM_INSTANCED, PROGRAM_INSTANCED_WIRE, PROGRAM_C3T3, + PROGRAM_C3T3_EDGES, NB_OF_PROGRAMS }; typedef CGAL::Three::Scene_interface::Bbox Bbox; diff --git a/Three/include/CGAL/Three/Viewer_interface.h b/Three/include/CGAL/Three/Viewer_interface.h index 12737e74c0d..c105d71fbcc 100644 --- a/Three/include/CGAL/Three/Viewer_interface.h +++ b/Three/include/CGAL/Three/Viewer_interface.h @@ -51,6 +51,7 @@ public: PROGRAM_INSTANCED, PROGRAM_INSTANCED_WIRE, PROGRAM_C3T3, + PROGRAM_C3T3_EDGES, NB_OF_PROGRAMS }; Viewer_interface(QWidget* parent) : QGLViewer(CGAL::Qt::createOpenGLContext(), parent) {}