Skip to content

Commit 3f1135a

Browse files
committed
Fixes for group, selection system changed.
1 parent 2c663b3 commit 3f1135a

File tree

9 files changed

+158
-58
lines changed

9 files changed

+158
-58
lines changed

Polyhedron/demo/Polyhedron/Scene.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ void Scene::renderScene(const QList<Scene_interface::Item_id> &items,
486486
{
487487
if( group || item.renderingMode() == Flat || item.renderingMode() == FlatPlusEdges || item.renderingMode() == Gouraud)
488488
{
489+
if(with_names) {
490+
viewer->glClearDepth(1.0);
491+
viewer->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
492+
}
489493
if(item.renderingMode() == Gouraud)
490494
viewer->glShadeModel(GL_SMOOTH);
491495
else
@@ -504,6 +508,8 @@ void Scene::renderScene(const QList<Scene_interface::Item_id> &items,
504508
picked_item_IDs[depth] = index;
505509
}
506510
}
511+
if(group)
512+
group->renderChildren(viewer, picked_item_IDs, picked_pixel, with_names, pass, writing_depth, fbo);
507513
}
508514
}
509515
}

Polyhedron/demo/Polyhedron/Scene_group_item.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,51 @@ void Scene_group_item::moveUp(int i)
122122
children.move(i, i-1);
123123
}
124124

