Skip to content

Commit

Permalink
UPBGE: Cleanup KX_NodeRelationships.[h/cpp] and KX_BoneParentNodeRela…
Browse files Browse the repository at this point in the history
…tionship.[h/cpp].
  • Loading branch information
panzergame committed Oct 14, 2017
1 parent 2b8f791 commit 6bc01d4
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 435 deletions.
8 changes: 4 additions & 4 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ static void bl_ConvertBlenderObject_Single(BL_BlenderSceneConverter& converter,
SG_Node *parentinversenode = new SG_Node(nullptr, kxscene, callback);

// Define a normal parent relationship for this node.
KX_NormalParentRelation *parent_relation = KX_NormalParentRelation::New();
KX_NormalParentRelation *parent_relation = new KX_NormalParentRelation();
parentinversenode->SetParentRelation(parent_relation);

BL_ParentChildLink pclink;
Expand Down Expand Up @@ -1516,14 +1516,14 @@ void BL_ConvertBlenderObjects(struct Main *maggie,
case PARVERT1:
{
// Create a new vertex parent relationship for this node.
KX_VertexParentRelation *vertex_parent_relation = KX_VertexParentRelation::New();
KX_VertexParentRelation *vertex_parent_relation = new KX_VertexParentRelation();
link.m_gamechildnode->SetParentRelation(vertex_parent_relation);
break;
}
case PARSLOW:
{
// Create a new slow parent relationship for this node.
KX_SlowParentRelation *slow_parent_relation = KX_SlowParentRelation::New(blenderchild->sf);
KX_SlowParentRelation *slow_parent_relation = new KX_SlowParentRelation(blenderchild->sf);
link.m_gamechildnode->SetParentRelation(slow_parent_relation);
break;
}
Expand All @@ -1534,7 +1534,7 @@ void BL_ConvertBlenderObjects(struct Main *maggie,
blenderchild->parsubstr);

if (parent_bone) {
KX_BoneParentRelation *bone_parent_relation = KX_BoneParentRelation::New(parent_bone);
KX_BoneParentRelation *bone_parent_relation = new KX_BoneParentRelation(parent_bone);
link.m_gamechildnode->SetParentRelation(bone_parent_relation);
}

Expand Down
125 changes: 46 additions & 79 deletions source/gameengine/Ketsji/KX_BoneParentNodeRelationship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,129 +29,96 @@
* \ingroup ketsji
*/


#include <iostream>

#include "BLI_utildefines.h"

#include "KX_BoneParentNodeRelationship.h"

#include "MT_Matrix4x4.h"
#include "BL_ArmatureObject.h"

#include "MT_Matrix4x4.h"

/**
* Implementation of classes defined in KX_BoneParentNodeRelationship.h
*/
#include "BLI_utildefines.h"

/**
* first of all KX_BoneParentRelation
*/
KX_BoneParentRelation::KX_BoneParentRelation(Bone *bone)
:m_bone(bone)
{
}

KX_BoneParentRelation *
KX_BoneParentRelation::
New(Bone* bone
) {
return new KX_BoneParentRelation(bone);
KX_BoneParentRelation::~KX_BoneParentRelation()
{
}

bool
KX_BoneParentRelation::
UpdateChildCoordinates(
SG_Node * child,
const SG_Node * parent,
bool& parentUpdated
) {
bool KX_BoneParentRelation::UpdateChildCoordinates(SG_Node *child, const SG_Node *parent, bool& parentUpdated)
{
BLI_assert(child != nullptr);
// This way of accessing child coordinates is a bit cumbersome
// be nice to have non constant reference access to these values.

/* This way of accessing child coordinates is a bit cumbersome
* be nice to have non constant reference access to these values. */

const MT_Vector3 & child_scale = child->GetLocalScale();
const MT_Vector3 & child_pos = child->GetLocalPosition();
const MT_Matrix3x3 & child_rotation = child->GetLocalOrientation();
// we don't know if the armature has been updated or not, assume yes
// We don't know if the armature has been updated or not, assume yes.
parentUpdated = true;

// the childs world locations which we will update.
// The childs world locations which we will update.

MT_Vector3 child_w_scale;
MT_Vector3 child_w_pos;
MT_Matrix3x3 child_w_rotation;

bool valid_parent_transform = false;

if (parent)
{
BL_ArmatureObject *armature = (BL_ArmatureObject*)(parent->GetSGClientObject());
if (armature)
{

if (parent) {
BL_ArmatureObject *armature = (BL_ArmatureObject *)(parent->GetSGClientObject());
if (armature) {
MT_Matrix4x4 parent_matrix;
if (armature->GetBoneMatrix(m_bone, parent_matrix))
{
if (armature->GetBoneMatrix(m_bone, parent_matrix)) {
// Get the child's transform, and the bone matrix.
MT_Matrix4x4 child_transform (
MT_Transform(child_pos + MT_Vector3(0.0f, armature->GetBoneLength(m_bone), 0.0f),
child_rotation.scaled(
child_scale[0],
child_scale[1],
child_scale[2])));
// The child's world transform is parent * child
MT_Matrix4x4 child_transform(
MT_Transform(child_pos + MT_Vector3(0.0f, armature->GetBoneLength(m_bone), 0.0f),
child_rotation.scaled(
child_scale[0],
child_scale[1],
child_scale[2])));

// The child's world transform is parent * child.
parent_matrix = MT_Matrix4x4(parent->GetWorldTransform()) * parent_matrix;
child_transform = parent_matrix * child_transform;

// Recompute the child transform components from the transform.
child_w_scale.setValue(
child_w_scale.setValue(
MT_Vector3(child_transform[0][0], child_transform[0][1], child_transform[0][2]).length(),
MT_Vector3(child_transform[1][0], child_transform[1][1], child_transform[1][2]).length(),
MT_Vector3(child_transform[2][0], child_transform[2][1], child_transform[2][2]).length());
child_w_rotation.setValue(child_transform[0][0], child_transform[0][1], child_transform[0][2],
child_transform[1][0], child_transform[1][1], child_transform[1][2],
child_transform[2][0], child_transform[2][1], child_transform[2][2]);
child_w_rotation.scale(1.0f/child_w_scale[0], 1.0f/child_w_scale[1], 1.0f/child_w_scale[2]);
child_w_rotation.setValue(child_transform[0][0], child_transform[0][1], child_transform[0][2],
child_transform[1][0], child_transform[1][1], child_transform[1][2],
child_transform[2][0], child_transform[2][1], child_transform[2][2]);
child_w_rotation.scale(1.0f / child_w_scale[0], 1.0f / child_w_scale[1], 1.0f / child_w_scale[2]);

child_w_pos = MT_Vector3(child_transform[0][3], child_transform[1][3], child_transform[2][3]);

valid_parent_transform = true;
}
}
}

if (valid_parent_transform)
{
}

if (valid_parent_transform) {
child->SetWorldScale(child_w_scale);
child->SetWorldPosition(child_w_pos);
child->SetWorldOrientation(child_w_rotation);
}
else {
child->SetWorldFromLocalTransform();
}

child->ClearModified();
// this node must always be updated, so reschedule it for next time
// This node must always be updated, so reschedule it for next time.
child->ActivateRecheduleUpdateCallback();
return valid_parent_transform;
}

SG_ParentRelation *
KX_BoneParentRelation::
NewCopy(
) {
KX_BoneParentRelation* bone_parent = new KX_BoneParentRelation(m_bone);
SG_ParentRelation *KX_BoneParentRelation::NewCopy()
{
KX_BoneParentRelation *bone_parent = new KX_BoneParentRelation(m_bone);
return bone_parent;
}

KX_BoneParentRelation::
~KX_BoneParentRelation(
) {
//nothing to do
}


KX_BoneParentRelation::
KX_BoneParentRelation(Bone* bone
)
: m_bone(bone)
{
// nothing to do
}
50 changes: 12 additions & 38 deletions source/gameengine/Ketsji/KX_BoneParentNodeRelationship.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*
*
*/

/** \file KX_BoneParentNodeRelationship.h
Expand All @@ -32,60 +32,34 @@

#ifndef __KX_BONEPARENTNODERELATIONSHIP_H__
#define __KX_BONEPARENTNODERELATIONSHIP_H__

#include "SG_Node.h"

#include "SG_ParentRelation.h"

struct Bone;

/**
* Bone parent relationship parents a child SG_Node frame to a
* Bone parent relationship parents a child SG_Node frame to a
* bone in an armature object.
*/
class KX_BoneParentRelation : public SG_ParentRelation
{
private:
Bone *m_bone;

public :
/**
* Allocate and construct a new KX_BoneParentRelation
* on the heap.
*
* bone is the bone id to use. Currently it is a pointer
* to a Blender struct Bone - this should be fixed if
*/

static
KX_BoneParentRelation *
New(Bone* bone
);
public:
KX_BoneParentRelation(Bone *bone);
virtual ~KX_BoneParentRelation();

/**
* Updates the childs world coordinates relative to the parent's
* world coordinates.
*
* Parent should be a BL_ArmatureObject.
*/
bool
UpdateChildCoordinates(
SG_Node * child,
const SG_Node * parent,
bool& parentUpdated
);

/**
* Create a copy of this relationship
*/
SG_ParentRelation *
NewCopy(
);

~KX_BoneParentRelation(
);
virtual bool UpdateChildCoordinates(SG_Node *child, const SG_Node *parent, bool& parentUpdated);

private :
Bone* m_bone;
KX_BoneParentRelation(Bone* bone
);
/// Create a copy of this relationship.
virtual SG_ParentRelation *NewCopy();
};

#endif
#endif // __KX_BONEPARENTNODERELATIONSHIP_H__
4 changes: 1 addition & 3 deletions source/gameengine/Ketsji/KX_GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ KX_GameObject::KX_GameObject(
m_pSGNode = new SG_Node(this,sgReplicationInfo,callbacks);

// define the relationship between this node and it's parent.

KX_NormalParentRelation * parent_relation =
KX_NormalParentRelation::New();
KX_NormalParentRelation *parent_relation = new KX_NormalParentRelation();
m_pSGNode->SetParentRelation(parent_relation);
};

Expand Down
Loading

0 comments on commit 6bc01d4

Please sign in to comment.