Adding nlohmanns json library
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "Actor.h"
|
||||
#include "Component.h"
|
||||
#include "Scene/Components/Component.h"
|
||||
#include "Scene/Scene.h"
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
@@ -11,6 +12,25 @@ Actor::~Actor()
|
||||
{
|
||||
|
||||
}
|
||||
void Actor::tick(float deltaTime)
|
||||
{
|
||||
rootComponent->tick(deltaTime);
|
||||
for(auto child : children)
|
||||
{
|
||||
child->tick(deltaTime);
|
||||
}
|
||||
}
|
||||
void Actor::notifySceneAttach(PScene scene)
|
||||
{
|
||||
owningScene = scene;
|
||||
rootComponent->notifySceneAttach(scene);
|
||||
}
|
||||
|
||||
PScene Actor::getScene()
|
||||
{
|
||||
return owningScene;
|
||||
}
|
||||
|
||||
void Actor::setParent(PActor newParent)
|
||||
{
|
||||
parent = newParent;
|
||||
@@ -18,10 +38,14 @@ void Actor::setParent(PActor newParent)
|
||||
void Actor::addChild(PActor child)
|
||||
{
|
||||
children.add(child);
|
||||
child->setParent(this);
|
||||
child->notifySceneAttach(owningScene);
|
||||
}
|
||||
void Actor::detachChild(PActor child)
|
||||
{
|
||||
children.remove(children.find(child), false);
|
||||
child->setParent(nullptr);
|
||||
child->notifySceneAttach(nullptr);
|
||||
}
|
||||
void Actor::setWorldLocation(Vector location)
|
||||
{
|
||||
@@ -55,15 +79,16 @@ void Actor::addWorldRotation(Vector4 rotation)
|
||||
{
|
||||
rootComponent->addWorldRotation(rotation);
|
||||
}
|
||||
void Actor::addWorldScale(Vector scale)
|
||||
{
|
||||
rootComponent->addWorldScale(scale);
|
||||
}
|
||||
PComponent Actor::getRootComponent()
|
||||
{
|
||||
return rootComponent;
|
||||
}
|
||||
void Actor::setRootComponent(PComponent newRoot)
|
||||
{
|
||||
if(rootComponent != nullptr)
|
||||
{
|
||||
rootComponent->owner = nullptr;
|
||||
}
|
||||
rootComponent = newRoot;
|
||||
rootComponent->owner = this;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
#pragma once
|
||||
#include "MinimalEngine.h"
|
||||
#include "Transform.h"
|
||||
#include "Math/Transform.h"
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
DECLARE_REF(Actor);
|
||||
DECLARE_REF(Component);
|
||||
DECLARE_REF(Scene);
|
||||
class Actor
|
||||
{
|
||||
public:
|
||||
Actor();
|
||||
virtual ~Actor();
|
||||
void setParent(PActor parent);
|
||||
|
||||
void tick(float deltaTime);
|
||||
void notifySceneAttach(PScene scene);
|
||||
PScene getScene();
|
||||
|
||||
PActor getParent();
|
||||
void addChild(PActor child);
|
||||
void detachChild(PActor child);
|
||||
@@ -23,18 +28,20 @@ public:
|
||||
void setRelativeLocation(Vector location);
|
||||
void setRelativeRotation(Vector4 rotation);
|
||||
void setRelativeScale(Vector scale);
|
||||
|
||||
|
||||
void addWorldTranslation(Vector translation);
|
||||
void addWorldRotation(Vector4 rotation);
|
||||
void addWorldScale(Vector scale);
|
||||
|
||||
|
||||
PComponent getRootComponent();
|
||||
void setRootComponent(PComponent newRoot);
|
||||
|
||||
private:
|
||||
void setParent(PActor parent);
|
||||
PScene owningScene;
|
||||
PActor parent;
|
||||
Array<PActor> children;
|
||||
PComponent rootComponent;
|
||||
Transform transform;
|
||||
};
|
||||
DEFINE_REF(Actor);
|
||||
}
|
||||
} // namespace Seele
|
||||
@@ -1,4 +1,6 @@
|
||||
#include "Component.h"
|
||||
#include "Scene/Actor/Actor.h"
|
||||
#include "Scene/Scene.h"
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
@@ -8,58 +10,123 @@ Component::Component()
|
||||
Component::~Component()
|
||||
{
|
||||
}
|
||||
void Component::tick(float deltaTime)
|
||||
{
|
||||
for (auto child : children)
|
||||
{
|
||||
child->tick(deltaTime);
|
||||
}
|
||||
}
|
||||
PComponent Component::getParent()
|
||||
{
|
||||
|
||||
return parent;
|
||||
}
|
||||
PActor Component::getOwner()
|
||||
{
|
||||
if (owner != nullptr)
|
||||
{
|
||||
return owner;
|
||||
}
|
||||
if (parent != nullptr)
|
||||
{
|
||||
return parent->getOwner();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Component::notifySceneAttach(PScene scene)
|
||||
{
|
||||
owningScene = scene;
|
||||
for (auto child : children)
|
||||
{
|
||||
child->notifySceneAttach(scene);
|
||||
}
|
||||
}
|
||||
void Component::setWorldLocation(Vector location)
|
||||
{
|
||||
Vector newRelLocation = location;
|
||||
if (parent != nullptr)
|
||||
{
|
||||
Transform parentToWorld = getParent()->getTransform();
|
||||
newRelLocation = parentToWorld.inverseTransformPosition(location);
|
||||
}
|
||||
setRelativeLocation(newRelLocation);
|
||||
}
|
||||
void Component::setWorldRotation(Vector rotation)
|
||||
{
|
||||
Vector newRelRotator = rotation;
|
||||
if (parent == nullptr)
|
||||
{
|
||||
setRelativeRotation(rotation);
|
||||
}
|
||||
else
|
||||
{
|
||||
setWorldRotation(toQuaternion(newRelRotator));
|
||||
}
|
||||
}
|
||||
void Component::setWorldRotation(Quaternion rotation)
|
||||
{
|
||||
Quaternion newRelRotation = getRelativeWorldRotation(rotation);
|
||||
setRelativeRotation(newRelRotation);
|
||||
}
|
||||
void Component::setWorldScale(Vector scale)
|
||||
{
|
||||
Vector newRelScale = scale;
|
||||
if (parent != nullptr)
|
||||
{
|
||||
Transform parentToWorld = parent->getTransform();
|
||||
newRelScale = scale * parentToWorld.getSafeScaleReciprocal(Vector4(parentToWorld.getScale(), 0));
|
||||
}
|
||||
setRelativeScale(newRelScale);
|
||||
}
|
||||
void Component::setRelativeLocation(Vector location)
|
||||
{
|
||||
setRelativeLocationAndRotation(location, relativeRotation);
|
||||
}
|
||||
void Component::setRelativeRotation(Vector rotation)
|
||||
{
|
||||
setRelativeLocationAndRotation(relativeLocation, toQuaternion(rotation));
|
||||
}
|
||||
void Component::setRelativeRotation(Quaternion rotation)
|
||||
{
|
||||
setRelativeLocationAndRotation(relativeLocation, rotation);
|
||||
}
|
||||
void Component::setRelativeScale(Vector scale)
|
||||
{
|
||||
if (scale != relativeScale)
|
||||
{
|
||||
relativeScale = scale;
|
||||
updateComponentTransform(relativeRotation);
|
||||
}
|
||||
}
|
||||
void Component::addWorldTranslation(Vector translation)
|
||||
{
|
||||
const Vector newWorldLocation = translation + transform.getPosition();
|
||||
setWorldLocation(newWorldLocation);
|
||||
}
|
||||
void Component::addWorldRotation(Vector rotation)
|
||||
{
|
||||
const Quaternion newWorldRotation = toQuaternion(rotation) * transform.getRotation();
|
||||
setWorldRotation(newWorldRotation);
|
||||
}
|
||||
void Component::addWorldRotation(Quaternion rotation)
|
||||
{
|
||||
}
|
||||
void Component::addWorldScale(Vector scale)
|
||||
{
|
||||
const Quaternion newWorldRotation = rotation * transform.getRotation();
|
||||
setWorldRotation(newWorldRotation);
|
||||
}
|
||||
Transform Component::getTransform() const
|
||||
{
|
||||
return transform;
|
||||
}
|
||||
|
||||
void Component::setParent(PComponent newParent)
|
||||
{
|
||||
parent = newParent;
|
||||
}
|
||||
|
||||
void Component::internalSetTransform(Vector newLocation, Quaternion newRotation)
|
||||
{
|
||||
if(parent != nullptr)
|
||||
if (parent != nullptr)
|
||||
{
|
||||
Transform parentTransform = parent->getTransform();
|
||||
newLocation = parentTransform.inverseTransformPosition(newLocation);
|
||||
@@ -67,7 +134,7 @@ void Component::internalSetTransform(Vector newLocation, Quaternion newRotation)
|
||||
}
|
||||
|
||||
const Vector newRelRotation = toRotator(newRotation);
|
||||
if(newLocation != relativeLocation || newRelRotation != relativeLocation)
|
||||
if (newLocation != relativeLocation || newRelRotation != relativeLocation)
|
||||
{
|
||||
relativeLocation = newLocation;
|
||||
relativeRotation = newRelRotation;
|
||||
@@ -75,24 +142,21 @@ void Component::internalSetTransform(Vector newLocation, Quaternion newRotation)
|
||||
}
|
||||
}
|
||||
|
||||
void Component::propagateTransformUpdate()
|
||||
void Component::propagateTransformUpdate()
|
||||
{
|
||||
for(auto child : children)
|
||||
for (auto child : children)
|
||||
{
|
||||
if(child != nullptr)
|
||||
{
|
||||
child->updateComponentTransform(child->relativeRotation);
|
||||
}
|
||||
child->updateComponentTransform(child->relativeRotation);
|
||||
}
|
||||
}
|
||||
|
||||
void Component::updateComponentTransform(Quaternion relativeRotationQuat)
|
||||
void Component::updateComponentTransform(Quaternion relativeRotationQuat)
|
||||
{
|
||||
if(parent != nullptr && !parent->bComponentTransformClean)
|
||||
if (parent != nullptr && !parent->bComponentTransformClean)
|
||||
{
|
||||
parent->updateComponentTransform(parent->relativeRotation);
|
||||
|
||||
if(bComponentTransformClean)
|
||||
if (bComponentTransformClean)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -100,9 +164,41 @@ void Component::updateComponentTransform(Quaternion relativeRotationQuat)
|
||||
bComponentTransformClean = true;
|
||||
Transform newTransform;
|
||||
const Transform relTransform(relativeLocation, relativeRotationQuat, relativeScale);
|
||||
if(parent != nullptr)
|
||||
if (parent != nullptr)
|
||||
{
|
||||
newTransform = parent->getTransform() * relTransform;
|
||||
}
|
||||
|
||||
bool bHasChanged = !getTransform().equals(newTransform);
|
||||
if (bHasChanged)
|
||||
{
|
||||
transform = newTransform;
|
||||
propagateTransformUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
Quaternion Component::getRelativeWorldRotation(Quaternion worldRotation)
|
||||
{
|
||||
Quaternion newRelRotation = worldRotation;
|
||||
if (parent != nullptr)
|
||||
{
|
||||
const Transform parentToWorld = parent->getTransform();
|
||||
|
||||
const Quaternion parentToWorldQuat = parentToWorld.getRotation();
|
||||
const Quaternion newRelQuat = glm::inverse(parentToWorldQuat) * worldRotation;
|
||||
newRelRotation = newRelQuat;
|
||||
}
|
||||
return newRelRotation;
|
||||
}
|
||||
|
||||
void Component::setRelativeLocationAndRotation(Vector newLocation, Quaternion newRotation)
|
||||
{
|
||||
if (!bComponentTransformClean)
|
||||
{
|
||||
updateComponentTransform(toQuaternion(relativeRotation));
|
||||
}
|
||||
|
||||
const Transform desiredRelTransform(newLocation, newRotation);
|
||||
const Transform desiredWorldTransform = parent->getTransform() * desiredRelTransform;
|
||||
|
||||
internalSetTransform(desiredWorldTransform.getPosition(), desiredWorldTransform.getRotation());
|
||||
}
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
#pragma once
|
||||
#include "MinimalEngine.h"
|
||||
#include "Transform.h"
|
||||
#include "Math/Transform.h"
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
DECLARE_REF(Actor);
|
||||
DECLARE_REF(Scene);
|
||||
DECLARE_REF(Component);
|
||||
class Component
|
||||
{
|
||||
public:
|
||||
Component();
|
||||
virtual ~Component();
|
||||
void tick(float deltaTime);
|
||||
PComponent getParent();
|
||||
void setParent(PComponent parent);
|
||||
PActor getOwner();
|
||||
void setOwner(PActor owner);
|
||||
virtual void notifySceneAttach(PScene scene);
|
||||
|
||||
void setWorldLocation(Vector location);
|
||||
void setWorldRotation(Vector rotation);
|
||||
@@ -24,25 +26,30 @@ public:
|
||||
void setRelativeRotation(Vector rotation);
|
||||
void setRelativeRotation(Quaternion rotation);
|
||||
void setRelativeScale(Vector scale);
|
||||
|
||||
|
||||
void addWorldTranslation(Vector translation);
|
||||
void addWorldRotation(Vector rotation);
|
||||
void addWorldRotation(Quaternion rotation);
|
||||
void addWorldScale(Vector scale);
|
||||
|
||||
Transform getTransform() const;
|
||||
|
||||
private:
|
||||
void setParent(PComponent parent);
|
||||
void internalSetTransform(Vector newLocation, Quaternion newRotation);
|
||||
void propagateTransformUpdate();
|
||||
void updateComponentTransform(Quaternion relativeRotationQuat);
|
||||
uint8 bComponentTransformClean:1;
|
||||
Quaternion getRelativeWorldRotation(Quaternion worldRotation);
|
||||
void setRelativeLocationAndRotation(Vector newLocation, Quaternion newRotation);
|
||||
uint8 bComponentTransformClean : 1;
|
||||
Vector relativeLocation;
|
||||
Vector relativeRotation;
|
||||
Vector relativeScale;
|
||||
Transform transform;
|
||||
PScene owningScene;
|
||||
PActor owner;
|
||||
PComponent parent;
|
||||
Array<PComponent> children;
|
||||
friend class Actor;
|
||||
};
|
||||
DEFINE_REF(Component)
|
||||
}
|
||||
} // namespace Seele
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "PrimitiveComponent.h"
|
||||
#include "Scene/Scene.h"
|
||||
#include "Material/MaterialInstance.h"
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
PrimitiveComponent::PrimitiveComponent()
|
||||
{
|
||||
}
|
||||
|
||||
PrimitiveComponent::~PrimitiveComponent()
|
||||
{
|
||||
}
|
||||
|
||||
void PrimitiveComponent::notifySceneAttach(PScene scene)
|
||||
{
|
||||
scene->addPrimitiveComponent(this);
|
||||
}
|
||||
|
||||
Matrix4 PrimitiveComponent::getRenderMatrix()
|
||||
{
|
||||
return getTransform().toMatrix();
|
||||
}
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
#pragma once
|
||||
#include "Component.h"
|
||||
#include "Graphics/GraphicsResources.h"
|
||||
#include "Material/MaterialInstance.h"
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
class PrimitiveComponent : public Component
|
||||
{
|
||||
public:
|
||||
private:
|
||||
PrimitiveComponent();
|
||||
~PrimitiveComponent();
|
||||
virtual void notifySceneAttach(PScene scene) override;
|
||||
Matrix4 getRenderMatrix();
|
||||
|
||||
private:
|
||||
PMaterialInstance instance;
|
||||
Gfx::PVertexBuffer vertexBuffer;
|
||||
Gfx::PIndexBuffer indexBuffer;
|
||||
friend class Scene;
|
||||
};
|
||||
}
|
||||
DEFINE_REF(PrimitiveComponent);
|
||||
} // namespace Seele
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "Scene.h"
|
||||
#include "Components/PrimitiveComponent.h"
|
||||
#include "Material/MaterialInstance.h"
|
||||
#include "Material/Material.h"
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
Scene::Scene()
|
||||
{
|
||||
}
|
||||
|
||||
Scene::~Scene()
|
||||
{
|
||||
}
|
||||
|
||||
void Scene::tick(float deltaTime)
|
||||
{
|
||||
for (auto actor : rootActors)
|
||||
{
|
||||
actor->tick(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::addActor(PActor actor)
|
||||
{
|
||||
rootActors.add(actor);
|
||||
actor->notifySceneAttach(this);
|
||||
}
|
||||
|
||||
void Scene::addPrimitiveComponent(PPrimitiveComponent comp)
|
||||
{
|
||||
primitives.add(comp);
|
||||
}
|
||||
|
||||
Map<PMaterial, DrawState> Scene::getMeshBatches()
|
||||
{
|
||||
meshBatches.clear();
|
||||
for (auto primitive : primitives)
|
||||
{
|
||||
PMaterialInstance matInstance = primitive->instance;
|
||||
PMaterial mat = matInstance->getBaseMaterial();
|
||||
DrawInstance inst;
|
||||
inst.instance = primitive->instance;
|
||||
inst.vertexBuffer = primitive->vertexBuffer;
|
||||
inst.indexBuffer = primitive->indexBuffer;
|
||||
inst.modelMatrix = primitive->getRenderMatrix();
|
||||
|
||||
if (meshBatches.find(mat) != meshBatches.end())
|
||||
{
|
||||
DrawState &state = meshBatches[mat];
|
||||
state.instances.add(inst);
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawState state;
|
||||
state.instances.add(inst);
|
||||
meshBatches[mat] = state;
|
||||
}
|
||||
}
|
||||
return meshBatches;
|
||||
}
|
||||
@@ -1,14 +1,28 @@
|
||||
#pragma once
|
||||
#include "MinimalEngine.h"
|
||||
#include "Actor/Actor.h"
|
||||
#include "Graphics/GraphicsResources.h"
|
||||
#include "Components/PrimitiveComponent.h"
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
DECLARE_REF(Material);
|
||||
|
||||
class Scene
|
||||
{
|
||||
public:
|
||||
Scene();
|
||||
~Scene();
|
||||
void tick(float deltaTime);
|
||||
void addActor(PActor actor);
|
||||
void addPrimitiveComponent(PPrimitiveComponent comp);
|
||||
|
||||
private:
|
||||
Map<PMaterial, DrawState> meshBatches;
|
||||
Array<PActor> rootActors;
|
||||
Array<PPrimitiveComponent> primitives;
|
||||
|
||||
public:
|
||||
Map<PMaterial, DrawState> getMeshBatches();
|
||||
};
|
||||
}
|
||||
} // namespace Seele
|
||||
Reference in New Issue
Block a user