GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/leaf-node-mesh.cpp Lines: 1 100 1.0 %
Date: 2020-05-14 11:23:33 Branches: 2 110 1.8 %

Line Branch Exec Source
1
//
2
//  leaf-node-mesh.cpp
3
//  gepetto-viewer
4
//
5
//  Created by Olivier Stasse, Anthony Couret, Mathieu Geisert.
6
//  Copyright (c) 2016 LAAS-CNRS. All rights reserved.
7
//
8
9
#include <gepetto/viewer/leaf-node-mesh.h>
10
11
#include <osg/Texture2D>
12
#include <osgDB/ReadFile>
13
14
namespace gepetto {
15
namespace viewer {
16
17
  /* Declaration of private function members */
18
19
  void LeafNodeMesh::init()
20
  {
21
22
    /* Create Geode for adding ShapeDrawable */
23
    geode_ptr_ = new osg::Geode ();
24
25
    /* Create mesh geometry */
26
    mesh_geometry_ptr_ = new osg::Geometry();
27
    geode_ptr_->addDrawable(mesh_geometry_ptr_);
28
29
    /* Create PositionAttitudeTransform */
30
    this->asQueue()->addChild(geode_ptr_);
31
32
    /* Allow transparency */
33
    if (mesh_geometry_ptr_->getOrCreateStateSet())
34
      {
35
	osg::ref_ptr<osg::StateSet> nodess (mesh_geometry_ptr_->getOrCreateStateSet());
36
	nodess->setMode(GL_BLEND, ::osg::StateAttribute::OFF);
37
	// Create Material and assign color.
38
39
	// Creating the material object
40
	osg::ref_ptr<osg::Material> mat (new osg::Material);
41
42
	//Attaching the newly defined state set object to the node state set
43
	mat->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
44
	//mat->setColorMode(osg::Material::SPECULAR);
45
	nodess->setAttribute(mat.get());
46
47
      }
48
  }
49
50
  LeafNodeMesh::LeafNodeMesh(const std::string & name) :
51
    Node(name)
52
  {
53
    init();
54
  }
55
56
  LeafNodeMesh::LeafNodeMesh(const std::string &name,
57
			     const osgVector4& color_diffuse) :
58
    Node(name)
59
  {
60
    init();
61
    setColor(color_diffuse);
62
  }
63
64
  LeafNodeMesh::LeafNodeMesh(const LeafNodeMesh& other) :
65
    Node(other.getID())
66
  {
67
    init();
68
  }
69
70
71
  void LeafNodeMesh::initWeakPtr(LeafNodeMeshWeakPtr other_weak_ptr)
72
  {
73
    weak_ptr_ = other_weak_ptr;
74
  }
75
76
  /* End of declaration of private function members */
77
78
  /* Declaration of protected function members */
79
80
  LeafNodeMeshPtr_t LeafNodeMesh::create(const std::string& name)
81
  {
82
    LeafNodeMeshPtr_t shared_ptr(new LeafNodeMesh
83
                                    (name));
84
85
    // Add reference to itself
86
    shared_ptr->initWeakPtr(shared_ptr);
87
88
    return shared_ptr;
89
  }
90
91
  LeafNodeMeshPtr_t LeafNodeMesh::create(const std::string& name, const osgVector4& color)
92
  {
93
    LeafNodeMeshPtr_t shared_ptr(new LeafNodeMesh
94
                                    (name, color));
95
96
    // Add reference to itself
97
    shared_ptr->initWeakPtr(shared_ptr);
98
99
    return shared_ptr;
100
  }
101
102
  LeafNodeMeshPtr_t LeafNodeMesh::createCopy(LeafNodeMeshPtr_t other)
103
  {
104
    LeafNodeMeshPtr_t shared_ptr(new LeafNodeMesh(*other));
105
106
    // Add reference to itself
107
    shared_ptr->initWeakPtr(shared_ptr);
108
109
    return shared_ptr;
110
  }
111
112
113
  /* End of declaration of protected function members */
114
115
  /* Declaration of public function members */
116
117
  LeafNodeMeshPtr_t LeafNodeMesh::clone(void) const
118
  {
119
    return LeafNodeMesh::createCopy(weak_ptr_.lock());
120
  }
121
122
  LeafNodeMeshPtr_t LeafNodeMesh::self(void) const
123
  {
124
    return weak_ptr_.lock();
125
  }
126
127
128
  void LeafNodeMesh::setColor(const osgVector4& color_diffuse,
129
			      const osgVector4& color_specular,
130
			      const osgVector4& color_emissive)
131
  {
132
    //setColor(collada_ptr_,color);
133
    osg::ref_ptr<osg::Material> mat_ptr (new osg::Material);
134
    osgVector4 color_zero(0.0f,0.0f,0.0f,0.0f);
135
    mat_ptr->setDiffuse(osg::Material::FRONT_AND_BACK,color_diffuse);
136
    mat_ptr->setAmbient(osg::Material::FRONT_AND_BACK,color_zero);
137
    mat_ptr->setSpecular(osg::Material::FRONT_AND_BACK,color_specular);
138
    mat_ptr->setEmission(osg::Material::FRONT_AND_BACK,color_emissive);
139
140
    if (mesh_geometry_ptr_->getStateSet()) {
141
      mesh_geometry_ptr_->getStateSet()->setAttribute(mat_ptr.get());
142
      setDirty();
143
    }
144
  }
145
  void LeafNodeMesh::setColor(const osgVector4& color_diffuse)
146
  {
147
    osgVector4 color_specular(0.0f,0.0f,0.0f,0.0f),
148
      color_emissive(0.0f,0.0f,0.0f,0.0f);
149
    setColor(color_diffuse,color_specular,color_emissive);
150
    setTransparentRenderingBin (color_diffuse[3] < Node::TransparencyRenderingBinThreshold);
151
  }
152
153
  void LeafNodeMesh::setAlpha(const float& alpha)
154
  {
155
    osg::StateSet* ss = mesh_geometry_ptr_->getOrCreateStateSet();
156
    alpha_ = alpha;
157
    osg::Material *mat;
158
    if (ss->getAttribute(osg::StateAttribute::MATERIAL))
159
      mat = dynamic_cast<osg::Material*>(ss->getAttribute(osg::StateAttribute::MATERIAL));
160
    else
161
    {
162
      mat = new osg::Material;
163
      ss->setAttribute(mat);
164
    }
165
    mat->setAlpha(osg::Material::FRONT_AND_BACK, alpha);
166
    setTransparentRenderingBin (alpha < Node::TransparencyRenderingBinThreshold, ss);
167
    setDirty();
168
  }
169
170
  void LeafNodeMesh::setTexture(const std::string& image_path)
171
  {
172
    osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
173
    texture->setDataVariance(osg::Object::DYNAMIC);
174
    osg::ref_ptr<osg::Image> image = osgDB::readImageFile(image_path);
175
    if (!image)
176
    {
177
      std::cout << " couldn't find texture, quiting." << std::endl;
178
      return;
179
    }
180
    texture->setImage(image);
181
    mesh_geometry_ptr_->getStateSet()->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
182
    setDirty();
183
  }
184
185
  osg::ref_ptr<osg::Node> LeafNodeMesh::getOsgNode() const
186
  {
187
    return geode_ptr_;
188
  }
189
190
  void LeafNodeMesh::setVertexArray(osg::Vec3ArrayRefPtr arrayOfVertices)
191
  {
192
    mesh_geometry_ptr_->setVertexArray(arrayOfVertices);
193
    setDirty();
194
  }
195
196
  void LeafNodeMesh::addPrimitiveSet(osg::DrawElementsUInt * aPrimitiveSet)
197
  {
198
    mesh_geometry_ptr_->addPrimitiveSet(aPrimitiveSet);
199
    setDirty();
200
  }
201
202
  void LeafNodeMesh::setColorArray(osg::Vec4ArrayRefPtr aSetOfColors)
203
  {
204
    mesh_geometry_ptr_->setColorArray(aSetOfColors);
205
    setDirty();
206
  }
207
208
  void LeafNodeMesh::setColorBinding(osg::Geometry::AttributeBinding aColorBinding)
209
  {
210
    mesh_geometry_ptr_->setColorBinding(aColorBinding);
211
    setDirty();
212
  }
213
214
  void LeafNodeMesh::setNormalArray(osg::Vec3ArrayRefPtr normals)
215
  {
216
    mesh_geometry_ptr_->setNormalArray(normals);
217
    setDirty();
218
  }
219
220
  void LeafNodeMesh::setNormalBinding(osg::Geometry::AttributeBinding aNormalBinding)
221
  {
222
    mesh_geometry_ptr_->setNormalBinding(aNormalBinding);
223
    setDirty();
224
  }
225
226
  LeafNodeMesh::~LeafNodeMesh()
227
  {
228
    /* Proper deletion of all tree scene */
229
    this->asQueue()->removeChild(geode_ptr_);
230
    mesh_geometry_ptr_ = NULL;
231
232
    weak_ptr_.reset();
233
  }
234
235
  /* End of declaration of public function members */
236
237
} /* namespace viewer */
238
239

3
} /* namespace gepetto */