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 "SGScaleTransform.hxx"
32 SGScaleTransform::SGScaleTransform() :
34 _scaleFactor(1, 1, 1),
37 setReferenceFrame(RELATIVE_RF);
38 // see osg::Transform doc: If the transformation matrix scales the subgraph
39 // then the normals of the underlying geometry will need to be renormalized
40 // to be unit vectors once more.
41 osg::StateSet* stateset = getOrCreateStateSet();
42 stateset->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
45 SGScaleTransform::SGScaleTransform(const SGScaleTransform& scale,
46 const osg::CopyOp& copyop) :
47 osg::Transform(scale, copyop),
48 _center(scale._center),
49 _scaleFactor(scale._scaleFactor),
50 _boundScale(scale._boundScale)
55 SGScaleTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
56 osg::NodeVisitor* nv) const
58 osg::Matrix transform;
59 transform(0,0) = _scaleFactor[0];
60 transform(1,1) = _scaleFactor[1];
61 transform(2,2) = _scaleFactor[2];
62 transform(3,0) = _center[0]*(1 - _scaleFactor[0]);
63 transform(3,1) = _center[1]*(1 - _scaleFactor[1]);
64 transform(3,2) = _center[2]*(1 - _scaleFactor[2]);
65 if (_referenceFrame == RELATIVE_RF)
66 matrix.preMult(transform);
73 SGScaleTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
74 osg::NodeVisitor* nv) const
76 if (fabs(_scaleFactor[0]) < SGLimitsd::min())
78 if (fabs(_scaleFactor[1]) < SGLimitsd::min())
80 if (fabs(_scaleFactor[2]) < SGLimitsd::min())
82 SGVec3d rScaleFactor(1/_scaleFactor[0],
85 osg::Matrix transform;
86 transform(0,0) = rScaleFactor[0];
87 transform(1,1) = rScaleFactor[1];
88 transform(2,2) = rScaleFactor[2];
89 transform(3,0) = _center[0]*(1 - rScaleFactor[0]);
90 transform(3,1) = _center[1]*(1 - rScaleFactor[1]);
91 transform(3,2) = _center[2]*(1 - rScaleFactor[2]);
92 if (_referenceFrame == RELATIVE_RF)
93 matrix.postMult(transform);
100 SGScaleTransform::computeBound() const
102 osg::BoundingSphere bs = osg::Group::computeBound();
103 _boundScale = normI(_scaleFactor);
104 bs.radius() *= _boundScale;
110 bool ScaleTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
112 SGScaleTransform& scale = static_cast<SGScaleTransform&>(obj);
113 if (fr[0].matchWord("center")) {
116 if (fr.readSequence(center))
120 scale.setCenter(toSG(center));
122 if (fr[0].matchWord("scaleFactor")) {
124 osg::Vec3d scaleFactor;
125 if (fr.readSequence(scaleFactor))
129 scale.setScaleFactor(toSG(scaleFactor));
134 bool ScaleTransform_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
136 const SGScaleTransform& scale = static_cast<const SGScaleTransform&>(obj);
137 const SGVec3d& center = scale.getCenter();
138 const SGVec3d& scaleFactor = scale.getScaleFactor();
139 int prec = fw.precision();
141 fw.indent() << "center ";
142 for (int i = 0; i < 3; i++)
143 fw << center(i) << " ";
146 fw.indent() << "scaleFactor ";
147 for (int i = 0; i < 3; i++)
148 fw << scaleFactor(i) << " ";
155 osgDB::RegisterDotOsgWrapperProxy g_ScaleTransformProxy
157 new SGScaleTransform,
159 "Object Node Transform SGScaleTransform Group",
160 &ScaleTransform_readLocalData,
161 &ScaleTransform_writeLocalData