feat: rendering improvemenets

This commit is contained in:
2025-10-18 17:55:26 +02:00
parent fa9076b834
commit ecba8247bf
4 changed files with 23 additions and 12 deletions

View File

@ -21,6 +21,7 @@ protected:
private: private:
unsigned int m_id; unsigned int m_id;
unsigned int m_instance_vbo { 0 }; unsigned int m_instance_vbo { 0 };
unsigned int m_instance_count { 0 };
private: private:
void prepare(glm::mat4 *instances, unsigned int count); void prepare(glm::mat4 *instances, unsigned int count);
}; };

View File

@ -11,9 +11,20 @@ batch::batch() {
void batch::prepare(glm::mat4 *instances, unsigned int count) { void batch::prepare(glm::mat4 *instances, unsigned int count) {
if (m_instance_vbo == 0) { if (m_instance_vbo == 0) {
glGenBuffers(1, &m_instance_vbo); 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); // Just update the data region — much cheaper
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * count, reinterpret_cast<void*>(instances), GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::mat4) * count, instances);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
} }

View File

@ -205,12 +205,9 @@ void Renderer::GenerateShadowMaps() {
void Renderer::Render() { void Renderer::Render() {
m_depthShader.use(); m_depthShader.use();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT); glCullFace(GL_FRONT);
auto lights = m_registry.view<light, transform>(); const auto lights = m_registry.view<light, transform>();
for (auto [_, l, t] : lights.each()) { for (auto [_, l, t] : lights.each()) {
// TODO: support other light types when ready // TODO: support other light types when ready
@ -218,10 +215,10 @@ void Renderer::Render() {
EnsureShadowResources(l); 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::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 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; glm::mat4 lightSpaceMatrix = lightProjection * lightView;
m_depthShader.setMat4("u_lightSpace", lightSpaceMatrix); m_depthShader.setMat4("u_lightSpace", lightSpaceMatrix);
@ -237,6 +234,8 @@ void Renderer::Render() {
RenderScene(m_depthShader); RenderScene(m_depthShader);
// glDisable(GL_POLYGON_OFFSET_FILL);
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }

View File

@ -35,9 +35,9 @@ public:
m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f)); m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f));
m_registry.emplace<camera>(cameraEntity); m_registry.emplace<camera>(cameraEntity);
Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj"); Object* targetObj = Object::LoadFile("./assets/car/car.obj");
const auto targetEntity = m_registry.create(); const auto targetEntity = m_registry.create();
m_registry.emplace<transform>(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<transform>(targetEntity, glm::vec3(0.f, 0.0f, 0.f));
m_registry.emplace<mesh>(targetEntity, std::shared_ptr<Object>(targetObj)); m_registry.emplace<mesh>(targetEntity, std::shared_ptr<Object>(targetObj));
m_registry.emplace<rotate>(targetEntity); m_registry.emplace<rotate>(targetEntity);