GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/leaf-node-face.cpp Lines: 1 104 1.0 %
Date: 2020-05-14 11:23:33 Branches: 2 132 1.5 %

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

3
} /* namespace gepetto */