refactor rendering

This commit is contained in:
2025-07-16 13:38:02 +02:00
parent 1bee6aed9c
commit 9667454811
14 changed files with 130 additions and 105 deletions

View File

@@ -196,7 +196,7 @@ int main(int argc, char** argv) {
td::render::Camera cam;
td::render::RenderPipeline renderer;
renderer.AddRenderer(std::make_unique<td::render::WorldRenderer>(cam, w));
renderer.AddRenderer<td::render::WorldRenderer>(cam, w);
cam.SetCamPos({77, 25, 13});
cam.UpdatePerspective(display.GetAspectRatio());

View File

@@ -8,7 +8,7 @@ namespace render {
void Camera::UpdatePerspective(float a_AspectRatio) {
m_ProjectionMatrix = maths::Perspective(80.0f / 180.0f * PI, a_AspectRatio, 0.1f, 160.0f);
m_InvProjectionMatrix = maths::Inverse(m_ProjectionMatrix);
NotifyListeners(&ICameraListener::OnPerspectiveChange);
OnPerspectiveChange();
}
void Camera::SetCamPos(const Vec3f& a_NewPos) {
@@ -21,7 +21,7 @@ void Camera::SetCamPos(const Vec3f& a_NewPos) {
m_CamPos = a_NewPos;
m_ViewMatrix = maths::Look(m_CamPos, front, { 0, 1, 0 });
m_InvViewMatrix = maths::Transpose(maths::Inverse(m_ViewMatrix)); // why transpose ? I don't know
NotifyListeners(&ICameraListener::OnViewChange);
OnViewChange();
}
} // namespace render

View File

@@ -5,13 +5,27 @@
namespace td {
namespace render {
void Renderer::Render(const GL::VertexArray& a_Vao) {
void BasicRenderer::Render(const GL::VertexArray& a_Vao) {
a_Vao.Bind();
glDrawArrays(GL_TRIANGLES, 0, a_Vao.GetVertexCount());
// glDrawElements(GL_TRIANGLES, a_Vao.GetVertexCount(), GL_UNSIGNED_INT, nullptr);
a_Vao.Unbind();
}
Renderer::Renderer(std::unique_ptr<shader::CameraShaderProgram>&& a_Shader, Camera& a_Camera) :
m_Shader(std::move(a_Shader)), m_Camera(a_Camera) {
a_Camera.OnPerspectiveChange.Connect([this]() {
m_Shader->Start();
m_Shader->SetProjectionMatrix(m_Camera.GetProjectionMatrix());
});
a_Camera.OnViewChange.Connect([this]() {
m_Shader->Start();
m_Shader->SetViewMatrix(m_Camera.GetViewMatrix());
});
}
RenderPipeline::RenderPipeline() {
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);

View File

@@ -7,28 +7,17 @@
namespace td {
namespace render {
WorldRenderer::WorldRenderer(Camera& a_Camera, const game::World& a_World) : Renderer(a_Camera), m_World(a_World) {
WorldRenderer::WorldRenderer(Camera& a_Camera, const game::World& a_World) : Renderer(std::make_unique<shader::WorldShader>(), a_Camera), m_World(a_World) {
m_WorldVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::LoadWorldModel(&a_World)));
a_Camera.BindListener(this);
}
WorldRenderer::~WorldRenderer() {}
void WorldRenderer::Render() {
m_Shader.Start();
m_Shader->Start();
Renderer::Render(*m_WorldVao);
ImGui::ShowDemoWindow();
}
void WorldRenderer::OnPerspectiveChange() {
m_Shader.Start();
m_Shader.SetProjectionMatrix(m_Camera.GetProjectionMatrix());
}
void WorldRenderer::OnViewChange() {
m_Shader.Start();
m_Shader.SetViewMatrix(m_Camera.GetViewMatrix());
}
} // namespace render
} // namespace td

View File

@@ -0,0 +1,21 @@
#include <td/render/shader/CameraShaderProgram.h>
namespace td {
namespace shader {
void CameraShaderProgram::SetProjectionMatrix(const Mat4f& proj) const {
LoadMat4(m_LocationProjection, proj);
}
void CameraShaderProgram::SetViewMatrix(const Mat4f& view) const {
LoadMat4(m_LocationView, view);
}
void CameraShaderProgram::GetAllUniformLocation() {
m_LocationProjection = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
m_LocationView = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
}
} // namespace shader
} // namespace td

View File

@@ -81,22 +81,9 @@ void main(void){
)";
#endif
WorldShader::WorldShader() : ShaderProgram() {
WorldShader::WorldShader() : CameraShaderProgram() {
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
}
void WorldShader::GetAllUniformLocation() {
m_LocationProjection = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
m_LocationView = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
}
void WorldShader::SetProjectionMatrix(const Mat4f& proj) const {
LoadMat4(m_LocationProjection, proj);
}
void WorldShader::SetViewMatrix(const Mat4f& view) const {
LoadMat4(m_LocationView, view);
}
} // namespace shader
} // namespace td