Skip to content

Commit eaa7d5a

Browse files
author
Adrien GIVRY
committed
Updating camera to cache matrices and to compute frustum
1 parent d433bce commit eaa7d5a

20 files changed

Lines changed: 529 additions & 117 deletions

File tree

Sources/Overload/OvCore/include/OvCore/ECS/Components/CCamera.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,6 @@ namespace OvCore::ECS::Components
3535
*/
3636
std::string GetName() override;
3737

38-
/**
39-
* Returns the projection matrix
40-
* @param p_windowWidth
41-
* @param p_windowHeight
42-
*/
43-
OvMaths::FMatrix4 GetProjectionMatrix(uint16_t p_windowWidth, uint16_t p_windowHeight);
44-
45-
/**
46-
* Returns the view matrix
47-
*/
48-
OvMaths::FMatrix4 GetViewMatrix();
49-
5038
/**
5139
* Sets the fov of the camera to the given value
5240
* @param p_value

Sources/Overload/OvCore/include/OvCore/ECS/Renderer.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <OvRendering/Core/Renderer.h>
1212
#include <OvRendering/Resources/Mesh.h>
13+
#include <OvRendering/Data/Frustum.h>
1314

1415
#include "OvCore/API/Export.h"
1516
#include "OvCore/Resources/Material.h"
@@ -59,9 +60,10 @@ namespace OvCore::ECS
5960
* Draw the given scene using the given default material (optional) if no material found on an actor
6061
* @param p_scene
6162
* @param p_cameraPosition
63+
* @param p_frustum
6264
* @param p_defaultMaterial
6365
*/
64-
void RenderScene(OvCore::SceneSystem::Scene& p_scene, const OvMaths::FVector3& p_cameraPosition, Resources::Material* p_defaultMaterial = nullptr);
66+
void RenderScene(OvCore::SceneSystem::Scene& p_scene, const OvMaths::FVector3& p_cameraPosition, OvRendering::Data::Frustum const* p_frustum = nullptr, Resources::Material* p_defaultMaterial = nullptr);
6567

6668
/**
6769
* Find every drawables objects in the scene. Sorting order:
@@ -71,9 +73,10 @@ namespace OvCore::ECS
7173
* @param p_transparents
7274
* @param p_scene
7375
* @param p_cameraPosition
76+
* @param p_frustum
7477
* @param p_defaultMaterial
7578
*/
76-
void FindAndSortDrawables(OpaqueDrawables& p_opaques, TransparentDrawables& p_transparents, const OvCore::SceneSystem::Scene& p_scene, const OvMaths::FVector3& p_cameraPosition, Resources::Material* p_defaultMaterial = nullptr);
79+
void FindAndSortDrawables(OpaqueDrawables& p_opaques, TransparentDrawables& p_transparents, const OvCore::SceneSystem::Scene& p_scene, const OvMaths::FVector3& p_cameraPosition, OvRendering::Data::Frustum const* p_frustum = nullptr, Resources::Material* p_defaultMaterial = nullptr);
7780

