GCC Code Coverage Report


Directory: ./
File: src/leaf-node-mesh.cpp
Date: 2024-12-20 15:53:58
Exec Total Coverage
Lines: 0 97 0.0%
Branches: 0 112 0.0%

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 #include "log.hh"
15
16 namespace gepetto {
17 namespace viewer {
18
19 /* Declaration of private function members */
20
21 void LeafNodeMesh::init() {
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 osg::ref_ptr<osg::StateSet> nodess(
35 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 LeafNodeMesh::LeafNodeMesh(const std::string& name) : Node(name) { init(); }
50
51 LeafNodeMesh::LeafNodeMesh(const std::string& name,
52 const osgVector4& color_diffuse)
53 : Node(name) {
54 init();
55 setColor(color_diffuse);
56 }
57
58 LeafNodeMesh::LeafNodeMesh(const LeafNodeMesh& other) : Node(other.getID()) {
59 init();
60 }
61
62 void LeafNodeMesh::initWeakPtr(LeafNodeMeshWeakPtr other_weak_ptr) {
63 weak_ptr_ = other_weak_ptr;
64 }
65
66 /* End of declaration of private function members */
67
68 /* Declaration of protected function members */
69
70 LeafNodeMeshPtr_t LeafNodeMesh::create(const std::string& name) {
71 LeafNodeMeshPtr_t shared_ptr(new LeafNodeMesh(name));
72
73 // Add reference to itself
74 shared_ptr->initWeakPtr(shared_ptr);
75
76 return shared_ptr;
77 }
78
79 LeafNodeMeshPtr_t LeafNodeMesh::create(const std::string& name,
80 const osgVector4& color) {
81 LeafNodeMeshPtr_t shared_ptr(new LeafNodeMesh(name, color));
82
83 // Add reference to itself
84 shared_ptr->initWeakPtr(shared_ptr);
85
86 return shared_ptr;
87 }
88
89 LeafNodeMeshPtr_t LeafNodeMesh::createCopy(LeafNodeMeshPtr_t other) {
90 LeafNodeMeshPtr_t shared_ptr(new LeafNodeMesh(*other));
91
92 // Add reference to itself
93 shared_ptr->initWeakPtr(shared_ptr);
94
95 return shared_ptr;
96 }
97
98 /* End of declaration of protected function members */
99
100 /* Declaration of public function members */
101
102 LeafNodeMeshPtr_t LeafNodeMesh::clone(void) const {
103 return LeafNodeMesh::createCopy(weak_ptr_.lock());
104 }
105
106 LeafNodeMeshPtr_t LeafNodeMesh::self(void) const { return weak_ptr_.lock(); }
107
108 void LeafNodeMesh::setColor(const osgVector4& color_diffuse,
109 const osgVector4& color_specular,
110 const osgVector4& color_emissive) {
111 // setColor(collada_ptr_,color);
112 osg::ref_ptr<osg::Material> mat_ptr(new osg::Material);
113 osgVector4 color_zero(0.0f, 0.0f, 0.0f, 0.0f);
114 mat_ptr->setDiffuse(osg::Material::FRONT_AND_BACK, color_diffuse);
115 mat_ptr->setAmbient(osg::Material::FRONT_AND_BACK, color_zero);
116 mat_ptr->setSpecular(osg::Material::FRONT_AND_BACK, color_specular);
117 mat_ptr->setEmission(osg::Material::FRONT_AND_BACK, color_emissive);
118
119 if (mesh_geometry_ptr_->getStateSet()) {
120 mesh_geometry_ptr_->getStateSet()->setAttribute(mat_ptr.get());
121 setDirty();
122 }
123 }
124 void LeafNodeMesh::setColor(const osgVector4& color_diffuse) {
125 osgVector4 color_specular(0.0f, 0.0f, 0.0f, 0.0f),
126 color_emissive(0.0f, 0.0f, 0.0f, 0.0f);
127 setColor(color_diffuse, color_specular, color_emissive);
128 setTransparentRenderingBin(color_diffuse[3] <
129 Node::TransparencyRenderingBinThreshold);
130 }
131
132 void LeafNodeMesh::setAlpha(const float& alpha) {
133 osg::StateSet* ss = mesh_geometry_ptr_->getOrCreateStateSet();
134 alpha_ = alpha;
135 osg::Material* mat;
136 if (ss->getAttribute(osg::StateAttribute::MATERIAL))
137 mat = dynamic_cast<osg::Material*>(
138 ss->getAttribute(osg::StateAttribute::MATERIAL));
139 else {
140 mat = new osg::Material;
141 ss->setAttribute(mat);
142 }
143 mat->setAlpha(osg::Material::FRONT_AND_BACK, alpha);
144 setTransparentRenderingBin(alpha < Node::TransparencyRenderingBinThreshold,
145 ss);
146 setDirty();
147 }
148
149 void LeafNodeMesh::setTexture(const std::string& image_path) {
150 osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
151 texture->setDataVariance(osg::Object::DYNAMIC);
152 osg::ref_ptr<osg::Image> image = osgDB::readImageFile(image_path);
153 if (!image) {
154 log() << "couldn't find texture " << image_path << ", quiting."
155 << std::endl;
156 return;
157 }
158 texture->setImage(image);
159 mesh_geometry_ptr_->getStateSet()->setTextureAttributeAndModes(
160 0, texture, osg::StateAttribute::ON);
161 setDirty();
162 }
163
164 osg::ref_ptr<osg::Node> LeafNodeMesh::getOsgNode() const { return geode_ptr_; }
165
166 void LeafNodeMesh::setVertexArray(osg::Vec3ArrayRefPtr arrayOfVertices) {
167 mesh_geometry_ptr_->setVertexArray(arrayOfVertices);
168 setDirty();
169 }
170
171 void LeafNodeMesh::addPrimitiveSet(osg::DrawElementsUInt* aPrimitiveSet) {
172 mesh_geometry_ptr_->addPrimitiveSet(aPrimitiveSet);
173 setDirty();
174 }
175
176 void LeafNodeMesh::setColorArray(osg::Vec4ArrayRefPtr aSetOfColors) {
177 mesh_geometry_ptr_->setColorArray(aSetOfColors);
178 setDirty();
179 }
180
181 void LeafNodeMesh::setColorBinding(
182 osg::Geometry::AttributeBinding aColorBinding) {
183 mesh_geometry_ptr_->setColorBinding(aColorBinding);
184 setDirty();
185 }
186
187 void LeafNodeMesh::setNormalArray(osg::Vec3ArrayRefPtr normals) {
188 mesh_geometry_ptr_->setNormalArray(normals);
189 setDirty();
190 }
191
192 void LeafNodeMesh::setNormalBinding(
193 osg::Geometry::AttributeBinding aNormalBinding) {
194 mesh_geometry_ptr_->setNormalBinding(aNormalBinding);
195 setDirty();
196 }
197
198 LeafNodeMesh::~LeafNodeMesh() {
199 /* Proper deletion of all tree scene */
200 this->asQueue()->removeChild(geode_ptr_);
201 mesh_geometry_ptr_ = NULL;
202
203 weak_ptr_.reset();
204 }
205
206 /* End of declaration of public function members */
207
208 } /* namespace viewer */
209
210 } /* namespace gepetto */
211