3 * Copyright (C) 2006-2007 Mathias Froehlich
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 # include <simgear_config.h>
26 #include <osgDB/Registry>
27 #include <osgDB/Input>
28 #include <osgDB/Output>
30 #include "SGOffsetTransform.hxx"
32 SGOffsetTransform::SGOffsetTransform(double scaleFactor) :
33 _scaleFactor(scaleFactor),
34 _rScaleFactor(1/scaleFactor)
38 SGOffsetTransform::SGOffsetTransform(const SGOffsetTransform& offset,
39 const osg::CopyOp& copyop) :
40 osg::Transform(offset, copyop),
41 _scaleFactor(offset._scaleFactor),
42 _rScaleFactor(offset._rScaleFactor)
47 SGOffsetTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
48 osg::NodeVisitor* nv) const
50 if (nv && nv->getVisitorType() == osg::NodeVisitor::CULL_VISITOR) {
51 osg::Vec3 center = nv->getEyePoint();
52 osg::Matrix transform;
53 transform(0,0) = _scaleFactor;
54 transform(1,1) = _scaleFactor;
55 transform(2,2) = _scaleFactor;
56 transform(3,0) = center[0]*(1 - _scaleFactor);
57 transform(3,1) = center[1]*(1 - _scaleFactor);
58 transform(3,2) = center[2]*(1 - _scaleFactor);
59 matrix.preMult(transform);
65 SGOffsetTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
66 osg::NodeVisitor* nv) const
68 if (nv && nv->getVisitorType() == osg::NodeVisitor::CULL_VISITOR) {
69 osg::Vec3 center = nv->getEyePoint();
70 osg::Matrix transform;
71 transform(0,0) = _rScaleFactor;
72 transform(1,1) = _rScaleFactor;
73 transform(2,2) = _rScaleFactor;
74 transform(3,0) = center[0]*(1 - _rScaleFactor);
75 transform(3,1) = center[1]*(1 - _rScaleFactor);
76 transform(3,2) = center[2]*(1 - _rScaleFactor);
77 matrix.postMult(transform);
84 bool OffsetTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
86 SGOffsetTransform& offset = static_cast<SGOffsetTransform&>(obj);
87 if (fr[0].matchWord("scaleFactor")) {
90 if (fr[0].getFloat(scaleFactor))
94 offset.setScaleFactor(scaleFactor);
99 bool OffsetTransform_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
101 const SGOffsetTransform& offset
102 = static_cast<const SGOffsetTransform&>(obj);
103 fw.indent() << "scaleFactor " << offset.getScaleFactor() << std::endl;
109 osgDB::RegisterDotOsgWrapperProxy g_SGOffsetTransformProxy
111 new SGOffsetTransform,
113 "Object Node Transform SGOffsetTransform Group",
114 &OffsetTransform_readLocalData,
115 &OffsetTransform_writeLocalData