~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/gameengine/Ketsji/KX_SG_BoneParentNodeRelationship.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2005-11-06 12:40:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051106124003-3pgs7tcg5rox96xg
Tags: 2.37a-1.1
* Non-maintainer upload.
* Split out parts of 01_SConstruct_debian.dpatch again: root_build_dir
  really needs to get adjusted before the clean target runs - closes: #333958,
  see #288882 for reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * $Id: KX_SG_BoneParentNodeRelationship.cpp,v 1.1 2005/04/23 11:36:44 kester Exp $
 
3
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version. The Blender
 
9
 * Foundation also sells licenses for use in proprietary software under
 
10
 * the Blender License.  See http://www.blender.org/BL/ for information
 
11
 * about this.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software Foundation,
 
20
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
21
 *
 
22
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 
23
 * All rights reserved.
 
24
 *
 
25
 * The Original Code is: all of this file.
 
26
 *
 
27
 * Contributor(s): none yet.
 
28
 *
 
29
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 
30
 */
 
31
 
 
32
#include <iostream>
 
33
 
 
34
#include "KX_SG_BoneParentNodeRelationship.h"
 
35
 
 
36
#include "MT_Matrix4x4.h"
 
37
#include "BL_ArmatureObject.h"
 
38
 
 
39
 
 
40
#ifdef HAVE_CONFIG_H
 
41
#include <config.h>
 
42
#endif
 
43
 
 
44
/**
 
45
 * Implementation of classes defined in KX_SG_BoneParentNodeRelationship.h
 
46
 */
 
47
 
 
48
/** 
 
49
 * first of all KX_SG_BoneParentRelation
 
50
 */
 
51
 
 
52
        KX_BoneParentRelation *
 
53
KX_BoneParentRelation::
 
54
New(Bone* bone
 
55
) {
 
56
        return new KX_BoneParentRelation(bone);
 
57
}               
 
58
 
 
59
        bool
 
60
KX_BoneParentRelation::
 
61
UpdateChildCoordinates(
 
62
        SG_Spatial * child,
 
63
        const SG_Spatial * parent
 
64
){
 
65
        MT_assert(child != NULL);
 
66
        
 
67
        // This way of accessing child coordinates is a bit cumbersome
 
68
        // be nice to have non constant reference access to these values.
 
69
 
 
70
        const MT_Vector3 & child_scale = child->GetLocalScale();
 
71
        const MT_Point3 & child_pos = child->GetLocalPosition();
 
72
        const MT_Matrix3x3 & child_rotation = child->GetLocalOrientation();
 
73
 
 
74
        // the childs world locations which we will update.     
 
75
        
 
76
        MT_Vector3 child_w_scale;
 
77
        MT_Point3 child_w_pos;
 
78
        MT_Matrix3x3 child_w_rotation;
 
79
        
 
80
        bool valid_parent_transform = false;
 
81
        
 
82
        if (parent)
 
83
        {
 
84
                const BL_ArmatureObject *armature = (const BL_ArmatureObject*)(parent->GetSGClientObject());
 
85
                if (armature)
 
86
                {
 
87
                        MT_Matrix4x4 parent_matrix;
 
88
                        if (armature->GetBoneMatrix(m_bone, parent_matrix))
 
89
                        {
 
90
                                // Get the child's transform, and the bone matrix.
 
91
                                MT_Matrix4x4 child_transform ( 
 
92
                                        MT_Transform(child_pos + MT_Vector3(0.0, armature->GetBoneLength(m_bone), 0.0), 
 
93
                                                child_rotation.scaled(
 
94
                                                        child_scale[0], 
 
95
                                                        child_scale[1], 
 
96
                                                        child_scale[2])));
 
97
                                
 
98
                                // The child's world transform is parent * child
 
99
                                parent_matrix = parent->GetWorldTransform() * parent_matrix;
 
100
                                child_transform = parent_matrix * child_transform;
 
101
                                
 
102
                                // Recompute the child transform components from the transform.
 
103
                                child_w_scale =  MT_Vector3( 
 
104
                                        MT_Vector3(child_transform[0][0], child_transform[0][1], child_transform[0][2]).length(),
 
105
                                        MT_Vector3(child_transform[1][0], child_transform[1][1], child_transform[1][2]).length(),
 
106
                                        MT_Vector3(child_transform[2][0], child_transform[2][1], child_transform[2][2]).length());
 
107
                                child_w_rotation = MT_Matrix3x3(child_transform[0][0], child_transform[0][1], child_transform[0][2], 
 
108
                                        child_transform[1][0], child_transform[1][1], child_transform[1][2], 
 
109
                                        child_transform[2][0], child_transform[2][1], child_transform[2][2]);
 
110
                                child_w_rotation.scale(1.0/child_w_scale[0], 1.0/child_w_scale[1], 1.0/child_w_scale[2]);
 
111
                                        
 
112
                                child_w_pos = MT_Point3(child_transform[0][3], child_transform[1][3], child_transform[2][3]);
 
113
                                        
 
114
                                valid_parent_transform = true;
 
115
                        }
 
116
                }
 
117
        } 
 
118
        
 
119
        if (!valid_parent_transform)
 
120
        {
 
121
                child_w_scale = child_scale;
 
122
                child_w_pos = child_pos;
 
123
                child_w_rotation = child_rotation;
 
124
        }
 
125
 
 
126
        child->SetWorldScale(child_w_scale);
 
127
        child->SetWorldPosition(child_w_pos);
 
128
        child->SetWorldOrientation(child_w_rotation);
 
129
        
 
130
        return valid_parent_transform;
 
131
}
 
132
 
 
133
        SG_ParentRelation *
 
134
KX_BoneParentRelation::
 
135
NewCopy(
 
136
){
 
137
        KX_BoneParentRelation* bone_parent = new KX_BoneParentRelation(m_bone);
 
138
        return bone_parent;
 
139
}
 
140
 
 
141
KX_BoneParentRelation::
 
142
~KX_BoneParentRelation(
 
143
){
 
144
        //nothing to do
 
145
}
 
146
 
 
147
 
 
148
KX_BoneParentRelation::
 
149
KX_BoneParentRelation(Bone* bone
 
150
)
 
151
: m_bone(bone)
 
152
{
 
153
        // nothing to do
 
154
}