Files
Seele/src/Engine/Scene/Scene.cpp
T
2022-04-13 13:01:46 +02:00

109 lines
3.1 KiB
C++

#include "Scene.h"
#include "Components/PrimitiveComponent.h"
#include "Components/PrimitiveUniformBufferLayout.h"
#include "Material/MaterialAsset.h"
#include "Graphics/Graphics.h"
using namespace Seele;
inline float frand()
{
return (float)rand()/RAND_MAX;
}
Scene::Scene(Gfx::PGraphics graphics)
: graphics(graphics)
{
lightEnv.directionalLights[0].color = Vector4(1, 1, 1, 1);
lightEnv.directionalLights[0].direction = Vector4(1, 1, 0, 1);
lightEnv.directionalLights[0].intensity = Vector4(1, 1, 1, 1);
lightEnv.numDirectionalLights = 1;
srand((unsigned int)time(NULL));
for(uint32 i = 0; i < 16; ++i)
{
lightEnv.pointLights[i].colorRange = Vector4(frand(), frand(), frand(), frand() * 300);
lightEnv.pointLights[i].positionWS = Vector4(frand() * 100-50, frand(), frand() * 100-50, 1);
}
lightEnv.numPointLights = 16;
lightEnv.pointLights[0].colorRange = Vector4(1, 0, 1, 1000);
lightEnv.pointLights[0].positionWS = Vector4(0, 10, 0, 1);
}
Scene::~Scene()
{
}
void Scene::start()
{
for(auto actor : rootActors)
{
actor->launchStart();
}
}
static int64 lastUpdate;
static uint64 numUpdates;
Job Scene::beginUpdate(double deltaTime)
{
//std::cout << "Scene::beginUpdate" << std::endl;
auto startTime = std::chrono::high_resolution_clock::now();
//Array<Job> jobs;
for(auto actor : rootActors)
{
co_await Job::all(actor->launchTick(static_cast<float>(deltaTime)));
}
//co_await Job::all(std::move(jobs));
auto endTime = std::chrono::high_resolution_clock::now();
int64 delta = (endTime - startTime).count();
lastUpdate += delta;
numUpdates++;
if(lastUpdate > 1000)
{
lastUpdate -= 1000;
std::cout << numUpdates << " updates per second" << std::endl;
numUpdates = 0;
}
}
Job Scene::commitUpdate()
{
//std::cout << "Scene::commitUpdate" << std::endl;
//std::vector<Job> jobs;
for(auto actor : rootActors)
{
co_await Job::all(actor->launchUpdate());
}
//co_await Job::all(std::move(jobs));
//std::cout << "Scene::commitUpdate finished waiting" << std::endl;
}
void Scene::addActor(PActor actor)
{
rootActors.push_back(actor);
actor->notifySceneAttach(this);
}
void Scene::addPrimitiveComponent(PPrimitiveComponent comp)
{
primitives.push_back(comp);
for(auto& batch : comp->getStaticMeshes())
{
PrimitiveUniformBuffer data;
data.actorWorldPosition = Vector4(comp->getTransform().getPosition(), 1);
data.localToWorld = comp->getRenderMatrix();
data.worldToLocal = glm::inverse(data.localToWorld);
UniformBufferCreateInfo createInfo;
createInfo.resourceData.data = reinterpret_cast<uint8*>(&data);
createInfo.resourceData.owner = Gfx::QueueType::GRAPHICS;
createInfo.resourceData.size = sizeof(data);
createInfo.bDynamic = true;
Gfx::PUniformBuffer uniformBuffer = graphics->createUniformBuffer(createInfo);
for(auto& element : batch.elements)
{
element.uniformBuffer = uniformBuffer;
}
staticMeshes.push_back(batch);
}
}