]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/placementtrans.cxx
Modified Files:
[simgear.git] / simgear / scene / model / placementtrans.cxx
1 // placementtrans.hxx -- class for carrying transforms for placing models in the world
2 //
3 // Written by Mathias Froehlich, started April 2005.
4 //
5 // Copyright (C) 2005 Mathias Froehlich
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21
22
23 #ifndef __cplusplus
24 # error This library requires C++
25 #endif
26
27 #include <simgear/compiler.h>
28 #include <simgear/constants.h>
29
30 #include "placementtrans.hxx"
31
32 SGPlacementTransform::SGPlacementTransform(void) :
33   _placement_offset(0, 0, 0),
34   _scenery_center(0, 0, 0),
35   _rotation(1, 0, 0, 0,
36             0, 1, 0, 0,
37             0, 0, 1, 0,
38             0, 0, 0, 1)
39 {
40 }
41
42 SGPlacementTransform::~SGPlacementTransform(void)
43 {
44 }
45
46 bool
47 SGPlacementTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
48                                                 osg::NodeVisitor*) const
49 {
50   osg::Matrix t;
51   for (int i = 0; i < 3; ++i) {
52     for (int j = 0; j < 3; ++j) {
53       t(j, i) = _rotation(i, j);
54     }
55     t(3, i) = _placement_offset(i) - _scenery_center(i);
56   }
57   
58   if (_referenceFrame == RELATIVE_RF)
59     matrix.preMult(t);
60   else
61     matrix = t;
62   return true;
63 }
64
65 bool
66 SGPlacementTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
67                                                 osg::NodeVisitor*) const
68 {
69   osg::Matrix t;
70   for (int i = 0; i < 3; ++i) {
71     for (int j = 0; j < 3; ++j) {
72       t(j, i) = _rotation(i, j);
73     }
74     t(3, i) = _placement_offset(i) - _scenery_center(i);
75   }
76   t = osg::Matrix::inverse(t);
77
78   if (_referenceFrame == RELATIVE_RF)
79     matrix.postMult(t);
80   else
81     matrix = t;
82   return true;
83 }