125-
void Scene_group_item::draw(CGAL::Three::Viewer_interface* viewer, int pass , bool is_writing, QOpenGLFramebufferObject *fbo)
125+
void Scene_group_item::draw(CGAL::Three::Viewer_interface* , int , bool , QOpenGLFramebufferObject *)
126126
{
127127
if(!isInit())
128128
initGL();
129+
}
130+
131+
void Scene_group_item::renderChildren(Viewer_interface *viewer,
132+
QMap<float, int>& picked_item_IDs,
133+
const QPoint& picked_pixel,
134+
bool with_names,
135+
int pass,
136+
bool is_writing,
137+
QOpenGLFramebufferObject *fbo)
138+
{
139+
129140
Q_FOREACH(Scene_interface::Item_id id, children){
141+
if(with_names) {
142+
viewer->glClearDepth(1.0);
143+
viewer->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
144+
}
145+
if(id == scene->mainSelectionIndex()|| scene->selectionIndices().contains(id))
146+
{
147+
getChild(id)->selection_changed(true);
148+
}
149+
else
150+
{
151+
getChild(id)->selection_changed(false);
152+
}
130153
if(getChild(id)->visible() &&
131154
(getChild(id)->renderingMode() == Flat ||
132155
getChild(id)->renderingMode() == FlatPlusEdges ||
133156
getChild(id)->renderingMode() == Gouraud))
134157
{
135158
getChild(id)->draw(viewer, pass, is_writing, fbo);
136159
}
160+
if(with_names) {
161+
// read depth buffer at pick location;
162+
float depth = 1.0;
163+
viewer->glReadPixels(picked_pixel.x(),viewer->camera()->screenHeight()-1-picked_pixel.y(),1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
164+
if (depth != 1.0)
165+
{
166+
//add object to list of picked objects;
167+
picked_item_IDs[depth] = id;
168+
}
169+
}
137170
}
138171
}
139172

@@ -142,6 +175,15 @@ void Scene_group_item::drawEdges(CGAL::Three::Viewer_interface* viewer)
142175
if(!isInit())
143176
initGL();
144177
Q_FOREACH(Scene_interface::Item_id id, children){
178+
if(id == scene->mainSelectionIndex()|| scene->selectionIndices().contains(id))
179+
{
180+
getChild(id)->selection_changed(true);
181+
}
182+
else
183+
184+
{
185+
getChild(id)->selection_changed(false);
186+
}
145187
if(getChild(id)->visible() &&
146188
(getChild(id)->renderingMode() == FlatPlusEdges
147189
|| getChild(id)->renderingMode() == Wireframe
@@ -157,6 +199,15 @@ void Scene_group_item::drawPoints(CGAL::Three::Viewer_interface* viewer)
157199
if(!isInit())
158200
initGL();
159201
Q_FOREACH(Scene_interface::Item_id id, children){
202+
if(id == scene->mainSelectionIndex()|| scene->selectionIndices().contains(id))
203+
{
204+
getChild(id)->selection_changed(true);
205+
}
206+
else
207+
208+
{
209+
getChild(id)->selection_changed(false);
210+
}
160211
if(getChild(id)->visible())
161212
{
162213

Polyhedron/demo/Polyhedron/Scene_item.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct D{
1818
visible_(true),
1919
parent_group(0),
2020
is_selected(false),
21+
was_selected(false),
2122
rendering_mode(FlatPlusEdges),
2223
defaultContextMenu(NULL),
2324
is_locked(false),
@@ -28,18 +29,20 @@ struct D{
2829

2930
int has_group;
3031

31-
//!The name of the item.
32+
//The name of the item.
3233
QString name_;
33-
//!The color of the item.
34+
//The color of the item.
3435
QColor color_;
35-
//!The visibility of the item.
36+
//The visibility of the item.
3637
bool visible_;
37-
//!The parent group, or 0 if the item is not in a group.
38+
//The parent group, or 0 if the item is not in a group.
3839
Scene_group_item* parent_group;
39-
//!Specifies if the item is currently selected.
40+
//Specifies if the item is currently selected.
4041
bool is_selected;
42+
//Last selection state
43+
bool was_selected;
4144
RenderingMode rendering_mode;
42-
//!The default context menu.
45+
//The default context menu.
4346
QMenu* defaultContextMenu;
4447
//when this is true, all the operations and options of this item are disabled.
4548
//Used in multithreading context.
@@ -54,6 +57,15 @@ const QColor Scene_item::defaultColor()
5457
return QColor(100, 100, 255);
5558
}
5659

60+
QColor Scene_item::selectionColor()
61+
{
62+
QColor c = color();
63+
return (QColor::fromHsv(
64+
c.hue()<270 && c.hue() > 90 ? 0
65+
: 180,
66+
255, 255));
67+
}
68+
5769
namespace CT = CGAL::Three;
5870
CGAL::Three::Scene_item::Scene_item()
5971
: d(new D(this)){}
@@ -160,7 +172,22 @@ moveToGroup(CGAL::Three::Scene_group_item* group) {
160172

161173
void CGAL::Three::Scene_item::invalidate(Gl_data_names) {}
162174

163-
void CGAL::Three::Scene_item::selection_changed(bool) {}
175+
void CGAL::Three::Scene_item::selection_changed(bool b)
176+
{
177+
if(b && !d->was_selected)
178+
{
179+
setSelected(true);
180+
d->was_selected = true;
181+
redraw();
182+
QTimer::singleShot(500, [this]{setSelected(false); redraw();});
183+
}
184+
else{
185+
if(!b)
186+
setSelected(false);
187+
d->was_selected = b;
188+
}
189+
190+
}
164191
void CGAL::Three::Scene_item::setVisible(bool b)
165192
{
166193
d->visible_ = b;

Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ void Scene_surface_mesh_item::draw(CGAL::Three::Viewer_interface *viewer,
679679
getTriangleContainer(0)->setNear(near);
680680
getTriangleContainer(0)->setFar(far);
681681
getTriangleContainer(0)->setDepthWriting(writing_depth);
682-
getTriangleContainer(0)->setColor(color());
682+
getTriangleContainer(0)->setColor(isSelected() ? selectionColor() : color());
683683
getTriangleContainer(0)->setSelected(isSelected());
684684
getTriangleContainer(0)->setAlpha(alpha());
685685
getTriangleContainer(0)->draw( viewer, !d->has_vcolors, fbo);
@@ -692,7 +692,7 @@ void Scene_surface_mesh_item::draw(CGAL::Three::Viewer_interface *viewer,
692692
getTriangleContainer(1)->setNear(near);
693693
getTriangleContainer(1)->setFar(far);
694694
getTriangleContainer(1)->setDepthWriting(writing_depth);
695-
getTriangleContainer(1)->setColor(color());
695+
getTriangleContainer(1)->setColor(isSelected() ? selectionColor() : color());
696696
getTriangleContainer(1)->setSelected(isSelected());
697697
getTriangleContainer(1)->setAlpha(alpha());
698698
getTriangleContainer(1)->draw( viewer, !d->has_fcolors, fbo);
@@ -735,14 +735,6 @@ void Scene_surface_mesh_item::drawPoints(CGAL::Three::Viewer_interface *viewer)
735735
//TODO
736736
}
737737

738-
void
739-
Scene_surface_mesh_item::selection_changed(bool p_is_selected)
740-
{
741-
if(p_is_selected != isSelected())
742-
{
743-
setSelected(p_is_selected);
744-
}
745-
}
746738

747739
bool
748740
Scene_surface_mesh_item::supportsRenderingMode(RenderingMode m) const

Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ class SCENE_SURFACE_MESH_ITEM_EXPORT Scene_surface_mesh_item
141141

142142
public Q_SLOTS:
143143
void itemAboutToBeDestroyed(Scene_item *) Q_DECL_OVERRIDE;
144-
virtual void selection_changed(bool) Q_DECL_OVERRIDE;
145144
void select(double orig_x,
146145
double orig_y,
147146
double orig_z,

Polyhedron/demo/Polyhedron/Viewer.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ void Viewer::initializeGL()
414414
" vec4 diffuse = max(abs(dot(N,L)),0.0) * light_diff*color; \n"
415415
" vec4 specular = pow(max(dot(R,V), 0.0), spec_power) * light_spec; \n"
416416

417-
"gl_FragColor = color*light_amb + diffuse + specular; \n"
417+
"gl_FragColor = vec4(vec3(color*light_amb + diffuse + specular).xyz, 1.0f); \n"
418418
"} \n"
419419
"\n"
420420
};
@@ -1106,43 +1106,43 @@ void Viewer::drawVisualHints()
11061106
d->rendering_program.bind();
11071107
qglviewer::Camera::Type camera_type = camera()->type();
11081108
camera()->setType(qglviewer::Camera::ORTHOGRAPHIC);
1109-
QMatrix4x4 mvpMatrix;
1110-
QMatrix4x4 mvMatrix;
1111-
for(int i=0; i < 16; i++)
1112-
{
1113-
mvMatrix.data()[i] = camera()->orientation().inverse().matrix()[i];
1114-
}
1115-
mvpMatrix.ortho(-1,1,-1,1,-1,1);
1116-
mvpMatrix = mvpMatrix*mvMatrix;
1117-
camera()->setType(camera_type);
1118-
1119-
d->rendering_program.setUniformValue("light_pos", d->lighting.position);
1120-
d->rendering_program.setUniformValue("mvp_matrix", mvpMatrix);
1121-
d->rendering_program.setUniformValue("mv_matrix", mvMatrix);
1122-
d->rendering_program.setUniformValue("light_diff", d->lighting.diffuse);
1123-
d->rendering_program.setUniformValue("light_spec", d->lighting.specular);
1124-
d->rendering_program.setUniformValue("light_amb", d->lighting.ambient);
1125-
d->rendering_program.setUniformValue("spec_power", d->lighting.shininess);
1126-
1127-
d->vao[0].bind();
1128-
int viewport[4];
1129-
int scissor[4];
1130-
1131-
// The viewport and the scissor are changed to fit the upper right
1132-
// corner. Original values are saved.
1133-
glGetIntegerv(GL_VIEWPORT, viewport);
1134-
glGetIntegerv(GL_SCISSOR_BOX, scissor);
1135-
1136-
// Axis viewport size, in pixels
1137-
const int size = 100;
1138-
glViewport(width()*devicePixelRatio()-size, height()*devicePixelRatio()-size, size, size);
1139-
glScissor (width()*devicePixelRatio()-size, height()*devicePixelRatio()-size, size, size);
1140-
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(d->v_Axis.size() / 3));
1141-
// The viewport and the scissor are restored.
1142-
glScissor(scissor[0],scissor[1],scissor[2],scissor[3]);
1143-
glViewport(viewport[0],viewport[1],viewport[2],viewport[3]);
1144-
d->vao[0].release();
1145-
d->rendering_program.release();
1109+
QMatrix4x4 mvpMatrix;
1110+
QMatrix4x4 mvMatrix;
1111+
for(int i=0; i < 16; i++)
1112+
{
1113+
mvMatrix.data()[i] = camera()->orientation().inverse().matrix()[i];
1114+
}
1115+
mvpMatrix.ortho(-1,1,-1,1,-1,1);
1116+
mvpMatrix = mvpMatrix*mvMatrix;
1117+
camera()->setType(camera_type);
1118+
1119+
d->rendering_program.setUniformValue("light_pos", d->lighting.position);
1120+
d->rendering_program.setUniformValue("mvp_matrix", mvpMatrix);
1121+
d->rendering_program.setUniformValue("mv_matrix", mvMatrix);
1122+
d->rendering_program.setUniformValue("light_diff", d->lighting.diffuse);
1123+
d->rendering_program.setUniformValue("light_spec", d->lighting.specular);
1124+
d->rendering_program.setUniformValue("light_amb", d->lighting.ambient);
1125+
d->rendering_program.setUniformValue("spec_power", d->lighting.shininess);
1126+
1127+
d->vao[0].bind();
1128+
int viewport[4];
1129+
int scissor[4];
1130+
1131+
// The viewport and the scissor are changed to fit the upper right
1132+
// corner. Original values are saved.
1133+
glGetIntegerv(GL_VIEWPORT, viewport);
1134+
glGetIntegerv(GL_SCISSOR_BOX, scissor);
1135+
1136+
// Axis viewport size, in pixels
1137+
const int size = 100;
1138+
glViewport(width()*devicePixelRatio()-size, height()*devicePixelRatio()-size, size, size);
1139+
glScissor (width()*devicePixelRatio()-size, height()*devicePixelRatio()-size, size, size);
1140+
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(d->v_Axis.size() / 3));
1141+
// The viewport and the scissor are restored.
1142+
glScissor(scissor[0],scissor[1],scissor[2],scissor[3]);
1143+
glViewport(viewport[0],viewport[1],viewport[2],viewport[3]);
1144+
d->vao[0].release();
1145+
d->rendering_program.release();
11461146
}
11471147

11481148
if(d->distance_is_displayed)

Polyhedron/demo/Polyhedron/resources/shader_with_light.f

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
highp vec4 specular = pow(max(dot(R,V), 0.0), spec_power) * light_spec;
6464
vec4 ret_color = vec4((my_color*light_amb).xyz + diffuse.xyz + specular.xyz,1);
6565
if(is_selected)
66-
gl_FragColor = vec4(ret_color.r+70.0/255.0, ret_color.g+70.0/255.0, ret_color.b+70.0/255.0, alpha);
66+
gl_FragColor = color;
6767
else
6868
gl_FragColor = vec4(ret_color.xyz, alpha);
6969
}

Three/include/CGAL/Three/Scene_group_item.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,27 @@ public :
131131
//! @see #RenderingMode
132132
virtual void draw(CGAL::Three::Viewer_interface*, int pass,
133133
bool is_writing, QOpenGLFramebufferObject*fbo);
134+
135+
//!
136+
//! \brief renderChildren deals with the rendering, selecting and picking of
137+
//! the group's children.
138+
//!
139+
//! \param picked_item_IDs the depth-index map
140+
//! \param picked_pixel the screen point that has been picked.
141+
//! \param with_names should be `true` if a picking is being performed.
142+
//! \param pass the dept peeling pass.
143+
//! \param is_writing means that the color of the faces will be drawn in a grayscale
144+
//! according to the depth of the fragment in the shader. It is used by the transparency.
145+
//! \param fbo contains the texture used by the Depth Peeling algorithm.
146+
//! Should be NULL if pass <= 0;
147+
//!
148+
virtual void renderChildren(Viewer_interface *,
149+
QMap<float, int>& picked_item_IDs, const QPoint &picked_pixel,
150+
bool with_names,
151+
int pass,
152+
bool is_writing,
153+
QOpenGLFramebufferObject *fbo);
154+
134155
//!\brief draws all the children
135156
//!
136157
//! Calls `Scene_item::drawEdges()`for each child if its current

Three/include/CGAL/Three/Scene_item.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class SCENE_ITEM_EXPORT Scene_item : public QObject{
8282
//!
8383
//! This color is the one that will be displayed if none is specified after its creation.
8484
static const QColor defaultColor();
85+
//!
86+
//! \brief selectionColor is the color used for selected item.
87+
//!
88+
QColor selectionColor();
8589
Scene_item();
8690

8791
virtual ~Scene_item();

0 commit comments

Comments
 (0)