From bbf0a438a421979147aa451a31f1dd0eae410057 Mon Sep 17 00:00:00 2001 From: turanszkij Date: Mon, 20 Jun 2016 21:14:41 +0200 Subject: [PATCH] Model serialization wrap-up --- WickedEngine/wiEmittedParticle.cpp | 10 +- WickedEngine/wiEmittedParticle.h | 1 + WickedEngine/wiLoader.cpp | 223 +++++++++++++++++------------ WickedEngine/wiLoader.h | 4 +- WickedEngine/wiTransform.cpp | 2 + 5 files changed, 144 insertions(+), 96 deletions(-) diff --git a/WickedEngine/wiEmittedParticle.cpp b/WickedEngine/wiEmittedParticle.cpp index 054920819..cef72144e 100644 --- a/WickedEngine/wiEmittedParticle.cpp +++ b/WickedEngine/wiEmittedParticle.cpp @@ -31,6 +31,7 @@ wiEmittedParticle::wiEmittedParticle() materialName = ""; light = nullptr; bounding_box = new AABB(); + lightName = ""; } wiEmittedParticle::wiEmittedParticle(std::string newName, std::string newMat, Object* newObject, float newSize, float newRandomFac, float newNormalFac ,float newCount, float newLife, float newRandLife, float newScaleX, float newScaleY, float newRot){ @@ -103,12 +104,13 @@ void wiEmittedParticle::CreateLight() { light = new Light(); light->SetUp(); - light->color.x = material->diffuseColor.x; - light->color.y = material->diffuseColor.y; - light->color.z = material->diffuseColor.z; + light->color.x = material->baseColor.x; + light->color.y = material->baseColor.y; + light->color.z = material->baseColor.z; light->type = Light::POINT; light->name = name + "_pslight"; light->shadow = true; + lightName = light->name; } } @@ -562,6 +564,7 @@ void wiEmittedParticle::Serialize(wiArchive& archive) archive >> scaleY; archive >> rotation; archive >> motionBlurAmount; + archive >> lightName; LoadVertexBuffer(); SetupLightInterpolators(); @@ -583,5 +586,6 @@ void wiEmittedParticle::Serialize(wiArchive& archive) archive << scaleY; archive << rotation; archive << motionBlurAmount; + archive << lightName; } } diff --git a/WickedEngine/wiEmittedParticle.h b/WickedEngine/wiEmittedParticle.h index ff13b2a8a..5920cd976 100644 --- a/WickedEngine/wiEmittedParticle.h +++ b/WickedEngine/wiEmittedParticle.h @@ -116,6 +116,7 @@ public: string materialName; Material* material; Light* light; + string lightName; AABB* bounding_box; long lastSquaredDistMulThousand; diff --git a/WickedEngine/wiLoader.cpp b/WickedEngine/wiLoader.cpp index 3a70bac94..796432441 100644 --- a/WickedEngine/wiLoader.cpp +++ b/WickedEngine/wiLoader.cpp @@ -102,27 +102,7 @@ void LoadWiArmatures(const string& directory, const string& name, const string& //CREATE FAMILY for(Armature* armature : armatures){ - for(Bone* i : armature->boneCollection){ - if(i->parentName.length()>0){ - for(Bone* j : armature->boneCollection){ - if(i!=j){ - if(!i->parentName.compare(j->name)){ - i->parent=j; - j->childrenN.push_back(i->name); - j->childrenI.push_back(i); - i->attachTo(j,1,1,1); - } - } - } - } - else{ - armature->rootbones.push_back(i); - } - } - - for (unsigned int i = 0; irootbones.size(); ++i){ - RecursiveRest(armature,armature->rootbones[i]); - } + armature->CreateFamily(); } } @@ -2268,8 +2248,12 @@ void Model::LoadFromDisk(const string& dir, const string& name, const string& id LoadWiActions(directory.str(), actionsFilePath.str(), identifier, armatures); LoadWiLights(directory.str(), lightsFilePath.str(), identifier, lights); LoadWiDecals(directory.str(), decalsFilePath.str(), "textures/", decals); - } + FinishLoading(); + } +} +void Model::FinishLoading() +{ vector transforms(0); transforms.reserve(armatures.size() + objects.size() + lights.size() + decals.size()); @@ -2286,9 +2270,39 @@ void Model::LoadFromDisk(const string& dir, const string& name, const string& id transforms.push_back(x); for (wiEmittedParticle* e : x->eParticleSystems) { + // If the particle system has light, then register it to the light array (if not already registered!) if (e->light != nullptr) { - lights.push_back(e->light); + bool registeredLight = false; + for (Light* l : lights) + { + if (e->light == l) + { + registeredLight = true; + } + } + if (!registeredLight) + { + lights.push_back(e->light); + } + } + } + // Match parentBone (not armaturedeform!) + if (!x->boneParent.empty() && x->mesh != nullptr) + { + Armature* armature = x->mesh->armature; + if (armature != nullptr) + { + for (Bone* b : armature->boneCollection) + { + if (!b->name.compare(x->boneParent)) + { + XMFLOAT4X4 saved_parent_rest_inv = x->parent_inv_rest; + x->attachTo(b); + x->parent_inv_rest = saved_parent_rest_inv; + break; + } + } } } } @@ -2306,37 +2320,22 @@ void Model::LoadFromDisk(const string& dir, const string& name, const string& id // Match loaded parenting information for (Transform* x : transforms) { - for (Transform* y : transforms) + if (x->parent == nullptr) { - if (x != y && x->parentName.length() > 0 && !x->parentName.compare(y->name)) + for (Transform* y : transforms) { - // Match parent - XMFLOAT4X4 saved_parent_rest_inv = x->parent_inv_rest; - x->attachTo(y); - x->parent_inv_rest = saved_parent_rest_inv; - break; - } - } - - if (x->parent != nullptr && x->parentName.length() > 0 && x->boneParent.length() > 0) - { - // Match Bone parent - Armature* armature = dynamic_cast(x->parent); - if (armature != nullptr) - { - // Only if the current parent is an Armature! - for (Bone* b : armature->boneCollection) { - if (!b->name.compare(x->boneParent)) - { - XMFLOAT4X4 saved_parent_rest_inv = x->parent_inv_rest; - x->attachTo(b); - x->parent_inv_rest = saved_parent_rest_inv; - break; - } + if (x != y && !x->parentName.empty() && !x->parentName.compare(y->name)) + { + // Match parent + XMFLOAT4X4 saved_parent_rest_inv = x->parent_inv_rest; + x->attachTo(y); + x->parent_inv_rest = saved_parent_rest_inv; + break; } } } + // If it has still not parent, then attach to this model! if (x->parent == nullptr) { x->attachTo(this); @@ -2368,7 +2367,47 @@ void Model::LoadFromDisk(const string& dir, const string& name, const string& id x->mesh->CreateBuffers(x); } } +} +void Model::UpdateModel() +{ + for (MaterialCollection::iterator iter = materials.begin(); iter != materials.end(); ++iter) { + Material* iMat = iter->second; + iMat->framesToWaitForTexCoordOffset -= 1.0f; + if (iMat->framesToWaitForTexCoordOffset <= 0) { + iMat->texMulAdd.z += iMat->movingTex.x*wiRenderer::GetGameSpeed(); + iMat->texMulAdd.w += iMat->movingTex.y*wiRenderer::GetGameSpeed(); + iMat->framesToWaitForTexCoordOffset = iMat->movingTex.z*wiRenderer::GetGameSpeed(); + } + } + for (Armature* x : armatures) + { + x->UpdateArmature(); + } + for (Object* x : objects) + { + x->UpdateObject(); + } + for (Light*x : lights) + { + x->UpdateLight(); + } + + list::iterator iter = decals.begin(); + while (iter != decals.end()) + { + Decal* decal = *iter; + decal->UpdateDecal(); + if (decal->life>-2) { + if (decal->life <= 0) { + decal->detach(); + decals.erase(iter++); + delete decal; + continue; + } + } + ++iter; + } } void Model::Serialize(wiArchive& archive) { @@ -2391,7 +2430,7 @@ void Model::Serialize(wiArchive& archive) { Mesh* x = new Mesh; x->Serialize(archive); - meshes.insert(pair(x->name, x)); + meshes.insert(pair(x->name, x)); } archive >> materialCount; @@ -2399,7 +2438,7 @@ void Model::Serialize(wiArchive& archive) { Material* x = new Material; x->Serialize(archive); - materials.insert(pair(x->name, x)); + materials.insert(pair(x->name, x)); } archive >> armaturesCount; @@ -2475,7 +2514,17 @@ void Model::Serialize(wiArchive& archive) if (it != materials.end()) { y->material = it->second; - y->CreateLight(); + if (!y->lightName.empty()) + { + for (auto& l : lights) + { + if (!l->name.compare(y->lightName)) + { + y->light = l; + break; + } + } + } } } for (auto& y : x->hParticleSystems) @@ -2489,6 +2538,8 @@ void Model::Serialize(wiArchive& archive) } } } + + FinishLoading(); } else { @@ -2529,47 +2580,6 @@ void Model::Serialize(wiArchive& archive) } } } -void Model::UpdateModel() -{ - for (MaterialCollection::iterator iter = materials.begin(); iter != materials.end(); ++iter) { - Material* iMat = iter->second; - iMat->framesToWaitForTexCoordOffset -= 1.0f; - if (iMat->framesToWaitForTexCoordOffset <= 0) { - iMat->texMulAdd.z += iMat->movingTex.x*wiRenderer::GetGameSpeed(); - iMat->texMulAdd.w += iMat->movingTex.y*wiRenderer::GetGameSpeed(); - iMat->framesToWaitForTexCoordOffset = iMat->movingTex.z*wiRenderer::GetGameSpeed(); - } - } - - for (Armature* x : armatures) - { - x->UpdateArmature(); - } - for (Object* x : objects) - { - x->UpdateObject(); - } - for (Light*x : lights) - { - x->UpdateLight(); - } - - list::iterator iter = decals.begin(); - while (iter != decals.end()) - { - Decal* decal = *iter; - decal->UpdateDecal(); - if (decal->life>-2) { - if (decal->life <= 0) { - decal->detach(); - decals.erase(iter++); - delete decal; - continue; - } - } - ++iter; - } -} #pragma endregion #pragma region AABB @@ -2858,6 +2868,7 @@ void Bone::Serialize(wiArchive& archive) } archive << restInv; archive << actionFrames.size(); + int i = 0; for (auto& x : actionFrames) { archive << x.keyframesRot.size(); @@ -3267,6 +3278,30 @@ void Armature::DeleteAnimLayer(const string& name) } } } +void Armature::CreateFamily() +{ + for (Bone* i : boneCollection) { + if (i->parentName.length()>0) { + for (Bone* j : boneCollection) { + if (i != j) { + if (!i->parentName.compare(j->name)) { + i->parent = j; + j->childrenN.push_back(i->name); + j->childrenI.push_back(i); + i->attachTo(j, 1, 1, 1); + } + } + } + } + else { + rootbones.push_back(i); + } + } + + for (unsigned int i = 0; i> animLayerCount; + animationLayers.clear(); for (size_t i = 0; i < animLayerCount; ++i) { AnimationLayer* layer = new AnimationLayer; @@ -3293,12 +3329,15 @@ void Armature::Serialize(wiArchive& archive) size_t actionCount; archive >> actionCount; Action tempAction; + actions.clear(); for (size_t i = 0; i < actionCount; ++i) { archive >> tempAction.name; archive >> tempAction.frameCount; actions.push_back(tempAction); } + + CreateFamily(); } else { diff --git a/WickedEngine/wiLoader.h b/WickedEngine/wiLoader.h index c32978031..017172972 100644 --- a/WickedEngine/wiLoader.h +++ b/WickedEngine/wiLoader.h @@ -661,6 +661,7 @@ public: void DeleteAnimLayer(const string& name); virtual void UpdateTransform(); void UpdateArmature(); + void CreateFamily(); void Serialize(wiArchive& archive); private: @@ -1001,8 +1002,9 @@ struct Model : public Transform virtual ~Model(); void CleanUp(); void LoadFromDisk(const string& dir, const string& name, const string& identifier); - void Serialize(wiArchive& archive); + void FinishLoading(); void UpdateModel(); + void Serialize(wiArchive& archive); }; struct Scene diff --git a/WickedEngine/wiTransform.cpp b/WickedEngine/wiTransform.cpp index f925b5d7e..6bb74c1a4 100644 --- a/WickedEngine/wiTransform.cpp +++ b/WickedEngine/wiTransform.cpp @@ -263,6 +263,8 @@ void Transform::Serialize(wiArchive& archive) archive >> copyParentT; archive >> copyParentR; archive >> copyParentS; + + UpdateTransform(); } else {