Skip to content

Commit

Permalink
UPBGE: Use BL_ConvertObjectInfo to store object conversion data.
Browse files Browse the repository at this point in the history
Previously the game objects were all storing a constraint list used for
the creation of physics constraint from user settings, but once the object
created, this list has no interest and over all it can be shared between
duplicated objects.

To solve this situation a BL_ConvertObjectInfo struct is introduced, this struct
store a pointer to the blender object which was converted and a list of the
blender constraints. These convert info are created in BL_BlenderSceneConverter
and kept in BL_BlenderConverter after conversion, as promissed they are shared
between game objects under a pointer member: m_convertInfo.

Getter GetBlenderObject and GetConstraints are rewritten to return data from
the convert info.

A special case is dedicated in GetBlenderObject to default camera which doesn't
have any convert info.
  • Loading branch information
panzergame committed Dec 17, 2017
1 parent f47a2dc commit fe801b4
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 52 deletions.
7 changes: 7 additions & 0 deletions source/gameengine/Converter/BL_BlenderConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "BL_BlenderConverter.h"
#include "BL_BlenderSceneConverter.h"
#include "BL_BlenderDataConversion.h"
#include "BL_ConvertObjectInfo.h"
#include "BL_ActionActuator.h"
#include "KX_BlenderMaterial.h"

Expand Down Expand Up @@ -106,6 +107,9 @@ void BL_BlenderConverter::SceneSlot::Merge(BL_BlenderConverter::SceneSlot& other
m_meshobjects.insert(m_meshobjects.begin(),
std::make_move_iterator(other.m_meshobjects.begin()),
std::make_move_iterator(other.m_meshobjects.end()));
m_objectInfos.insert(m_objectInfos.begin(),
std::make_move_iterator(other.m_objectInfos.begin()),
std::make_move_iterator(other.m_objectInfos.end()));
m_actionToInterp.insert(other.m_actionToInterp.begin(), other.m_actionToInterp.end());
}

Expand All @@ -117,6 +121,9 @@ void BL_BlenderConverter::SceneSlot::Merge(const BL_BlenderSceneConverter& conve
for (RAS_MeshObject *meshobj : converter.m_meshobjects) {
m_meshobjects.emplace_back(meshobj);
}
for (BL_ConvertObjectInfo *info : converter.m_objectInfos) {
m_objectInfos.emplace_back(info);
}
}

BL_BlenderConverter::BL_BlenderConverter(Main *maggie, KX_KetsjiEngine *engine)
Expand Down
4 changes: 3 additions & 1 deletion source/gameengine/Converter/BL_BlenderConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@
#ifdef _MSC_VER // MSVC doesn't support incomplete type in std::unique_ptr.
# include "KX_BlenderMaterial.h"
# include "RAS_MeshObject.h"

# include "BL_ConvertObjectInfo.h"
# include "BL_BlenderScalarInterpolator.h"
#endif

#include "CM_Thread.h"

class EXP_StringValue;
class BL_BlenderSceneConverter;
class BL_ConvertObjectInfo;
class KX_KetsjiEngine;
class KX_LibLoadStatus;
class KX_BlenderMaterial;
Expand Down Expand Up @@ -75,6 +76,7 @@ class BL_BlenderConverter
UniquePtrList<KX_BlenderMaterial> m_materials;
UniquePtrList<RAS_MeshObject> m_meshobjects;
UniquePtrList<BL_InterpolatorList> m_interpolators;
UniquePtrList<BL_ConvertObjectInfo> m_objectInfos;

std::map<bAction *, BL_InterpolatorList *> m_actionToInterp;

Expand Down
8 changes: 6 additions & 2 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
#include "BL_ConvertControllers.h"
#include "BL_ConvertSensors.h"
#include "BL_ConvertProperties.h"
#include "BL_ConvertObjectInfo.h"
#include "BL_ArmatureObject.h"
#include "BL_DeformableGameObject.h"

