Line |
Branch |
Exec |
Source |
1 |
|
|
// |
2 |
|
|
// leaf-node-face.cpp |
3 |
|
|
// gepetto-viewer |
4 |
|
|
// |
5 |
|
|
// Created by Justin Carpentier, Mathieu Geisert in November 2014. |
6 |
|
|
// Copyright (c) 2014 LAAS-CNRS. All rights reserved. |
7 |
|
|
// |
8 |
|
|
|
9 |
|
|
#include <gepetto/viewer/leaf-node-face.h> |
10 |
|
|
|
11 |
|
|
#include <osg/CullFace> |
12 |
|
|
#include <osg/Texture2D> |
13 |
|
|
#include <osgDB/ReadFile> |
14 |
|
|
#include <osgUtil/SmoothingVisitor> |
15 |
|
|
|
16 |
|
|
#include "log.hh" |
17 |
|
|
|
18 |
|
|
namespace gepetto { |
19 |
|
|
namespace viewer { |
20 |
|
|
|
21 |
|
|
/* Declaration of private function members */ |
22 |
|
|
|
23 |
|
✗ |
void LeafNodeFace::init() { |
24 |
|
|
/* Set face Geometry */ |
25 |
|
✗ |
face_ptr_ = new ::osg::Geometry(); |
26 |
|
|
|
27 |
|
|
/* Create Geode for adding geometry */ |
28 |
|
✗ |
geode_ptr_ = new osg::Geode(); |
29 |
|
✗ |
geode_ptr_->addDrawable(face_ptr_); |
30 |
|
|
|
31 |
|
✗ |
vertices_ = new ::osg::Vec3Array(); |
32 |
|
✗ |
face_ptr_->setVertexArray(vertices_); |
33 |
|
|
|
34 |
|
|
/* Define the face color */ |
35 |
|
✗ |
color_ptr_ = new ::osg::Vec4Array(); |
36 |
|
✗ |
color_ptr_->push_back(osgVector4(1., 1., 1., 1.)); |
37 |
|
✗ |
face_ptr_->setColorArray(color_ptr_.get()); |
38 |
|
✗ |
face_ptr_->setColorBinding(::osg::Geometry::BIND_OVERALL); |
39 |
|
|
|
40 |
|
|
/* Allow transparency */ |
41 |
|
✗ |
geode_ptr_->getOrCreateStateSet()->setMode(GL_BLEND, |
42 |
|
|
::osg::StateAttribute::ON); |
43 |
|
|
|
44 |
|
|
/* Add geode to the queue */ |
45 |
|
✗ |
this->asQueue()->addChild(geode_ptr_); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
✗ |
LeafNodeFace::LeafNodeFace(const std::string& name, const osgVector3& point1, |
49 |
|
✗ |
const osgVector3& point2, const osgVector3& point3) |
50 |
|
✗ |
: Node(name) { |
51 |
|
✗ |
init(); |
52 |
|
✗ |
addVertex(point1); |
53 |
|
✗ |
addVertex(point2); |
54 |
|
✗ |
addVertex(point3); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
✗ |
LeafNodeFace::LeafNodeFace(const std::string& name, const osgVector3& point1, |
58 |
|
|
const osgVector3& point2, const osgVector3& point3, |
59 |
|
✗ |
const osgVector4& color) |
60 |
|
✗ |
: Node(name) { |
61 |
|
✗ |
init(); |
62 |
|
✗ |
addVertex(point1); |
63 |
|
✗ |
addVertex(point2); |
64 |
|
✗ |
addVertex(point3); |
65 |
|
✗ |
setColor(color); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
✗ |
LeafNodeFace::LeafNodeFace(const std::string& name, const osgVector3& point1, |
69 |
|
|
const osgVector3& point2, const osgVector3& point3, |
70 |
|
✗ |
const osgVector3& point4) |
71 |
|
✗ |
: Node(name) { |
72 |
|
✗ |
init(); |
73 |
|
✗ |
addVertex(point1); |
74 |
|
✗ |
addVertex(point2); |
75 |
|
✗ |
addVertex(point3); |
76 |
|
✗ |
addVertex(point4); |
77 |
|
|
} |
78 |
|
✗ |
LeafNodeFace::LeafNodeFace(const std::string& name, const osgVector3& point1, |
79 |
|
|
const osgVector3& point2, const osgVector3& point3, |
80 |
|
✗ |
const osgVector3& point4, const osgVector4& color) |
81 |
|
✗ |
: Node(name) { |
82 |
|
✗ |
init(); |
83 |
|
✗ |
addVertex(point1); |
84 |
|
✗ |
addVertex(point2); |
85 |
|
✗ |
addVertex(point3); |
86 |
|
✗ |
addVertex(point4); |
87 |
|
✗ |
setColor(color); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
✗ |
LeafNodeFace::LeafNodeFace(const LeafNodeFace& other) : Node(other.getID()) { |
91 |
|
✗ |
init(); |
92 |
|
✗ |
for (::osg::Vec3Array::const_iterator i = other.vertices_->begin(); |
93 |
|
✗ |
i != other.vertices_->end(); i++) { |
94 |
|
✗ |
addVertex(*i); |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
|
98 |
|
✗ |
void LeafNodeFace::initWeakPtr(LeafNodeFaceWeakPtr other_weak_ptr) { |
99 |
|
✗ |
weak_ptr_ = other_weak_ptr; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
/* End of declaration of private function members */ |
103 |
|
|
|
104 |
|
|
/* Declaration of protected function members */ |
105 |
|
|
|
106 |
|
✗ |
LeafNodeFacePtr_t LeafNodeFace::create(const std::string& name, |
107 |
|
|
const osgVector3& point1, |
108 |
|
|
const osgVector3& point2, |
109 |
|
|
const osgVector3& point3) { |
110 |
|
✗ |
LeafNodeFacePtr_t shared_ptr(new LeafNodeFace(name, point1, point2, point3)); |
111 |
|
|
|
112 |
|
|
// Add reference to itself |
113 |
|
✗ |
shared_ptr->initWeakPtr(shared_ptr); |
114 |
|
|
|
115 |
|
✗ |
return shared_ptr; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
✗ |
LeafNodeFacePtr_t LeafNodeFace::create(const std::string& name, |
119 |
|
|
const osgVector3& point1, |
120 |
|
|
const osgVector3& point2, |
121 |
|
|
const osgVector3& point3, |
122 |
|
|
const osgVector4& color) { |
123 |
|
|
LeafNodeFacePtr_t shared_ptr( |
124 |
|
✗ |
new LeafNodeFace(name, point1, point2, point3, color)); |
125 |
|
|
|
126 |
|
|
// Add reference to itself |
127 |
|
✗ |
shared_ptr->initWeakPtr(shared_ptr); |
128 |
|
|
|
129 |
|
✗ |
return shared_ptr; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
✗ |
LeafNodeFacePtr_t LeafNodeFace::create(const std::string& name, |
133 |
|
|
const osgVector3& point1, |
134 |
|
|
const osgVector3& point2, |
135 |
|
|
const osgVector3& point3, |
136 |
|
|
const osgVector3& point4) { |
137 |
|
|
LeafNodeFacePtr_t shared_ptr( |
138 |
|
✗ |
new LeafNodeFace(name, point1, point2, point3, point4)); |
139 |
|
|
|
140 |
|
|
// Add reference to itself |
141 |
|
✗ |
shared_ptr->initWeakPtr(shared_ptr); |
142 |
|
|
|
143 |
|
✗ |
return shared_ptr; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
✗ |
LeafNodeFacePtr_t LeafNodeFace::create(const std::string& name, |
147 |
|
|
const osgVector3& point1, |
148 |
|
|
const osgVector3& point2, |
149 |
|
|
const osgVector3& point3, |
150 |
|
|
const osgVector3& point4, |
151 |
|
|
const osgVector4& color) { |
152 |
|
|
LeafNodeFacePtr_t shared_ptr( |
153 |
|
✗ |
new LeafNodeFace(name, point1, point2, point3, point4, color)); |
154 |
|
|
|
155 |
|
|
// Add reference to itself |
156 |
|
✗ |
shared_ptr->initWeakPtr(shared_ptr); |
157 |
|
|
|
158 |
|
✗ |
return shared_ptr; |
159 |
|
|
} |
160 |
|
✗ |
LeafNodeFacePtr_t LeafNodeFace::createCopy(LeafNodeFacePtr_t other) { |
161 |
|
✗ |
LeafNodeFacePtr_t shared_ptr(new LeafNodeFace(*other)); |
162 |
|
|
|
163 |
|
|
// Add reference to itself |
164 |
|
✗ |
shared_ptr->initWeakPtr(shared_ptr); |
165 |
|
|
|
166 |
|
✗ |
return shared_ptr; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
/* End of declaration of protected function members */ |
170 |
|
|
|
171 |
|
|
/* Declaration of public function members */ |
172 |
|
|
|
173 |
|
✗ |
LeafNodeFacePtr_t LeafNodeFace::clone(void) const { |
174 |
|
✗ |
return LeafNodeFace::createCopy(weak_ptr_.lock()); |
175 |
|
|
} |
176 |
|
|
|
177 |
|
✗ |
LeafNodeFacePtr_t LeafNodeFace::self(void) const { return weak_ptr_.lock(); } |
178 |
|
|
|
179 |
|
✗ |
void LeafNodeFace::addVertex(const osgVector3& vertex) { |
180 |
|
✗ |
vertices_->push_back(vertex); |
181 |
|
✗ |
face_ptr_->addPrimitiveSet( |
182 |
|
✗ |
new ::osg::DrawArrays(GL_POLYGON, 0, (GLsizei)vertices_->size())); |
183 |
|
|
// osgUtil::Tessellator::retessellatePolygons( *(face_ptr_.get()) ); |
184 |
|
✗ |
osgUtil::SmoothingVisitor::smooth(*(face_ptr_.get())); |
185 |
|
✗ |
setDirty(); |
186 |
|
|
} |
187 |
|
|
|
188 |
|
✗ |
void LeafNodeFace::setColor(const osgVector4& color) { |
189 |
|
✗ |
color_ptr_->at(0) = color; |
190 |
|
✗ |
setTransparentRenderingBin(color[3] < |
191 |
|
✗ |
Node::TransparencyRenderingBinThreshold); |
192 |
|
✗ |
setDirty(); |
193 |
|
|
} |
194 |
|
|
|
195 |
|
✗ |
void LeafNodeFace::setTexture(const std::string& image_path) { |
196 |
|
✗ |
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D; |
197 |
|
|
// Do not resize image to closest power of two values for width and height |
198 |
|
✗ |
texture->setResizeNonPowerOfTwoHint(false); |
199 |
|
|
// Disable interpolation between pixels. |
200 |
|
✗ |
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST); |
201 |
|
✗ |
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(image_path); |
202 |
|
✗ |
if (!image) { |
203 |
|
✗ |
log() << "couldn't find texture " << image_path << ", quiting." |
204 |
|
✗ |
<< std::endl; |
205 |
|
✗ |
return; |
206 |
|
|
} |
207 |
|
✗ |
texture->setImage(image); |
208 |
|
✗ |
osg::Vec2Array* texcoords = new osg::Vec2Array(4); |
209 |
|
✗ |
(*texcoords)[0].set(0.00f, 0.0f); // texture coord for vertex 0 |
210 |
|
✗ |
(*texcoords)[1].set(1.00f, 0.0f); // texture coord for vertex 1 |
211 |
|
✗ |
(*texcoords)[2].set(1.00f, 1.0f); // texture coord for vertex 2 |
212 |
|
✗ |
(*texcoords)[3].set(0.00f, 1.0f); // texture coord for vertex 3 |
213 |
|
✗ |
face_ptr_->setTexCoordArray(0, texcoords); |
214 |
|
|
|
215 |
|
✗ |
geode_ptr_->getStateSet()->setTextureAttributeAndModes( |
216 |
|
|
0, texture, osg::StateAttribute::ON); |
217 |
|
✗ |
setDirty(); |
218 |
|
|
} |
219 |
|
|
|
220 |
|
✗ |
LeafNodeFace::~LeafNodeFace() { |
221 |
|
|
/* Proper deletion of all tree scene */ |
222 |
|
✗ |
geode_ptr_->removeDrawable(face_ptr_); |
223 |
|
✗ |
face_ptr_ = NULL; |
224 |
|
✗ |
geode_ptr_ = NULL; |
225 |
|
|
|
226 |
|
✗ |
weak_ptr_.reset(); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
/* End of declaration of public function members */ |
230 |
|
|
|
231 |
|
|
} /* namespace viewer */ |
232 |
|
|
|
233 |
|
|
} /* namespace gepetto */ |
234 |
|
|
|