4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
31 #include "SG_Spatial.h"
32 #include "SG_Controller.h"
33 #include "SG_ParentRelation.h"
43 SG_Callbacks callbacks
46 SG_IObject(clientobj,clientinfo,callbacks),
47 m_localPosition(0.0,0.0,0.0),
48 m_localRotation(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),
49 m_localScaling(1.f,1.f,1.f),
51 m_worldPosition(0.0,0.0,0.0),
52 m_worldRotation(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),
53 m_worldScaling(1.f,1.f,1.f),
55 m_parent_relation (NULL),
57 m_bbox(MT_Point3(-1.0, -1.0, -1.0), MT_Point3(1.0, 1.0, 1.0)),
65 const SG_Spatial& other
68 m_localPosition(other.m_localPosition),
69 m_localRotation(other.m_localRotation),
70 m_localScaling(other.m_localScaling),
72 m_worldPosition(other.m_worldPosition),
73 m_worldRotation(other.m_worldRotation),
74 m_worldScaling(other.m_worldScaling),
76 m_parent_relation(NULL),
79 m_radius(other.m_radius)
81 // duplicate the parent relation for this object
82 m_parent_relation = other.m_parent_relation->NewCopy();
88 delete (m_parent_relation);
95 return m_parent_relation;
101 SG_ParentRelation *relation
103 delete (m_parent_relation);
104 m_parent_relation = relation;
110 * Update Spatial Data.
111 * Calculates WorldTransform., (either doing itsself or using the linked SGControllers)
118 const SG_Spatial *parent,
123 bool bComputesWorldTransform = false;
125 // update spatial controllers
127 SGControllerList::iterator cit = GetSGControllerList().begin();
128 SGControllerList::const_iterator c_end = GetSGControllerList().end();
130 for (;cit!=c_end;++cit)
132 if ((*cit)->Update(time))
133 bComputesWorldTransform = true;
136 // If none of the objects updated our values then we ask the
137 // parent_relation object owned by this class to update
138 // our world coordinates.
140 if (!bComputesWorldTransform)
141 bComputesWorldTransform = ComputeWorldTransforms(parent, parentUpdated);
143 return bComputesWorldTransform;
146 bool SG_Spatial::ComputeWorldTransforms(const SG_Spatial *parent, bool& parentUpdated)
148 return m_parent_relation->UpdateChildCoordinates(this,parent,parentUpdated);
152 * Position and translation methods
159 const MT_Vector3& trans,
160 const SG_Spatial *parent,
164 m_localPosition += m_localRotation * trans;
167 m_localPosition += trans * parent->GetWorldOrientation();
169 m_localPosition += trans;
178 const MT_Point3& trans
180 m_localPosition = trans;
187 const MT_Point3& trans
189 m_worldPosition = trans;
199 const MT_Vector3& scale
201 m_localScaling = m_localScaling * scale;
208 const MT_Vector3& scale
210 m_localScaling = scale;
218 const MT_Vector3& scale
220 m_worldScaling = scale;
224 * Orientation and rotation methods.
231 const MT_Matrix3x3& rot,
234 m_localRotation = m_localRotation * (
238 (GetWorldOrientation().inverse() * rot * GetWorldOrientation()));
244 SetLocalOrientation(const MT_Matrix3x3& rot)
246 m_localRotation = rot;
255 const MT_Matrix3x3& rot
257 m_worldRotation = rot;
265 return m_localPosition;
273 return m_localRotation;
281 return m_localScaling;
290 return m_worldPosition;
298 return m_worldRotation;
306 return m_worldScaling;
309 void SG_Spatial::SetWorldFromLocalTransform()
311 m_worldPosition= m_localPosition;
312 m_worldScaling= m_localScaling;
313 m_worldRotation= m_localRotation;
316 SG_BBox& SG_Spatial::BBox()
321 void SG_Spatial::SetBBox(SG_BBox& bbox)
326 MT_Transform SG_Spatial::GetWorldTransform() const
328 return MT_Transform(m_worldPosition,
329 m_worldRotation.scaled(
330 m_worldScaling[0], m_worldScaling[1], m_worldScaling[2]));
333 bool SG_Spatial::inside(const MT_Point3 &point) const
335 MT_Scalar radius = m_worldScaling[m_worldScaling.closestAxis()]*m_radius;
336 return (m_worldPosition.distance2(point) <= radius*radius) ?
337 m_bbox.transform(GetWorldTransform()).inside(point) :
341 void SG_Spatial::getBBox(MT_Point3 *box) const
343 m_bbox.get(box, GetWorldTransform());
346 void SG_Spatial::getAABBox(MT_Point3 *box) const
348 m_bbox.getaa(box, GetWorldTransform());