Skip to content

Commit e81bd78

Browse files
committed
UPBGE: Refactor scene converter.
Previously the converter was using to store the data of the scenes and the links to them during a scene conversion. These two goals could be split into two classes to avoid update the links and clear the code by avoiding access data that are dangling. Also in case of two lib loading in asynchronous the links are written and read by two conversion task at the same time, which involved unstable behavior. To avoid the issues described before the scene converter is distinguished of the scene data store. The converter keep is name as KX_BlenderSceneConverter but its goal is restricted. The scene converter is temporary for the scene conversion, it is passed as reference to ensure to not store a pointer to it. It contains the map between the blender data and the game engine data like: objects, meshes, material, actuators and controllers. To access and register these data the functions follow the patterns: RegisterGEDataType(BlenderType, GEDataType) FindGEDataType(BlenderType) The scene converter also store a list of the game engine data types registered during the scene conversion. These are poly material and mesh objects. Once the conversion is over, the scene converter is deleted but the list of materials and meshes in it are copied into the KX_BlenderConverter. This new class has the goal of launching the scene conversion and storing data slots per scenes. These slots contains approximately the same data than the list stored in the scene converter excepted for the action interpolators which are converted dynamically after the scene conversion. These scene slots own the materials and meshes in they. A scene slot can be created using a scene converter or merged with an other scene slot in case of scene lib loading conversion. Other than that the new KX_BlenderConverter is keeping the functions in the previous KX_BlenderSceneConverter without the one used for data linking. Note: MSVC doesn't support using a type without destructor in a std::map even if the destructor are all defined in .cpp, to solve this issue we need to add extra includes.
1 parent da59b5b commit e81bd78

35 files changed

+1241
-1345
lines changed

source/gameengine/Converter/BL_ArmatureObject.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ BL_ArmatureObject::~BL_ArmatureObject()
247247
}
248248
}
249249

250-
void BL_ArmatureObject::LoadConstraints(KX_BlenderSceneConverter *converter)
250+
void BL_ArmatureObject::LoadConstraints(KX_BlenderSceneConverter& converter)
251251
{
252252
// first delete any existing constraint (should not have any)
253253
m_controlledConstraints->ReleaseAndRemoveAll();
@@ -288,14 +288,14 @@ void BL_ArmatureObject::LoadConstraints(KX_BlenderSceneConverter *converter)
288288
bConstraintTarget *target = (bConstraintTarget *)listb.first;
289289
if (target->tar && target->tar != m_objArma) {
290290
// only remember external objects, self target is handled automatically
291-
gametarget = converter->FindGameObject(target->tar);
291+
gametarget = converter.FindGameObject(target->tar);
292292
}
293293
if (target->next != nullptr) {
294294
// secondary target
295295
target = target->next;
296296
if (target->tar && target->tar != m_objArma) {
297297
// only track external object
298-
gamesubtarget = converter->FindGameObject(target->tar);
298+
gamesubtarget = converter.FindGameObject(target->tar);
299299
}
300300
}
301301
}

source/gameengine/Converter/BL_ArmatureObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class BL_ArmatureObject : public KX_GameObject
111111
void DrawDebug(RAS_DebugDraw& debugDraw);
112112

113113
// for constraint python API
114-
void LoadConstraints(KX_BlenderSceneConverter *converter);
114+
void LoadConstraints(KX_BlenderSceneConverter& converter);
115115
size_t GetConstraintNumber() const;
116116
BL_ArmatureConstraint *GetConstraint(const std::string& posechannel, const std::string& constraint);
117117
BL_ArmatureConstraint *GetConstraint(const std::string& posechannelconstraint);

