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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 *****
30 /** \file SG_Spatial.h
34 #ifndef __SG_SPATIAL_H
35 #define __SG_SPATIAL_H
37 #include <MT_Vector3.h>
38 #include <MT_Point3.h>
39 #include <MT_Matrix3x3.h> // or Quaternion later ?
40 #include "SG_IObject.h"
42 #include "SG_ParentRelation.h"
46 class SG_ParentRelation;
49 * SG_Spatial contains spatial information (local & world position, rotation
50 * and scaling) for a Scene graph node.
51 * It also contains a link to the node's parent.
53 class SG_Spatial : public SG_IObject
57 MT_Point3 m_localPosition;
58 MT_Matrix3x3 m_localRotation;
59 MT_Vector3 m_localScaling;
61 MT_Point3 m_worldPosition;
62 MT_Matrix3x3 m_worldRotation;
63 MT_Vector3 m_worldScaling;
65 SG_ParentRelation * m_parent_relation;
70 bool m_ogldirty; // true if the openGL matrix for this object must be recomputed
73 inline void ClearModified()
78 inline void SetModified()
81 ActivateScheduleUpdateCallback();
83 inline void ClearDirty()
88 * Define the realtionship this node has with it's parent
89 * node. You should pass an unshared instance of an SG_ParentRelation
90 * allocated on the heap to this method. Ownership of this
91 * instance is assumed by this class.
92 * You may call this function several times in the lifetime
93 * of a node to change the relationship dynamically.
94 * You must call this method before the first call to UpdateSpatialData().
95 * An assertion willl be fired at run-time in debug if this is not
97 * The relation is activated only if no controllers of this object
98 * updated the coordinates of the child.
103 SG_ParentRelation *relation
106 SG_ParentRelation * GetParentRelation()
108 return m_parent_relation;
115 * Apply a translation relative to the current position.
116 * if local then the translation is assumed to be in the
117 * local coordinates of this object. If not then the translation
118 * is assumed to be in global coordinates. In this case
119 * you must provide a pointer to the parent of this object if it
120 * exists otherwise if there is no parent set it to NULL
125 const MT_Vector3& trans,
126 const SG_Spatial *parent,
130 void SetLocalPosition(const MT_Point3& trans)
132 m_localPosition = trans;
136 void SetWorldPosition(const MT_Point3& trans)
138 m_worldPosition = trans;
144 const MT_Matrix3x3& rot,
148 void SetLocalOrientation(const MT_Matrix3x3& rot)
150 m_localRotation = rot;
154 // rot is arrange like openGL matrix
155 void SetLocalOrientation(const float* rot)
157 m_localRotation.setValue(rot);
161 void SetWorldOrientation(const MT_Matrix3x3& rot)
163 m_worldRotation = rot;
166 void RelativeScale(const MT_Vector3& scale)
168 m_localScaling = m_localScaling * scale;
172 void SetLocalScale(const MT_Vector3& scale)
174 m_localScaling = scale;
178 void SetWorldScale(const MT_Vector3& scale)
180 m_worldScaling = scale;
183 const MT_Point3& GetLocalPosition() const
185 return m_localPosition;
188 const MT_Matrix3x3& GetLocalOrientation() const
190 return m_localRotation;
193 const MT_Vector3& GetLocalScale() const
195 return m_localScaling;
198 const MT_Point3& GetWorldPosition() const
200 return m_worldPosition;
203 const MT_Matrix3x3& GetWorldOrientation() const
205 return m_worldRotation;
208 const MT_Vector3& GetWorldScaling() const
210 return m_worldScaling;
213 void SetWorldFromLocalTransform()
215 m_worldPosition= m_localPosition;
216 m_worldScaling= m_localScaling;
217 m_worldRotation= m_localRotation;
222 MT_Transform GetWorldTransform() const;
224 bool ComputeWorldTransforms(const SG_Spatial *parent, bool& parentUpdated)
226 return m_parent_relation->UpdateChildCoordinates(this,parent,parentUpdated);
231 * Bounding box functions.
238 void SetBBox(SG_BBox& bbox)
244 bool inside(const MT_Point3 &point) const;
245 void getBBox(MT_Point3 *box) const;
246 void getAABBox(MT_Point3 *box) const;
248 MT_Scalar Radius() const { return m_radius; }
249 void SetRadius(MT_Scalar radius) { m_radius = radius; }
250 bool IsModified() { return m_modified; }
251 bool IsDirty() { return m_ogldirty; }
254 friend class SG_Controller;
255 friend class KX_BoneParentRelation;
256 friend class KX_VertexParentRelation;
257 friend class KX_SlowParentRelation;
258 friend class KX_NormalParentRelation;
261 * Protected constructor this class is not
262 * designed for direct instantiation
268 SG_Callbacks& callbacks
272 const SG_Spatial& other
276 virtual ~SG_Spatial();
279 * Update the world coordinates of this spatial node. This also informs
280 * any controllers to update this object.
285 const SG_Spatial *parent,
291 #ifdef WITH_CXX_GUARDEDALLOC
293 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:SG_Spatial"); }
294 void operator delete( void *mem ) { MEM_freeN(mem); }
298 #endif //__SG_SPATIAL_H