Expand Down Expand Up @@ -933,7 +934,8 @@ static KX_GameObject *BL_GameObjectFromBlenderObject(Object *ob, KX_Scene *kxsce
}
if (gameobj) {
gameobj->SetLayer(ob->lay);
gameobj->SetBlenderObject(ob);
BL_ConvertObjectInfo *info = converter.GetObjectInfo(ob);
gameobj->SetConvertObjectInfo(info);
gameobj->SetObjectColor(mt::vec4(ob->col));
// Set the visibility state based on the objects render option in the outliner.
if (ob->restrictflag & OB_RESTRICT_RENDER) {
Expand Down Expand Up @@ -1617,6 +1619,8 @@ void BL_ConvertBlenderObjects(struct Main *maggie,
continue;
}

BL_ConvertObjectInfo *info = gameobj->GetConvertObjectInfo();

for (bConstraint *curcon = (bConstraint *)conlist->first; curcon; curcon = (bConstraint *)curcon->next) {
if (curcon->type != CONSTRAINT_TYPE_RIGIDBODYJOINT) {
continue;
Expand All @@ -1630,7 +1634,7 @@ void BL_ConvertBlenderObjects(struct Main *maggie,
}

// Store constraints of grouped and instanced objects for all layers.
gameobj->AddConstraint(dat);
info->m_constraints.push_back(dat);

/** if it's during libload we only add constraints in the object but
* doesn't create it. Constraint will be replicated later in scene->MergeScene
Expand Down
16 changes: 16 additions & 0 deletions source/gameengine/Converter/BL_BlenderSceneConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

#include "BL_BlenderSceneConverter.h"
#include "BL_ConvertObjectInfo.h"
#include "KX_GameObject.h"

BL_BlenderSceneConverter::BL_BlenderSceneConverter(KX_Scene *scene)
Expand All @@ -40,6 +41,9 @@ BL_BlenderSceneConverter::BL_BlenderSceneConverter(KX_Scene *scene)
BL_BlenderSceneConverter::BL_BlenderSceneConverter(BL_BlenderSceneConverter&& other)
:m_scene(other.m_scene),
m_materials(std::move(other.m_materials)),
m_meshobjects(std::move(other.m_meshobjects)),
m_objectInfos(std::move(other.m_objectInfos)),
m_blenderToObjectInfos(std::move(other.m_blenderToObjectInfos)),
m_map_blender_to_gameobject(std::move(other.m_map_blender_to_gameobject)),
m_map_mesh_to_gamemesh(std::move(other.m_map_mesh_to_gamemesh)),
m_map_mesh_to_polyaterial(std::move(other.m_map_mesh_to_polyaterial)),
Expand Down Expand Up @@ -124,3 +128,15 @@ SCA_IController *BL_BlenderSceneConverter::FindGameController(bController *for_c
{
return m_map_blender_to_gamecontroller[for_controller];
}

BL_ConvertObjectInfo *BL_BlenderSceneConverter::GetObjectInfo(Object *blenderobj)
{
const auto& it = m_blenderToObjectInfos.find(blenderobj);
if (it == m_blenderToObjectInfos.end()) {
BL_ConvertObjectInfo *info = m_blenderToObjectInfos[blenderobj] = new BL_ConvertObjectInfo{blenderobj, {}};
m_objectInfos.push_back(info);
return info;
}

return it->second;
}
5 changes: 5 additions & 0 deletions source/gameengine/Converter/BL_BlenderSceneConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SCA_IController;
class RAS_MeshObject;
class KX_BlenderMaterial;
class BL_BlenderConverter;
class BL_ConvertObjectInfo;
class KX_GameObject;
class KX_Scene;
class KX_LibLoadStatus;
Expand All @@ -63,7 +64,9 @@ class BL_BlenderSceneConverter

std::vector<KX_BlenderMaterial *> m_materials;
std::vector<RAS_MeshObject *> m_meshobjects;
std::vector<BL_ConvertObjectInfo *> m_objectInfos;

std::map<Object *, BL_ConvertObjectInfo *> m_blenderToObjectInfos;
std::map<Object *, KX_GameObject *> m_map_blender_to_gameobject;
std::map<Mesh *, RAS_MeshObject *> m_map_mesh_to_gamemesh;
std::map<Material *, KX_BlenderMaterial *> m_map_mesh_to_polyaterial;
Expand Down Expand Up @@ -96,6 +99,8 @@ class BL_BlenderSceneConverter

void RegisterGameController(SCA_IController *cont, bController *for_controller);
SCA_IController *FindGameController(bController *for_controller);

BL_ConvertObjectInfo *GetObjectInfo(Object *blenderobj);
};

#endif // __KX_BLENDERSCENECONVERTER_H__
16 changes: 16 additions & 0 deletions source/gameengine/Converter/BL_ConvertObjectInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __BL_CONVERT_OBJECT_INFO__
#define __BL_CONVERT_OBJECT_INFO__

#include <vector>

struct bRigidBodyJointConstraint;

struct BL_ConvertObjectInfo
{
/// Blender object used during conversion.
Object *m_blenderObject;
/// Object constraints defined by the user.
std::vector<bRigidBodyJointConstraint *> m_constraints;
};

#endif // __BL_CONVERT_OBJECT_INFO__
23 changes: 12 additions & 11 deletions source/gameengine/Converter/BL_DeformableGameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,22 @@ void BL_DeformableGameObject::LoadDeformer()
/* Object that owns the mesh. If this is not the current blender object, look at one of the object registered
* along the blender mesh. */
Object *meshblendobj;
if (m_blenderObject->data != mesh) {
Object *blenderobj = GetBlenderObject();
if (blenderobj->data != mesh) {
meshblendobj = static_cast<Object *>(scene->GetLogicManager()->FindBlendObjByGameMeshName(meshobj->GetName()));
}
else {
meshblendobj = m_blenderObject;
meshblendobj = blenderobj;
}

const bool isParentArmature = parentobj && parentobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE;
const bool bHasModifier = BL_ModifierDeformer::HasCompatibleDeformer(m_blenderObject);
const bool bHasModifier = BL_ModifierDeformer::HasCompatibleDeformer(blenderobj);
const bool bHasShapeKey = mesh->key && mesh->key->type == KEY_RELATIVE;
const bool bHasDvert = mesh->dvert && m_blenderObject->defbase.first;
const bool bHasArmature = BL_ModifierDeformer::HasArmatureDeformer(m_blenderObject) &&
const bool bHasDvert = mesh->dvert && blenderobj->defbase.first;
const bool bHasArmature = BL_ModifierDeformer::HasArmatureDeformer(blenderobj) &&
isParentArmature && meshblendobj && bHasDvert;
#ifdef WITH_BULLET
const bool bHasSoftBody = (!parentobj && (m_blenderObject->gameflag & OB_SOFT_BODY));
const bool bHasSoftBody = (!parentobj && (blenderobj->gameflag & OB_SOFT_BODY));
#endif

if (!meshblendobj) {
Expand All @@ -175,28 +176,28 @@ void BL_DeformableGameObject::LoadDeformer()

if (bHasModifier) {
if (isParentArmature) {
BL_ModifierDeformer *modifierDeformer = new BL_ModifierDeformer(this, blenderScene, meshblendobj, m_blenderObject,
BL_ModifierDeformer *modifierDeformer = new BL_ModifierDeformer(this, blenderScene, meshblendobj, blenderobj,
meshobj, static_cast<BL_ArmatureObject *>(parentobj));
modifierDeformer->LoadShapeDrivers(parentobj);
m_deformer = modifierDeformer;
}
else {
m_deformer = new BL_ModifierDeformer(this, blenderScene, meshblendobj, m_blenderObject, meshobj, nullptr);
m_deformer = new BL_ModifierDeformer(this, blenderScene, meshblendobj, blenderobj, meshobj, nullptr);
}
}
else if (bHasShapeKey) {
if (isParentArmature) {
BL_ShapeDeformer *shapeDeformer = new BL_ShapeDeformer(this, meshblendobj, m_blenderObject, meshobj,
BL_ShapeDeformer *shapeDeformer = new BL_ShapeDeformer(this, meshblendobj, blenderobj, meshobj,
static_cast<BL_ArmatureObject *>(parentobj));
shapeDeformer->LoadShapeDrivers(parentobj);
m_deformer = shapeDeformer;
}
else {
m_deformer = new BL_ShapeDeformer(this, meshblendobj, m_blenderObject, meshobj, nullptr);
m_deformer = new BL_ShapeDeformer(this, meshblendobj, blenderobj, meshobj, nullptr);
}
}
else if (bHasArmature) {
m_deformer = new BL_SkinDeformer(this, meshblendobj, m_blenderObject, meshobj,
m_deformer = new BL_SkinDeformer(this, meshblendobj, blenderobj, meshobj,
static_cast<BL_ArmatureObject *>(parentobj));
}
else if (bHasDvert) {
Expand Down
1 change: 1 addition & 0 deletions source/gameengine/Converter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ set(SRC
BL_BlenderSceneConverter.h
BL_ConvertActuators.h
BL_ConvertControllers.h
BL_ConvertObjectInfo.h
BL_ConvertProperties.h
BL_ConvertSensors.h
BL_IpoConvert.h
Expand Down
34 changes: 20 additions & 14 deletions source/gameengine/Ketsji/KX_GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

#include "BKE_object.h"

#include "BL_ConvertObjectInfo.h"
#include "BL_ActionManager.h"
#include "BL_Action.h"

Expand Down Expand Up @@ -110,7 +111,6 @@ KX_GameObject::KX_GameObject(
m_lodManager(nullptr),
m_currentLodLevel(0),
m_meshUser(nullptr),
m_blenderObject(nullptr),
m_bIsNegativeScaling(false),
m_objectColor(mt::one4),
m_bVisible(true),
Expand Down Expand Up @@ -273,19 +273,9 @@ void KX_GameObject::SetDupliGroupObject(KX_GameObject* obj)
m_dupliGroupObject = obj;
}

void KX_GameObject::AddConstraint(bRigidBodyJointConstraint *cons)
const std::vector<bRigidBodyJointConstraint *>& KX_GameObject::GetConstraints()
{
m_constraints.push_back(cons);
}

std::vector<bRigidBodyJointConstraint*> KX_GameObject::GetConstraints()
{
return m_constraints;
}

void KX_GameObject::ClearConstraints()
{
m_constraints.clear();
return m_convertInfo->m_constraints;
}

KX_GameObject* KX_GameObject::GetParent()
Expand Down Expand Up @@ -684,7 +674,7 @@ void KX_GameObject::ApplyRotation(const mt::vec3& drot,bool local)
void KX_GameObject::UpdateBlenderObjectMatrix(Object* blendobj)
{
if (!blendobj)
blendobj = m_blenderObject;
blendobj = m_convertInfo->m_blenderObject;
if (blendobj) {
const mt::mat3x4 trans = NodeGetWorldTransform();
trans.PackFromAffineTransform(blendobj->obmat);
Expand Down Expand Up @@ -1377,6 +1367,22 @@ mt::mat3x4 KX_GameObject::NodeGetLocalTransform() const
return m_sgNode->GetLocalTransform();
}

Object *KX_GameObject::GetBlenderObject() const
{
// Non converted objects has default camera doesn't have convert info.
return (m_convertInfo) ? m_convertInfo->m_blenderObject : nullptr;
}

BL_ConvertObjectInfo *KX_GameObject::GetConvertObjectInfo() const
{
return m_convertInfo;
}

void KX_GameObject::SetConvertObjectInfo(BL_ConvertObjectInfo *info)
{
m_convertInfo = info;
}

void KX_GameObject::UpdateBounds(bool force)
{
if ((!m_autoUpdateBounds && !force) || !m_meshUser) {
Expand Down
34 changes: 12 additions & 22 deletions source/gameengine/Ketsji/KX_GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class PHY_IGraphicController;
class PHY_IPhysicsEnvironment;
class PHY_IPhysicsController;
class BL_ActionManager;
class BL_ConvertObjectInfo;
struct Object;
class KX_ObstacleSimulation;
class KX_CollisionContactPointList;
Expand Down Expand Up @@ -108,8 +109,9 @@ class KX_GameObject : public SCA_IObject, public mt::SimdClassAllocator
KX_LodManager *m_lodManager;
short m_currentLodLevel;
RAS_MeshUser *m_meshUser;
struct Object* m_blenderObject;

/// Info about blender object convert from.
BL_ConvertObjectInfo *m_convertInfo;

bool m_bIsNegativeScaling;
mt::vec4 m_objectColor;

Expand All @@ -131,8 +133,6 @@ class KX_GameObject : public SCA_IObject, public mt::SimdClassAllocator

EXP_ListValue<KX_PythonComponent> *m_components;

std::vector<bRigidBodyJointConstraint*> m_constraints;

EXP_ListValue<KX_GameObject> *m_instanceObjects;
KX_GameObject* m_dupliGroupObject;

Expand Down Expand Up @@ -193,9 +193,7 @@ class KX_GameObject : public SCA_IObject, public mt::SimdClassAllocator
* Used for constraint replication for group instances.
* The list of constraints is filled during data conversion.
*/
void AddConstraint(bRigidBodyJointConstraint *cons);
std::vector<bRigidBodyJointConstraint*> GetConstraints();
void ClearConstraints();
const std::vector<bRigidBodyJointConstraint*>& GetConstraints();

/**
* Get a pointer to the game object that is the parent of
Expand Down Expand Up @@ -533,25 +531,17 @@ class KX_GameObject : public SCA_IObject, public mt::SimdClassAllocator
return m_sgNode;
}

/**
* \section blender object accessor functions.
*/

struct Object* GetBlenderObject( )
{
return m_blenderObject;
}
Object *GetBlenderObject() const;

void SetBlenderObject(struct Object* obj)
{
m_blenderObject = obj;
}
BL_ConvertObjectInfo *GetConvertObjectInfo() const;
void SetConvertObjectInfo(BL_ConvertObjectInfo *info);

bool IsDupliGroup()
{
return (m_blenderObject &&
(m_blenderObject->transflag & OB_DUPLIGROUP) &&
m_blenderObject->dup_group != nullptr) ? true : false;
Object *blenderobj = GetBlenderObject();
return (blenderobj &&
(blenderobj->transflag & OB_DUPLIGROUP) &&
blenderobj->dup_group != nullptr) ? true : false;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions source/gameengine/Ketsji/KX_Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,6 @@ void KX_Scene::DupliGroupRecurse(KX_GameObject *groupobj, int level)
// Replicate all constraints.
if (gameobj->GetPhysicsController()) {
gameobj->GetPhysicsController()->ReplicateConstraints(gameobj, m_logicHierarchicalGameObjects);
gameobj->ClearConstraints();
}

if (gameobj != groupobj && gameobj->IsDupliGroup()) {
Expand Down Expand Up @@ -1698,7 +1697,6 @@ bool KX_Scene::MergeScene(KX_Scene *other)
KX_GameObject *gameobj = physicsObjects[i];
// Replicate all constraints in the right physics environment.
gameobj->GetPhysicsController()->ReplicateConstraints(gameobj, physicsObjects);
gameobj->ClearConstraints();
}
}

Expand Down

0 comments on commit fe801b4

Please sign in to comment.