bool SceneSerializer::DeserializeColourFadeAffector(XMLReader& reader, std::shared_ptr<ColourFadeParticleAffector> &affector) { if(reader.MoveToChildNode()) { std::string currNodeName; physx::PxVec4 startColour, endColour; bool onGPU = false, success = true; do { currNodeName = reader.GetCurrentNodeName(); if(currNodeName == StartColour) if(!DeserializeVector4(reader, startColour)) success = false; if(currNodeName == EndColour) if(!DeserializeVector4(reader, endColour)) success = false; } while (reader.MoveToNextSiblingNode()); reader.PopNode(); if(reader.NodeHasAttributes()) { if(reader.GetCurrentAttributeName() == OnGPU) success = reader.GetBoolValue(onGPU, true); } if(success) affector = std::make_shared<ColourFadeParticleAffector>(startColour, endColour, onGPU); return success; } return false; }
bool SceneSerializer::DeserializeParticleEmitter(XMLReader& reader, std::shared_ptr<AbstractParticleEmitter>& outEmitter) { //<PointEmitter ParticlesPerSecond="5000.000000" MinSpeed="10.000000" MaxSpeed="20.000000" Duration="2.000000"> // <Position X="0.000000" Y="0.000000" Z="0.000000"/> // <MinDirection X="-0.707107" Y="-0.707107" Z="0.000000"/> // <MaxDirection X="0.707107" Y="0.707107" Z="0.000000"/> // </PointEmitter> bool failureDetected = false; bool currentResult = true; if(reader.NodeHasAttributes()) { std::string currAttr; float pps, minSpeed, maxSpeed, duration; physx::PxVec3 position, minDirection, maxDirection; bool emitLoop; do { currAttr = reader.GetCurrentAttributeName(); if(currAttr == PPS) currentResult = reader.GetFloatValue(pps, true); else if(currAttr == MinEmitSpeed) currentResult = reader.GetFloatValue(minSpeed, true); else if(currAttr == MaxEmitSpeed) currentResult = reader.GetFloatValue(maxSpeed, true); else if(currAttr == Duration) currentResult = reader.GetFloatValue(duration, true); else if(currAttr == EmitLoop) currentResult = reader.GetBoolValue(emitLoop, true); if(!currentResult) failureDetected = true; } while (reader.MoveToNextAttribute()); if(reader.MoveToChildNode()) { std::string currNodeName; do { currNodeName = reader.GetCurrentNodeName(); if(currNodeName == Position) currentResult = DeserializeVector3(reader, position); else if(currNodeName == MinEmitDirection) currentResult = DeserializeVector3(reader, minDirection); else if(currNodeName == MaxEmitDirection) currentResult = DeserializeVector3(reader, maxDirection); if(!currentResult) failureDetected = true; } while (reader.MoveToNextSiblingNode()); //Move back up a level reader.PopNode(); } std::string nodeName = reader.GetCurrentNodeName(); bool emitterCreated = false; outEmitter = nullptr; if (nodeName == PointEmitter) { outEmitter = std::make_shared<ParticlePointEmitter>(ParticlePointEmitter(pps, position, minDirection, maxDirection, duration, minSpeed, maxSpeed)); emitterCreated = true; } } //return true if no failure detected and out emitter was assigned return (!failureDetected && outEmitter); }
bool SceneSerializer::DeserializeParticleComponent(XMLReader& reader, SharedGameObject& objectToAdd) { if(reader.NodeHasAttributes()) { std::shared_ptr<AbstractParticleEmitter> emitter; std::vector<std::shared_ptr<ParticleAffector>> affectorList; bool emitterLoaded = false; bool affectorsLoaded = false; if(reader.MoveToChildNode()) { std::string currNodeName; do { currNodeName = reader.GetCurrentNodeName(); if(currNodeName == ParticleAffectors) affectorsLoaded = DeserializeParticleAffectors(reader, affectorList); else if(currNodeName.find(Emitter)) emitterLoaded = DeserializeParticleEmitter(reader, emitter); } while (reader.MoveToNextSiblingNode()); reader.PopNode(); } else return false; bool enabled, particleSpace, onGPU; long long particleID, maxParticles; float initialLifetime; physx::PxParticleSystem* particleBase = NULL; bool currResult = true; bool failureDetected = false; std::string currAttr = reader.GetCurrentNodeName(); do { currAttr = reader.GetCurrentAttributeName(); if(currAttr == Enabled) currResult = reader.GetBoolValue(enabled, true); else if(currAttr == ParticleSpace) currResult = reader.GetBoolValue(particleSpace, true); else if(currAttr == ParticleActorID) { currResult = reader.GetLongValue(particleID, true); if(currResult) { physx::PxBase* actor = PhysXSerializerWrapper::FindByID(ActorCollection, particleID); if(actor) { if(actor->getConcreteType() == physx::PxConcreteType::ePARTICLE_SYSTEM) particleBase = (physx::PxParticleSystem*)actor; } } } else if(currAttr == MaxParticles) currResult = reader.GetLongValue(maxParticles, true); else if(currAttr == OnGPU) currResult = reader.GetBoolValue(onGPU, true); else if(currAttr == InitialLifetime) currResult = reader.GetFloatValue(initialLifetime, true); if(!currResult) failureDetected = true; } while (reader.MoveToNextAttribute()); if(!failureDetected) { ParticleSystemBase* particleSystem = new ParticleSystemBase(particleBase, emitter, maxParticles, initialLifetime); ParticleComponent* particleComponent = new ParticleComponent(particleSystem, particleSpace); objectToAdd->AttachComponent(particleComponent); for (int i = 0; i < affectorList.size(); ++i) { particleSystem->AddAffector(affectorList[i]); } if(onGPU) { physx::PxCudaContextManager* cudaMan = objectToAdd->GetOwningScene()->GetCudaContextManager(); if(cudaMan) { particleSystem->onGPU = onGPU; particleSystem->SetPhysXCudaManager(cudaMan); particleSystem->AssignAffectorKernel(particleSystem->FindBestKernel()); } } particleSystem->GetMaterial()->CreateMaterial(particleSystem); particleSystem->setMaterial(particleSystem->GetMaterial()->GetMaterialName()); return true; } } return false; }
bool SceneSerializer::DeserializeRigidBodyComponent(XMLReader& reader, SharedGameObject& objectToAdd) { if(reader.NodeHasAttributes()) { std::string currName; RigidBodyComponent* rigid = new RigidBodyComponent(); objectToAdd->AttachComponent(rigid); bool success = true; bool rigidCreated = false; do { currName = reader.GetCurrentAttributeName(); if(currName == Enabled) { bool enabVal = false; if(reader.GetBoolValue(enabVal, true)) rigid->enabled = enabVal; else success = false; } if(currName == DynamicBodyType) { bool dynamic = false; if(reader.GetBoolValue(dynamic, true)) { if(dynamic) rigid->CreateRigidBody(RigidBodyComponent::DYNAMIC); else rigid->CreateRigidBody(RigidBodyComponent::STATIC, HelperFunctions::OgreToPhysXVec3(objectToAdd->GetWorldPosition())); rigidCreated = true; } else success = false; } } while (reader.MoveToNextAttribute()); //if rigid body wasn't created, don't try to add shapes if(!rigidCreated) return success; //try to move to the child shapes, otherwise continue on if(!reader.MoveToChildNode()) return success; do { currName = reader.GetCurrentAttributeName(); if(currName == ShapeName) { std::string shapeName = reader.GetStringValue(true); if(!rigid->AttachShape(shapeName)) success = false; } } while (reader.MoveToNextSiblingNode()); //pop off shapes node reader.PopNode(); //rigid->CreateDebugDraw(); return success; } return false; }
bool SceneSerializer::DeserializeMeshRenderComponent(XMLReader& reader, SharedGameObject& objectToAdd) { if(reader.NodeHasAttributes()) { MeshRenderComponent* mesh = new MeshRenderComponent(); //Attach even if model loading failed objectToAdd->AttachComponent(mesh); bool modelLoaded = false; std::string currAttr; do { currAttr = reader.GetCurrentAttributeName(); if(currAttr == Enabled) { //Assume enabled is true unless explicitly otherwise bool enabledVal = true; reader.GetBoolValue(enabledVal, true); mesh->enabled = enabledVal; } else if(currAttr == EntityName) { modelLoaded = mesh->LoadModel(reader.GetStringValue(true)); //If errors loading model, Log it if(!modelLoaded) Ogre::LogManager::getSingletonPtr()->getDefaultLog()->logMessage( "Mesh Render Component Deserialization Error: Mesh Not Loaded", Ogre::LML_CRITICAL); } } while (reader.MoveToNextAttribute()); if(reader.MoveToChildNode()) { do { long long currentID = -1; std::string currentMaterial = ""; do { if (reader.GetCurrentAttributeName() == SubentityID) { if(!reader.GetLongValue(currentID, true)) currentID = -1; } else if(reader.GetCurrentAttributeName() == SubentityMaterial) currentMaterial = reader.GetStringValue(true); if(currentID != -1 && currentMaterial != "") mesh->entity->getSubEntity(currentID)->setMaterialName(currentMaterial); } while (reader.MoveToNextAttribute()); } while (reader.MoveToNextSiblingNode()); } reader.PopNode(); return modelLoaded; } return false; }
SharedGameObject SceneSerializer::DeserializedGameObject(XMLReader& reader, IScene* const scene) { SharedGameObject newObject = SharedGameObject(new GameObject(scene)); std::string currNode = reader.GetCurrentNodeName(); //Handle game object attributes if(reader.NodeHasAttributes()) { std::string currAttr; do { currAttr = reader.GetCurrentAttributeName(); if(currAttr == ObjectName) newObject->name = reader.GetStringValue(true); if(currAttr == ObjectTag) newObject->tag = reader.GetStringValue(true); } while (reader.MoveToNextAttribute()); } Ogre::Vector3 objectPosition = Ogre::Vector3(0.0f); Ogre::Quaternion objectRotation = Ogre::Quaternion::IDENTITY; Ogre::Vector3 objectScale = Ogre::Vector3(1.0f); if(reader.MoveToChildNode()) { do { currNode = reader.GetCurrentNodeName(); if(currNode == Position) { if(DeserializeVector3(reader, objectPosition)) newObject->SetWorldPosition(objectPosition); } else if(currNode == Rotation) { if(!DeserializeVector4(reader, objectRotation.x, objectRotation.y, objectRotation.z, objectRotation.w)) printf("Rot Fail\n"); else newObject->SetWorldOrientation(objectRotation); bool inheritRot = true; if(reader.GetBoolValue(inheritRot, false)) newObject->SetInheritOrientation(inheritRot); } else if(currNode == Scale) { if(DeserializeVector3(reader, objectScale)) newObject->SetScale(objectScale); bool inheritScale = true; if(reader.GetBoolValue(inheritScale, false)) newObject->SetInheritScale(inheritScale); } //Handle game object components else if(currNode == MeshRenderComponen) //Not worried about return type because component attaches to game object anyways //and errors get logged into Log Manager DeserializeMeshRenderComponent(reader, newObject); else if(currNode == RigidBodyComponen) { if(!DeserializeRigidBodyComponent(reader, newObject)) printf("Rigid Body Failed\n"); } else if(currNode == ParticleComponen) { DeserializeParticleComponent(reader, newObject); } else if(currNode == GameObjectNode) { SharedGameObject childObject = DeserializedGameObject(reader, scene); if(childObject) newObject->AddChild(childObject); } } while (reader.MoveToNextSiblingNode()); } reader.PopNode(); newObject->SetWorldTransform(objectPosition, objectRotation, objectScale); return newObject; }