]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGScaleTransform.cxx
Merge branch 'next' of git.gitorious.org:fg/simgear into next
[simgear.git] / simgear / scene / model / SGScaleTransform.cxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2006-2007 Mathias Froehlich 
4  *
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.
9  *
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.
14  *
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,
18  * MA 02110-1301, USA.
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include <simgear_config.h>
24 #endif
25
26 #include <osgDB/Registry>
27 #include <osgDB/Input>
28 #include <osgDB/Output>
29
30 #include "SGScaleTransform.hxx"
31
32 SGScaleTransform::SGScaleTransform() :
33   _center(0, 0, 0),
34   _scaleFactor(1, 1, 1),
35   _boundScale(1)
36 {
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);
43 }
44
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)
51 {
52 }
53
54 bool
55 SGScaleTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
56                                             osg::NodeVisitor* nv) const
57 {
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);
67   else
68     matrix = transform;
69   return true;
70 }
71
72 bool
73 SGScaleTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
74                                             osg::NodeVisitor* nv) const
75 {
76   if (fabs(_scaleFactor[0]) < SGLimitsd::min())
77     return false;
78   if (fabs(_scaleFactor[1]) < SGLimitsd::min())
79     return false;
80   if (fabs(_scaleFactor[2]) < SGLimitsd::min())
81     return false;
82   SGVec3d rScaleFactor(1/_scaleFactor[0],
83                        1/_scaleFactor[1],
84                        1/_scaleFactor[2]);
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);
94   else
95     matrix = transform;
96   return true;
97 }
98
99 osg::BoundingSphere
100 SGScaleTransform::computeBound() const
101 {
102   osg::BoundingSphere bs = osg::Group::computeBound();
103   _boundScale = normI(_scaleFactor);
104   bs.radius() *= _boundScale;
105   return bs;
106 }
107
108 namespace {
109
110 bool ScaleTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
111 {
112     SGScaleTransform& scale = static_cast<SGScaleTransform&>(obj);
113     if (fr[0].matchWord("center")) {
114         ++fr;
115         osg::Vec3d center;
116         if (fr.readSequence(center))
117             fr += 3;
118         else
119             return false;
120         scale.setCenter(toSG(center));
121     }
122     if (fr[0].matchWord("scaleFactor")) {
123         ++fr;
124         osg::Vec3d scaleFactor;
125         if (fr.readSequence(scaleFactor))
126             fr += 3;
127         else
128             return false;
129         scale.setScaleFactor(toSG(scaleFactor));
130     }
131     return true;
132 }
133
134 bool ScaleTransform_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
135 {
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();
140     fw.precision(15);
141     fw.indent() << "center ";
142     for (int i = 0; i < 3; i++)
143         fw << center(i) << " ";
144     fw << std::endl;
145     fw.precision(prec);
146     fw.indent() << "scaleFactor ";
147     for (int i = 0; i < 3; i++)
148         fw << scaleFactor(i) << " ";
149     fw << std::endl;
150     return true;
151 }
152
153 }
154
155 osgDB::RegisterDotOsgWrapperProxy g_ScaleTransformProxy
156 (
157     new SGScaleTransform,
158     "SGScaleTransform",
159     "Object Node Transform SGScaleTransform Group",
160     &ScaleTransform_readLocalData,
161     &ScaleTransform_writeLocalData
162 );