1 // Copyright (C) 2008 Timothy Moore timoore@redhat.com
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.
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.
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.
18 # include <simgear_config.h>
21 #include "EffectGeode.hxx"
23 #include "Technique.hxx"
25 #include <osgUtil/CullVisitor>
26 #include <osgUtil/TangentSpaceGenerator>
28 #include <osgDB/Registry>
29 #include <osgDB/Input>
30 #include <osgDB/ParameterOutput>
36 using namespace osgUtil;
38 EffectGeode::EffectGeode()
42 EffectGeode::EffectGeode(const EffectGeode& rhs, const osg::CopyOp& copyop) :
44 _effect(static_cast<Effect*>(copyop(rhs._effect.get())))
48 void EffectGeode::setEffect(Effect* effect)
53 addUpdateCallback(new Effect::InitializeCallback);
56 void EffectGeode::resizeGLObjectBuffers(unsigned int maxSize)
59 _effect->resizeGLObjectBuffers(maxSize);
60 Geode::resizeGLObjectBuffers(maxSize);
63 void EffectGeode::releaseGLObjects(osg::State* state) const
66 _effect->releaseGLObjects(state);
67 Geode::releaseGLObjects(state);
70 // Generates tangent space vectors or other data from geom, as defined by effect
71 void EffectGeode::runGenerators(osg::Geometry *geometry)
73 if(geometry && _effect.valid()) {
74 // Generate tangent vectors for the geometry
75 osg::ref_ptr<osgUtil::TangentSpaceGenerator> tsg = new osgUtil::TangentSpaceGenerator;
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));
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));
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));
95 bool EffectGeode_writeLocalData(const Object& obj, osgDB::Output& fw)
97 const EffectGeode& eg = static_cast<const EffectGeode&>(obj);
99 fw.indent() << "effect\n";
100 fw.writeObject(*eg.getEffect());
106 osgDB::RegisterDotOsgWrapperProxy effectGeodeProxy
109 "simgear::EffectGeode",
110 "Object Node Geode simgear::EffectGeode",
112 &EffectGeode_writeLocalData