Files
Seele/src/Engine/Scene/Scene.cpp
T

91 lines
2.8 KiB
C++
Raw Normal View History

2020-05-05 01:51:13 +02:00
#include "Scene.h"
2020-09-19 14:36:50 +02:00
#include "Graphics/Graphics.h"
2022-11-30 10:19:45 +01:00
#include "Graphics/Mesh.h"
#include "Component/StaticMesh.h"
#include "Component/Transform.h"
2020-05-05 01:51:13 +02:00
using namespace Seele;
2021-05-06 17:02:10 +02:00
inline float frand()
{
return (float)rand()/RAND_MAX;
}
2020-09-19 14:36:50 +02:00
Scene::Scene(Gfx::PGraphics graphics)
: graphics(graphics)
2023-01-21 18:43:21 +01:00
, physics(registry)
2020-05-05 01:51:13 +02:00
{
}
Scene::~Scene()
{
}
2023-01-21 18:43:21 +01:00
void Scene::update(float deltaTime)
2020-05-05 01:51:13 +02:00
{
2023-01-21 18:43:21 +01:00
physics.update(deltaTime);
2020-05-05 01:51:13 +02:00
}
Array<StaticMeshBatch> Scene::getStaticMeshes()
2020-05-05 01:51:13 +02:00
{
Array<StaticMeshBatch> result;
auto view = registry.view<Component::StaticMesh, Component::Transform>();
2023-01-21 18:43:21 +01:00
uint32 sceneDataIndex = 0;
sceneData.clear();
for(auto&& [entity, mesh, transform] : view.each())
{
2023-01-21 18:43:21 +01:00
sceneData.add(PrimitiveSceneData {
.localToWorld = transform.toMatrix(),
.worldToLocal = glm::inverse(transform.toMatrix()),
2023-01-21 18:43:21 +01:00
.actorLocation = Vector4(transform.getPosition(), 1.0f)
});
2022-11-30 10:19:45 +01:00
for(auto& m : mesh.mesh->meshes)
{
auto& batch = result.add();
batch.material = m->referencedMaterial;
batch.isBackfaceCullingDisabled = false;
batch.isCastingShadow = true;
batch.topology = Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
batch.useReverseCulling = false;
batch.useWireframe = false;
batch.vertexInput = m->vertexInput;
MeshBatchElement batchElement;
batchElement.baseVertexIndex = 0;
batchElement.firstIndex = 0;
batchElement.indexBuffer = m->indexBuffer;
batchElement.indirectArgsBuffer = nullptr;
batchElement.isInstanced = false;
batchElement.numInstances = 1;
2023-01-21 18:43:21 +01:00
batchElement.sceneDataIndex = sceneDataIndex;
2022-11-30 10:19:45 +01:00
batch.elements.add(batchElement);
}
2023-01-21 18:43:21 +01:00
sceneDataIndex++;
}
2023-01-21 18:43:21 +01:00
if(sceneDataBuffer == nullptr || sceneDataBuffer->getNumElements() != sceneData.size())
{
StructuredBufferCreateInfo createInfo = StructuredBufferCreateInfo {
.resourceData = {
.size = sceneData.size() * sizeof(PrimitiveSceneData),
.data = nullptr,
},
.stride = (uint32)sceneData.size(),
};
sceneDataBuffer = graphics->createStructuredBuffer(createInfo);
}
sceneDataBuffer->updateContents(BulkResourceData {
.size = sceneData.size() * sizeof(PrimitiveSceneData),
.data = (uint8*)sceneData.data(),
});
return result;
2020-05-05 01:51:13 +02:00
}
LightEnv Scene::getLightBuffer() const
2020-05-05 01:51:13 +02:00
{
LightEnv result;
2023-01-21 18:43:21 +01:00
result.directionalLights[0].color = Vector4(0.4, 0.3, 0.5, 1.0);
result.directionalLights[0].direction = Vector4(0.5, 0.5, 0, 0);
result.directionalLights[0].intensity = Vector4(1.0, 0.9, 0.7, 0.5);\
result.numDirectionalLights = 1;
result.numPointLights = 0;
return result;
}