]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/EffectGeode.cxx
Use of copy-constructors
[simgear.git] / simgear / scene / material / EffectGeode.cxx
1 // Copyright (C) 2008  Timothy Moore timoore@redhat.com
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17 #ifdef HAVE_CONFIG_H
18 #  include <simgear_config.h>
19 #endif
20
21 #include "EffectGeode.hxx"
22 #include "Effect.hxx"
23 #include "Technique.hxx"
24
25 #include <osgUtil/CullVisitor>
26 #include <osgUtil/TangentSpaceGenerator>
27
28 #include <osgDB/Registry>
29 #include <osgDB/Input>
30 #include <osgDB/ParameterOutput>
31
32 namespace simgear
33 {
34
35 using namespace osg;
36 using namespace osgUtil;
37
38 EffectGeode::EffectGeode()
39 {
40 }
41
42 EffectGeode::EffectGeode(const EffectGeode& rhs, const osg::CopyOp& copyop) :
43     Geode(rhs, copyop),
44     _effect(static_cast<Effect*>(copyop(rhs._effect.get())))
45 {
46 }
47
48 void EffectGeode::setEffect(Effect* effect)
49 {
50     _effect = effect;
51     if (!_effect)
52         return;
53     addUpdateCallback(new Effect::InitializeCallback);
54 }
55
56 void EffectGeode::resizeGLObjectBuffers(unsigned int maxSize)
57 {
58     if (_effect.valid())
59         _effect->resizeGLObjectBuffers(maxSize);
60     Geode::resizeGLObjectBuffers(maxSize);
61 }
62
63 void EffectGeode::releaseGLObjects(osg::State* state) const
64 {
65     if (_effect.valid())
66         _effect->releaseGLObjects(state);
67     Geode::releaseGLObjects(state);
68 }
69
70 // Generates tangent space vectors or other data from geom, as defined by effect
71 void EffectGeode::runGenerators(osg::Geometry *geometry)
72 {
73       if(geometry && _effect.valid()) {
74         // Generate tangent vectors for the geometry
75         osg::ref_ptr<osgUtil::TangentSpaceGenerator> tsg = new osgUtil::TangentSpaceGenerator;
76
77         // Generating only tangent vector should be enough
78         // since the binormal is a cross product of normal and tangent
79         // This saves a bit of memory & memory bandwidth!
80         int n = _effect->getGenerator(Effect::TANGENT);
81         tsg->generate(geometry, 0);  // 0 is normal_unit, but I have no idea what that is!
82         if (n != -1 && !geometry->getVertexAttribArray(n))
83             geometry->setVertexAttribData(n, osg::Geometry::ArrayData(tsg->getTangentArray(), osg::Geometry::BIND_PER_VERTEX,GL_FALSE));
84
85         n = _effect->getGenerator(Effect::BINORMAL);
86         if (n != -1 && !geometry->getVertexAttribArray(n))
87             geometry->setVertexAttribData(n, osg::Geometry::ArrayData(tsg->getBinormalArray(), osg::Geometry::BIND_PER_VERTEX,GL_FALSE));
88
89         n = _effect->getGenerator(Effect::NORMAL);
90         if (n != -1 && !geometry->getVertexAttribArray(n))
91             geometry->setVertexAttribData(n, osg::Geometry::ArrayData(tsg->getNormalArray(), osg::Geometry::BIND_PER_VERTEX,GL_FALSE));
92     }
93 }
94
95 bool EffectGeode_writeLocalData(const Object& obj, osgDB::Output& fw)
96 {
97     const EffectGeode& eg = static_cast<const EffectGeode&>(obj);
98
99     if (eg.getEffect()) {
100         fw.indent() << "effect\n";
101         fw.writeObject(*eg.getEffect());
102     }
103
104     return true;
105 }
106
107 namespace
108 {
109 osgDB::RegisterDotOsgWrapperProxy effectGeodeProxy
110 (
111     new EffectGeode,
112     "simgear::EffectGeode",
113     "Object Node Geode simgear::EffectGeode",
114     0,
115     &EffectGeode_writeLocalData
116     );
117 }
118 }