]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGTranslateTransform.cxx
Add preliminary spot light animation
[simgear.git] / simgear / scene / model / SGTranslateTransform.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 <simgear/scene/util/OsgMath.hxx>
31
32 #include "SGTranslateTransform.hxx"
33
34 SGTranslateTransform::SGTranslateTransform() :
35   _axis(0, 0, 0),
36   _value(0)
37 {
38   setReferenceFrame(RELATIVE_RF);
39 }
40
41 SGTranslateTransform::SGTranslateTransform(const SGTranslateTransform& trans,
42                                            const osg::CopyOp& copyop) :
43   osg::Transform(trans, copyop),
44   _axis(trans._axis),
45   _value(trans._value)
46 {
47 }
48
49 bool
50 SGTranslateTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
51                                                 osg::NodeVisitor* nv) const 
52 {
53   if (_referenceFrame == RELATIVE_RF) {
54     matrix.preMultTranslate(toOsg(_value*_axis));
55   } else {
56     matrix.setTrans(toOsg(_value*_axis));
57   }
58   return true;
59 }
60
61 bool
62 SGTranslateTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
63                                                 osg::NodeVisitor* nv) const
64 {
65   if (_referenceFrame == RELATIVE_RF) {
66     matrix.postMultTranslate(toOsg(-_value*_axis));
67   } else {
68     matrix.setTrans(toOsg(-_value*_axis));
69   }
70   return true;
71 }
72
73 osg::BoundingSphere
74 SGTranslateTransform::computeBound() const
75 {
76   osg::BoundingSphere bs = osg::Group::computeBound();
77   bs._center += toOsg(_axis)*_value;
78   return bs;
79 }
80
81 namespace {
82
83 bool TranslateTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
84 {
85     SGTranslateTransform& trans = static_cast<SGTranslateTransform&>(obj);
86
87     if (fr[0].matchWord("axis")) {
88         ++fr;
89         osg::Vec3d axis;
90         if (fr.readSequence(axis))
91             fr += 3;
92         else
93             return false;
94         trans.setAxis(toSG(axis));
95     }
96     if (fr[0].matchWord("value")) {
97         ++fr;
98         double value;
99         if (fr[0].getFloat(value))
100             ++fr;
101         else
102             return false;
103         trans.setValue(value);
104     }
105     return true;
106 }
107
108 bool TranslateTransform_writeLocalData(const osg::Object& obj,
109                                        osgDB::Output& fw)
110 {
111     const SGTranslateTransform& trans
112         = static_cast<const SGTranslateTransform&>(obj);
113     const SGVec3d& axis = trans.getAxis();
114     double value = trans.getValue();
115     
116     fw.indent() << "axis ";
117     for (int i = 0; i < 3; i++)
118         fw << axis(i) << " ";
119     fw << std::endl;
120     fw.indent() << "value " << value << std::endl;
121     return true;
122 }
123
124 }
125
126 osgDB::RegisterDotOsgWrapperProxy g_SGTranslateTransformProxy
127 (
128     new SGTranslateTransform,
129     "SGTranslateTransform",
130     "Object Node Transform SGTranslateTransform Group",
131     &TranslateTransform_readLocalData,
132     &TranslateTransform_writeLocalData
133 );