7881
/**
7982
* Draw a Drawable instance

Sources/Overload/OvCore/src/OvCore/ECS/Components/CCamera.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@ std::string OvCore::ECS::Components::CCamera::GetName()
3535
return "Camera";
3636
}
3737

38-
39-
OvMaths::FMatrix4 OvCore::ECS::Components::CCamera::GetProjectionMatrix(uint16_t p_windowWidth, uint16_t p_windowHeight)
40-
{
41-
return m_camera.GetProjectionMatrix(p_windowWidth, p_windowHeight);
42-
}
43-
44-
OvMaths::FMatrix4 OvCore::ECS::Components::CCamera::GetViewMatrix()
45-
{
46-
return m_camera.GetViewMatrix(owner.transform.GetWorldPosition());
47-
}
48-
4938
void OvCore::ECS::Components::CCamera::SetFov(float p_value)
5039
{
5140
m_camera.SetFov(p_value);

Sources/Overload/OvCore/src/OvCore/ECS/Renderer.cpp

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <OvRendering/Resources/Loaders/TextureLoader.h>
8+
#include <OvRendering/Data/Frustum.h>
89

910
#include "OvCore/ECS/Renderer.h"
1011
#include "OvCore/ECS/Components/CModelRenderer.h"
@@ -45,12 +46,12 @@ void OvCore::ECS::Renderer::FindLightMatrices(const OvCore::SceneSystem::Scene&
4546
p_out.push_back(light->GetData().GenerateMatrix());
4647
}
4748

48-
void OvCore::ECS::Renderer::RenderScene(OvCore::SceneSystem::Scene& p_scene, const OvMaths::FVector3& p_cameraPosition, Resources::Material* p_defaultMaterial)
49+
void OvCore::ECS::Renderer::RenderScene(OvCore::SceneSystem::Scene& p_scene, const OvMaths::FVector3& p_cameraPosition, OvRendering::Data::Frustum const* p_frustum, Resources::Material* p_defaultMaterial)
4950
{
5051
OpaqueDrawables opaqueMeshes;
5152
TransparentDrawables transparentMeshes;
5253

53-
FindAndSortDrawables(opaqueMeshes, transparentMeshes, p_scene, p_cameraPosition, p_defaultMaterial);
54+
FindAndSortDrawables(opaqueMeshes, transparentMeshes, p_scene, p_cameraPosition, p_frustum, p_defaultMaterial);
5455

5556
for (const auto&[distance, drawable] : opaqueMeshes)
5657
DrawDrawable(drawable);
@@ -59,39 +60,53 @@ void OvCore::ECS::Renderer::RenderScene(OvCore::SceneSystem::Scene& p_scene, con
5960
DrawDrawable(drawable);
6061
}
6162

62-
void OvCore::ECS::Renderer::FindAndSortDrawables(OpaqueDrawables& p_opaques, TransparentDrawables& p_transparents, const OvCore::SceneSystem::Scene& p_scene, const OvMaths::FVector3& p_cameraPosition, Resources::Material* p_defaultMaterial)
63+
void OvCore::ECS::Renderer::FindAndSortDrawables
64+
(
65+
OpaqueDrawables& p_opaques,
66+
TransparentDrawables& p_transparents,
67+
const OvCore::SceneSystem::Scene& p_scene,
68+
const OvMaths::FVector3& p_cameraPosition,
69+
OvRendering::Data::Frustum const* p_frustum,
70+
Resources::Material* p_defaultMaterial
71+
)
6372
{
73+
auto camera = FindMainCamera(p_scene);
74+
6475
for (OvCore::ECS::Components::CModelRenderer* modelRenderer : p_scene.GetFastAccessComponents().modelRenderers)
6576
{
77+
auto position = modelRenderer->owner.transform.GetWorldPosition();
6678
if (modelRenderer->owner.IsActive())
6779
{
6880
if (auto model = modelRenderer->GetModel())
6981
{
70-
float distanceToActor = OvMaths::FVector3::Distance(modelRenderer->owner.transform.GetWorldPosition(), p_cameraPosition);
71-
72-
if (auto materialRenderer = modelRenderer->owner.GetComponent<OvCore::ECS::Components::CMaterialRenderer>())
82+
if (!p_frustum || p_frustum->PointInFrustum(position.x, position.y, position.z))
7383
{
74-
const OvCore::ECS::Components::CMaterialRenderer::MaterialList& materials = materialRenderer->GetMaterials();
84+
float distanceToActor = OvMaths::FVector3::Distance(modelRenderer->owner.transform.GetWorldPosition(), p_cameraPosition);
7585

76-
for (auto mesh : model->GetMeshes())
86+
if (auto materialRenderer = modelRenderer->owner.GetComponent<OvCore::ECS::Components::CMaterialRenderer>())
7787
{
78-
OvCore::Resources::Material* material = nullptr;
79-
80-
if (mesh->GetMaterialIndex() < MAX_MATERIAL_COUNT)
81-
{
82-
material = materials.at(mesh->GetMaterialIndex());
83-
if (!material || !material->GetShader())
84-
material = p_defaultMaterial;
85-
}
88+
const OvCore::ECS::Components::CMaterialRenderer::MaterialList& materials = materialRenderer->GetMaterials();
8689

87-
if (material)
90+
for (auto mesh : model->GetMeshes())
8891
{
89-
Drawable element = { modelRenderer->owner.transform.GetWorldMatrix(), mesh, material, materialRenderer->GetUserMatrix() };
90-
91-
if (material->IsBlendable())
92-
p_transparents.emplace(distanceToActor, element);
93-
else
94-
p_opaques.emplace(distanceToActor, element);
92+
OvCore::Resources::Material* material = nullptr;
93+
94+
if (mesh->GetMaterialIndex() < MAX_MATERIAL_COUNT)
95+
{
96+
material = materials.at(mesh->GetMaterialIndex());
97+
if (!material || !material->GetShader())
98+
material = p_defaultMaterial;
99+
}
100+
101+
if (material)
102+
{
103+
Drawable element = { modelRenderer->owner.transform.GetWorldMatrix(), mesh, material, materialRenderer->GetUserMatrix() };
104+
105+
if (material->IsBlendable())
106+
p_transparents.emplace(distanceToActor, element);
107+
else
108+
p_opaques.emplace(distanceToActor, element);
109+
}
95110
}
96111
}
97112
}

Sources/Overload/OvEditor/include/OvEditor/Core/EditorRenderer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ namespace OvEditor::Core
5454
/**
5555
* Render the scene
5656
* @param p_cameraPosition
57+
* @param p_frustum
5758
*/
58-
void RenderScene(const OvMaths::FVector3& p_cameraPosition);
59+
void RenderScene(const OvMaths::FVector3& p_cameraPosition, OvRendering::Data::Frustum const* p_frustum = nullptr);
5960

