From ecba8247bfdfa0f477c99a7a313e7b166d785527 Mon Sep 17 00:00:00 2001 From: LandaMm Date: Sat, 18 Oct 2025 17:55:26 +0200 Subject: [PATCH] feat: rendering improvemenets --- engine/include/engine/components/batch.h | 1 + engine/src/components/batch.cpp | 17 ++++++++++++++--- engine/src/renderer/renderer.cpp | 13 ++++++------- sandbox/src/main.cpp | 4 ++-- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/engine/include/engine/components/batch.h b/engine/include/engine/components/batch.h index efc2f55..afe609f 100644 --- a/engine/include/engine/components/batch.h +++ b/engine/include/engine/components/batch.h @@ -21,6 +21,7 @@ protected: private: unsigned int m_id; unsigned int m_instance_vbo { 0 }; + unsigned int m_instance_count { 0 }; private: void prepare(glm::mat4 *instances, unsigned int count); }; diff --git a/engine/src/components/batch.cpp b/engine/src/components/batch.cpp index 632c643..8952898 100644 --- a/engine/src/components/batch.cpp +++ b/engine/src/components/batch.cpp @@ -11,9 +11,20 @@ batch::batch() { void batch::prepare(glm::mat4 *instances, unsigned int count) { if (m_instance_vbo == 0) { glGenBuffers(1, &m_instance_vbo); + glBindBuffer(GL_ARRAY_BUFFER, m_instance_vbo); + // Allocate *once*, no data yet — just reserve space + glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * count, nullptr, GL_DYNAMIC_DRAW); + m_instance_count = count; + } else if (count > m_instance_count) { + // Optional: reallocate only if you *really* have more instances than before + glBindBuffer(GL_ARRAY_BUFFER, m_instance_vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * count, nullptr, GL_DYNAMIC_DRAW); + m_instance_count = count; + } else { + glBindBuffer(GL_ARRAY_BUFFER, m_instance_vbo); } - - glBindBuffer(GL_ARRAY_BUFFER, m_instance_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * count, reinterpret_cast(instances), GL_DYNAMIC_DRAW); + + // Just update the data region — much cheaper + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::mat4) * count, instances); glBindBuffer(GL_ARRAY_BUFFER, 0); } \ No newline at end of file diff --git a/engine/src/renderer/renderer.cpp b/engine/src/renderer/renderer.cpp index 3c57ec9..5b1285b 100644 --- a/engine/src/renderer/renderer.cpp +++ b/engine/src/renderer/renderer.cpp @@ -205,12 +205,9 @@ void Renderer::GenerateShadowMaps() { void Renderer::Render() { m_depthShader.use(); - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_FRONT); - auto lights = m_registry.view(); + const auto lights = m_registry.view(); for (auto [_, l, t] : lights.each()) { // TODO: support other light types when ready @@ -218,10 +215,10 @@ void Renderer::Render() { EnsureShadowResources(l); - float near_plane = 0.1f, far_plane = 20.0f; + // float near_plane = 0.1f, far_plane = 50.0f; glm::vec3 target = glm::vec3(0.0f, 0.5f, 0.0f); glm::mat4 lightView = glm::lookAt(t.position, target, glm::vec3(0.0f, 1.0f, 0.0f)); - glm::mat4 lightProjection = glm::ortho(-6.0f, 6.0f, -6.0f, 6.0f, near_plane, far_plane); + glm::mat4 lightProjection = glm::ortho(-6.0f, 6.0f, -6.0f, 6.0f, 1.0f, 20.0f); glm::mat4 lightSpaceMatrix = lightProjection * lightView; m_depthShader.setMat4("u_lightSpace", lightSpaceMatrix); @@ -237,6 +234,8 @@ void Renderer::Render() { RenderScene(m_depthShader); + // glDisable(GL_POLYGON_OFFSET_FILL); + glBindFramebuffer(GL_FRAMEBUFFER, 0); } @@ -246,7 +245,7 @@ void Renderer::Render() { glViewport(0, 0, Window::GetWidth(), Window::GetHeight()); glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - + m_shader.use(); ApplyLights(m_shader); UpdateView(); diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index c4fb424..3016b75 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -35,9 +35,9 @@ public: m_registry.emplace(cameraEntity, glm::vec3(0.f, 2.f, 2.f)); m_registry.emplace(cameraEntity); - Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj"); + Object* targetObj = Object::LoadFile("./assets/car/car.obj"); const auto targetEntity = m_registry.create(); - m_registry.emplace(targetEntity, glm::vec3(0.f, 0.0f, 0.f), glm::vec3(0.f, 0.0f, 0.f), glm::vec3(0.5f, 0.5f, 0.5f)); + m_registry.emplace(targetEntity, glm::vec3(0.f, 0.0f, 0.f)); m_registry.emplace(targetEntity, std::shared_ptr(targetObj)); m_registry.emplace(targetEntity);