2020-05-05 01:51:13 +02:00
|
|
|
#include "Scene.h"
|
2021-10-19 23:04:38 +02:00
|
|
|
#include "Material/MaterialAsset.h"
|
2020-09-19 14:36:50 +02:00
|
|
|
#include "Graphics/Graphics.h"
|
2022-11-15 12:19:11 +01:00
|
|
|
#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)
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Scene::~Scene()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 14:40:26 +01:00
|
|
|
void Scene::start()
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
//for(auto actor : rootActors)
|
|
|
|
|
//{
|
|
|
|
|
// actor->launchStart();
|
|
|
|
|
//}
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-13 13:01:35 +02:00
|
|
|
static int64 lastUpdate;
|
2022-03-19 22:45:30 +01:00
|
|
|
static uint64 numUpdates;
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
void Scene::beginUpdate(double)
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
2022-02-14 16:29:26 +01:00
|
|
|
//std::cout << "Scene::beginUpdate" << std::endl;
|
2022-03-19 22:45:30 +01:00
|
|
|
auto startTime = std::chrono::high_resolution_clock::now();
|
2022-11-15 12:19:11 +01:00
|
|
|
// TODO
|
2022-03-19 22:45:30 +01:00
|
|
|
auto endTime = std::chrono::high_resolution_clock::now();
|
2022-04-15 23:45:44 +02:00
|
|
|
int64 delta = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
|
2022-03-19 22:45:30 +01:00
|
|
|
lastUpdate += delta;
|
|
|
|
|
numUpdates++;
|
2022-04-15 23:45:44 +02:00
|
|
|
if(lastUpdate > 1000000)
|
2022-02-14 16:29:26 +01:00
|
|
|
{
|
2022-04-15 23:45:44 +02:00
|
|
|
lastUpdate -= 1000000;
|
2022-03-19 22:45:30 +01:00
|
|
|
std::cout << numUpdates << " updates per second" << std::endl;
|
|
|
|
|
numUpdates = 0;
|
2022-01-12 14:40:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:45:44 +02:00
|
|
|
void Scene::commitUpdate()
|
2022-01-12 14:40:26 +01:00
|
|
|
{
|
2022-02-14 16:29:26 +01:00
|
|
|
//std::cout << "Scene::commitUpdate" << std::endl;
|
2022-11-15 12:19:11 +01:00
|
|
|
//for(auto actor : rootActors)
|
|
|
|
|
//{
|
|
|
|
|
// actor->launchUpdate();
|
|
|
|
|
//}
|
2022-02-14 16:29:26 +01:00
|
|
|
//std::cout << "Scene::commitUpdate finished waiting" << std::endl;
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
Array<StaticMeshBatch> Scene::getStaticMeshes()
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
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;
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
LightEnv Scene::getLightBuffer() const
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
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;
|
|
|
|
|
}
|