6061
/**
6162
* Render the scene for actor picking (Unlit version of the scene with colors indicating actor IDs)
63+
* @param p_frustum
6264
*/
63-
void RenderSceneForActorPicking();
65+
void RenderSceneForActorPicking(OvRendering::Data::Frustum const* p_frustum = nullptr);
6466

6567
/**
6668
* Render the User Interface

Sources/Overload/OvEditor/include/OvEditor/Panels/AView.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ namespace OvEditor::Panels
9494
*/
9595
void FillEngineUBO();
9696

97+
protected:
98+
/**
99+
* Update camera matrices
100+
*/
101+
void PrepareCamera();
102+
97103
protected:
98104
OvEditor::Core::EditorRenderer& m_editorRenderer;
99105
OvRendering::LowRenderer::Camera m_camera;

Sources/Overload/OvEditor/include/OvEditor/Panels/GameView.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ namespace OvEditor::Panels
4141
*/
4242
virtual void _Render_Impl() override;
4343

44+
/**
45+
* Returns true if the game view has a camera
46+
*/
47+
bool HasCamera() const;
48+
4449
private:
4550
OvCore::SceneSystem::SceneManager& m_sceneManager;
4651
bool m_hasCamera = false;

Sources/Overload/OvEditor/src/OvEditor/Core/Editor.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ void OvEditor::Core::Editor::RenderViews(float p_deltaTime)
197197
PROFILER_SPY("Editor Views Update");
198198

199199
assetView.Update(p_deltaTime);
200-
sceneView.Update(p_deltaTime);
201200
gameView.Update(p_deltaTime);
201+
sceneView.Update(p_deltaTime);
202202
}
203203

204204
if (assetView.IsOpened())
@@ -212,20 +212,20 @@ void OvEditor::Core::Editor::RenderViews(float p_deltaTime)
212212

213213
m_context.lightSSBO->Bind(0);
214214

215-
if (sceneView.IsOpened())
216-
{
217-
PROFILER_SPY("Scene View Rendering");
218-
219-
sceneView.Render();
220-
}
221-
222215
if (gameView.IsOpened())
223216
{
224217
PROFILER_SPY("Game View Rendering");
225218

226219
gameView.Render();
227220
}
228221

222+
if (sceneView.IsOpened())
223+
{
224+
PROFILER_SPY("Scene View Rendering");
225+
226+
sceneView.Render();
227+
}
228+
229229
m_context.lightSSBO->Unbind();
230230
}
231231

Sources/Overload/OvEditor/src/OvEditor/Core/EditorRenderer.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ OvMaths::FMatrix4 OvEditor::Core::EditorRenderer::CalculateCameraModelMatrix(OvC
143143
return translation * rotation * scale;
144144
}
145145

146-
void OvEditor::Core::EditorRenderer::RenderScene(const OvMaths::FVector3& p_cameraPosition)
146+
void OvEditor::Core::EditorRenderer::RenderScene(const OvMaths::FVector3& p_cameraPosition, OvRendering::Data::Frustum const* p_frustum)
147147
{
148148
/* Render the actors */
149149
m_context.lightSSBO->Bind(0);
150-
m_context.renderer->RenderScene(*m_context.sceneManager.GetCurrentScene(), p_cameraPosition, &m_emptyMaterial);
150+
m_context.renderer->RenderScene(*m_context.sceneManager.GetCurrentScene(), p_cameraPosition, p_frustum, &m_emptyMaterial);
151151
m_context.lightSSBO->Unbind();
152152
}
153153

