Adding basic depth prepass
This commit is contained in:
@@ -14,16 +14,17 @@ Actor::~Actor()
|
||||
}
|
||||
void Actor::tick(float deltaTime)
|
||||
{
|
||||
rootComponent->tick(deltaTime);
|
||||
for(auto child : children)
|
||||
{
|
||||
child->tick(deltaTime);
|
||||
}
|
||||
addWorldRotation(glm::vec3(0, 1 * deltaTime, 0));
|
||||
}
|
||||
void Actor::notifySceneAttach(PScene scene)
|
||||
{
|
||||
owningScene = scene;
|
||||
scene->getSceneUpdater()->registerActorUpdate(this);
|
||||
rootComponent->notifySceneAttach(scene);
|
||||
for(auto child : children)
|
||||
{
|
||||
child->notifySceneAttach(scene);
|
||||
}
|
||||
}
|
||||
|
||||
PScene Actor::getScene()
|
||||
|
||||
@@ -15,10 +15,6 @@ Component::~Component()
|
||||
}
|
||||
void Component::tick(float deltaTime)
|
||||
{
|
||||
for (auto child : children)
|
||||
{
|
||||
child->tick(deltaTime);
|
||||
}
|
||||
}
|
||||
PComponent Component::getParent()
|
||||
{
|
||||
@@ -40,6 +36,7 @@ PActor Component::getOwner()
|
||||
void Component::notifySceneAttach(PScene scene)
|
||||
{
|
||||
owningScene = scene;
|
||||
scene->getSceneUpdater()->registerComponentUpdate(this);
|
||||
for (auto child : children)
|
||||
{
|
||||
child->notifySceneAttach(scene);
|
||||
|
||||
+15
-11
@@ -7,15 +7,26 @@
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
inline float frand()
|
||||
{
|
||||
return (float)rand()/RAND_MAX;
|
||||
}
|
||||
|
||||
Scene::Scene(Gfx::PGraphics graphics)
|
||||
: graphics(graphics)
|
||||
, updater(new SceneUpdater())
|
||||
{
|
||||
lightEnv.directionalLights[0].color = Vector4(1, 0, 0, 1);
|
||||
lightEnv.directionalLights[0].direction = Vector4(0, 0, 0, 1);
|
||||
lightEnv.directionalLights[0].color = Vector4(1, 1, 1, 1);
|
||||
lightEnv.directionalLights[0].direction = Vector4(1, 0, 0, 1);
|
||||
lightEnv.directionalLights[0].intensity = Vector4(1, 1, 1, 1);
|
||||
lightEnv.numDirectionalLights = 1;
|
||||
lightEnv.numPointLights = 0;
|
||||
srand((unsigned int)time(NULL));
|
||||
for(uint32 i = 0; i < MAX_POINT_LIGHTS; ++i)
|
||||
{
|
||||
lightEnv.pointLights[i].colorRange = Vector4(frand(), frand(), frand(), frand() * 30);
|
||||
lightEnv.pointLights[i].positionWS = Vector4(frand() * 200-100, frand(), frand() * 200-100, 1);
|
||||
}
|
||||
lightEnv.numPointLights = MAX_POINT_LIGHTS;
|
||||
}
|
||||
|
||||
Scene::~Scene()
|
||||
@@ -24,14 +35,7 @@ Scene::~Scene()
|
||||
|
||||
void Scene::tick(double deltaTime)
|
||||
{
|
||||
lightEnv.directionalLights[0].direction.x += ((rand() / (double)RAND_MAX) - 0.5f) * 100.f * deltaTime;
|
||||
lightEnv.directionalLights[0].direction.y += ((rand() / (double)RAND_MAX) - 0.5f) * 100.f * deltaTime;
|
||||
lightEnv.directionalLights[0].direction.z += ((rand() / (double)RAND_MAX) - 0.5f) * 100.f * deltaTime;
|
||||
std::cout << lightEnv.directionalLights[0].direction << std::endl;
|
||||
for (auto actor : rootActors)
|
||||
{
|
||||
actor->tick(static_cast<float>(deltaTime));
|
||||
}
|
||||
updater->runUpdates(static_cast<float>(deltaTime));
|
||||
}
|
||||
|
||||
void Scene::addActor(PActor actor)
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "Components/PrimitiveComponent.h"
|
||||
#include "Graphics/MeshBatch.h"
|
||||
#include "Material/Material.h"
|
||||
#include "SceneUpdater.h"
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
@@ -46,11 +47,13 @@ public:
|
||||
const Array<PPrimitiveComponent>& getPrimitives() const { return primitives; }
|
||||
const Array<MeshBatch>& getStaticMeshes() const { return staticMeshes; }
|
||||
const LightEnv& getLightEnvironment() const { return lightEnv; }
|
||||
UPSceneUpdater& getSceneUpdater() { return updater; }
|
||||
private:
|
||||
Array<MeshBatch> staticMeshes;
|
||||
Array<PActor> rootActors;
|
||||
Array<PPrimitiveComponent> primitives;
|
||||
LightEnv lightEnv;
|
||||
Gfx::PGraphics graphics;
|
||||
UPSceneUpdater updater;
|
||||
};
|
||||
} // namespace Seele
|
||||
@@ -0,0 +1,75 @@
|
||||
#include "SceneUpdater.h"
|
||||
#include "Components/Component.h"
|
||||
#include "Actor/Actor.h"
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
SceneUpdater::SceneUpdater()
|
||||
: pendingUpdatesSem(0)
|
||||
{
|
||||
running.store(true);
|
||||
workers.resize(std::thread::hardware_concurrency());
|
||||
for (size_t i = 0; i < workers.size(); i++)
|
||||
{
|
||||
workers[i] = std::thread(&SceneUpdater::work, this);
|
||||
}
|
||||
}
|
||||
|
||||
SceneUpdater::~SceneUpdater()
|
||||
{
|
||||
running.store(false);
|
||||
for (size_t i = 0; i < workers.size(); i++)
|
||||
{
|
||||
workers[i].join();
|
||||
}
|
||||
}
|
||||
|
||||
void SceneUpdater::registerComponentUpdate(PComponent component)
|
||||
{
|
||||
std::unique_lock lck(pendingUpdatesLock);
|
||||
updatesRan.add([component](float delta) mutable { component->tick(delta); });
|
||||
}
|
||||
|
||||
void SceneUpdater::registerActorUpdate(PActor actor)
|
||||
{
|
||||
std::unique_lock lck(pendingUpdatesLock);
|
||||
updatesRan.add([actor](float delta) mutable { actor->tick(delta); });
|
||||
}
|
||||
|
||||
void SceneUpdater::runUpdates(float delta)
|
||||
{
|
||||
|
||||
std::unique_lock pendingLock(pendingUpdatesLock);
|
||||
frameDelta = delta;
|
||||
pendingUpdates = std::move(updatesRan);
|
||||
updatesRan = List<std::function<void(float)>>();
|
||||
|
||||
std::unique_lock lck(frameFinishedLock);
|
||||
pendingLock.unlock();
|
||||
pendingUpdatesSem.release(pendingUpdates.size());
|
||||
frameFinishedCV.wait(lck);
|
||||
}
|
||||
|
||||
void SceneUpdater::work()
|
||||
{
|
||||
while(running.load())
|
||||
{
|
||||
pendingUpdatesSem.acquire();
|
||||
std::function<void(float)> function;
|
||||
{
|
||||
std::unique_lock lck(pendingUpdatesLock);
|
||||
function = std::move(pendingUpdates.front());
|
||||
pendingUpdates.popFront();
|
||||
if(pendingUpdates.empty())
|
||||
{
|
||||
std::unique_lock lck(frameFinishedLock);
|
||||
frameFinishedCV.notify_all();
|
||||
}
|
||||
}
|
||||
function(frameDelta);
|
||||
{
|
||||
std::unique_lock lck(pendingUpdatesLock);
|
||||
updatesRan.add(std::move(function));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,30 @@
|
||||
#pragma once
|
||||
#include "Containers/Array.h"
|
||||
#include "Containers/List.h"
|
||||
#include <semaphore>
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
DECLARE_REF(Component);
|
||||
DECLARE_REF(Actor);
|
||||
class SceneUpdater
|
||||
{
|
||||
public:
|
||||
SceneUpdater();
|
||||
~SceneUpdater();
|
||||
void registerComponentUpdate(PComponent component);
|
||||
void registerActorUpdate(PActor actor);
|
||||
void runUpdates(float delta);
|
||||
private:
|
||||
Array<std::thread> workers;
|
||||
List<std::function<void(float)> pendingUpdates;
|
||||
std::mutex pendingUpdatesLock;
|
||||
std::counting_semaphore<> pendingUpdatesSem;
|
||||
List<std::function<void(float)>> pendingUpdates;
|
||||
List<std::function<void(float)>> updatesRan;
|
||||
void work();
|
||||
float frameDelta;
|
||||
std::atomic_bool running;
|
||||
std::mutex frameFinishedLock;
|
||||
std::condition_variable frameFinishedCV;
|
||||
};
|
||||
DEFINE_REF(SceneUpdater)
|
||||
} // namespace Seele
|
||||
|
||||
Reference in New Issue
Block a user