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 <osg/Version>
27 #include <osgUtil/CullVisitor>
28 #include <osgUtil/TangentSpaceGenerator>
30 #include <osgDB/Registry>
31 #include <osgDB/Input>
32 #include <osgDB/ParameterOutput>
38 using namespace osgUtil;
40 EffectGeode::EffectGeode()
44 EffectGeode::EffectGeode(const EffectGeode& rhs, const osg::CopyOp& copyop) :
46 _effect(static_cast<Effect*>(copyop(rhs._effect.get())))
50 void EffectGeode::setEffect(Effect* effect)
55 addUpdateCallback(new Effect::InitializeCallback);
58 void EffectGeode::resizeGLObjectBuffers(unsigned int maxSize)
61 _effect->resizeGLObjectBuffers(maxSize);
62 Geode::resizeGLObjectBuffers(maxSize);
65 void EffectGeode::releaseGLObjects(osg::State* state) const
68 _effect->releaseGLObjects(state);
69 Geode::releaseGLObjects(state);
72 // Generates tangent space vectors or other data from geom, as defined by effect
73 void EffectGeode::runGenerators(osg::Geometry *geometry)
75 if(geometry && _effect.valid()) {
76 // Generate tangent vectors for the geometry
77 osg::ref_ptr<osgUtil::TangentSpaceGenerator> tsg = new osgUtil::TangentSpaceGenerator;
79 // Generating only tangent vector should be enough
80 // since the binormal is a cross product of normal and tangent
81 // This saves a bit of memory & memory bandwidth!
82 int n = _effect->getGenerator(Effect::TANGENT);
83 tsg->generate(geometry, 0); // 0 is normal_unit, but I have no idea what that is!
84 if (n != -1 && !geometry->getVertexAttribArray(n))
85 #if OSG_MIN_VERSION_REQUIRED(3,1,8)
86 geometry->setVertexAttribArray(n, tsg->getTangentArray(), osg::Array::BIND_PER_VERTEX);
88 geometry->setVertexAttribData(n, osg::Geometry::ArrayData(tsg->getTangentArray(), osg::Geometry::BIND_PER_VERTEX,GL_FALSE));
91 n = _effect->getGenerator(Effect::BINORMAL);
92 if (n != -1 && !geometry->getVertexAttribArray(n))
93 #if OSG_MIN_VERSION_REQUIRED(3,1,8)
94 geometry->setVertexAttribArray(n, tsg->getBinormalArray(), osg::Array::BIND_PER_VERTEX);
96 geometry->setVertexAttribData(n, osg::Geometry::ArrayData(tsg->getBinormalArray(), osg::Geometry::BIND_PER_VERTEX,GL_FALSE));
99 n = _effect->getGenerator(Effect::NORMAL);
100 if (n != -1 && !geometry->getVertexAttribArray(n))
101 #if OSG_MIN_VERSION_REQUIRED(3,1,8)
102 geometry->setVertexAttribArray(n, tsg->getNormalArray(), osg::Array::BIND_PER_VERTEX);
104 geometry->setVertexAttribData(n, osg::Geometry::ArrayData(tsg->getNormalArray(), osg::Geometry::BIND_PER_VERTEX,GL_FALSE));
109 bool EffectGeode_writeLocalData(const Object& obj, osgDB::Output& fw)
111 const EffectGeode& eg = static_cast<const EffectGeode&>(obj);
113 if (eg.getEffect()) {
114 fw.indent() << "effect\n";
115 fw.writeObject(*eg.getEffect());
123 osgDB::RegisterDotOsgWrapperProxy effectGeodeProxy
126 "simgear::EffectGeode",
127 "Object Node Geode simgear::EffectGeode",
129 &EffectGeode_writeLocalData