From a9ce353a9da693a5009d0647b963c987b0d1c7a9 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 18 Oct 2023 00:18:44 +0200 Subject: [PATCH] add very basic 3D --- include/client/render/Camera.h | 34 +++++++++++++++++++++ include/client/render/MainRenderer.h | 3 ++ src/ClientMain.cpp | 1 + src/client/render/Camera.cpp | 37 +++++++++++++++++++++++ src/client/render/MainRenderer.cpp | 14 ++++++++- src/client/render/shader/EntityShader.cpp | 10 +++--- 6 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 include/client/render/Camera.h create mode 100644 src/client/render/Camera.cpp diff --git a/include/client/render/Camera.h b/include/client/render/Camera.h new file mode 100644 index 0000000..bbb893e --- /dev/null +++ b/include/client/render/Camera.h @@ -0,0 +1,34 @@ +#pragma once + +#include "blitz/misc/Maths.h" +#include + +namespace blitz { +namespace render { + +class Camera { + private: + float m_Yaw, m_Pitch; + Vec3f m_Position; + Mat4f m_PerspectiveMatrix; + Vec2i m_LastWindowSize; + + public: + Camera() : m_Yaw(-PI / 2.0f), m_Pitch(0), m_Position(10, 1, 10) {} + ~Camera() {} + + void Update(std::uint64_t delta); + + Mat4f GetViewMatrix() const; + + const Mat4f& GetPerspectiveMatrix() const { + return m_PerspectiveMatrix; + } + + const Vec3f& GetPosition() const { + return m_Position; + } +}; + +} // namespace render +} // namespace blitz diff --git a/include/client/render/MainRenderer.h b/include/client/render/MainRenderer.h index 4fcd378..85fba25 100644 --- a/include/client/render/MainRenderer.h +++ b/include/client/render/MainRenderer.h @@ -1,5 +1,6 @@ #pragma once +#include "client/render/Camera.h" #include "client/render/loader/GLLoader.h" #include @@ -21,11 +22,13 @@ class MainRenderer { Client* m_Client; std::unique_ptr m_Vao; std::unique_ptr m_Shader; + Camera m_Camera; public: MainRenderer(Client* client); ~MainRenderer(); + void Update(); void Render(); void Render(const GL::VertexArray& vao); diff --git a/src/ClientMain.cpp b/src/ClientMain.cpp index 5598bf7..94d9ccc 100644 --- a/src/ClientMain.cpp +++ b/src/ClientMain.cpp @@ -15,6 +15,7 @@ int main(int argc, char** argv) { while (!display.IsCloseRequested()) { client.Update(); display.PollEvents(); + renderer.Update(); renderer.Render(); display.Render(); display.Update(); diff --git a/src/client/render/Camera.cpp b/src/client/render/Camera.cpp new file mode 100644 index 0000000..9b356c9 --- /dev/null +++ b/src/client/render/Camera.cpp @@ -0,0 +1,37 @@ +#include "client/render/Camera.h" + +#include "imgui.h" +#include + +namespace blitz { +namespace render { + +void Camera::Update(std::uint64_t delta) { + if (ImGui::GetIO().MouseDown[0]) { + m_Yaw += ImGui::GetIO().MouseDelta.x * delta / 5000; + m_Pitch -= ImGui::GetIO().MouseDelta.y * delta / 5000; + m_Pitch = std::clamp(m_Pitch, -PI / 2, PI / 2); + } + + int windowWidth = ImGui::GetIO().DisplaySize.x; + int windowHeight = ImGui::GetIO().DisplaySize.y; + + if (windowWidth != m_LastWindowSize.x || windowHeight != m_LastWindowSize.y) { + m_LastWindowSize = {windowWidth, windowHeight}; + + m_PerspectiveMatrix = maths::Perspective(80.0f / 180.0f * PI, static_cast(windowWidth) / windowHeight, 0.1f, 160.0f); + } +} + +Mat4f Camera::GetViewMatrix() const { + Vec3f front = { + std::cos(m_Yaw) * std::cos(m_Pitch), + std::sin(m_Pitch), + std::sin(m_Yaw) * std::cos(m_Pitch), + }; + + return maths::Look(m_Position, front, {0, 1, 0}); +} + +} // namespace render +} // namespace blitz diff --git a/src/client/render/MainRenderer.cpp b/src/client/render/MainRenderer.cpp index c3fcd87..5545ff9 100644 --- a/src/client/render/MainRenderer.cpp +++ b/src/client/render/MainRenderer.cpp @@ -4,6 +4,7 @@ #include "client/Client.h" #include "client/render/loader/GLLoader.h" #include "client/render/shader/EntityShader.h" +#include "imgui.h" #include #include // C++ importer interface #include // Post processing flags @@ -20,6 +21,8 @@ MainRenderer::MainRenderer(Client* client) : m_Client(client) { m_Shader = std::make_unique(); m_Shader->LoadShader(); + + glEnable(GL_DEPTH_TEST); } MainRenderer::~MainRenderer() {} @@ -69,7 +72,7 @@ void MainRenderer::LoadModel() { GL::VertexBuffer positionVBO(positions, VERTEX_SIZE); positionVBO.AddVertexAttribPointer(0, VERTEX_SIZE, 0); - m_Vao = std::make_unique(GL::ElementBuffer{indicies}); // each pos = 1 color + m_Vao = std::make_unique(GL::ElementBuffer{indicies}); // each pos = 1 color m_Vao->Bind(); m_Vao->BindVertexBuffer(positionVBO); m_Vao->Unbind(); @@ -86,8 +89,17 @@ void MainRenderer::Render() { if (!m_Client->IsConnected()) return; + glClear(GL_DEPTH_BUFFER_BIT); + Render(*m_Vao.get()); } +void MainRenderer::Update() { + m_Camera.Update(ImGui::GetIO().DeltaTime * 1000); + m_Shader->Start(); + m_Shader->SetProjectionMatrix(m_Camera.GetPerspectiveMatrix()); + m_Shader->SetViewMatrix(m_Camera.GetViewMatrix()); +} + } // namespace render } // namespace blitz diff --git a/src/client/render/shader/EntityShader.cpp b/src/client/render/shader/EntityShader.cpp index d58b001..3cfe9a0 100644 --- a/src/client/render/shader/EntityShader.cpp +++ b/src/client/render/shader/EntityShader.cpp @@ -56,11 +56,11 @@ uniform mat4 viewMatrix; uniform mat4 projectionMatrix; uniform vec3 modelPosition; -//flat out int pass_color; +out vec3 pass_color; void main(void){ - //pass_color = color; - gl_Position = vec4(position, 1.0); + pass_color = position; + gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0); //gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0); } )"; @@ -68,7 +68,7 @@ void main(void){ static const char fragmentSource[] = R"( #version 330 -flat in int pass_color; +in vec3 pass_color; out vec4 out_color; @@ -76,7 +76,7 @@ uniform vec3 ColorEffect; void main(void){ - out_color = vec4(1, 1, 1, 1); + out_color = vec4(pass_color, 1); } )";