From 14646f4b880fc5560437263a753b5ab895820d39 Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 14:19:12 -0500 Subject: [PATCH 01/20] camera controller --- Sandbox/src/SandboxApp.cpp | 52 ++++++---------- StarStudio/src/StarStudio.h | 1 + .../OrthographicCameraController.cpp | 62 +++++++++++++++++++ .../StarStudio/OrthographicCameraController.h | 39 ++++++++++++ .../Renderer/OrthographicCamera.cpp | 6 ++ .../StarStudio/Renderer/OrthographicCamera.h | 6 +- 6 files changed, 131 insertions(+), 35 deletions(-) create mode 100644 StarStudio/src/StarStudio/OrthographicCameraController.cpp create mode 100644 StarStudio/src/StarStudio/OrthographicCameraController.h diff --git a/Sandbox/src/SandboxApp.cpp b/Sandbox/src/SandboxApp.cpp index e2c9e246..9efd0375 100644 --- a/Sandbox/src/SandboxApp.cpp +++ b/Sandbox/src/SandboxApp.cpp @@ -5,13 +5,15 @@ #include "imgui/imgui.h" #include + +#include "StarStudio/OrthographicCameraController.h" #include "glm/gtc/type_ptr.hpp" class ExampleLayer : public StarStudio::Layer { public: ExampleLayer() - : Layer("Example"), m_Camera(-1.6f, 1.6f, -0.9f, 0.9f), m_CameraPosition(0.0f) + : Layer("Example"), m_CameraController(1280.0f / 720.0f, true) { m_VertexArray.reset(StarStudio::VertexArray::Create()); @@ -139,28 +141,14 @@ class ExampleLayer : public StarStudio::Layer void OnUpdate(StarStudio::Timestep ts) override { - if (StarStudio::Input::IsKeyPressed(SS_KEY_LEFT)) - m_CameraPosition.x -= m_CameraMoveSpeed * ts; - else if (StarStudio::Input::IsKeyPressed(SS_KEY_RIGHT)) - m_CameraPosition.x += m_CameraMoveSpeed * ts; - - if (StarStudio::Input::IsKeyPressed(SS_KEY_UP)) - m_CameraPosition.y += m_CameraMoveSpeed * ts; - else if (StarStudio::Input::IsKeyPressed(SS_KEY_DOWN)) - m_CameraPosition.y -= m_CameraMoveSpeed * ts; - - if (StarStudio::Input::IsKeyPressed(SS_KEY_A)) - m_CameraRotation += m_CameraRotationSpeed * ts; - if (StarStudio::Input::IsKeyPressed(SS_KEY_D)) - m_CameraRotation -= m_CameraRotationSpeed * ts; + // Update + m_CameraController.OnUpdate(ts); + // Render StarStudio::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 }); StarStudio::RenderCommand::Clear(); - m_Camera.SetPosition(m_CameraPosition); - m_Camera.SetRotation(m_CameraRotation); - - StarStudio::Renderer::BeginScene(m_Camera); + StarStudio::Renderer::BeginScene(m_CameraController.GetCamera()); glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f)); @@ -199,15 +187,15 @@ class ExampleLayer : public StarStudio::Layer ImGui::Begin("Camera Info"); ImGui::Text("Camera Position"); - ImGui::Text("X: %.2f", m_CameraPosition.x); - ImGui::Text("Y: %.2f", m_CameraPosition.y); - ImGui::Text("Z: %.2f", m_CameraPosition.z); + ImGui::Text("X: %.2f", m_CameraController.GetCamera().GetPosition().x); + ImGui::Text("Y: %.2f", m_CameraController.GetCamera().GetPosition().y); + ImGui::Text("Z: %.2f", m_CameraController.GetCamera().GetPosition().z); - ImGui::Text("Camera Rotation: %.2f", m_CameraRotation); + ImGui::Text("Camera Rotation: %.2f", m_CameraController.GetCamera().GetRotation()); if (ImGui::Button("Reset")) { - m_CameraPosition = glm::vec3(0.0f); - m_CameraRotation = 0.0f; + m_CameraController.GetCamera().SetPosition(glm::vec3(0.0f)); + m_CameraController.GetCamera().SetRotation(0.0f); } ImGui::End(); @@ -223,10 +211,11 @@ class ExampleLayer : public StarStudio::Layer ImGui::End(); */ - } - void OnEvent(StarStudio::Event& event) override - { + } + void OnEvent(StarStudio::Event& e) override + { + m_CameraController.OnEvent(e); } private: @@ -239,12 +228,7 @@ class ExampleLayer : public StarStudio::Layer StarStudio::Ref m_Texture, m_starLogTexture; - StarStudio::OrthographicCamera m_Camera; - glm::vec3 m_CameraPosition; - float m_CameraMoveSpeed = 5.0f; - - float m_CameraRotation = 0.0f; - float m_CameraRotationSpeed = 180.0f; + StarStudio::OrthographicCameraController m_CameraController; glm::vec3 m_SquareColor = { 0.2f, 0.2f, 0.2f}; }; diff --git a/StarStudio/src/StarStudio.h b/StarStudio/src/StarStudio.h index a2877204..13075f47 100644 --- a/StarStudio/src/StarStudio.h +++ b/StarStudio/src/StarStudio.h @@ -11,6 +11,7 @@ #include "StarStudio/Input.h" #include "StarStudio/KeyCodes.h" #include "StarStudio/MouseButtonCodes.h" +#include "StarStudio/OrthographicCameraController.h" // --------------------------------- #include "StarStudio/ImGui/ImGuiLayer.h" diff --git a/StarStudio/src/StarStudio/OrthographicCameraController.cpp b/StarStudio/src/StarStudio/OrthographicCameraController.cpp new file mode 100644 index 00000000..ed4dacf1 --- /dev/null +++ b/StarStudio/src/StarStudio/OrthographicCameraController.cpp @@ -0,0 +1,62 @@ +#include "sspch.h" +#include "OrthographicCameraController.h" + +#include "StarStudio/Input.h" +#include "StarStudio/KeyCodes.h" + +namespace StarStudio { + + OrthographicCameraController::OrthographicCameraController(float aspectRatio, bool rotation) + : m_AspectRatio(aspectRatio), m_Camera(-m_AspectRatio * m_ZoomLevel, m_AspectRatio* m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel), m_Rotation(rotation), m_CameraPosition(), m_CameraRotation() + { + + } + void OrthographicCameraController::OnUpdate(Timestep ts) + { + if (Input::IsKeyPressed(SS_KEY_A)) + m_CameraPosition.x -= m_CameraTranslationSpeed * ts; + else if (Input::IsKeyPressed(SS_KEY_D)) + m_CameraPosition.x += m_CameraTranslationSpeed * ts; + + if (Input::IsKeyPressed(SS_KEY_W)) + m_CameraPosition.y += m_CameraTranslationSpeed * ts; + else if (Input::IsKeyPressed(SS_KEY_S)) + m_CameraPosition.y -= m_CameraTranslationSpeed * ts; + + if (m_Rotation) + { + if (Input::IsKeyPressed(SS_KEY_Q)) + m_CameraRotation += m_CameraRotationSpeed * ts; + + if (Input::IsKeyPressed(SS_KEY_E)) + m_CameraRotation -= m_CameraRotationSpeed * ts; + + m_Camera.SetRotation(m_CameraRotation); + } + + m_Camera.SetPosition(m_CameraPosition); + m_CameraTranslationSpeed = m_ZoomLevel; + } + + void OrthographicCameraController::OnEvent(Event& e) + { + EventDispatcher dispatcher(e); + dispatcher.Dispatch(SS_BIND_EVENT_FN(OrthographicCameraController::OnMouseScrolled)); + dispatcher.Dispatch(SS_BIND_EVENT_FN(OrthographicCameraController::OnWindowResized)); + } + + bool OrthographicCameraController::OnMouseScrolled(MouseScrolledEvent& e) + { + m_ZoomLevel -= e.GetYOffset() * 0.25f; + m_ZoomLevel = std::max(m_ZoomLevel, 0.25f); + m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel); + return false; + } + + bool OrthographicCameraController::OnWindowResized(WindowResizeEvent& e) + { + m_AspectRatio = (float)e.GetWidth() / (float)e.GetHeight(); + m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel); + return false; + } +} \ No newline at end of file diff --git a/StarStudio/src/StarStudio/OrthographicCameraController.h b/StarStudio/src/StarStudio/OrthographicCameraController.h new file mode 100644 index 00000000..15c3ff44 --- /dev/null +++ b/StarStudio/src/StarStudio/OrthographicCameraController.h @@ -0,0 +1,39 @@ +#pragma once +#include "StarStudio/Renderer/OrthographicCamera.h" +#include "StarStudio/Core/Timestep.h" + +#include "StarStudio/Events/ApplicationEvent.h" +#include "StarStudio/Events/MouseEvent.h" + +namespace StarStudio { + + class OrthographicCameraController + { + + public: + OrthographicCameraController(float aspectRatio, bool rotation = false); + + void OnUpdate(Timestep ts); + void OnEvent(Event& e); + + OrthographicCamera& GetCamera() { return m_Camera; } + const OrthographicCamera& GetCamera() const { return m_Camera; } + private: + bool OnMouseScrolled(MouseScrolledEvent& e); + bool OnWindowResized(WindowResizeEvent& e); + private: + float m_AspectRatio; + float m_ZoomLevel = 1.0f; + + OrthographicCamera m_Camera; + + bool m_Rotation; + + glm::vec3 m_CameraPosition = { 0.0f, 0.0f, 0.0f }; + + float m_CameraRotation = 0.0f; + + float m_CameraTranslationSpeed = 5.0f; + float m_CameraRotationSpeed = 90.0f; + }; +} \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Renderer/OrthographicCamera.cpp b/StarStudio/src/StarStudio/Renderer/OrthographicCamera.cpp index 32bd8d8b..1baabca6 100644 --- a/StarStudio/src/StarStudio/Renderer/OrthographicCamera.cpp +++ b/StarStudio/src/StarStudio/Renderer/OrthographicCamera.cpp @@ -11,6 +11,12 @@ namespace StarStudio { m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix; } + void OrthographicCamera::SetProjection(float left, float right, float bottom, float top) + { + m_ProjectionMatrix = glm::ortho(left, right, bottom, top, -1.0f, 1.0f); + m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix; + } + void OrthographicCamera::RecalculateViewMatrix() { glm::mat4 transform = glm::translate(glm::mat4(1.0f), m_Position) * diff --git a/StarStudio/src/StarStudio/Renderer/OrthographicCamera.h b/StarStudio/src/StarStudio/Renderer/OrthographicCamera.h index f78f63a7..d53703d3 100644 --- a/StarStudio/src/StarStudio/Renderer/OrthographicCamera.h +++ b/StarStudio/src/StarStudio/Renderer/OrthographicCamera.h @@ -2,6 +2,8 @@ #include +#include "glm/ext/matrix_clip_space.hpp" + namespace StarStudio { class OrthographicCamera @@ -15,6 +17,8 @@ namespace StarStudio { float GetRotation() const { return m_Rotation; } void SetRotation(float rotation) { m_Rotation = rotation; RecalculateViewMatrix(); } + void SetProjection(float left, float right, float bottom, float top); + const glm::mat4& GetProjectionMatrix() const { return m_ProjectionMatrix; } const glm::mat4& GetViewMatrix() const { return m_ViewMatrix; } const glm::mat4& GetViewProjectionMatrix() const { return m_ViewProjectionMatrix; } @@ -29,4 +33,4 @@ namespace StarStudio { float m_Rotation = 0.0f; }; -} \ No newline at end of file +} From ee366e45268d74a8ef09e4e0d52972b8f8659ae5 Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 17:00:21 -0500 Subject: [PATCH 02/20] proper window resize handling --- .../src/Platform/OpenGL/OpenGLRendererAPI.cpp | 5 +++ .../src/Platform/OpenGL/OpenGLRendererAPI.h | 1 + StarStudio/src/StarStudio/Application.cpp | 31 ++++++++++++------- StarStudio/src/StarStudio/Application.h | 12 +++---- .../StarStudio/OrthographicCameraController.h | 3 ++ .../src/StarStudio/Renderer/RenderCommand.h | 5 +++ .../src/StarStudio/Renderer/Renderer.cpp | 5 +++ StarStudio/src/StarStudio/Renderer/Renderer.h | 1 + .../src/StarStudio/Renderer/RendererAPI.h | 1 + 9 files changed, 46 insertions(+), 18 deletions(-) diff --git a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp index 994ca42c..51fd2bcc 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp @@ -11,6 +11,11 @@ namespace StarStudio glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } + void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) + { + glViewport(x, y, width, height); + } + void OpenGLRendererAPI::SetClearColor(const glm::vec4& color) { glClearColor(color.r, color.g, color.b, color.a); diff --git a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.h b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.h index 9e1adc78..7226160f 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.h +++ b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.h @@ -9,6 +9,7 @@ namespace StarStudio { public: virtual void Init() override; + virtual void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) override; virtual void SetClearColor(const glm::vec4& color) override; virtual void Clear() override; diff --git a/StarStudio/src/StarStudio/Application.cpp b/StarStudio/src/StarStudio/Application.cpp index 3b523351..9441ca87 100644 --- a/StarStudio/src/StarStudio/Application.cpp +++ b/StarStudio/src/StarStudio/Application.cpp @@ -19,23 +19,15 @@ namespace StarStudio SS_CORE_ASSERT(!s_Instance, "Application already exists!"); s_Instance = this; - m_Window = Scope(Window::Create()); + m_Window = std::unique_ptr(Window::Create()); m_Window->SetEventCallback(BIND_EVENT_FN(OnEvent)); Renderer::Init(); - SS_CORE_INFO("In order to use StarStudio, you need the assets folder from Sandbox, in the root folder of the .exe"); - SS_CORE_INFO("If you don't have the assets folder, you can download it from the StarStudio repository"); - m_ImGuiLayer = new ImGuiLayer(); PushOverlay(m_ImGuiLayer); } - Application::~Application() - { - - } - void Application::PushLayer(Layer* layer) { m_LayerStack.PushLayer(layer); @@ -50,6 +42,7 @@ namespace StarStudio { EventDispatcher dispatcher(e); dispatcher.Dispatch(BIND_EVENT_FN(OnWindowClose)); + dispatcher.Dispatch(BIND_EVENT_FN(OnWindowResize)); for (auto it = m_LayerStack.end(); it != m_LayerStack.begin(); ) { @@ -67,15 +60,17 @@ namespace StarStudio Timestep timestep = time - m_LastFrameTime; m_LastFrameTime = time; - for (Layer* layer : m_LayerStack) - layer->OnUpdate(timestep); + if (!m_Minimized) + { + for (Layer* layer : m_LayerStack) + layer->OnUpdate(timestep); + } m_ImGuiLayer->Begin(); for (Layer* layer : m_LayerStack) layer->OnImGuiRender(); m_ImGuiLayer->End(); - m_Window->OnUpdate(); } } @@ -86,4 +81,16 @@ namespace StarStudio return true; } + bool Application::OnWindowResize(WindowResizeEvent& e) + { + if (e.GetWidth() == 0 || e.GetHeight() == 0) + { + m_Minimized = true; + return false; + } + m_Minimized = false; + Renderer::OnWindowResize(e.GetWidth(), e.GetHeight()); + return false; + } + } diff --git a/StarStudio/src/StarStudio/Application.h b/StarStudio/src/StarStudio/Application.h index d94ef1a3..31399261 100644 --- a/StarStudio/src/StarStudio/Application.h +++ b/StarStudio/src/StarStudio/Application.h @@ -29,16 +29,16 @@ namespace StarStudio inline static Application& Get() { return *s_Instance; } private: bool OnWindowClose(WindowCloseEvent& e); - - Scope m_Window; + bool OnWindowResize(WindowResizeEvent& e); + private: + std::unique_ptr m_Window; ImGuiLayer* m_ImGuiLayer; - bool m_Running = true; + bool m_Minimized = false; LayerStack m_LayerStack; - float m_LastFrameTime = 0.0f; - private: - static Application* s_Instance; + private: + static Application* s_Instance; }; // To be defined in CLIENT diff --git a/StarStudio/src/StarStudio/OrthographicCameraController.h b/StarStudio/src/StarStudio/OrthographicCameraController.h index 15c3ff44..c458cf04 100644 --- a/StarStudio/src/StarStudio/OrthographicCameraController.h +++ b/StarStudio/src/StarStudio/OrthographicCameraController.h @@ -18,6 +18,9 @@ namespace StarStudio { OrthographicCamera& GetCamera() { return m_Camera; } const OrthographicCamera& GetCamera() const { return m_Camera; } + + float GetZoomLevel() const { return m_ZoomLevel; } + void SetZoomLevel(float level) { m_ZoomLevel = level; } private: bool OnMouseScrolled(MouseScrolledEvent& e); bool OnWindowResized(WindowResizeEvent& e); diff --git a/StarStudio/src/StarStudio/Renderer/RenderCommand.h b/StarStudio/src/StarStudio/Renderer/RenderCommand.h index 6357ee5b..c5db2c10 100644 --- a/StarStudio/src/StarStudio/Renderer/RenderCommand.h +++ b/StarStudio/src/StarStudio/Renderer/RenderCommand.h @@ -12,6 +12,11 @@ namespace StarStudio s_RendererAPI->Init(); } + inline static void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) + { + s_RendererAPI->SetViewport(x, y, width, height); + } + inline static void SetClearColor(const glm::vec4& color) { s_RendererAPI->SetClearColor(color); diff --git a/StarStudio/src/StarStudio/Renderer/Renderer.cpp b/StarStudio/src/StarStudio/Renderer/Renderer.cpp index bc768f0d..f8408e3c 100644 --- a/StarStudio/src/StarStudio/Renderer/Renderer.cpp +++ b/StarStudio/src/StarStudio/Renderer/Renderer.cpp @@ -12,6 +12,11 @@ namespace StarStudio { RenderCommand::Init(); } + void Renderer::OnWindowResize(uint32_t width, uint32_t height) + { + RenderCommand::SetViewport(0, 0, width, height); + } + void Renderer::BeginScene(OrthographicCamera& camera) { s_SceneData->ViewProjectionMatrix = camera.GetViewProjectionMatrix(); diff --git a/StarStudio/src/StarStudio/Renderer/Renderer.h b/StarStudio/src/StarStudio/Renderer/Renderer.h index bab044de..612eafad 100644 --- a/StarStudio/src/StarStudio/Renderer/Renderer.h +++ b/StarStudio/src/StarStudio/Renderer/Renderer.h @@ -12,6 +12,7 @@ namespace StarStudio { { public: static void Init(); + static void OnWindowResize(uint32_t width, uint32_t height); static void BeginScene(OrthographicCamera& camera); static void EndScene(); diff --git a/StarStudio/src/StarStudio/Renderer/RendererAPI.h b/StarStudio/src/StarStudio/Renderer/RendererAPI.h index a2146902..1d8ce5c8 100644 --- a/StarStudio/src/StarStudio/Renderer/RendererAPI.h +++ b/StarStudio/src/StarStudio/Renderer/RendererAPI.h @@ -14,6 +14,7 @@ namespace StarStudio }; public: virtual void Init() = 0; + virtual void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) = 0; virtual void SetClearColor(const glm::vec4& color) = 0; virtual void Clear() = 0; From e226fdf8366c9d066efb0b83520c7b5855241dff Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 17:43:22 -0500 Subject: [PATCH 03/20] Using default destructors over empty ones the issue from the last commit :/ --- StarStudio/src/StarStudio/Application.h | 2 +- StarStudio/src/StarStudio/ImGui/ImGuiLayer.cpp | 3 --- StarStudio/src/StarStudio/ImGui/ImGuiLayer.h | 2 +- StarStudio/src/StarStudio/Layer.cpp | 4 ---- StarStudio/src/StarStudio/Layer.h | 2 +- StarStudio/src/StarStudio/OrthographicCameraController.h | 3 +-- StarStudio/src/StarStudio/Renderer/Buffer.h | 4 ++-- 7 files changed, 6 insertions(+), 14 deletions(-) diff --git a/StarStudio/src/StarStudio/Application.h b/StarStudio/src/StarStudio/Application.h index 31399261..cd974fc8 100644 --- a/StarStudio/src/StarStudio/Application.h +++ b/StarStudio/src/StarStudio/Application.h @@ -17,7 +17,7 @@ namespace StarStudio { public: Application(); - virtual ~Application(); + virtual ~Application() = default; void Run(); diff --git a/StarStudio/src/StarStudio/ImGui/ImGuiLayer.cpp b/StarStudio/src/StarStudio/ImGui/ImGuiLayer.cpp index 344e0d25..22e7bc76 100644 --- a/StarStudio/src/StarStudio/ImGui/ImGuiLayer.cpp +++ b/StarStudio/src/StarStudio/ImGui/ImGuiLayer.cpp @@ -16,10 +16,7 @@ namespace StarStudio { ImGuiLayer::ImGuiLayer() : Layer("ImGuiLayer") { - } - ImGuiLayer::~ImGuiLayer() - { } void ImGuiLayer::OnAttach() diff --git a/StarStudio/src/StarStudio/ImGui/ImGuiLayer.h b/StarStudio/src/StarStudio/ImGui/ImGuiLayer.h index a0e3be44..49d7a781 100644 --- a/StarStudio/src/StarStudio/ImGui/ImGuiLayer.h +++ b/StarStudio/src/StarStudio/ImGui/ImGuiLayer.h @@ -12,7 +12,7 @@ namespace StarStudio { { public: ImGuiLayer(); - ~ImGuiLayer(); + ~ImGuiLayer() = default; virtual void OnAttach() override; virtual void OnDetach() override; diff --git a/StarStudio/src/StarStudio/Layer.cpp b/StarStudio/src/StarStudio/Layer.cpp index 2ab667ef..38ac5cb5 100644 --- a/StarStudio/src/StarStudio/Layer.cpp +++ b/StarStudio/src/StarStudio/Layer.cpp @@ -7,9 +7,5 @@ namespace StarStudio { : m_DebugName(debugName) { - } - Layer::~Layer() - { - } } diff --git a/StarStudio/src/StarStudio/Layer.h b/StarStudio/src/StarStudio/Layer.h index 112eba9f..e6892fef 100644 --- a/StarStudio/src/StarStudio/Layer.h +++ b/StarStudio/src/StarStudio/Layer.h @@ -10,7 +10,7 @@ namespace StarStudio { public: Layer(const std::string& name = "Layer"); - virtual ~Layer(); + virtual ~Layer() = default; virtual void OnAttach() {} virtual void OnDetach() {} diff --git a/StarStudio/src/StarStudio/OrthographicCameraController.h b/StarStudio/src/StarStudio/OrthographicCameraController.h index c458cf04..cd40e793 100644 --- a/StarStudio/src/StarStudio/OrthographicCameraController.h +++ b/StarStudio/src/StarStudio/OrthographicCameraController.h @@ -36,7 +36,6 @@ namespace StarStudio { float m_CameraRotation = 0.0f; - float m_CameraTranslationSpeed = 5.0f; - float m_CameraRotationSpeed = 90.0f; + float m_CameraTranslationSpeed = 5.0f, m_CameraRotationSpeed = 90.0f; }; } \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Renderer/Buffer.h b/StarStudio/src/StarStudio/Renderer/Buffer.h index 34e58079..ba6d6430 100644 --- a/StarStudio/src/StarStudio/Renderer/Buffer.h +++ b/StarStudio/src/StarStudio/Renderer/Buffer.h @@ -107,7 +107,7 @@ namespace StarStudio { class VertexBuffer { public: - virtual ~VertexBuffer() {} + virtual ~VertexBuffer() = default; virtual void Bind() const = 0; virtual void Unbind() const = 0; @@ -121,7 +121,7 @@ namespace StarStudio { class IndexBuffer { public: - virtual ~IndexBuffer() {} + virtual ~IndexBuffer() = default; virtual void Bind() const = 0; virtual void Unbind() const = 0; From 261dd5a2c14434dbd7e81000a82163f6d48bbc9f Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 18:05:05 -0500 Subject: [PATCH 04/20] fix include due to merge --- StarStudio/src/StarStudio/OrthographicCameraController.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/StarStudio/src/StarStudio/OrthographicCameraController.cpp b/StarStudio/src/StarStudio/OrthographicCameraController.cpp index ed4dacf1..ee0fed35 100644 --- a/StarStudio/src/StarStudio/OrthographicCameraController.cpp +++ b/StarStudio/src/StarStudio/OrthographicCameraController.cpp @@ -1,8 +1,8 @@ #include "sspch.h" #include "OrthographicCameraController.h" -#include "StarStudio/Input.h" -#include "StarStudio/KeyCodes.h" +#include "StarStudio/Core/Input.h" +#include "StarStudio/Core/KeyCodes.h" namespace StarStudio { From 3d1f7edff3f1f921fc449c0302a414ceb7f49af2 Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 18:12:20 -0500 Subject: [PATCH 05/20] remove unnecessary token-pasting --- StarStudio/src/StarStudio/Events/Event.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/StarStudio/src/StarStudio/Events/Event.h b/StarStudio/src/StarStudio/Events/Event.h index fdc21280..4fcf30b4 100644 --- a/StarStudio/src/StarStudio/Events/Event.h +++ b/StarStudio/src/StarStudio/Events/Event.h @@ -29,7 +29,7 @@ namespace StarStudio { EventCategoryMouseButton = BIT(4) }; -#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::##type; }\ +#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::type; }\ virtual EventType GetEventType() const override { return GetStaticType(); }\ virtual const char* GetName() const override { return #type; } From aa04495ef9a98170798571a2cd3a6a164010a73a Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 18:13:51 -0500 Subject: [PATCH 06/20] prevent leak --- StarStudio/src/Platform/OpenGL/OpenGLShader.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp b/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp index 62d3f061..84fbf991 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp @@ -154,7 +154,10 @@ namespace StarStudio { } for (auto id : glShaderIDs) + { glDetachShader(program, id); + glDeleteShader(id); + } } From 039d716604efff31919294c644e329b8a5a20a2c Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 18:24:00 -0500 Subject: [PATCH 07/20] auto detect platform --- StarStudio/src/StarStudio/Core/Core.h | 73 +++++++++++++++++++++------ premake5.lua | 8 --- 2 files changed, 58 insertions(+), 23 deletions(-) diff --git a/StarStudio/src/StarStudio/Core/Core.h b/StarStudio/src/StarStudio/Core/Core.h index 707fb158..fbe89d8b 100644 --- a/StarStudio/src/StarStudio/Core/Core.h +++ b/StarStudio/src/StarStudio/Core/Core.h @@ -2,38 +2,81 @@ #include +// Platform detection using predefined macros +#ifdef _WIN32 + /* Windows x64/x86 */ +#ifdef _WIN64 + /* Windows x64 */ +#define SS_PLATFORM_WINDOWS +#else + /* Windows x86 */ +#error "x86 Builds are not supported!" +#endif +#elif defined(__APPLE__) || defined(__MACH__) +#include +/* TARGET_OS_MAC exists on all the platforms + * so we must check all of them (in this order) + * to ensure that we're running on MAC + * and not some other Apple platform */ +#if TARGET_IPHONE_SIMULATOR == 1 +#error "IOS simulator is not supported!" +#elif TARGET_OS_IPHONE == 1 +#define SS_PLATFORM_IOS +#error "IOS is not supported!" +#elif TARGET_OS_MAC == 1 +#define SS_PLATFORM_MACOS +#error "MacOS is not supported!" +#else +#error "Unknown Apple platform!" +#endif + /* We also have to check __ANDROID__ before __linux__ + * since android is based on the linux kernel + * it has __linux__ defined */ +#elif defined(__ANDROID__) +#define SS_PLATFORM_ANDROID +#error "Android is not supported!" +#elif defined(__linux__) +#define SS_PLATFORM_LINUX +#error "Linux is not supported!" +#else + /* Unknown compiler/platform */ +#error "Unknown platform!" +#endif // End of platform detection + + +// DLL support #ifdef SS_PLATFORM_WINDOWS #if SS_DYNAMIC_LINK - #ifdef SS_BUILD_DLL - #define STARSTUDIO_API __declspec(dllexport) - #else - #define STARSTUDIO_API __declspec(dllimport) - #endif +#ifdef SS_BUILD_DLL +#define STARSTUDIO_API __declspec(dllexport) #else - #define STARSTUDIO_API +#define STARSTUDIO_API __declspec(dllimport) #endif #else - #error StarStudio only supports Windows! +#define STARSTUDIO_API #endif +#else +#error StarStudio only supports Windows! +#endif // End of DLL support #ifdef SS_DEBUG - #define SS_ENABLE_ASSERTS +#define SS_ENABLE_ASSERTS #endif #ifdef SS_ENABLE_ASSERTS - #define SS_ASSERT(x, ...) { if(!(x)) { SS_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } } - #define SS_CORE_ASSERT(x, ...) { if(!(x)) { SS_CORE_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } } +#define SS_ASSERT(x, ...) { if(!(x)) { SS_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } } +#define SS_CORE_ASSERT(x, ...) { if(!(x)) { SS_CORE_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } } #else - #define SS_ASSERT(x, ...) - #define SS_CORE_ASSERT(x, ...) +#define SS_ASSERT(x, ...) +#define SS_CORE_ASSERT(x, ...) #endif - #define BIT(x) (1 << x) + #define SS_BIND_EVENT_FN(fn) std::bind(&fn, this, std::placeholders::_1) -namespace StarStudio -{ +namespace StarStudio { + template using Scope = std::unique_ptr; diff --git a/premake5.lua b/premake5.lua index 84969be6..b337b7b1 100644 --- a/premake5.lua +++ b/premake5.lua @@ -85,7 +85,6 @@ project "StarStudio" defines { - "SS_PLATFORM_WINDOWS", "SS_BUILD_DLL", "GLFW_INCLUDE_NONE" } @@ -137,13 +136,6 @@ project "Sandbox" "StarStudio" } - filter "system:windows" - systemversion "latest" - defines - { - "SS_PLATFORM_WINDOWS" - } - filter "configurations:Debug" defines "SS_DEBUG" runtime "Debug" From a1dd40893b7849e3a664965588d644ef2a4a454d Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 18:24:04 -0500 Subject: [PATCH 08/20] Fixed edge case in OpenGLShader::PreProcess Logic --- StarStudio/src/Platform/OpenGL/OpenGLShader.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp b/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp index 84fbf991..1c05a029 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp @@ -73,18 +73,20 @@ namespace StarStudio { const char* typeToken = "#type"; size_t typeTokenLength = strlen(typeToken); - size_t pos = source.find(typeToken, 0); + size_t pos = source.find(typeToken, 0); //Start of shader type declaration line while (pos != std::string::npos) { - size_t eol = source.find_first_of("\r\n", pos); + size_t eol = source.find_first_of("\r\n", pos); //End of shader type declaration line SS_CORE_ASSERT(eol != std::string::npos, "Syntax error"); - size_t begin = pos + typeTokenLength + 1; + size_t begin = pos + typeTokenLength + 1; //Start of shader type name (after "#type " keyword) std::string type = source.substr(begin, eol - begin); SS_CORE_ASSERT(ShaderTypeFromString(type), "Invalid shader type specified"); - size_t nextLinePos = source.find_first_not_of("\r\n", eol); - pos = source.find(typeToken, nextLinePos); - shaderSources[ShaderTypeFromString(type)] = source.substr(nextLinePos, pos - (nextLinePos == std::string::npos ? source.size() - 1 : nextLinePos)); + size_t nextLinePos = source.find_first_not_of("\r\n", eol); //Start of shader code after shader type declaration line + SS_CORE_ASSERT(nextLinePos != std::string::npos, "Syntax error"); + pos = source.find(typeToken, nextLinePos); //Start of next shader type declaration line + + shaderSources[ShaderTypeFromString(type)] = (pos == std::string::npos) ? source.substr(nextLinePos) : source.substr(nextLinePos, pos - nextLinePos); } return shaderSources; From fd562301f7a3defee334dd7e6a1fee1fc53cc5de Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 18:50:28 -0500 Subject: [PATCH 09/20] Added check for OpenGL version --- StarStudio/src/Platform/OpenGL/OpenGLContext.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/StarStudio/src/Platform/OpenGL/OpenGLContext.cpp b/StarStudio/src/Platform/OpenGL/OpenGLContext.cpp index 29ae087a..6f1466ab 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLContext.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLContext.cpp @@ -23,6 +23,14 @@ namespace StarStudio { SS_CORE_INFO(" Vendor: {0}", (const char*)glGetString(GL_VENDOR)); SS_CORE_INFO(" Renderer: {0}", (const char*)glGetString(GL_RENDERER)); SS_CORE_INFO(" Version: {0}", (const char*)glGetString(GL_VERSION)); + + #ifdef SS_ENABLE_ASSERTS + int versionMajor; + int versionMinor; + glGetIntegerv(GL_MAJOR_VERSION, &versionMajor); + glGetIntegerv(GL_MINOR_VERSION, &versionMinor); + SS_CORE_ASSERT(versionMajor > 4 || (versionMajor == 4 && versionMinor >= 5), "StarStudio requires at least OpenGL version 4.5!"); + #endif } void OpenGLContext::SwapBuffers() From 56b0a5445d3d63f99c9f5a13098ee9282624192d Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 18:52:06 -0500 Subject: [PATCH 10/20] Fixes for OrthographicCameraController Movement --- .../OrthographicCameraController.cpp | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/StarStudio/src/StarStudio/OrthographicCameraController.cpp b/StarStudio/src/StarStudio/OrthographicCameraController.cpp index ee0fed35..599d8bfa 100644 --- a/StarStudio/src/StarStudio/OrthographicCameraController.cpp +++ b/StarStudio/src/StarStudio/OrthographicCameraController.cpp @@ -14,14 +14,26 @@ namespace StarStudio { void OrthographicCameraController::OnUpdate(Timestep ts) { if (Input::IsKeyPressed(SS_KEY_A)) - m_CameraPosition.x -= m_CameraTranslationSpeed * ts; + { + m_CameraPosition.x -= cos(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + m_CameraPosition.y -= sin(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + } else if (Input::IsKeyPressed(SS_KEY_D)) - m_CameraPosition.x += m_CameraTranslationSpeed * ts; + { + m_CameraPosition.x += cos(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + m_CameraPosition.y += sin(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + } if (Input::IsKeyPressed(SS_KEY_W)) - m_CameraPosition.y += m_CameraTranslationSpeed * ts; + { + m_CameraPosition.x += -sin(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + m_CameraPosition.y += cos(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + } else if (Input::IsKeyPressed(SS_KEY_S)) - m_CameraPosition.y -= m_CameraTranslationSpeed * ts; + { + m_CameraPosition.x -= -sin(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + m_CameraPosition.y -= cos(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + } if (m_Rotation) { @@ -32,6 +44,11 @@ namespace StarStudio { m_CameraRotation -= m_CameraRotationSpeed * ts; m_Camera.SetRotation(m_CameraRotation); + + if (m_CameraRotation > 180.0f) + m_CameraRotation -= 360.0f; + else if (m_CameraRotation <= -180.0f) + m_CameraRotation += 360.0f; } m_Camera.SetPosition(m_CameraPosition); From a727da1b4c84c8dc0235a3b0e6493f1a598f9f0a Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 19:02:53 -0500 Subject: [PATCH 11/20] Use 'Scope's instead of raw pointers (mem leak) --- StarStudio/src/Platform/Windows/WindowsInput.cpp | 2 +- StarStudio/src/Platform/Windows/WindowsWindow.cpp | 2 +- StarStudio/src/Platform/Windows/WindowsWindow.h | 2 +- StarStudio/src/StarStudio/Core/Core.h | 10 ++++++++++ StarStudio/src/StarStudio/Core/Input.h | 2 +- StarStudio/src/StarStudio/Renderer/RenderCommand.cpp | 2 +- StarStudio/src/StarStudio/Renderer/RenderCommand.h | 2 +- StarStudio/src/StarStudio/Renderer/Renderer.cpp | 2 +- StarStudio/src/StarStudio/Renderer/Renderer.h | 2 +- StarStudio/src/StarStudio/Renderer/Shader.cpp | 6 +++--- 10 files changed, 21 insertions(+), 11 deletions(-) diff --git a/StarStudio/src/Platform/Windows/WindowsInput.cpp b/StarStudio/src/Platform/Windows/WindowsInput.cpp index a029501c..e7b340e8 100644 --- a/StarStudio/src/Platform/Windows/WindowsInput.cpp +++ b/StarStudio/src/Platform/Windows/WindowsInput.cpp @@ -6,7 +6,7 @@ namespace StarStudio { - Input* Input::s_Instance = new WindowsInput(); + Scope Input::s_Instance = CreateScope(); bool WindowsInput::IsKeyPressedImpl(int keycode) { diff --git a/StarStudio/src/Platform/Windows/WindowsWindow.cpp b/StarStudio/src/Platform/Windows/WindowsWindow.cpp index 7ab2bd60..9972e47b 100644 --- a/StarStudio/src/Platform/Windows/WindowsWindow.cpp +++ b/StarStudio/src/Platform/Windows/WindowsWindow.cpp @@ -50,7 +50,7 @@ namespace StarStudio { m_Window = glfwCreateWindow((int)props.Width, (int)props.Height, m_Data.Title.c_str(), nullptr, nullptr); - m_Context = new OpenGLContext(m_Window); + m_Context = CreateScope(m_Window); m_Context->Init(); glfwSetWindowUserPointer(m_Window, &m_Data); diff --git a/StarStudio/src/Platform/Windows/WindowsWindow.h b/StarStudio/src/Platform/Windows/WindowsWindow.h index 8c087a96..b8ae4bed 100644 --- a/StarStudio/src/Platform/Windows/WindowsWindow.h +++ b/StarStudio/src/Platform/Windows/WindowsWindow.h @@ -29,7 +29,7 @@ namespace StarStudio { virtual void Shutdown(); private: GLFWwindow* m_Window; - GraphicsContext* m_Context; + Scope m_Context; struct WindowData { diff --git a/StarStudio/src/StarStudio/Core/Core.h b/StarStudio/src/StarStudio/Core/Core.h index fbe89d8b..d4d100ac 100644 --- a/StarStudio/src/StarStudio/Core/Core.h +++ b/StarStudio/src/StarStudio/Core/Core.h @@ -79,8 +79,18 @@ namespace StarStudio { template using Scope = std::unique_ptr; + template + constexpr Scope CreateScope(Args&& ... args) + { + return std::make_unique(std::forward(args)...); + } template using Ref = std::shared_ptr; + template + constexpr Ref CreateRef(Args&& ... args) + { + return std::make_shared(std::forward(args)...); + } } \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Core/Input.h b/StarStudio/src/StarStudio/Core/Input.h index 83ce3bc8..21bbdc5f 100644 --- a/StarStudio/src/StarStudio/Core/Input.h +++ b/StarStudio/src/StarStudio/Core/Input.h @@ -26,7 +26,7 @@ namespace StarStudio { virtual float GetMouseXImpl() = 0; virtual float GetMouseYImpl() = 0; private: - static Input* s_Instance; + static Scope s_Instance; }; } \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Renderer/RenderCommand.cpp b/StarStudio/src/StarStudio/Renderer/RenderCommand.cpp index 5dc99ee6..ce3a29df 100644 --- a/StarStudio/src/StarStudio/Renderer/RenderCommand.cpp +++ b/StarStudio/src/StarStudio/Renderer/RenderCommand.cpp @@ -5,5 +5,5 @@ namespace StarStudio { - RendererAPI* RenderCommand::s_RendererAPI = new OpenGLRendererAPI(); + Scope RenderCommand::s_RendererAPI = CreateScope(); } \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Renderer/RenderCommand.h b/StarStudio/src/StarStudio/Renderer/RenderCommand.h index c5db2c10..f6307fae 100644 --- a/StarStudio/src/StarStudio/Renderer/RenderCommand.h +++ b/StarStudio/src/StarStudio/Renderer/RenderCommand.h @@ -31,6 +31,6 @@ namespace StarStudio s_RendererAPI->DrawIndexed(vertexArray); } private: - static RendererAPI* s_RendererAPI; + static Scope s_RendererAPI; }; } diff --git a/StarStudio/src/StarStudio/Renderer/Renderer.cpp b/StarStudio/src/StarStudio/Renderer/Renderer.cpp index f8408e3c..ec83890e 100644 --- a/StarStudio/src/StarStudio/Renderer/Renderer.cpp +++ b/StarStudio/src/StarStudio/Renderer/Renderer.cpp @@ -5,7 +5,7 @@ namespace StarStudio { - Renderer::SceneData* Renderer::s_SceneData = new Renderer::SceneData; + Scope Renderer::s_SceneData = CreateScope(); void Renderer::Init() { diff --git a/StarStudio/src/StarStudio/Renderer/Renderer.h b/StarStudio/src/StarStudio/Renderer/Renderer.h index 612eafad..bf927ab4 100644 --- a/StarStudio/src/StarStudio/Renderer/Renderer.h +++ b/StarStudio/src/StarStudio/Renderer/Renderer.h @@ -25,6 +25,6 @@ namespace StarStudio { { glm::mat4 ViewProjectionMatrix; }; - static SceneData* s_SceneData; + static Scope s_SceneData; }; } diff --git a/StarStudio/src/StarStudio/Renderer/Shader.cpp b/StarStudio/src/StarStudio/Renderer/Shader.cpp index 55c1c558..8ed8b8bb 100644 --- a/StarStudio/src/StarStudio/Renderer/Shader.cpp +++ b/StarStudio/src/StarStudio/Renderer/Shader.cpp @@ -43,21 +43,21 @@ namespace StarStudio { Add(name, shader); } - Ref ShaderLibrary::Load(const std::string& filepath) + Ref ShaderLibrary::Load(const std::string& filepath) { auto shader = Shader::Create(filepath); Add(shader); return shader; } - Ref ShaderLibrary::Load(const std::string& name, const std::string& filepath) + Ref ShaderLibrary::Load(const std::string& name, const std::string& filepath) { auto shader = Shader::Create(filepath); Add(name, shader); return shader; } - Ref ShaderLibrary::Get(const std::string& name) + Ref ShaderLibrary::Get(const std::string& name) { SS_CORE_ASSERT(Exists(name), "Shader not found!"); return m_Shaders[name]; From 80ae204b19aeb11bc809dea59df2229df29c3fef Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 20:01:25 -0500 Subject: [PATCH 12/20] move imguilayer --- StarStudio/src/StarStudio.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/StarStudio/src/StarStudio.h b/StarStudio/src/StarStudio.h index 171be63e..3abd78bb 100644 --- a/StarStudio/src/StarStudio.h +++ b/StarStudio/src/StarStudio.h @@ -7,14 +7,14 @@ #include "StarStudio/Core/Timestep.h" +#include "StarStudio/ImGui/ImGuiLayer.h" + // ---Input------------------------- #include "StarStudio/Core/Input.h" #include "StarStudio/Core/KeyCodes.h" #include "StarStudio/Core/MouseButtonCodes.h" // --------------------------------- -#include "StarStudio/ImGui/ImGuiLayer.h" - // ---Renderer------------------------ #include "StarStudio/Renderer/Renderer.h" #include "StarStudio/Renderer/RenderCommand.h" From 630221ca93e17ae49f50f8b68600df03805a5391 Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 20:21:14 -0500 Subject: [PATCH 13/20] prepare for 2d --- Sandbox/assets/shaders/FlatColor.glsl | 26 ++++++ Sandbox/src/Sandbox2D.cpp | 88 +++++++++++++++++++ Sandbox/src/Sandbox2D.h | 25 ++++++ Sandbox/src/SandboxApp.cpp | 19 ++-- StarStudio/src/StarStudio.h | 4 - .../src/StarStudio/Renderer/VertexArray.cpp | 4 +- .../src/StarStudio/Renderer/VertexArray.h | 2 +- 7 files changed, 152 insertions(+), 16 deletions(-) create mode 100644 Sandbox/assets/shaders/FlatColor.glsl create mode 100644 Sandbox/src/Sandbox2D.cpp create mode 100644 Sandbox/src/Sandbox2D.h diff --git a/Sandbox/assets/shaders/FlatColor.glsl b/Sandbox/assets/shaders/FlatColor.glsl new file mode 100644 index 00000000..2d39fc5b --- /dev/null +++ b/Sandbox/assets/shaders/FlatColor.glsl @@ -0,0 +1,26 @@ +// Flat Color Shader + +#type vertex +#version 330 core + +layout(location = 0) in vec3 a_Position; + +uniform mat4 u_ViewProjection; +uniform mat4 u_Transform; + +void main() +{ + gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0); +} + +#type fragment +#version 330 core + +layout(location = 0) out vec4 color; + +uniform vec4 u_Color; + +void main() +{ + color = u_Color; +} \ No newline at end of file diff --git a/Sandbox/src/Sandbox2D.cpp b/Sandbox/src/Sandbox2D.cpp new file mode 100644 index 00000000..6ca425b0 --- /dev/null +++ b/Sandbox/src/Sandbox2D.cpp @@ -0,0 +1,88 @@ +#include "Sandbox2D.h" + +#include "imgui/imgui.h" + +#include +#include + +#include "Platform/OpenGL/OpenGLShader.h" + +Sandbox2D::Sandbox2D() + :Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f) +{ + +} + +void Sandbox2D::OnAttach() +{ + m_SquareVA = StarStudio::VertexArray::Create(); + float squareVertices[5 * 4] = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.5f, 0.5f, 0.0f, + -0.5f, 0.5f, 0.0f + }; + StarStudio::Ref squareVB; + squareVB.reset(StarStudio::VertexBuffer::Create(squareVertices, sizeof(squareVertices))); + squareVB->SetLayout({ + { StarStudio::ShaderDataType::Float3, "a_Position" } + }); + m_SquareVA->AddVertexBuffer(squareVB); + uint32_t squareIndices[6] = { 0, 1, 2, 2, 3, 0 }; + StarStudio::Ref squareIB; + squareIB.reset(StarStudio::IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))); + m_SquareVA->SetIndexBuffer(squareIB); + m_FlatColorShader = StarStudio::Shader::Create("assets/shaders/FlatColor.glsl"); +} + +void Sandbox2D::OnDetach() +{ + +} + +void Sandbox2D::OnUpdate(StarStudio::Timestep ts) +{ + // Update + m_CameraController.OnUpdate(ts); + // Render + StarStudio::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 }); + StarStudio::RenderCommand::Clear(); + StarStudio::Renderer::BeginScene(m_CameraController.GetCamera()); + std::dynamic_pointer_cast(m_FlatColorShader)->Bind(); + std::dynamic_pointer_cast(m_FlatColorShader)->UploadUniformFloat4("u_Color", m_SquareColor); + StarStudio::Renderer::Submit(m_FlatColorShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f))); + StarStudio::Renderer::EndScene(); +} + +void Sandbox2D::OnImGuiRender() +{ + //Camera Info + ImGui::Begin("Camera Info"); + + ImGui::Text("Camera Position"); + ImGui::Text("X: %.2f", m_CameraController.GetCamera().GetPosition().x); + ImGui::Text("Y: %.2f", m_CameraController.GetCamera().GetPosition().y); + ImGui::Text("Z: %.2f", m_CameraController.GetCamera().GetPosition().z); + + ImGui::Text("Camera Rotation: %.2f", m_CameraController.GetCamera().GetRotation()); + + if (ImGui::Button("Reset")) { + m_CameraController.GetCamera().SetPosition(glm::vec3(0.0f)); + m_CameraController.GetCamera().SetRotation(0.0f); + } + + ImGui::End(); + + //Color Picker + ImGui::Begin("Color Picker"); + + ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor)); + + ImGui::End(); + +} + +void Sandbox2D::OnEvent(StarStudio::Event& e) +{ + m_CameraController.OnEvent(e); +} diff --git a/Sandbox/src/Sandbox2D.h b/Sandbox/src/Sandbox2D.h new file mode 100644 index 00000000..4e951ba5 --- /dev/null +++ b/Sandbox/src/Sandbox2D.h @@ -0,0 +1,25 @@ +#pragma once +#include "StarStudio.h" +#include "StarStudio/OrthographicCameraController.h" + +class Sandbox2D : public StarStudio::Layer +{ +public: + Sandbox2D(); + virtual ~Sandbox2D() = default; + + virtual void OnAttach() override; + virtual void OnDetach() override; + + void OnUpdate(StarStudio::Timestep ts) override; + virtual void OnImGuiRender() override; + void OnEvent(StarStudio::Event& e) override; +private: + StarStudio::OrthographicCameraController m_CameraController; + + // Temp + StarStudio::Ref m_SquareVA; + StarStudio::Ref m_FlatColorShader; + + glm::vec4 m_SquareColor = { 0.2f, 0.3f, 0.8f, 1.0f }; +}; diff --git a/Sandbox/src/SandboxApp.cpp b/Sandbox/src/SandboxApp.cpp index 9efd0375..01103a5a 100644 --- a/Sandbox/src/SandboxApp.cpp +++ b/Sandbox/src/SandboxApp.cpp @@ -1,4 +1,5 @@ #include +#include #include "Platform/OpenGL/OpenGLShader.h" @@ -6,16 +7,19 @@ #include +#include "Sandbox2D.h" #include "StarStudio/OrthographicCameraController.h" #include "glm/gtc/type_ptr.hpp" +#include "Sandbox2D.h" + class ExampleLayer : public StarStudio::Layer { public: ExampleLayer() : Layer("Example"), m_CameraController(1280.0f / 720.0f, true) { - m_VertexArray.reset(StarStudio::VertexArray::Create()); + m_VertexArray = StarStudio::VertexArray::Create(); float vertices[3 * 7] = { -0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f, @@ -37,7 +41,7 @@ class ExampleLayer : public StarStudio::Layer indexBuffer.reset(StarStudio::IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t))); m_VertexArray->SetIndexBuffer(indexBuffer); - m_SquareVA.reset(StarStudio::VertexArray::Create()); + m_SquareVA = StarStudio::VertexArray::Create(); float squareVertices[5 * 4] = { -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, @@ -199,18 +203,14 @@ class ExampleLayer : public StarStudio::Layer } ImGui::End(); - /* + //Color Picker ImGui::Begin("Color Picker"); ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor)); - if (ImGui::Button("Reset")) - { - m_SquareColor = { 0.2f, 0.2f, 0.2f}; - } ImGui::End(); - */ + } void OnEvent(StarStudio::Event& e) override @@ -238,7 +238,8 @@ class Sandbox : public StarStudio::Application public: Sandbox() { - PushLayer(new ExampleLayer()); + // PushLayer(new ExampleLayer()); + PushLayer(new Sandbox2D()); } ~Sandbox() diff --git a/StarStudio/src/StarStudio.h b/StarStudio/src/StarStudio.h index 3abd78bb..162b017f 100644 --- a/StarStudio/src/StarStudio.h +++ b/StarStudio/src/StarStudio.h @@ -25,8 +25,4 @@ #include "StarStudio/Renderer/VertexArray.h" #include "StarStudio/Renderer/OrthographicCamera.h" -// ----------------------------------- - -// ---Entry Point--------------------- -#include "StarStudio/Core/EntryPoint.h" // ----------------------------------- \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Renderer/VertexArray.cpp b/StarStudio/src/StarStudio/Renderer/VertexArray.cpp index 40d13d80..0bf3fb74 100644 --- a/StarStudio/src/StarStudio/Renderer/VertexArray.cpp +++ b/StarStudio/src/StarStudio/Renderer/VertexArray.cpp @@ -6,12 +6,12 @@ namespace StarStudio { - VertexArray* VertexArray::Create() { + Ref VertexArray::Create() { switch (Renderer::GetAPI()) { case RendererAPI::API::None: SS_CORE_ASSERT(false, "RendererAPI::None is currently not supported") return nullptr; - case RendererAPI::API::OpenGL: return new OpenGLVertexArray(); + case RendererAPI::API::OpenGL: return std::make_shared(); } SS_CORE_ASSERT(false, "Unknown RendererAPI!"); diff --git a/StarStudio/src/StarStudio/Renderer/VertexArray.h b/StarStudio/src/StarStudio/Renderer/VertexArray.h index e9356e9e..2991dc26 100644 --- a/StarStudio/src/StarStudio/Renderer/VertexArray.h +++ b/StarStudio/src/StarStudio/Renderer/VertexArray.h @@ -19,6 +19,6 @@ namespace StarStudio { virtual const std::vector>& GetVertexBuffers() const = 0; virtual const Ref& GetIndexBuffer() const = 0; - static VertexArray* Create(); + static Ref Create(); }; } From 4f114deceff98e49deb75af4fc827f1e15ceacfc Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Sun, 2 Feb 2025 21:08:25 -0500 Subject: [PATCH 14/20] starting the 2d renderer --- Sandbox/src/Sandbox2D.cpp | 28 ++----- Sandbox/src/Sandbox2D.h | 2 +- Sandbox/src/SandboxApp.cpp | 5 +- StarStudio/src/StarStudio.h | 1 + .../OrthographicCameraController.cpp | 0 .../OrthographicCameraController.h | 0 .../src/StarStudio/Renderer/Renderer.cpp | 4 + .../src/StarStudio/Renderer/Renderer2D.cpp | 78 +++++++++++++++++++ .../src/StarStudio/Renderer/Renderer2D.h | 20 +++++ 9 files changed, 111 insertions(+), 27 deletions(-) rename StarStudio/src/StarStudio/{ => Renderer}/OrthographicCameraController.cpp (100%) rename StarStudio/src/StarStudio/{ => Renderer}/OrthographicCameraController.h (100%) create mode 100644 StarStudio/src/StarStudio/Renderer/Renderer2D.cpp create mode 100644 StarStudio/src/StarStudio/Renderer/Renderer2D.h diff --git a/Sandbox/src/Sandbox2D.cpp b/Sandbox/src/Sandbox2D.cpp index 6ca425b0..c26fbbcb 100644 --- a/Sandbox/src/Sandbox2D.cpp +++ b/Sandbox/src/Sandbox2D.cpp @@ -15,24 +15,7 @@ Sandbox2D::Sandbox2D() void Sandbox2D::OnAttach() { - m_SquareVA = StarStudio::VertexArray::Create(); - float squareVertices[5 * 4] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.5f, 0.5f, 0.0f, - -0.5f, 0.5f, 0.0f - }; - StarStudio::Ref squareVB; - squareVB.reset(StarStudio::VertexBuffer::Create(squareVertices, sizeof(squareVertices))); - squareVB->SetLayout({ - { StarStudio::ShaderDataType::Float3, "a_Position" } - }); - m_SquareVA->AddVertexBuffer(squareVB); - uint32_t squareIndices[6] = { 0, 1, 2, 2, 3, 0 }; - StarStudio::Ref squareIB; - squareIB.reset(StarStudio::IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))); - m_SquareVA->SetIndexBuffer(squareIB); - m_FlatColorShader = StarStudio::Shader::Create("assets/shaders/FlatColor.glsl"); + } void Sandbox2D::OnDetach() @@ -47,11 +30,10 @@ void Sandbox2D::OnUpdate(StarStudio::Timestep ts) // Render StarStudio::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 }); StarStudio::RenderCommand::Clear(); - StarStudio::Renderer::BeginScene(m_CameraController.GetCamera()); - std::dynamic_pointer_cast(m_FlatColorShader)->Bind(); - std::dynamic_pointer_cast(m_FlatColorShader)->UploadUniformFloat4("u_Color", m_SquareColor); - StarStudio::Renderer::Submit(m_FlatColorShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f))); - StarStudio::Renderer::EndScene(); + + StarStudio::Renderer2D::BeginScene(m_CameraController.GetCamera()); + StarStudio::Renderer2D::DrawQuad({ 0.0f, 0.0f }, { 1.0f, 1.0f }, { 0.8f, 0.2f, 0.3f, 1.0f }); + StarStudio::Renderer2D::EndScene(); } void Sandbox2D::OnImGuiRender() diff --git a/Sandbox/src/Sandbox2D.h b/Sandbox/src/Sandbox2D.h index 4e951ba5..ab766ad7 100644 --- a/Sandbox/src/Sandbox2D.h +++ b/Sandbox/src/Sandbox2D.h @@ -1,6 +1,6 @@ #pragma once #include "StarStudio.h" -#include "StarStudio/OrthographicCameraController.h" +#include "StarStudio/Renderer/OrthographicCameraController.h" class Sandbox2D : public StarStudio::Layer { diff --git a/Sandbox/src/SandboxApp.cpp b/Sandbox/src/SandboxApp.cpp index 01103a5a..39dc10aa 100644 --- a/Sandbox/src/SandboxApp.cpp +++ b/Sandbox/src/SandboxApp.cpp @@ -8,10 +8,9 @@ #include #include "Sandbox2D.h" -#include "StarStudio/OrthographicCameraController.h" -#include "glm/gtc/type_ptr.hpp" -#include "Sandbox2D.h" +#include "StarStudio/Renderer/OrthographicCameraController.h" +#include "glm/gtc/type_ptr.hpp" class ExampleLayer : public StarStudio::Layer { diff --git a/StarStudio/src/StarStudio.h b/StarStudio/src/StarStudio.h index 162b017f..95f6f24e 100644 --- a/StarStudio/src/StarStudio.h +++ b/StarStudio/src/StarStudio.h @@ -17,6 +17,7 @@ // ---Renderer------------------------ #include "StarStudio/Renderer/Renderer.h" +#include "StarStudio/Renderer/Renderer2D.h" #include "StarStudio/Renderer/RenderCommand.h" #include "StarStudio/Renderer/Buffer.h" diff --git a/StarStudio/src/StarStudio/OrthographicCameraController.cpp b/StarStudio/src/StarStudio/Renderer/OrthographicCameraController.cpp similarity index 100% rename from StarStudio/src/StarStudio/OrthographicCameraController.cpp rename to StarStudio/src/StarStudio/Renderer/OrthographicCameraController.cpp diff --git a/StarStudio/src/StarStudio/OrthographicCameraController.h b/StarStudio/src/StarStudio/Renderer/OrthographicCameraController.h similarity index 100% rename from StarStudio/src/StarStudio/OrthographicCameraController.h rename to StarStudio/src/StarStudio/Renderer/OrthographicCameraController.h diff --git a/StarStudio/src/StarStudio/Renderer/Renderer.cpp b/StarStudio/src/StarStudio/Renderer/Renderer.cpp index ec83890e..a6b54188 100644 --- a/StarStudio/src/StarStudio/Renderer/Renderer.cpp +++ b/StarStudio/src/StarStudio/Renderer/Renderer.cpp @@ -1,6 +1,9 @@ #include "sspch.h" + #include "Renderer.h" +#include "Renderer2D.h" +#include "Platform/OpenGL/OpenGLShader.h" #include "Platform/OpenGL/OpenGLShader.h" namespace StarStudio { @@ -10,6 +13,7 @@ namespace StarStudio { void Renderer::Init() { RenderCommand::Init(); + Renderer2D::Init(); } void Renderer::OnWindowResize(uint32_t width, uint32_t height) diff --git a/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp b/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp new file mode 100644 index 00000000..a2b17e18 --- /dev/null +++ b/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp @@ -0,0 +1,78 @@ +#include "sspch.h" +#include "Renderer2D.h" + +#include "StarStudio/Renderer/VertexArray.h" +#include "StarStudio/Renderer/Shader.h" +#include "StarStudio/Renderer/RenderCommand.h" + +#include "Platform/OpenGL/OpenGLShader.h" + +namespace StarStudio { + + struct Renderer2DStorage + { + Ref QuadVertexArray; + Ref FlatColorShader; + }; + + static Renderer2DStorage* s_Data; + + void Renderer2D::Init() + { + s_Data = new Renderer2DStorage(); + s_Data->QuadVertexArray = VertexArray::Create(); + + float squareVertices[4 * 3] = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.5f, 0.5f, 0.0f, + -0.5f, 0.5f, 0.0f + }; + + Ref squareVB; + squareVB.reset(VertexBuffer::Create(squareVertices, sizeof(squareVertices))); + squareVB->SetLayout({ + { ShaderDataType::Float3, "a_Position" } + }); + s_Data->QuadVertexArray->AddVertexBuffer(squareVB); + + uint32_t squareIndices[6] = { 0, 1, 2, 2, 3, 0 }; + Ref squareIB; + squareIB.reset(IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))); + s_Data->QuadVertexArray->SetIndexBuffer(squareIB); + + s_Data->FlatColorShader = Shader::Create("assets/shaders/FlatColor.glsl"); + } + + void Renderer2D::Shutdown() + { + delete s_Data; + } + + void Renderer2D::BeginScene(const OrthographicCamera& camera) + { + std::dynamic_pointer_cast(s_Data->FlatColorShader)->Bind(); + std::dynamic_pointer_cast(s_Data->FlatColorShader)->UploadUniformMat4("u_ViewProjection", camera.GetViewProjectionMatrix()); + std::dynamic_pointer_cast(s_Data->FlatColorShader)->UploadUniformMat4("u_Transform", glm::mat4(1.0f)); + } + + void Renderer2D::EndScene() + { + + } + + void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color) + { + DrawQuad({ position.x, position.y, 0.0f }, size, color); + } + + void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color) + { + std::dynamic_pointer_cast(s_Data->FlatColorShader)->Bind(); + std::dynamic_pointer_cast(s_Data->FlatColorShader)->UploadUniformFloat4("u_Color", color); + + s_Data->QuadVertexArray->Bind(); + + RenderCommand::DrawIndexed(s_Data->QuadVertexArray); + } +} diff --git a/StarStudio/src/StarStudio/Renderer/Renderer2D.h b/StarStudio/src/StarStudio/Renderer/Renderer2D.h new file mode 100644 index 00000000..9a0a2307 --- /dev/null +++ b/StarStudio/src/StarStudio/Renderer/Renderer2D.h @@ -0,0 +1,20 @@ +#pragma once + +#include "OrthographicCamera.h" + +namespace StarStudio { + class Renderer2D + { + public: + + static void Init(); + static void Shutdown(); + + static void BeginScene(const OrthographicCamera& camera); + static void EndScene(); + + // Primitives + static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color); + static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color); + }; +} From df0ef9bfd75f44cc8198f7485b12b874cfdf8e75 Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Mon, 3 Feb 2025 19:01:43 -0500 Subject: [PATCH 15/20] made rotation true --- Sandbox/src/Sandbox2D.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sandbox/src/Sandbox2D.cpp b/Sandbox/src/Sandbox2D.cpp index c26fbbcb..5c785d7c 100644 --- a/Sandbox/src/Sandbox2D.cpp +++ b/Sandbox/src/Sandbox2D.cpp @@ -8,7 +8,7 @@ #include "Platform/OpenGL/OpenGLShader.h" Sandbox2D::Sandbox2D() - :Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f) + :Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f, true) { } From a3e6558af24e92435cdf2b9faf50df6ae7f43c97 Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Mon, 3 Feb 2025 19:05:54 -0500 Subject: [PATCH 16/20] Basic ref-counting system to terminate glfw --- StarStudio/src/Platform/Windows/WindowsWindow.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/StarStudio/src/Platform/Windows/WindowsWindow.cpp b/StarStudio/src/Platform/Windows/WindowsWindow.cpp index 9972e47b..6428467c 100644 --- a/StarStudio/src/Platform/Windows/WindowsWindow.cpp +++ b/StarStudio/src/Platform/Windows/WindowsWindow.cpp @@ -9,7 +9,7 @@ namespace StarStudio { - static bool s_GLFWInitialized = false; + static uint8_t s_GLFWWindowCount = 0; static void GLFWErrorCallback(int error, const char* description) { @@ -39,16 +39,16 @@ namespace StarStudio { SS_CORE_INFO("Creating window {0} ({1}, {2})", props.Title, props.Width, props.Height); - if (!s_GLFWInitialized) + if (s_GLFWWindowCount == 0) { - // TODO: glfwTerminate on system shutdown + SS_CORE_INFO("Initializing GLFW"); int success = glfwInit(); SS_CORE_ASSERT(success, "Could not intialize GLFW!"); glfwSetErrorCallback(GLFWErrorCallback); - s_GLFWInitialized = true; } m_Window = glfwCreateWindow((int)props.Width, (int)props.Height, m_Data.Title.c_str(), nullptr, nullptr); + ++s_GLFWWindowCount; m_Context = CreateScope(m_Window); m_Context->Init(); @@ -146,6 +146,12 @@ namespace StarStudio { void WindowsWindow::Shutdown() { glfwDestroyWindow(m_Window); + + if (--s_GLFWWindowCount == 0) + { + SS_CORE_INFO("Terminating GLFW"); + glfwTerminate(); + } } void WindowsWindow::OnUpdate() From 3b1c1563b7d3cc74afb7ce265454b17547a5ced5 Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Mon, 3 Feb 2025 19:07:52 -0500 Subject: [PATCH 17/20] Fix Build warnings to do with BufferElement Offset Type --- StarStudio/src/Platform/OpenGL/OpenGLVertexArray.cpp | 2 +- StarStudio/src/StarStudio/Renderer/Buffer.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/StarStudio/src/Platform/OpenGL/OpenGLVertexArray.cpp b/StarStudio/src/Platform/OpenGL/OpenGLVertexArray.cpp index cafa03ce..8c15b86f 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLVertexArray.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLVertexArray.cpp @@ -58,7 +58,7 @@ namespace StarStudio { ShaderDataTypeToOpenGLBaseType(element.Type), element.Normalized ? GL_TRUE : GL_FALSE, layout.GetStride(), - (const void*)(intptr_t)element.Offset); + (const void*)element.Offset); m_VertexBufferIndex++; } diff --git a/StarStudio/src/StarStudio/Renderer/Buffer.h b/StarStudio/src/StarStudio/Renderer/Buffer.h index ba6d6430..0d9d413c 100644 --- a/StarStudio/src/StarStudio/Renderer/Buffer.h +++ b/StarStudio/src/StarStudio/Renderer/Buffer.h @@ -34,10 +34,10 @@ namespace StarStudio { std::string Name; ShaderDataType Type; uint32_t Size; - uint32_t Offset; + size_t Offset; bool Normalized; - BufferElement() {} + BufferElement() = default; BufferElement(ShaderDataType type, const std::string& name, bool normalized = false) : Name(name), Type(type), Size(ShaderDataTypeSize(type)), Offset(0), Normalized(normalized) { @@ -89,7 +89,7 @@ namespace StarStudio { private: void CalculateOffsetsAndStride() { - uint32_t offset = 0; + size_t offset = 0; m_Stride = 0; for (auto& element : m_Elements) From 3cbc7dd066530871a0a2ee5d5a076779b1506978 Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Mon, 3 Feb 2025 19:17:08 -0500 Subject: [PATCH 18/20] Added transforms and textures to Renderer2D --- Sandbox/assets/shaders/Texture.glsl | 2 +- Sandbox/src/Sandbox2D.cpp | 8 +-- Sandbox/src/Sandbox2D.h | 2 + .../src/Platform/OpenGL/OpenGLRendererAPI.cpp | 1 + .../src/Platform/OpenGL/OpenGLShader.cpp | 20 ++++++++ StarStudio/src/Platform/OpenGL/OpenGLShader.h | 5 ++ .../src/Platform/OpenGL/OpenGLTexture.cpp | 3 ++ .../src/StarStudio/Renderer/Renderer2D.cpp | 51 ++++++++++++++----- .../src/StarStudio/Renderer/Renderer2D.h | 4 ++ StarStudio/src/StarStudio/Renderer/Shader.h | 7 +++ 10 files changed, 86 insertions(+), 17 deletions(-) diff --git a/Sandbox/assets/shaders/Texture.glsl b/Sandbox/assets/shaders/Texture.glsl index 70f82152..09f9cbe1 100644 --- a/Sandbox/assets/shaders/Texture.glsl +++ b/Sandbox/assets/shaders/Texture.glsl @@ -28,5 +28,5 @@ void main() { - color = texture(u_Texture, v_TexCoord); + color = texture(u_Texture, v_TexCoord * 10.0) * vec4(1.0, 0.8, 0.8, 1.0); } \ No newline at end of file diff --git a/Sandbox/src/Sandbox2D.cpp b/Sandbox/src/Sandbox2D.cpp index 5c785d7c..65b98008 100644 --- a/Sandbox/src/Sandbox2D.cpp +++ b/Sandbox/src/Sandbox2D.cpp @@ -5,8 +5,6 @@ #include #include -#include "Platform/OpenGL/OpenGLShader.h" - Sandbox2D::Sandbox2D() :Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f, true) { @@ -15,7 +13,7 @@ Sandbox2D::Sandbox2D() void Sandbox2D::OnAttach() { - + m_CheckerboardTexture = StarStudio::Texture2D::Create("assets/textures/Checkerboard.png"); } void Sandbox2D::OnDetach() @@ -32,7 +30,9 @@ void Sandbox2D::OnUpdate(StarStudio::Timestep ts) StarStudio::RenderCommand::Clear(); StarStudio::Renderer2D::BeginScene(m_CameraController.GetCamera()); - StarStudio::Renderer2D::DrawQuad({ 0.0f, 0.0f }, { 1.0f, 1.0f }, { 0.8f, 0.2f, 0.3f, 1.0f }); + StarStudio::Renderer2D::DrawQuad({ -1.0f, 0.0f }, { 0.8f, 0.8f }, { 0.8f, 0.2f, 0.3f, 1.0f }); + StarStudio::Renderer2D::DrawQuad({ 0.5f, -0.5f }, { 0.5f, 0.75f }, { 0.2f, 0.3f, 0.8f, 1.0f }); + StarStudio::Renderer2D::DrawQuad({ 0.0f, 0.0f, -0.1f }, { 10.0f, 10.0f }, m_CheckerboardTexture); StarStudio::Renderer2D::EndScene(); } diff --git a/Sandbox/src/Sandbox2D.h b/Sandbox/src/Sandbox2D.h index ab766ad7..6aef8da9 100644 --- a/Sandbox/src/Sandbox2D.h +++ b/Sandbox/src/Sandbox2D.h @@ -21,5 +21,7 @@ class Sandbox2D : public StarStudio::Layer StarStudio::Ref m_SquareVA; StarStudio::Ref m_FlatColorShader; + StarStudio::Ref m_CheckerboardTexture; + glm::vec4 m_SquareColor = { 0.2f, 0.3f, 0.8f, 1.0f }; }; diff --git a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp index 51fd2bcc..e7ae9ad0 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp @@ -9,6 +9,7 @@ namespace StarStudio { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_DEPTH_TEST); } void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) diff --git a/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp b/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp index 1c05a029..7173a380 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp @@ -173,6 +173,26 @@ namespace StarStudio { glUseProgram(0); } + void OpenGLShader::SetInt(const std::string& name, int value) + { + UploadUniformInt(name, value); + } + + void OpenGLShader::SetFloat3(const std::string& name, const glm::vec3& value) + { + UploadUniformFloat3(name, value); + } + + void OpenGLShader::SetFloat4(const std::string& name, const glm::vec4& value) + { + UploadUniformFloat4(name, value); + } + + void OpenGLShader::SetMat4(const std::string& name, const glm::mat4& value) + { + UploadUniformMat4(name, value); + } + void OpenGLShader::UploadUniformInt(const std::string& name, int value) { GLint location = glGetUniformLocation(m_RendererID, name.c_str()); diff --git a/StarStudio/src/Platform/OpenGL/OpenGLShader.h b/StarStudio/src/Platform/OpenGL/OpenGLShader.h index fca88ae2..bc6425a3 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLShader.h +++ b/StarStudio/src/Platform/OpenGL/OpenGLShader.h @@ -21,6 +21,11 @@ namespace StarStudio { virtual void Bind() const override; virtual void Unbind() const override; + virtual void SetInt(const std::string& name, int value) override; + virtual void SetFloat3(const std::string& name, const glm::vec3& value) override; + virtual void SetFloat4(const std::string& name, const glm::vec4& value) override; + virtual void SetMat4(const std::string& name, const glm::mat4& value) override; + virtual const std::string& GetName() const override { return m_Name; } void UploadUniformInt(const std::string& name, int value); diff --git a/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp b/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp index bdafec3a..026e6ae4 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp @@ -40,6 +40,9 @@ namespace StarStudio { glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, dataFormat, GL_UNSIGNED_BYTE, data); stbi_image_free(data); diff --git a/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp b/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp index a2b17e18..9cea97f0 100644 --- a/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp +++ b/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp @@ -5,7 +5,7 @@ #include "StarStudio/Renderer/Shader.h" #include "StarStudio/Renderer/RenderCommand.h" -#include "Platform/OpenGL/OpenGLShader.h" +#include namespace StarStudio { @@ -13,6 +13,7 @@ namespace StarStudio { { Ref QuadVertexArray; Ref FlatColorShader; + Ref TextureShader; }; static Renderer2DStorage* s_Data; @@ -22,17 +23,18 @@ namespace StarStudio { s_Data = new Renderer2DStorage(); s_Data->QuadVertexArray = VertexArray::Create(); - float squareVertices[4 * 3] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.5f, 0.5f, 0.0f, - -0.5f, 0.5f, 0.0f + float squareVertices[5 * 4] = { + -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.0f, 0.0f, 1.0f }; Ref squareVB; squareVB.reset(VertexBuffer::Create(squareVertices, sizeof(squareVertices))); squareVB->SetLayout({ - { ShaderDataType::Float3, "a_Position" } + { ShaderDataType::Float3, "a_Position" }, + { ShaderDataType::Float2, "a_TexCoord" } }); s_Data->QuadVertexArray->AddVertexBuffer(squareVB); @@ -42,6 +44,9 @@ namespace StarStudio { s_Data->QuadVertexArray->SetIndexBuffer(squareIB); s_Data->FlatColorShader = Shader::Create("assets/shaders/FlatColor.glsl"); + s_Data->TextureShader = Shader::Create("assets/shaders/Texture.glsl"); + s_Data->TextureShader->Bind(); + s_Data->TextureShader->SetInt("u_Texture", 0); } void Renderer2D::Shutdown() @@ -51,9 +56,10 @@ namespace StarStudio { void Renderer2D::BeginScene(const OrthographicCamera& camera) { - std::dynamic_pointer_cast(s_Data->FlatColorShader)->Bind(); - std::dynamic_pointer_cast(s_Data->FlatColorShader)->UploadUniformMat4("u_ViewProjection", camera.GetViewProjectionMatrix()); - std::dynamic_pointer_cast(s_Data->FlatColorShader)->UploadUniformMat4("u_Transform", glm::mat4(1.0f)); + s_Data->FlatColorShader->Bind(); + s_Data->FlatColorShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix()); + s_Data->TextureShader->Bind(); + s_Data->TextureShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix()); } void Renderer2D::EndScene() @@ -68,8 +74,29 @@ namespace StarStudio { void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color) { - std::dynamic_pointer_cast(s_Data->FlatColorShader)->Bind(); - std::dynamic_pointer_cast(s_Data->FlatColorShader)->UploadUniformFloat4("u_Color", color); + s_Data->FlatColorShader->Bind(); + s_Data->FlatColorShader->SetFloat4("u_Color", color); + + glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); + s_Data->FlatColorShader->SetMat4("u_Transform", transform); + + s_Data->QuadVertexArray->Bind(); + RenderCommand::DrawIndexed(s_Data->QuadVertexArray); + } + + void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref& texture) + { + DrawQuad({ position.x, position.y, 0.0f }, size, texture); + } + + void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& texture) + { + s_Data->TextureShader->Bind(); + + glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); + s_Data->TextureShader->SetMat4("u_Transform", transform); + + texture->Bind(); s_Data->QuadVertexArray->Bind(); diff --git a/StarStudio/src/StarStudio/Renderer/Renderer2D.h b/StarStudio/src/StarStudio/Renderer/Renderer2D.h index 9a0a2307..a65d8272 100644 --- a/StarStudio/src/StarStudio/Renderer/Renderer2D.h +++ b/StarStudio/src/StarStudio/Renderer/Renderer2D.h @@ -2,6 +2,8 @@ #include "OrthographicCamera.h" +#include "Texture.h" + namespace StarStudio { class Renderer2D { @@ -16,5 +18,7 @@ namespace StarStudio { // Primitives static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color); static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color); + static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref& texture); + static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& texture); }; } diff --git a/StarStudio/src/StarStudio/Renderer/Shader.h b/StarStudio/src/StarStudio/Renderer/Shader.h index 46738ae2..0bf28fe7 100644 --- a/StarStudio/src/StarStudio/Renderer/Shader.h +++ b/StarStudio/src/StarStudio/Renderer/Shader.h @@ -3,6 +3,8 @@ #include #include +#include + namespace StarStudio { class Shader @@ -13,6 +15,11 @@ namespace StarStudio { virtual void Bind() const = 0; virtual void Unbind() const = 0; + virtual void SetInt(const std::string& name, int value) = 0; + virtual void SetFloat3(const std::string& name, const glm::vec3& value) = 0; + virtual void SetFloat4(const std::string& name, const glm::vec4& value) = 0; + virtual void SetMat4(const std::string& name, const glm::mat4& value) = 0; + virtual const std::string& GetName() const = 0; static Ref Create(const std::string& filepath); static Ref Create(const std::string& name, const std::string& vertexSrc, const std::string& fragmentSrc); From a107b3fc3edfbcbf94156d2cc85d89430b071013 Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Mon, 3 Feb 2025 19:26:51 -0500 Subject: [PATCH 19/20] FPS Counter i have no idea if this is the correct way of doing this or if its any good --- Sandbox/src/Sandbox2D.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sandbox/src/Sandbox2D.cpp b/Sandbox/src/Sandbox2D.cpp index 65b98008..f81dacca 100644 --- a/Sandbox/src/Sandbox2D.cpp +++ b/Sandbox/src/Sandbox2D.cpp @@ -52,14 +52,16 @@ void Sandbox2D::OnImGuiRender() m_CameraController.GetCamera().SetPosition(glm::vec3(0.0f)); m_CameraController.GetCamera().SetRotation(0.0f); } - - ImGui::End(); //Color Picker - ImGui::Begin("Color Picker"); + ImGui::Text("Color Picker"); ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor)); + ImGui::Text("Frame Counter"); + + ImGui::Text("FPS: %.u", ImGui::GetIO().Framerate); + ImGui::End(); } From 0db58964950e817dd9648c28b5e6d8e1e074680e Mon Sep 17 00:00:00 2001 From: sheazy_wi Date: Mon, 3 Feb 2025 21:50:58 -0500 Subject: [PATCH 20/20] Renderer2D now uses a single shader to render both textured and non-textured quads finally the bg is white --- Sandbox/assets/shaders/Texture.glsl | 3 ++- .../src/Platform/OpenGL/OpenGLRendererAPI.cpp | 1 + .../src/Platform/OpenGL/OpenGLTexture.cpp | 25 +++++++++++++++++-- .../src/Platform/OpenGL/OpenGLTexture.h | 7 ++++++ .../src/StarStudio/Renderer/Renderer2D.cpp | 20 +++++++-------- .../src/StarStudio/Renderer/Texture.cpp | 15 +++++++++-- StarStudio/src/StarStudio/Renderer/Texture.h | 4 +++ 7 files changed, 60 insertions(+), 15 deletions(-) diff --git a/Sandbox/assets/shaders/Texture.glsl b/Sandbox/assets/shaders/Texture.glsl index 09f9cbe1..7ae91bce 100644 --- a/Sandbox/assets/shaders/Texture.glsl +++ b/Sandbox/assets/shaders/Texture.glsl @@ -24,9 +24,10 @@ in vec2 v_TexCoord; + uniform vec4 u_Color; uniform sampler2D u_Texture; void main() { - color = texture(u_Texture, v_TexCoord * 10.0) * vec4(1.0, 0.8, 0.8, 1.0); + color = texture(u_Texture, v_TexCoord * 10.0) * u_Color; } \ No newline at end of file diff --git a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp index e7ae9ad0..0126dcc7 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp @@ -28,5 +28,6 @@ namespace StarStudio void OpenGLRendererAPI::DrawIndexed(const Ref& vertexArray) { glDrawElements(GL_TRIANGLES, vertexArray->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr); + glBindTexture(GL_TEXTURE_2D, 0); } } \ No newline at end of file diff --git a/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp b/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp index 026e6ae4..725f34fc 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp @@ -3,10 +3,21 @@ #include "stb_image.h" -#include - namespace StarStudio { + OpenGLTexture2D::OpenGLTexture2D(uint32_t width, uint32_t height) + : m_Width(width), m_Height(height) + { + m_InternalFormat = GL_RGBA8; + m_DataFormat = GL_RGBA; + glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID); + glTextureStorage2D(m_RendererID, 1, m_InternalFormat, m_Width, m_Height); + glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_T, GL_REPEAT); + } + OpenGLTexture2D::OpenGLTexture2D(const std::string& path) : m_Path(path) { @@ -31,6 +42,9 @@ namespace StarStudio { dataFormat = GL_RGB; } + m_InternalFormat = internalFormat; + m_DataFormat = dataFormat; + SS_CORE_ASSERT(internalFormat & dataFormat, "Format not supported!"); glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID); @@ -48,6 +62,13 @@ namespace StarStudio { stbi_image_free(data); } + void OpenGLTexture2D::SetData(void* data, uint32_t size) + { + uint32_t bpp = m_DataFormat == GL_RGBA ? 4 : 3; + SS_CORE_ASSERT(size == m_Width * m_Height * bpp, "Data must be entire texture!"); + glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, m_DataFormat, GL_UNSIGNED_BYTE, data); + } + OpenGLTexture2D::~OpenGLTexture2D() { glDeleteTextures(1, &m_RendererID); diff --git a/StarStudio/src/Platform/OpenGL/OpenGLTexture.h b/StarStudio/src/Platform/OpenGL/OpenGLTexture.h index 934a8727..52f987cc 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLTexture.h +++ b/StarStudio/src/Platform/OpenGL/OpenGLTexture.h @@ -2,12 +2,15 @@ #include "StarStudio/Renderer/Texture.h" +#include + namespace StarStudio { class OpenGLTexture2D : public Texture2D { public: + OpenGLTexture2D(uint32_t width, uint32_t height); OpenGLTexture2D(const std::string& path); virtual ~OpenGLTexture2D(); @@ -15,11 +18,15 @@ namespace StarStudio { virtual uint32_t GetWidth() const override { return m_Width; } virtual uint32_t GetHeight() const override { return m_Height; } + virtual void SetData(void* data, uint32_t size) override; + virtual void Bind(uint32_t slot = 0) const override; private: std::string m_Path; uint32_t m_Width, m_Height; uint32_t m_RendererID; + + GLenum m_InternalFormat, m_DataFormat; }; } \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp b/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp index 9cea97f0..42439918 100644 --- a/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp +++ b/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp @@ -12,8 +12,8 @@ namespace StarStudio { struct Renderer2DStorage { Ref QuadVertexArray; - Ref FlatColorShader; Ref TextureShader; + Ref WhiteTexture; }; static Renderer2DStorage* s_Data; @@ -43,7 +43,10 @@ namespace StarStudio { squareIB.reset(IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))); s_Data->QuadVertexArray->SetIndexBuffer(squareIB); - s_Data->FlatColorShader = Shader::Create("assets/shaders/FlatColor.glsl"); + s_Data->WhiteTexture = Texture2D::Create(1, 1); + uint32_t whiteTextureData = 0xffffffff; + s_Data->WhiteTexture->SetData(&whiteTextureData, sizeof(uint32_t)); + s_Data->TextureShader = Shader::Create("assets/shaders/Texture.glsl"); s_Data->TextureShader->Bind(); s_Data->TextureShader->SetInt("u_Texture", 0); @@ -56,8 +59,6 @@ namespace StarStudio { void Renderer2D::BeginScene(const OrthographicCamera& camera) { - s_Data->FlatColorShader->Bind(); - s_Data->FlatColorShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix()); s_Data->TextureShader->Bind(); s_Data->TextureShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix()); } @@ -74,11 +75,11 @@ namespace StarStudio { void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color) { - s_Data->FlatColorShader->Bind(); - s_Data->FlatColorShader->SetFloat4("u_Color", color); + s_Data->TextureShader->SetFloat4("u_Color", color); + s_Data->WhiteTexture->Bind(); glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); - s_Data->FlatColorShader->SetMat4("u_Transform", transform); + s_Data->TextureShader->SetMat4("u_Transform", transform); s_Data->QuadVertexArray->Bind(); RenderCommand::DrawIndexed(s_Data->QuadVertexArray); @@ -91,13 +92,12 @@ namespace StarStudio { void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& texture) { - s_Data->TextureShader->Bind(); + s_Data->TextureShader->SetFloat4("u_Color", glm::vec4(1.0f)); + texture->Bind(); glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); s_Data->TextureShader->SetMat4("u_Transform", transform); - texture->Bind(); - s_Data->QuadVertexArray->Bind(); RenderCommand::DrawIndexed(s_Data->QuadVertexArray); diff --git a/StarStudio/src/StarStudio/Renderer/Texture.cpp b/StarStudio/src/StarStudio/Renderer/Texture.cpp index 0c0c111c..e99f9bdb 100644 --- a/StarStudio/src/StarStudio/Renderer/Texture.cpp +++ b/StarStudio/src/StarStudio/Renderer/Texture.cpp @@ -6,12 +6,23 @@ namespace StarStudio { + Ref Texture2D::Create(uint32_t width, uint32_t height) + { + switch (Renderer::GetAPI()) + { + case RendererAPI::API::None: SS_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr; + case RendererAPI::API::OpenGL: return CreateRef(width, height); + } + SS_CORE_ASSERT(false, "Unknown RendererAPI!"); + return nullptr; + } + Ref Texture2D::Create(const std::string& path) { switch (Renderer::GetAPI()) { - case RendererAPI::API::None: SS_CORE_ASSERT(false, "RendererAPI::None is currently not supported") return nullptr; - case RendererAPI::API::OpenGL: return std::make_shared(path); + case RendererAPI::API::None: SS_CORE_ASSERT(false, "RendererAPI::None is currently not supported") return nullptr; + case RendererAPI::API::OpenGL: return CreateRef(path); } SS_CORE_ASSERT(false, "Unknown RendererAPI!"); diff --git a/StarStudio/src/StarStudio/Renderer/Texture.h b/StarStudio/src/StarStudio/Renderer/Texture.h index ae92c3c3..0719f95f 100644 --- a/StarStudio/src/StarStudio/Renderer/Texture.h +++ b/StarStudio/src/StarStudio/Renderer/Texture.h @@ -10,8 +10,12 @@ namespace StarStudio{ { public: virtual ~Texture() = default; + virtual uint32_t GetWidth() const = 0; virtual uint32_t GetHeight() const = 0; + + virtual void SetData(void* data, uint32_t size) = 0; + virtual void Bind(uint32_t slot = 0) const = 0; };