source/gameengine/Converter/BL_BlenderDataConversion.cpp

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,9 @@ static KX_BlenderMaterial *ConvertMaterial(
450450
static RAS_MaterialBucket *material_from_mesh(
451451
Material *ma, MFace *mface, MTFace *tface, const RAS_MeshObject::LayerList& layers, int lightlayer,
452452
unsigned int rgb[4][RAS_ITexVert::MAX_UNIT], MT_Vector2 uvs[4][RAS_ITexVert::MAX_UNIT],
453-
KX_Scene* scene, KX_BlenderSceneConverter *converter)
453+
KX_Scene* scene, KX_BlenderSceneConverter& converter)
454454
{
455-
RAS_IPolyMaterial* polymat = converter->FindCachedPolyMaterial(scene, ma);
455+
RAS_IPolyMaterial* polymat = converter.FindPolyMaterial(ma);
456456

457457
if (mface) {
458458
GetRGB(mface, layers, rgb);
@@ -462,31 +462,27 @@ static RAS_MaterialBucket *material_from_mesh(
462462

463463
if (!polymat) {
464464
polymat = ConvertMaterial(ma, tface, lightlayer, scene);
465-
converter->CachePolyMaterial(scene, ma, polymat);
465+
// this is needed to free up memory afterwards.
466+
converter.RegisterPolyMaterial(polymat, ma);
466467
}
467-
468+
468469
// see if a bucket was reused or a new one was created
469470
// this way only one KX_BlenderMaterial object has to exist per bucket
470471
bool bucketCreated;
471472
RAS_MaterialBucket* bucket = scene->FindBucket(polymat, bucketCreated);
472473

473-
// this is needed to free up memory afterwards.
474-
// the converter will also prevent duplicates from being registered,
475-
// so just register everything.
476-
converter->RegisterPolyMaterial(scene, polymat);
477-
478474
return bucket;
479475
}
480476

481477
/* blenderobj can be nullptr, make sure its checked for */
482-
RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, KX_Scene* scene, KX_BlenderSceneConverter *converter, bool libloading)
478+
RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, KX_Scene* scene, KX_BlenderSceneConverter& converter, bool libloading)
483479
{
484480
RAS_MeshObject *meshobj;
485481
int lightlayer = blenderobj ? blenderobj->lay:(1<<20)-1; // all layers if no object.
486482

487483
// Without checking names, we get some reuse we don't want that can cause
488484
// problems with material LoDs.
489-
if (blenderobj && ((meshobj = converter->FindGameMesh(mesh/*, ob->lay*/)) != nullptr)) {
485+
if (blenderobj && ((meshobj = converter.FindGameMesh(mesh/*, ob->lay*/)) != nullptr)) {
490486
const std::string bge_name = meshobj->GetName();
491487
const std::string blender_name = ((ID *)blenderobj->data)->name + 2;
492488
if (bge_name == blender_name) {
@@ -744,7 +740,7 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, KX_Scene* scene,
744740

745741
dm->release(dm);
746742

747-
converter->RegisterGameMesh(scene, meshobj, mesh);
743+
converter.RegisterGameMesh(meshobj, mesh);
748744
return meshobj;
749745
}
750746

@@ -839,7 +835,7 @@ static void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
839835
RAS_MeshObject* meshobj,
840836
KX_Scene* kxscene,
841837
int activeLayerBitInfo,
842-
KX_BlenderSceneConverter *converter,
838+
KX_BlenderSceneConverter& converter,
843839
bool processCompoundChildren
844840
)
845841

@@ -889,7 +885,7 @@ static void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
889885

890886
class PHY_IMotionState* motionstate = new KX_MotionState(gameobj->GetSGNode());
891887

892-
kxscene->GetPhysicsEnvironment()->ConvertObject(gameobj, meshobj, dm, kxscene, shapeprops, motionstate, activeLayerBitInfo, isCompoundChild, hasCompoundChildren);
888+
kxscene->GetPhysicsEnvironment()->ConvertObject(converter, gameobj, meshobj, dm, kxscene, shapeprops, motionstate, activeLayerBitInfo, isCompoundChild, hasCompoundChildren);
893889

894890
bool isActor = (blenderobject->gameflag & OB_ACTOR)!=0;
895891
bool isSensor = (blenderobject->gameflag & OB_SENSOR) != 0;
@@ -904,7 +900,7 @@ static void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
904900
}
905901
}
906902

