00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- 00002 00003 this file is part of rcssserver3D 00004 Fri May 9 2003 00005 Copyright (C) 2002,2003 Koblenz University 00006 Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group 00007 $Id: transform.cpp,v 1.9 2004/04/30 09:34:44 rollmark Exp $ 00008 00009 This program is free software; you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation; version 2 of the License. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00021 */ 00022 00023 #include "transform.h" 00024 #include "sceneserver.h" 00025 00026 using namespace boost; 00027 using namespace oxygen; 00028 using namespace salt; 00029 using namespace zeitgeist; 00030 00031 Transform::Transform() : 00032 BaseNode() 00033 { 00034 mChangedMark = -1; 00035 mLocalTransform.Identity(); 00036 mOldLocalTransform.Identity(); 00037 mWorldTransform.Identity(); 00038 00039 SetName("transform"); 00040 } 00041 00042 Transform::~Transform() 00043 { 00044 } 00045 00046 int Transform::GetChangedMark() const 00047 { 00048 return mChangedMark; 00049 } 00050 00051 const salt::Matrix& Transform::GetLocalTransform() const 00052 { 00053 return mLocalTransform; 00054 } 00055 00056 const salt::Matrix& Transform::GetOldLocalTransform() const 00057 { 00058 return mOldLocalTransform; 00059 } 00060 00061 const salt::Matrix& Transform::GetWorldTransform() const 00062 { 00063 return mWorldTransform; 00064 } 00065 00066 void Transform::SetLocalTransform(const salt::Matrix &transform) 00067 { 00068 mChangedMark = SceneServer::GetTransformMark(); 00069 mOldLocalTransform = mLocalTransform; 00070 00071 mLocalTransform = transform; 00072 } 00073 00074 void Transform::SetWorldTransform(const salt::Matrix &transform) 00075 { 00076 shared_ptr<BaseNode> parent = shared_static_cast<BaseNode> 00077 (make_shared(mParent)); 00078 00079 if (parent.get() == 0) 00080 { 00081 return; 00082 } 00083 00084 mChangedMark = SceneServer::GetTransformMark(); 00085 mOldLocalTransform = mLocalTransform; 00086 00087 mLocalTransform = transform; 00088 parent->SetWorldTransform(mIdentityMatrix); 00089 } 00090 00091 void Transform::SetLocalPos(const salt::Vector3f &pos) 00092 { 00093 mChangedMark = SceneServer::GetTransformMark(); 00094 mOldLocalTransform = mLocalTransform; 00095 00096 mLocalTransform.Pos() = pos; 00097 UpdateHierarchyInternal(); 00098 } 00099 00100 void Transform::SetLocalRotation(const salt::Vector3f &rot) 00101 { 00102 mChangedMark = SceneServer::GetTransformMark(); 00103 mOldLocalTransform = mLocalTransform; 00104 00105 Vector3f pos = mLocalTransform.Pos(); 00106 mLocalTransform.RotationX(gDegToRad(rot[0])); 00107 mLocalTransform.RotateY(gDegToRad(rot[1])); 00108 mLocalTransform.RotateZ(gDegToRad(rot[2])); 00109 mLocalTransform.Pos() = pos; 00110 00111 UpdateHierarchyInternal(); 00112 } 00113 00114 void Transform::OnLink() 00115 { 00116 UpdateHierarchyInternal(); 00117 } 00118 00119 void Transform::UpdateHierarchyInternal() 00120 { 00121 shared_ptr<BaseNode> parent = shared_static_cast<BaseNode>(make_shared(mParent)); 00122 00123 // no parent, return local transform 00124 if (parent.get() == NULL) 00125 { 00126 mWorldTransform = mLocalTransform; 00127 } else 00128 { 00129 mWorldTransform = parent->GetWorldTransform() * mLocalTransform; 00130 } 00131 }