Implementing ECS SystemBase and updating slang

This commit is contained in:
Dynamitos
2022-11-15 12:19:11 +01:00
parent 05bc31a2b4
commit f635ee2100
106 changed files with 1083 additions and 1675 deletions
+65 -51
View File
@@ -1,7 +1,8 @@
#include "Scene.h"
#include "Components/PrimitiveComponent.h"
#include "Material/MaterialAsset.h"
#include "Graphics/Graphics.h"
#include "Component/StaticMesh.h"
#include "Component/Transform.h"
using namespace Seele;
@@ -13,19 +14,6 @@ inline float frand()
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()
@@ -34,23 +22,20 @@ Scene::~Scene()
void Scene::start()
{
for(auto actor : rootActors)
{
actor->launchStart();
}
//for(auto actor : rootActors)
//{
// actor->launchStart();
//}
}
static int64 lastUpdate;
static uint64 numUpdates;
void Scene::beginUpdate(double deltaTime)
void Scene::beginUpdate(double)
{
//std::cout << "Scene::beginUpdate" << std::endl;
auto startTime = std::chrono::high_resolution_clock::now();
for(auto actor : rootActors)
{
actor->launchTick(static_cast<float>(deltaTime));
}
// TODO
auto endTime = std::chrono::high_resolution_clock::now();
int64 delta = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
lastUpdate += delta;
@@ -66,38 +51,67 @@ void Scene::beginUpdate(double deltaTime)
void Scene::commitUpdate()
{
//std::cout << "Scene::commitUpdate" << std::endl;
for(auto actor : rootActors)
{
actor->launchUpdate();
}
//for(auto actor : rootActors)
//{
// actor->launchUpdate();
//}
//std::cout << "Scene::commitUpdate finished waiting" << std::endl;
}
void Scene::addActor(PActor actor)
Array<StaticMeshBatch> Scene::getStaticMeshes()
{
rootActors.push_back(actor);
actor->notifySceneAttach(this);
Array<StaticMeshBatch> result;
auto view = registry.view<Component::StaticMesh, Component::Transform>();
struct PrimitiveSceneData
{
Math::Matrix4 localToWorld;
Math::Matrix4 worldToLocal;
Math::Vector4 actorLocation;
};
for(auto&& [entity, mesh, transform] : view.each())
{
PrimitiveSceneData sceneData = {
.localToWorld = transform.toMatrix(),
.worldToLocal = glm::inverse(transform.toMatrix()),
.actorLocation = Math::Vector4(transform.getPosition(), 1.0f)
};
UniformBufferCreateInfo info = {
.resourceData = {
.size = sizeof(PrimitiveSceneData),
.data = (uint8_t*)&sceneData,
}
};
Gfx::PUniformBuffer transformBuf = graphics->createUniformBuffer(info);
auto& batch = result.add();
batch.material = mesh.material;
batch.isBackfaceCullingDisabled = false;
batch.isCastingShadow = true;
batch.topology = Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
batch.useReverseCulling = false;
batch.useWireframe = false;
batch.vertexInput = mesh.vertexBuffer;
MeshBatchElement batchElement;
batchElement.baseVertexIndex = 0;
batchElement.firstIndex = 0;
batchElement.indexBuffer = mesh.indexBuffer;
batchElement.indirectArgsBuffer = nullptr;
batchElement.instanceRuns = nullptr;
batchElement.isInstanced = false;
batchElement.numInstances = 1;
batchElement.uniformBuffer = transformBuf;
batchElement.numPrimitives = static_cast<uint32>(mesh.indexBuffer->getNumIndices() / 3); //TODO: hardcoded
batch.elements.add(batchElement);
}
return result;
}
void Scene::addPrimitiveComponent(PPrimitiveComponent comp)
LightEnv Scene::getLightBuffer() const
{
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);
}
}
LightEnv result;
result.directionalLights[0].color = Math::Vector4(0.4, 0.3, 0.5, 1.0);
result.directionalLights[0].direction = Math::Vector4(0.5, 0.5, 0, 0);
result.directionalLights[0].intensity = Math::Vector4(1.0, 0.9, 0.7, 0.5);\
result.numDirectionalLights = 1;
result.numPointLights = 0;
return result;
}