154-
void OvEditor::Core::EditorRenderer::RenderSceneForActorPicking()
154+
void OvEditor::Core::EditorRenderer::RenderSceneForActorPicking(OvRendering::Data::Frustum const* p_frustum)
155155
{
156156
auto& scene = *m_context.sceneManager.GetCurrentScene();
157157

@@ -164,33 +164,37 @@ void OvEditor::Core::EditorRenderer::RenderSceneForActorPicking()
164164
{
165165
if (auto model = modelRenderer->GetModel())
166166
{
167+
const auto& position = actor.transform.GetWorldPosition();
167168

168-
if (auto materialRenderer = modelRenderer->owner.GetComponent<OvCore::ECS::Components::CMaterialRenderer>())
169+
if (!p_frustum || p_frustum->PointInFrustum(position.x, position.y, position.z))
169170
{
170-
const OvCore::ECS::Components::CMaterialRenderer::MaterialList& materials = materialRenderer->GetMaterials();
171-
auto modelMatrix = actor.transform.GetWorldMatrix();
172-
PreparePickingMaterial(actor);
173-
174-
for (auto mesh : model->GetMeshes())
171+
if (auto materialRenderer = modelRenderer->owner.GetComponent<OvCore::ECS::Components::CMaterialRenderer>())
175172
{
176-
OvCore::Resources::Material* material = nullptr;
173+
const OvCore::ECS::Components::CMaterialRenderer::MaterialList& materials = materialRenderer->GetMaterials();
174+
auto modelMatrix = actor.transform.GetWorldMatrix();
175+
PreparePickingMaterial(actor);
177176

178-
if (mesh->GetMaterialIndex() < MAX_MATERIAL_COUNT)
177+
for (auto mesh : model->GetMeshes())
179178
{
180-
material = materials.at(mesh->GetMaterialIndex());
181-
if (!material || !material->GetShader())
182-
material = &m_emptyMaterial;
183-
}
184-
185-
if (material)
186-
{
187-
m_actorPickingMaterial.SetBackfaceCulling(material->HasBackfaceCulling());
188-
m_actorPickingMaterial.SetFrontfaceCulling(material->HasFrontfaceCulling());
189-
m_actorPickingMaterial.SetColorWriting(material->HasColorWriting());
190-
m_actorPickingMaterial.SetDepthTest(material->HasDepthTest());
191-
m_actorPickingMaterial.SetDepthWriting(material->HasDepthWriting());
192-
193-
m_context.renderer->DrawMesh(*mesh, m_actorPickingMaterial, &modelMatrix);
179+
OvCore::Resources::Material* material = nullptr;
180+
181+
if (mesh->GetMaterialIndex() < MAX_MATERIAL_COUNT)
182+
{
183+
material = materials.at(mesh->GetMaterialIndex());
184+
if (!material || !material->GetShader())
185+
material = &m_emptyMaterial;
186+
}
187+
188+
if (material)
189+
{
190+
m_actorPickingMaterial.SetBackfaceCulling(material->HasBackfaceCulling());
191+
m_actorPickingMaterial.SetFrontfaceCulling(material->HasFrontfaceCulling());
192+
m_actorPickingMaterial.SetColorWriting(material->HasColorWriting());
193+
m_actorPickingMaterial.SetDepthTest(material->HasDepthTest());
194+
m_actorPickingMaterial.SetDepthWriting(material->HasDepthWriting());
195+
196+
m_context.renderer->DrawMesh(*mesh, m_actorPickingMaterial, &modelMatrix);
197+
}
194198
}
195199
}
196200
}

Sources/Overload/OvEditor/src/OvEditor/Panels/AView.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ void OvEditor::Panels::AView::Render()
4646
FillEngineUBO();
4747

4848
auto [winWidth, winHeight] = GetSafeSize();
49-
auto projection = m_camera.GetProjectionMatrix(winWidth, winHeight);
50-
auto view = m_camera.GetViewMatrix(m_cameraPosition);
51-
EDITOR_CONTEXT(shapeDrawer)->SetViewProjection(projection * view);
49+
50+
EDITOR_CONTEXT(shapeDrawer)->SetViewProjection(m_camera.GetProjectionMatrix() * m_camera.GetViewMatrix());
51+
5252
glViewport(0, 0, winWidth, winHeight); // TODO: Move this OpenGL call to OvRendering
5353

5454
_Render_Impl();
@@ -92,8 +92,14 @@ void OvEditor::Panels::AView::FillEngineUBO()
9292
auto[winWidth, winHeight] = GetSafeSize();
9393

9494
size_t offset = sizeof(OvMaths::FMatrix4); // We skip the model matrix (Which is a special case, modified every draw calls)
95-
engineUBO.SetSubData(OvMaths::FMatrix4::Transpose(m_camera.GetViewMatrix(m_cameraPosition)), std::ref(offset));
96-
engineUBO.SetSubData(OvMaths::FMatrix4::Transpose(m_camera.GetProjectionMatrix(winWidth, winHeight)), std::ref(offset));
95+
engineUBO.SetSubData(OvMaths::FMatrix4::Transpose(m_camera.GetViewMatrix()), std::ref(offset));
96+
engineUBO.SetSubData(OvMaths::FMatrix4::Transpose(m_camera.GetProjectionMatrix()), std::ref(offset));
9797
engineUBO.SetSubData(m_cameraPosition, std::ref(offset));
9898
}
9999

100+
void OvEditor::Panels::AView::PrepareCamera()
101+
{
102+
auto [winWidth, winHeight] = GetSafeSize();
103+
m_camera.CacheMatrices(winWidth, winHeight, m_cameraPosition);
104+
}
105+

0 commit comments

Comments
 (0)