Skip to content

Commit 79cfe93

Browse files
committed
Improvements in visualization.
1 parent ba513b6 commit 79cfe93

File tree

4 files changed

+98
-32
lines changed

4 files changed

+98
-32
lines changed

examples/deform_bar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ int main(int argc, char **argv) {
8080
// Create an OSG viewer to visualize incremental deformation.
8181
float inc = 0.001;
8282
float t = 0.f;
83-
deform::example::OSGViewer viewer(argc, argv, mesh, [&](Mesh &mesh, double time) {
83+
deform::example::OSGViewer viewer(mesh, anchors, handles);
84+
viewer.onFrame([&](Mesh &mesh, double time) {
8485

8586
t+= inc;
8687
if (t > 1.f)

examples/deform_sphere.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ int main(int argc, char **argv) {
6767
double add = 0.01;
6868

6969
// Create an OSG viewer to visualize incremental deformation.
70-
deform::example::OSGViewer viewer(argc, argv, mesh, [&p, &pi, &add, &arap](Mesh &mesh, double time) {
70+
deform::example::OSGViewer viewer(mesh, {37}, {32});
71+
viewer.onFrame([&p, &pi, &add, &arap](Mesh &mesh, double time) {
7172

7273
// Update motion
7374

examples/osg_viewer.cpp

Lines changed: 89 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include <osgViewer/ViewerEventHandlers>
1919
#include <osgFX/Scribe>
2020
#include <osg/ShadeModel>
21+
#include <osg/LightModel>
22+
#include <osg/Material>
23+
24+
#include <unordered_set>
2125

2226
namespace deform {
2327
namespace example {
@@ -27,6 +31,7 @@ namespace deform {
2731
DeformCallback dc;
2832
osg::ref_ptr<osgViewer::Viewer> viewer;
2933
osg::ref_ptr<osg::Group> root;
34+
osg::ref_ptr<osg::Geode> geode;
3035
};
3136

3237
class OSGUpdateCallback : public osg::NodeCallback
@@ -71,48 +76,51 @@ namespace deform {
7176
OSGViewer::DeformCallback *_dc;
7277
};
7378

74-
OSGViewer::OSGViewer(int argc, char **argv, OSGViewer::Mesh &mesh, DeformCallback dc)
75-
: _data(new data())
79+
OSGViewer::OSGViewer(OSGViewer::Mesh &mesh)
80+
: OSGViewer(mesh, std::vector<int>(), std::vector<int>())
81+
{
82+
}
83+
84+
OSGViewer::OSGViewer(Mesh &mesh, const std::vector<int> &anchors, const std::vector<int> &handles)
85+
:_data(new data())
7686
{
7787
_data->mesh = &mesh;
7888
_data->mesh->request_face_normals();
7989
_data->mesh->request_vertex_normals();
8090
_data->mesh->update_normals();
81-
_data->dc = dc;
8291

83-
setupScene();
92+
setupScene(anchors, handles);
8493

85-
osg::ArgumentParser arguments(&argc, argv);
86-
_data->viewer = new osgViewer::Viewer(arguments);
94+
_data->viewer = new osgViewer::Viewer();
95+
osg::DisplaySettings::instance()->setNumMultiSamples( 4 );
8796
_data->viewer->setUpViewInWindow(100, 100, 640, 360);
88-
//osg::DisplaySettings::instance()->setNumMultiSamples(8);
8997
_data->viewer->setSceneData(_data->root);
9098
_data->viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
9199
_data->viewer->getCamera()->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
100+
_data->viewer->getCamera()->setClearColor(osg::Vec4(1,1,1,1));
92101

93102
_data->viewer->realize();
94-
103+
95104
typedef osgViewer::Viewer::Windows Windows;
96105
Windows windows;
97106
_data->viewer->getWindows(windows);
98107
for (auto w : windows) {
99108
w->setWindowName("Mesh-Deform https://github.com/cheind/mesh-deform");
100109
}
101110

102-
103111
_data->viewer->addEventHandler(new osgViewer::StatsHandler) ;
104-
105112
}
106113

107114
OSGViewer::~OSGViewer() {
108115
_data->mesh->release_face_normals();
109116
_data->mesh->release_vertex_normals();
110117
}
118+
111119

112-
void OSGViewer::setupScene() {
120+
void OSGViewer::setupScene(const std::vector<int> &anchors, const std::vector<int> &handles) {
113121
_data->root = new osg::Group();
114122

115-
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
123+
_data->geode = new osg::Geode();
116124
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();
117125
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();
118126
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array();
@@ -125,42 +133,95 @@ namespace deform {
125133
normals->push_back(osg::Vec3(n[0], n[1], n[2]));
126134
}
127135

128-
osg::ref_ptr<osg::DrawElementsUInt> faces = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
136+
137+
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();
138+
colors->push_back(osg::Vec4(0.9, 0.9, 0.9, 1));
139+
colors->push_back(osg::Vec4(1.0, 0.0, 0.0, 1));
140+
colors->push_back(osg::Vec4(0.0, 1.0, 0.0, 1));
141+
142+
osg::ref_ptr<osg::DrawElementsUInt> faces[3] = {
143+
new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0),
144+
new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0),
145+
new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0)
146+
};
147+
148+
std::unordered_set<int> a_ids(anchors.begin(), anchors.end());
149+
std::unordered_set<int> h_ids(handles.begin(), handles.end());
150+
151+
auto aend = a_ids.end();
152+
auto hend = h_ids.end();
153+
129154
for (auto f = _data->mesh->faces_begin(); f != _data->mesh->faces_end(); ++f) {
130155
auto v = _data->mesh->fv_ccwbegin(*f);
131156

132-
faces->push_back(v->idx()); ++v;
133-
faces->push_back(v->idx()); ++v;
134-
faces->push_back(v->idx());
157+
int id0 = v->idx(); ++v;
158+
int id1 = v->idx(); ++v;
159+
int id2 = v->idx();
160+
161+
// Classify face
162+
bool isAnchor = a_ids.find(id0) != aend || a_ids.find(id1) != aend || a_ids.find(id2) != aend;
163+
bool isHandle = h_ids.find(id0) != hend || h_ids.find(id1) != hend || h_ids.find(id2) != hend;
135164

136-
Mesh::Point p = _data->mesh->normal(*f);
137-
normals->push_back(osg::Vec3(p[0], p[1], p[2]));
165+
if (isAnchor) {
166+
faces[1]->push_back(id0);
167+
faces[1]->push_back(id1);
168+
faces[1]->push_back(id2);
169+
} else if (isHandle) {
170+
faces[2]->push_back(id0);
171+
faces[2]->push_back(id1);
172+
faces[2]->push_back(id2);
173+
} else {
174+
faces[0]->push_back(id0);
175+
faces[0]->push_back(id1);
176+
faces[0]->push_back(id2);
177+
}
138178
}
139179

140180
geometry->setVertexArray(vertices);
141181
geometry->setNormalArray(normals, osg::Array::BIND_PER_VERTEX);
142-
geometry->addPrimitiveSet(faces);
182+
geometry->addPrimitiveSet(faces[0]);
183+
geometry->addPrimitiveSet(faces[1]);
184+
geometry->addPrimitiveSet(faces[2]);
143185
geometry->setUseVertexBufferObjects(true);
144186
geometry->setUseDisplayList(false);
187+
geometry->setColorArray(colors);
188+
geometry->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
145189

146-
geode->addDrawable(geometry);
147-
geode->setDataVariance(osg::Object::DYNAMIC);
148-
geode->setUpdateCallback(new OSGUpdateCallback(_data->mesh, &_data->dc));
190+
_data->geode->addDrawable(geometry);
191+
_data->geode->setDataVariance(osg::Object::DYNAMIC);
149192

150-
osg::StateSet* state = geode->getOrCreateStateSet();
151-
osg::ShadeModel* sm = new osg::ShadeModel();
193+
osg::StateSet* state = _data->geode->getOrCreateStateSet();
194+
osg::ref_ptr<osg::ShadeModel> sm = new osg::ShadeModel();
152195
sm->setMode(osg::ShadeModel::SMOOTH);
153196
state->setAttributeAndModes(sm, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
154-
geode->setStateSet(state);
197+
198+
osg::ref_ptr<osg::LightModel> lm = new osg::LightModel();
199+
lm->setTwoSided(true);
200+
state->setAttributeAndModes(lm, osg::StateAttribute::ON );
201+
202+
osg::ref_ptr<osg::Material> mat = new osg::Material;
203+
mat->setColorMode(osg::Material::ColorMode::DIFFUSE);
204+
mat->setSpecular(osg::Material::FRONT, osg::Vec4f(0.8f, 0.8f, 0.8f, 1.0f));
205+
mat->setShininess(osg::Material::FRONT, 6.0f);
206+
state->setAttributeAndModes(mat, osg::StateAttribute::ON );
207+
208+
state->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
209+
210+
_data->geode->setStateSet(state);
155211

156212
osg::ref_ptr<osgFX::Scribe> scribe = new osgFX::Scribe();
157-
scribe->setWireframeColor(osg::Vec4d(0,0,0,1));
158-
scribe->setWireframeLineWidth(1.f);
159-
scribe->addChild(geode);
213+
scribe->setWireframeColor(osg::Vec4d(0.2,0.2,0.2,1));
214+
scribe->setWireframeLineWidth(0.5f);
215+
scribe->addChild(_data->geode);
160216

161217
_data->root->addChild(scribe);
162218
}
163219

220+
void OSGViewer::onFrame(DeformCallback dc) {
221+
_data->dc = dc;
222+
_data->geode->setUpdateCallback(new OSGUpdateCallback(_data->mesh, &_data->dc));
223+
}
224+
164225
void OSGViewer::run() {
165226
_data->viewer->run();
166227
}

examples/osg_viewer.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@ namespace deform {
2424

2525
typedef std::function<bool(Mesh&, double)> DeformCallback;
2626

27-
OSGViewer(int argc, char **argv, Mesh &mesh, DeformCallback dc);
27+
OSGViewer(Mesh &mesh);
28+
OSGViewer(Mesh &mesh, const std::vector<int> &anchors, const std::vector<int> &handles);
2829
OSGViewer(const OSGViewer &other) = delete;
2930
OSGViewer &operator=(const OSGViewer &other) = delete;
3031
~OSGViewer();
3132

33+
void onFrame(DeformCallback dc);
34+
3235
void run();
3336

3437
private:
3538

36-
void setupScene();
39+
void setupScene(const std::vector<int> &anchors, const std::vector<int> &handles);
3740

3841
struct data;
3942
std::unique_ptr<data> _data;

0 commit comments

Comments
 (0)