overhauled physics engine
This commit is contained in:
+28
-59
@@ -1,5 +1,4 @@
|
||||
#include "Scene.h"
|
||||
#include "Material/MaterialAsset.h"
|
||||
#include "Graphics/Graphics.h"
|
||||
#include "Graphics/Mesh.h"
|
||||
#include "Component/StaticMesh.h"
|
||||
@@ -14,6 +13,7 @@ inline float frand()
|
||||
|
||||
Scene::Scene(Gfx::PGraphics graphics)
|
||||
: graphics(graphics)
|
||||
, physics(registry)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -21,69 +21,24 @@ Scene::~Scene()
|
||||
{
|
||||
}
|
||||
|
||||
void Scene::start()
|
||||
void Scene::update(float deltaTime)
|
||||
{
|
||||
//for(auto actor : rootActors)
|
||||
//{
|
||||
// actor->launchStart();
|
||||
//}
|
||||
}
|
||||
|
||||
static int64 lastUpdate;
|
||||
static uint64 numUpdates;
|
||||
|
||||
void Scene::beginUpdate(double)
|
||||
{
|
||||
//std::cout << "Scene::beginUpdate" << std::endl;
|
||||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
// TODO
|
||||
auto endTime = std::chrono::high_resolution_clock::now();
|
||||
int64 delta = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
|
||||
lastUpdate += delta;
|
||||
numUpdates++;
|
||||
if(lastUpdate > 1000000)
|
||||
{
|
||||
lastUpdate -= 1000000;
|
||||
std::cout << numUpdates << " updates per second" << std::endl;
|
||||
numUpdates = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::commitUpdate()
|
||||
{
|
||||
//std::cout << "Scene::commitUpdate" << std::endl;
|
||||
//for(auto actor : rootActors)
|
||||
//{
|
||||
// actor->launchUpdate();
|
||||
//}
|
||||
//std::cout << "Scene::commitUpdate finished waiting" << std::endl;
|
||||
physics.update(deltaTime);
|
||||
}
|
||||
|
||||
Array<StaticMeshBatch> Scene::getStaticMeshes()
|
||||
{
|
||||
Array<StaticMeshBatch> result;
|
||||
auto view = registry.view<Component::StaticMesh, Component::Transform>();
|
||||
struct PrimitiveSceneData
|
||||
{
|
||||
Math::Matrix4 localToWorld;
|
||||
Math::Matrix4 worldToLocal;
|
||||
Math::Vector4 actorLocation;
|
||||
};
|
||||
uint32 sceneDataIndex = 0;
|
||||
sceneData.clear();
|
||||
for(auto&& [entity, mesh, transform] : view.each())
|
||||
{
|
||||
PrimitiveSceneData sceneData = {
|
||||
sceneData.add(PrimitiveSceneData {
|
||||
.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);
|
||||
// TODO
|
||||
.actorLocation = Vector4(transform.getPosition(), 1.0f)
|
||||
});
|
||||
for(auto& m : mesh.mesh->meshes)
|
||||
{
|
||||
auto& batch = result.add();
|
||||
@@ -99,23 +54,37 @@ Array<StaticMeshBatch> Scene::getStaticMeshes()
|
||||
batchElement.firstIndex = 0;
|
||||
batchElement.indexBuffer = m->indexBuffer;
|
||||
batchElement.indirectArgsBuffer = nullptr;
|
||||
batchElement.instanceRuns = nullptr;
|
||||
batchElement.isInstanced = false;
|
||||
batchElement.numInstances = 1;
|
||||
batchElement.uniformBuffer = transformBuf;
|
||||
batchElement.numPrimitives = static_cast<uint32>(m->indexBuffer->getNumIndices() / 3); //TODO: hardcoded
|
||||
batchElement.sceneDataIndex = sceneDataIndex;
|
||||
batch.elements.add(batchElement);
|
||||
}
|
||||
sceneDataIndex++;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
LightEnv Scene::getLightBuffer() const
|
||||
{
|
||||
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.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;
|
||||
|
||||
+24
-11
@@ -3,6 +3,7 @@
|
||||
#include "MinimalEngine.h"
|
||||
#include "Graphics/GraphicsResources.h"
|
||||
#include "Graphics/MeshBatch.h"
|
||||
#include "Physics/PhysicsSystem.h"
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
@@ -11,16 +12,16 @@ DECLARE_REF(Entity)
|
||||
|
||||
struct DirectionalLight
|
||||
{
|
||||
Math::Vector4 color;
|
||||
Math::Vector4 direction;
|
||||
Math::Vector4 intensity;
|
||||
Vector4 color;
|
||||
Vector4 direction;
|
||||
Vector4 intensity;
|
||||
};
|
||||
|
||||
struct PointLight
|
||||
{
|
||||
Math::Vector4 positionWS;
|
||||
Vector4 positionWS;
|
||||
//Vector4 positionVS;
|
||||
Math::Vector4 colorRange;
|
||||
Vector4 colorRange;
|
||||
};
|
||||
|
||||
#define MAX_DIRECTIONAL_LIGHTS 4
|
||||
@@ -38,17 +39,19 @@ class Scene
|
||||
public:
|
||||
Scene(Gfx::PGraphics graphics);
|
||||
~Scene();
|
||||
void start();
|
||||
void beginUpdate(double deltaTime);
|
||||
void commitUpdate();
|
||||
void update(float deltaTime);
|
||||
entt::entity createEntity()
|
||||
{
|
||||
return registry.create();
|
||||
}
|
||||
template<typename Component, typename... Args>
|
||||
Component& attachComponent(entt::entity entity, Args... args)
|
||||
void destroyEntity(entt::entity identifier)
|
||||
{
|
||||
return registry.emplace<Component>(entity, args...);
|
||||
registry.destroy(identifier);
|
||||
}
|
||||
template<typename Component, typename... Args>
|
||||
Component& attachComponent(entt::entity entity, Args&&... args)
|
||||
{
|
||||
return registry.emplace<Component>(entity, std::forward<Args>(args)...);
|
||||
}
|
||||
template<typename Component>
|
||||
Component& accessComponent(entt::entity entity)
|
||||
@@ -62,9 +65,19 @@ public:
|
||||
}
|
||||
Array<StaticMeshBatch> getStaticMeshes();
|
||||
LightEnv getLightBuffer() const;
|
||||
Gfx::PStructuredBuffer getSceneDataBuffer() const { return sceneDataBuffer; }
|
||||
|
||||
entt::registry registry;
|
||||
private:
|
||||
struct PrimitiveSceneData
|
||||
{
|
||||
Matrix4 localToWorld;
|
||||
Matrix4 worldToLocal;
|
||||
Vector4 actorLocation;
|
||||
};
|
||||
Array<PrimitiveSceneData> sceneData;
|
||||
Gfx::PStructuredBuffer sceneDataBuffer;
|
||||
PhysicsSystem physics;
|
||||
Gfx::PGraphics graphics;
|
||||
};
|
||||
DEFINE_REF(Scene)
|
||||
|
||||
Reference in New Issue
Block a user