Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 * 00018 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00019 * All rights reserved. 00020 * 00021 * The Original Code is: all of this file. 00022 * 00023 * Contributor(s): none yet. 00024 * 00025 * ***** END GPL LICENSE BLOCK ***** 00026 */ 00027 00033 #include "SG_Node.h" 00034 #include "SG_Spatial.h" 00035 #include "SG_Controller.h" 00036 #include "SG_ParentRelation.h" 00037 00038 SG_Spatial:: 00039 SG_Spatial( 00040 void* clientobj, 00041 void* clientinfo, 00042 SG_Callbacks& callbacks 00043 ): 00044 00045 SG_IObject(clientobj,clientinfo,callbacks), 00046 m_localPosition(0.0,0.0,0.0), 00047 m_localRotation(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0), 00048 m_localScaling(1.f,1.f,1.f), 00049 00050 m_worldPosition(0.0,0.0,0.0), 00051 m_worldRotation(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0), 00052 m_worldScaling(1.f,1.f,1.f), 00053 00054 m_parent_relation (NULL), 00055 00056 m_bbox(MT_Point3(-1.0, -1.0, -1.0), MT_Point3(1.0, 1.0, 1.0)), 00057 m_radius(1.0), 00058 m_modified(false), 00059 m_ogldirty(false) 00060 { 00061 } 00062 00063 SG_Spatial:: 00064 SG_Spatial( 00065 const SG_Spatial& other 00066 ) : 00067 SG_IObject(other), 00068 m_localPosition(other.m_localPosition), 00069 m_localRotation(other.m_localRotation), 00070 m_localScaling(other.m_localScaling), 00071 00072 m_worldPosition(other.m_worldPosition), 00073 m_worldRotation(other.m_worldRotation), 00074 m_worldScaling(other.m_worldScaling), 00075 00076 m_parent_relation(NULL), 00077 00078 m_bbox(other.m_bbox), 00079 m_radius(other.m_radius), 00080 m_modified(false), 00081 m_ogldirty(false) 00082 { 00083 // duplicate the parent relation for this object 00084 m_parent_relation = other.m_parent_relation->NewCopy(); 00085 } 00086 00087 SG_Spatial:: 00088 ~SG_Spatial() 00089 { 00090 delete (m_parent_relation); 00091 } 00092 00093 void 00094 SG_Spatial:: 00095 SetParentRelation( 00096 SG_ParentRelation *relation 00097 ){ 00098 delete (m_parent_relation); 00099 m_parent_relation = relation; 00100 SetModified(); 00101 } 00102 00103 00110 bool 00111 SG_Spatial:: 00112 UpdateSpatialData( 00113 const SG_Spatial *parent, 00114 double time, 00115 bool& parentUpdated 00116 ){ 00117 bool bComputesWorldTransform = false; 00118 00119 // update spatial controllers 00120 00121 SGControllerList::iterator cit = GetSGControllerList().begin(); 00122 SGControllerList::const_iterator c_end = GetSGControllerList().end(); 00123 00124 for (;cit!=c_end;++cit) 00125 { 00126 if ((*cit)->Update(time)) 00127 bComputesWorldTransform = true; 00128 } 00129 00130 // If none of the objects updated our values then we ask the 00131 // parent_relation object owned by this class to update 00132 // our world coordinates. 00133 00134 if (!bComputesWorldTransform) 00135 bComputesWorldTransform = ComputeWorldTransforms(parent, parentUpdated); 00136 00137 return bComputesWorldTransform; 00138 } 00139 00145 void 00146 SG_Spatial:: 00147 RelativeTranslate( 00148 const MT_Vector3& trans, 00149 const SG_Spatial *parent, 00150 bool local 00151 ){ 00152 if (local) { 00153 m_localPosition += m_localRotation * trans; 00154 } else { 00155 if (parent) { 00156 m_localPosition += trans * parent->GetWorldOrientation(); 00157 } else { 00158 m_localPosition += trans; 00159 } 00160 } 00161 SetModified(); 00162 } 00163 00164 00175 void 00176 SG_Spatial:: 00177 RelativeRotate( 00178 const MT_Matrix3x3& rot, 00179 bool local 00180 ){ 00181 m_localRotation = m_localRotation * ( 00182 local ? 00183 rot 00184 : 00185 (GetWorldOrientation().inverse() * rot * GetWorldOrientation())); 00186 SetModified(); 00187 } 00188 00189 00190 00191 MT_Transform SG_Spatial::GetWorldTransform() const 00192 { 00193 return MT_Transform(m_worldPosition, 00194 m_worldRotation.scaled( 00195 m_worldScaling[0], m_worldScaling[1], m_worldScaling[2])); 00196 } 00197 00198 bool SG_Spatial::inside(const MT_Point3 &point) const 00199 { 00200 MT_Scalar radius = m_worldScaling[m_worldScaling.closestAxis()]*m_radius; 00201 return (m_worldPosition.distance2(point) <= radius*radius) ? 00202 m_bbox.transform(GetWorldTransform()).inside(point) : 00203 false; 00204 } 00205 00206 void SG_Spatial::getBBox(MT_Point3 *box) const 00207 { 00208 m_bbox.get(box, GetWorldTransform()); 00209 } 00210 00211 void SG_Spatial::getAABBox(MT_Point3 *box) const 00212 { 00213 m_bbox.getaa(box, GetWorldTransform()); 00214 } 00215