From ebfa4a7ea547bcd3b5015ee0eb06a5eb49254307 Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Thu, 2 Feb 2017 14:21:14 +0100 Subject: [PATCH] Add a shrinking possibility to the facets of the c3t3_item. To apply it, either use the slider in the item's menu, or use + and - when the item is selected. --- Polyhedron/demo/Polyhedron/Polyhedron_3.qrc | 1 + .../demo/Polyhedron/Scene_c3t3_item.cpp | 87 ++++++++++++++++--- Polyhedron/demo/Polyhedron/Scene_c3t3_item.h | 2 + Polyhedron/demo/Polyhedron/Viewer.cpp | 23 +++++ .../demo/Polyhedron/resources/shader_c3t3.v | 8 +- .../Polyhedron/resources/shader_c3t3_tets.v | 30 +++++++ Three/include/CGAL/Three/Scene_item.h | 1 + Three/include/CGAL/Three/Viewer_interface.h | 1 + 8 files changed, 135 insertions(+), 18 deletions(-) create mode 100644 Polyhedron/demo/Polyhedron/resources/shader_c3t3_tets.v diff --git a/Polyhedron/demo/Polyhedron/Polyhedron_3.qrc b/Polyhedron/demo/Polyhedron/Polyhedron_3.qrc index 85ba633591e..ecbd89be571 100644 --- a/Polyhedron/demo/Polyhedron/Polyhedron_3.qrc +++ b/Polyhedron/demo/Polyhedron/Polyhedron_3.qrc @@ -47,6 +47,7 @@ resources/euler_facet.png resources/euler_vertex.png javascript/lib.js + resources/shader_c3t3_tets.v resources/rotate_around_cursor.png diff --git a/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp b/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp index 0c8736c7fc9..fcfc15db00d 100644 --- a/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include #include #include @@ -71,7 +74,7 @@ public : { //vao containing the data for the facets { - program = getShaderProgram(PROGRAM_WITH_LIGHT, viewer); + program = getShaderProgram(PROGRAM_C3T3_TETS, viewer); program->bind(); vaos[Facets]->bind(); @@ -96,6 +99,13 @@ public : program->setAttributeBuffer("colors", GL_FLOAT, 0, 3); buffers[Colors].release(); + buffers[Barycenters].bind(); + buffers[Barycenters].allocate(barycenters->data(), + static_cast(barycenters->size()*sizeof(float))); + program->enableAttributeArray("barycenter"); + program->setAttributeBuffer("barycenter", GL_FLOAT, 0, 3); + buffers[Barycenters].release(); + vaos[Facets]->release(); program->release(); @@ -121,10 +131,11 @@ public : void draw(CGAL::Three::Viewer_interface* viewer) const { vaos[Facets]->bind(); - program = getShaderProgram(PROGRAM_WITH_LIGHT); - attribBuffers(viewer, PROGRAM_WITH_LIGHT); + program = getShaderProgram(PROGRAM_C3T3_TETS); + attribBuffers(viewer, PROGRAM_C3T3_TETS); program->bind(); - + float shrink_factor = qobject_cast(this->parent())->getShrinkFactor(); + program->setUniformValue("shrink_factor", shrink_factor); // positions_poly is also used for the faces in the cut plane // and changes when the cut plane is moved viewer->glDrawArrays(GL_TRIANGLES, 0, static_cast(vertices->size() / 3)); @@ -192,15 +203,15 @@ public : edges->push_back(pa.z()+offset.z); - Kernel::Point_3 bary = (pa+pb+pc)/3.0; for(int i=0; i<3; i++) { colors->push_back((float)color.red()/255); colors->push_back((float)color.green()/255); colors->push_back((float)color.blue()/255); - barycenters->push_back(bary.x()); - barycenters->push_back(bary.y()); - barycenters->push_back(bary.z()); + + barycenters->push_back((pa[0]+pb[0]+pc[0])/3.0); + barycenters->push_back((pa[1]+pb[1]+pc[1])/3.0); + barycenters->push_back((pa[2]+pb[2]+pc[2])/3.0); } } @@ -214,6 +225,7 @@ private: Normals, Colors, Edges, + Barycenters, NumberOfBuffers }; enum Vao @@ -244,6 +256,10 @@ struct Scene_c3t3_item_priv { , is_valid(true) { init_default_values(); + tet_Slider = new QSlider(Qt::Horizontal); + tet_Slider->setValue(100); + tet_Slider->setMinimum(0); + tet_Slider->setMaximum(100); } Scene_c3t3_item_priv(const C3t3& c3t3_, Scene_c3t3_item* item) : item(item), c3t3(c3t3_) @@ -255,10 +271,15 @@ struct Scene_c3t3_item_priv { , is_valid(true) { init_default_values(); + tet_Slider = new QSlider(Qt::Horizontal); + tet_Slider->setValue(100); + tet_Slider->setMinimum(0); + tet_Slider->setMaximum(100); } ~Scene_c3t3_item_priv() { delete frame; + delete tet_Slider; } void init_default_values() { @@ -332,6 +353,7 @@ struct Scene_c3t3_item_priv { Facet_vertices =0, Facet_normals, Facet_colors, + Facet_barycenters, Edges_vertices, Edges_CNC, Grid_vertices, @@ -366,6 +388,7 @@ struct Scene_c3t3_item_priv { Indices surface_patch_indices_; Indices subdomain_indices_; std::set intersected_cells; + QSlider* tet_Slider; //!Allows OpenGL 2.1 context to get access to glDrawArraysInstanced. typedef void (APIENTRYP PFNGLDRAWARRAYSINSTANCEDARBPROC) (GLenum mode, GLint first, GLsizei count, GLsizei primcount); @@ -804,6 +827,8 @@ void Scene_c3t3_item::draw(CGAL::Three::Viewer_interface* viewer) const { d->program->bind(); QVector4D cp(this->plane().a(),this->plane().b(),this->plane().c(),this->plane().d()); d->program->setUniformValue("cutplane", cp); + float shrink_factor = getShrinkFactor(); + d->program->setUniformValue("shrink_factor", shrink_factor); // positions_poly_size is the number of total facets in the C3T3 // it is only computed once and positions_poly is emptied at the end viewer->glDrawArrays(GL_TRIANGLES, 0, static_cast(d->positions_poly_size / 3)); @@ -988,12 +1013,11 @@ void Scene_c3t3_item_priv::draw_triangle(const Kernel::Point_3& pa, positions_poly.push_back(pc.y()+offset.y); positions_poly.push_back(pc.z()+offset.z); - Kernel::Point_3 bary = (pa+pb+pc)/3.0; for(int i=0; i<3; ++i) { - positions_barycenter.push_back(bary.x()); - positions_barycenter.push_back(bary.y()); - positions_barycenter.push_back(bary.z()); + positions_barycenter.push_back((pa[0]+pb[0]+pc[0])/3.0); + positions_barycenter.push_back((pa[1]+pb[1]+pc[1])/3.0); + positions_barycenter.push_back((pa[2]+pb[2]+pc[2])/3.0); } @@ -1143,6 +1167,13 @@ QMenu* Scene_c3t3_item::contextMenu() bool menuChanged = menu->property(prop_name).toBool(); if (!menuChanged) { + + QMenu *container = new QMenu(tr("Tetrahedra's Shrink Factor")); + QWidgetAction *sliderAction = new QWidgetAction(0); + connect(d->tet_Slider, &QSlider::valueChanged, this, &Scene_c3t3_item::itemChanged); + sliderAction->setDefaultWidget(d->tet_Slider); + container->addAction(sliderAction); + menu->addMenu(container); QAction* actionExportFacetsInComplex = menu->addAction(tr("Export facets in complex")); actionExportFacetsInComplex->setObjectName("actionExportFacetsInComplex"); @@ -1215,9 +1246,15 @@ void Scene_c3t3_item_priv::initializeBuffers(CGAL::Three::Viewer_interface *view program->setAttributeBuffer("colors", GL_FLOAT, 0, 3); item->buffers[Facet_colors].release(); + item->buffers[Facet_barycenters].bind(); + item->buffers[Facet_barycenters].allocate(positions_barycenter.data(), + static_cast(positions_barycenter.size()*sizeof(float))); + program->enableAttributeArray("barycenter"); + program->setAttributeBuffer("barycenter", GL_FLOAT, 0, 3); + item->buffers[Facet_barycenters].release(); + item->vaos[Facets]->release(); program->release(); - positions_poly_size = positions_poly.size(); positions_poly.clear(); positions_poly.swap(positions_poly); @@ -1225,6 +1262,8 @@ void Scene_c3t3_item_priv::initializeBuffers(CGAL::Three::Viewer_interface *view normals.swap(normals); f_colors.clear(); f_colors.swap(f_colors); + positions_barycenter.clear(); + positions_barycenter.swap(positions_barycenter); } //vao containing the data for the lines @@ -1358,6 +1397,7 @@ void Scene_c3t3_item_priv::computeIntersections() normals.clear(); f_colors.clear(); positions_lines.clear(); + positions_barycenter.clear(); const Kernel::Plane_3& plane = item->plane(offset); tree.all_intersected_primitives(plane, boost::make_function_output_iterator(ComputeIntersection(*this))); @@ -1575,7 +1615,8 @@ void Scene_c3t3_item::show_intersection(bool b) d->intersection->init_vectors(&d->positions_poly, &d->normals, &d->positions_lines, - &d->f_colors); + &d->f_colors, + &d->positions_barycenter); d->intersection->setName("Intersection tetrahedra"); d->intersection->setRenderingMode(renderingMode()); connect(d->intersection, SIGNAL(destroyed()), this, SLOT(reset_intersection_item())); @@ -1656,4 +1697,22 @@ void Scene_c3t3_item::set_valid(bool b) { d->is_valid = b; } +float Scene_c3t3_item::getShrinkFactor() const +{ + return d->tet_Slider->value()/100.0f; +} +bool Scene_c3t3_item::keyPressEvent(QKeyEvent *event) +{ + if(event->key() == Qt::Key_Plus) + { + d->tet_Slider->setValue(d->tet_Slider->value() + 5); + itemChanged(); + } + else if(event->key() == Qt::Key_Minus) + { + d->tet_Slider->setValue(d->tet_Slider->value() -5); + itemChanged(); + } + return true; +} #include "Scene_c3t3_item.moc" diff --git a/Polyhedron/demo/Polyhedron/Scene_c3t3_item.h b/Polyhedron/demo/Polyhedron/Scene_c3t3_item.h index 79a6ca348e3..af7da182adb 100644 --- a/Polyhedron/demo/Polyhedron/Scene_c3t3_item.h +++ b/Polyhedron/demo/Polyhedron/Scene_c3t3_item.h @@ -118,6 +118,8 @@ public: public: QMenu* contextMenu(); void copyProperties(Scene_item *); + float getShrinkFactor() const; + bool keyPressEvent(QKeyEvent *); public Q_SLOTS: void export_facets_in_complex(); diff --git a/Polyhedron/demo/Polyhedron/Viewer.cpp b/Polyhedron/demo/Polyhedron/Viewer.cpp index 2ce3a12f0f9..dd1dcbd91fa 100644 --- a/Polyhedron/demo/Polyhedron/Viewer.cpp +++ b/Polyhedron/demo/Polyhedron/Viewer.cpp @@ -683,6 +683,7 @@ void Viewer::attribBuffers(int program_name) const { case PROGRAM_WITH_TEXTURE: case PROGRAM_CUTPLANE_SPHERES: case PROGRAM_SPHERES: + case PROGRAM_C3T3_TETS: program->setUniformValue("light_pos", position); program->setUniformValue("light_diff",diffuse); program->setUniformValue("light_spec", specular); @@ -699,6 +700,7 @@ void Viewer::attribBuffers(int program_name) const { case PROGRAM_INSTANCED: case PROGRAM_CUTPLANE_SPHERES: case PROGRAM_SPHERES: + case PROGRAM_C3T3_TETS: program->setUniformValue("mv_matrix", mv_mat); break; case PROGRAM_WITHOUT_LIGHT: @@ -1349,6 +1351,27 @@ QOpenGLShaderProgram* Viewer::getShaderProgram(int name) const d->shader_programs[PROGRAM_CUTPLANE_SPHERES] = program; return program; } + case PROGRAM_C3T3_TETS: + if( d->shader_programs[PROGRAM_C3T3_TETS]) + { + return d->shader_programs[PROGRAM_C3T3_TETS]; + } + else + { + QOpenGLShaderProgram *program = new QOpenGLShaderProgram(viewer); + if(!program->addShaderFromSourceFile(QOpenGLShader::Vertex,":/cgal/Polyhedron_3/resources/shader_c3t3_tets.v")) + { + std::cerr<<"adding vertex shader FAILED"<addShaderFromSourceFile(QOpenGLShader::Fragment,":/cgal/Polyhedron_3/resources/shader_with_light.f" )) + { + std::cerr<<"adding fragment shader FAILED"<bindAttributeLocation("colors", 1); + program->link(); + d->shader_programs[PROGRAM_C3T3_TETS] = program; + return program; + } case PROGRAM_SPHERES: if( d->shader_programs[PROGRAM_SPHERES]) { diff --git a/Polyhedron/demo/Polyhedron/resources/shader_c3t3.v b/Polyhedron/demo/Polyhedron/resources/shader_c3t3.v index 240f59105dc..8f61d02d2e7 100644 --- a/Polyhedron/demo/Polyhedron/resources/shader_c3t3.v +++ b/Polyhedron/demo/Polyhedron/resources/shader_c3t3.v @@ -15,17 +15,17 @@ void main(void) color = vec4(colors, vertex.x * cutplane.x + vertex.y * cutplane.y + vertex.z * cutplane.z + cutplane.w); fP = mv_matrix * vertex; fN = mat3(mv_matrix)* normals; - mat4 transOB = mat4(1, 0, 0, 0, // first column + highp mat4 transOB = mat4(1, 0, 0, 0, // first column 0, 1, 0, 0, // second column 0, 0, 1, 0, // third column barycenter.x, barycenter.y, barycenter.z, 1); // fourth column - mat4 transBO = mat4(1, 0, 0, 0, // first column + highp mat4 transBO = mat4(1, 0, 0, 0, // first column 0, 1, 0, 0, // second column 0, 0, 1, 0, // third column -barycenter.x, -barycenter.y, -barycenter.z, 1); // fourth column - mat4 scaling = mat4(shrink_factor, 0, 0, 0, + highp mat4 scaling = mat4(shrink_factor, 0, 0, 0, 0, shrink_factor, 0, 0, 0, 0, shrink_factor, 0, - 0, 0, 0, shrink_factor); + 0, 0, 0, 1); gl_Position = mvp_matrix *transOB * scaling * transBO * vertex; } diff --git a/Polyhedron/demo/Polyhedron/resources/shader_c3t3_tets.v b/Polyhedron/demo/Polyhedron/resources/shader_c3t3_tets.v new file mode 100644 index 00000000000..89becb1d2b6 --- /dev/null +++ b/Polyhedron/demo/Polyhedron/resources/shader_c3t3_tets.v @@ -0,0 +1,30 @@ +#version 120 +attribute highp vec4 vertex; +attribute highp vec3 normals; +attribute highp vec3 colors; +attribute highp vec3 barycenter; +uniform highp mat4 mvp_matrix; +uniform highp mat4 mv_matrix; +uniform highp float shrink_factor; +varying highp vec4 fP; +varying highp vec3 fN; +varying highp vec4 color; +void main(void) +{ + color = vec4(colors, 1.0); + fP = mv_matrix * vertex; + fN = mat3(mv_matrix)* normals; + highp mat4 transOB = mat4(1, 0, 0, 0, // first column + 0, 1, 0, 0, // second column + 0, 0, 1, 0, // third column + barycenter.x, barycenter.y, barycenter.z, 1); // fourth column + highp mat4 transBO = mat4(1, 0, 0, 0, // first column + 0, 1, 0, 0, // second column + 0, 0, 1, 0, // third column + -barycenter.x, -barycenter.y, -barycenter.z, 1); // fourth column + highp mat4 scaling = mat4(shrink_factor, 0, 0, 0, + 0, shrink_factor, 0, 0, + 0, 0, shrink_factor, 0, + 0, 0, 0, 1); + gl_Position = mvp_matrix *transOB * scaling * transBO * vertex; +} diff --git a/Three/include/CGAL/Three/Scene_item.h b/Three/include/CGAL/Three/Scene_item.h index 70db572d0c7..4a118032a12 100644 --- a/Three/include/CGAL/Three/Scene_item.h +++ b/Three/include/CGAL/Three/Scene_item.h @@ -80,6 +80,7 @@ public: PROGRAM_C3T3_EDGES, /** Used to render the edges of a c3t3_item. It discards any fragment on a side of a plane, meaning that nothing is displayed on this side of the plane. Not affected by light.*/ PROGRAM_CUTPLANE_SPHERES, /** Used to render the spheres of an item with a cut plane.*/ PROGRAM_SPHERES, /** Used to render one or several spheres.*/ + PROGRAM_C3T3_TETS, /** Used to render the tetrahedra of the intersection of a c3t3_item.*/ NB_OF_PROGRAMS /** Holds the number of different programs in this enum.*/ }; typedef CGAL::Bbox_3 Bbox; diff --git a/Three/include/CGAL/Three/Viewer_interface.h b/Three/include/CGAL/Three/Viewer_interface.h index 774323efe33..e0fa33169a4 100644 --- a/Three/include/CGAL/Three/Viewer_interface.h +++ b/Three/include/CGAL/Three/Viewer_interface.h @@ -69,6 +69,7 @@ public: PROGRAM_C3T3_EDGES, /** Used to render the edges of a c3t3_item. It discards any fragment on a side of a plane, meaning that nothing is displayed on this side of the plane. Not affected by light.*/ PROGRAM_CUTPLANE_SPHERES, /** Used to render the spheres of an item with a cut plane.*/ PROGRAM_SPHERES, /** Used to render one or several spheres.*/ + PROGRAM_C3T3_TETS, /** Used to render the tetrahedra of the intersection of a c3t3_item.*/ NB_OF_PROGRAMS /** Holds the number of different programs in this enum.*/ };