907-
static KX_LodManager *lodmanager_from_blenderobject(Object *ob, KX_Scene *scene, KX_BlenderSceneConverter *converter, bool libloading)
903+
static KX_LodManager *lodmanager_from_blenderobject(Object *ob, KX_Scene *scene, KX_BlenderSceneConverter& converter, bool libloading)
908904
{
909905
if (BLI_listbase_count_ex(&ob->lodlevels, 2) <= 1) {
910906
return nullptr;
@@ -920,7 +916,7 @@ static KX_LodManager *lodmanager_from_blenderobject(Object *ob, KX_Scene *scene,
920916
return lodManager;
921917
}
922918

923-
static KX_LightObject *gamelight_from_blamp(Object *ob, Lamp *la, unsigned int layerflag, KX_Scene *kxscene, RAS_Rasterizer *rasterizer, KX_BlenderSceneConverter *converter)
919+
static KX_LightObject *gamelight_from_blamp(Object *ob, Lamp *la, unsigned int layerflag, KX_Scene *kxscene, RAS_Rasterizer *rasterizer, KX_BlenderSceneConverter& converter)
924920
{
925921
RAS_ILightObject *lightobj = rasterizer->CreateLight();
926922
KX_LightObject *gamelight;
@@ -974,7 +970,7 @@ static KX_LightObject *gamelight_from_blamp(Object *ob, Lamp *la, unsigned int l
974970
return gamelight;
975971
}
976972

977-
static KX_Camera *gamecamera_from_bcamera(Object *ob, KX_Scene *kxscene, KX_BlenderSceneConverter *converter)
973+
static KX_Camera *gamecamera_from_bcamera(Object *ob, KX_Scene *kxscene, KX_BlenderSceneConverter& converter)
978974
{
979975
Camera* ca = static_cast<Camera*>(ob->data);
980976
RAS_CameraData camdata(ca->lens, ca->ortho_scale, ca->sensor_x, ca->sensor_y, ca->sensor_fit, ca->shiftx, ca->shifty, ca->clipsta, ca->clipend, ca->type == CAM_PERSP, ca->YF_dofdist);
@@ -993,7 +989,7 @@ static KX_GameObject *gameobject_from_blenderobject(
993989
Object *ob,
994990
KX_Scene *kxscene,
995991
RAS_Rasterizer *rendertools,
996-
KX_BlenderSceneConverter *converter,
992+
KX_BlenderSceneConverter& converter,
997993
bool libloading)
998994
{
999995
KX_GameObject *gameobj = nullptr;
@@ -1307,7 +1303,7 @@ static void BL_ConvertComponentsObject(KX_GameObject *gameobj, Object *blenderob
13071303
/* helper for BL_ConvertBlenderObjects, avoids code duplication
13081304
* note: all var names match args are passed from the caller */
13091305
static void bl_ConvertBlenderObject_Single(
1310-
KX_BlenderSceneConverter *converter,
1306+
KX_BlenderSceneConverter& converter,
13111307
Object *blenderobject,
13121308
std::vector<parentChildLink> &vec_parent_child,
13131309
CListValue* logicbrick_conversionlist,
@@ -1395,7 +1391,7 @@ static void bl_ConvertBlenderObject_Single(
13951391
for (int i = 0; i < gameobj->GetMeshCount(); i++)
13961392
logicmgr->RegisterGameMeshName(gameobj->GetMesh(i)->GetName(), blenderobject);
13971393

1398-
converter->RegisterGameObject(gameobj, blenderobject);
1394+
converter.RegisterGameObject(gameobj, blenderobject);
13991395
// this was put in rapidly, needs to be looked at more closely
14001396
// only draw/use objects in active 'blender' layers
14011397

@@ -1425,7 +1421,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
14251421
e_PhysicsEngine physics_engine,
14261422
RAS_Rasterizer* rendertools,
14271423
RAS_ICanvas* canvas,
1428-
KX_BlenderSceneConverter* converter,
1424+
KX_BlenderSceneConverter& converter,
14291425
bool alwaysUseExpandFraming,
14301426
bool libloading
14311427
)
@@ -1597,7 +1593,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
15971593
for (go=(GroupObject*)group->gobject.first; go; go=(GroupObject*)go->next)
15981594
{
15991595
Object* blenderobject = go->ob;
1600-
if (converter->FindGameObject(blenderobject) == nullptr)
1596+
if (converter.FindGameObject(blenderobject) == nullptr)
16011597
{
16021598
allblobj.insert(blenderobject);
16031599
groupobj.insert(blenderobject);
@@ -1637,7 +1633,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
16371633

16381634
// non-camera objects not supported as camera currently
16391635
if (blenderscene->camera && blenderscene->camera->type == OB_CAMERA) {
1640-
KX_Camera *gamecamera= (KX_Camera*) converter->FindGameObject(blenderscene->camera);
1636+
KX_Camera *gamecamera= (KX_Camera*) converter.FindGameObject(blenderscene->camera);
16411637

16421638
if (gamecamera)
16431639
kxscene->SetActiveCamera(gamecamera);
@@ -1652,10 +1648,10 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
16521648
Mesh *me = (Mesh*)blenderobj->data;
16531649

16541650
if (me->dvert) {
1655-
BL_DeformableGameObject *obj = (BL_DeformableGameObject*)converter->FindGameObject(blenderobj);
1651+
BL_DeformableGameObject *obj = (BL_DeformableGameObject*)converter.FindGameObject(blenderobj);
16561652

16571653
if (obj && BL_ModifierDeformer::HasArmatureDeformer(blenderobj) && blenderobj->parent && blenderobj->parent->type==OB_ARMATURE) {
1658-
KX_GameObject *par = converter->FindGameObject(blenderobj->parent);
1654+
KX_GameObject *par = converter.FindGameObject(blenderobj->parent);
16591655
if (par && obj->GetDeformer())
16601656
((BL_SkinDeformer*)obj->GetDeformer())->SetArmature((BL_ArmatureObject*) par);
16611657
}
@@ -1671,8 +1667,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
16711667

16721668
struct Object* blenderchild = pcit->m_blenderchild;
16731669
struct Object* blenderparent = blenderchild->parent;
1674-
KX_GameObject* parentobj = converter->FindGameObject(blenderparent);
1675-
KX_GameObject* childobj = converter->FindGameObject(blenderchild);
1670+
KX_GameObject* parentobj = converter.FindGameObject(blenderparent);
1671+
KX_GameObject* childobj = converter.FindGameObject(blenderchild);
16761672

16771673
BLI_assert(childobj);
16781674

@@ -1703,7 +1699,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
17031699
childrenlist->Release();
17041700

17051701
// now destroy recursively
1706-
converter->UnregisterGameObject(childobj); // removing objects during conversion make sure this runs too
1702+
converter.UnregisterGameObject(childobj); // removing objects during conversion make sure this runs too
17071703
kxscene->RemoveObject(childobj);
17081704

17091705
continue;
@@ -1862,7 +1858,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
18621858

18631859
KX_GameObject *viewpoint = gameobj;
18641860
if (env->object) {
1865-
KX_GameObject *obj = converter->FindGameObject(env->object);
1861+
KX_GameObject *obj = converter.FindGameObject(env->object);
18661862
if (obj) {
18671863
viewpoint = obj;
18681864
}
@@ -1882,7 +1878,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
18821878
Mesh *predifinedBoundMesh = blenderobject->gamePredefinedBound;
18831879

18841880
if (predifinedBoundMesh) {
1885-
RAS_MeshObject *meshobj = converter->FindGameMesh(predifinedBoundMesh);
1881+
RAS_MeshObject *meshobj = converter.FindGameMesh(predifinedBoundMesh);
18861882
// In case of mesh taken in a other scene.
18871883
if (!meshobj) {
18881884
continue;

source/gameengine/Converter/BL_BlenderDataConversion.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@
3737
#include "KX_PhysicsEngineEnums.h"
3838
#include "SCA_IInputDevice.h"
3939

40-
class RAS_MeshObject* BL_ConvertMesh(struct Mesh* mesh,struct Object* lightobj,class KX_Scene* scene, class KX_BlenderSceneConverter *converter, bool libloading);
40+
class RAS_MeshObject* BL_ConvertMesh(struct Mesh* mesh,struct Object* lightobj,class KX_Scene* scene, class KX_BlenderSceneConverter& converter, bool libloading);
4141

4242
void BL_ConvertBlenderObjects(struct Main* maggie,
4343
class KX_Scene* kxscene,
4444
class KX_KetsjiEngine* ketsjiEngine,
4545
e_PhysicsEngine physics_engine,
4646
class RAS_Rasterizer* rendertools,
47-
class RAS_ICanvas* canvas,
48-
class KX_BlenderSceneConverter* sceneconverter,
47+
class RAS_ICanvas* canvas,
48+
class KX_BlenderSceneConverter& sceneconverter,
4949
bool alwaysUseExpandFraming,
5050
bool libloading=false
5151
);

source/gameengine/Converter/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ set(SRC
7575
BL_ModifierDeformer.cpp
7676
BL_ShapeDeformer.cpp
7777
BL_SkinDeformer.cpp
78+
KX_BlenderConverter.cpp
7879
KX_BlenderScalarInterpolator.cpp
7980
KX_BlenderSceneConverter.cpp
8081
KX_ConvertActuators.cpp
@@ -96,6 +97,7 @@ set(SRC
9697
BL_ModifierDeformer.h
9798
BL_ShapeDeformer.h
9899
BL_SkinDeformer.h
100+
KX_BlenderConverter.h
99101
KX_BlenderScalarInterpolator.h
100102
KX_BlenderSceneConverter.h
101103
KX_ConvertActuators.h

0 commit comments

Comments
 (0)