From a957b5bd28d96bb34defeb6d85bb5bd3e21efdd4 Mon Sep 17 00:00:00 2001 From: cxgslegend Date: Sat, 23 Nov 2019 17:16:03 -0500 Subject: [PATCH] Move Sandbox ExampleLayer to separate file Cleaning up SandboxApp #170 --- Sandbox/src/ExampleLayer.cpp | 188 +++++++++++++++++++++++++++++++++ Sandbox/src/ExampleLayer.h | 30 ++++++ Sandbox/src/SandboxApp.cpp | 196 +---------------------------------- 3 files changed, 219 insertions(+), 195 deletions(-) create mode 100644 Sandbox/src/ExampleLayer.cpp create mode 100644 Sandbox/src/ExampleLayer.h diff --git a/Sandbox/src/ExampleLayer.cpp b/Sandbox/src/ExampleLayer.cpp new file mode 100644 index 000000000..fcbfbdc61 --- /dev/null +++ b/Sandbox/src/ExampleLayer.cpp @@ -0,0 +1,188 @@ +#include "ExampleLayer.h" + +#include "imgui/imgui.h" + +#include +#include + +ExampleLayer::ExampleLayer() + : Layer("ExampleLayer"), m_CameraController(1280.0f / 720.0f) +{ + m_VertexArray = Hazel::VertexArray::Create(); + + float vertices[3 * 7] = { + -0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f, + 0.5f, -0.5f, 0.0f, 0.2f, 0.3f, 0.8f, 1.0f, + 0.0f, 0.5f, 0.0f, 0.8f, 0.8f, 0.2f, 1.0f + }; + + Hazel::Ref vertexBuffer = Hazel::VertexBuffer::Create(vertices, sizeof(vertices)); + Hazel::BufferLayout layout = { + { Hazel::ShaderDataType::Float3, "a_Position" }, + { Hazel::ShaderDataType::Float4, "a_Color" } + }; + vertexBuffer->SetLayout(layout); + m_VertexArray->AddVertexBuffer(vertexBuffer); + + uint32_t indices[3] = { 0, 1, 2 }; + Hazel::Ref indexBuffer = Hazel::IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t)); + m_VertexArray->SetIndexBuffer(indexBuffer); + + m_SquareVA = Hazel::VertexArray::Create(); + + 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 + }; + + Hazel::Ref squareVB = Hazel::VertexBuffer::Create(squareVertices, sizeof(squareVertices)); + squareVB->SetLayout({ + { Hazel::ShaderDataType::Float3, "a_Position" }, + { Hazel::ShaderDataType::Float2, "a_TexCoord" } + }); + m_SquareVA->AddVertexBuffer(squareVB); + + uint32_t squareIndices[6] = { 0, 1, 2, 2, 3, 0 }; + Hazel::Ref squareIB = Hazel::IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t)); + m_SquareVA->SetIndexBuffer(squareIB); + + std::string vertexSrc = R"( + #version 330 core + + layout(location = 0) in vec3 a_Position; + layout(location = 1) in vec4 a_Color; + + uniform mat4 u_ViewProjection; + uniform mat4 u_Transform; + + out vec3 v_Position; + out vec4 v_Color; + + void main() + { + v_Position = a_Position; + v_Color = a_Color; + gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0); + } + )"; + + std::string fragmentSrc = R"( + #version 330 core + + layout(location = 0) out vec4 color; + + in vec3 v_Position; + in vec4 v_Color; + + void main() + { + color = vec4(v_Position * 0.5 + 0.5, 1.0); + color = v_Color; + } + )"; + + m_Shader = Hazel::Shader::Create("VertexPosColor", vertexSrc, fragmentSrc); + + std::string flatColorShaderVertexSrc = R"( + #version 330 core + + layout(location = 0) in vec3 a_Position; + + uniform mat4 u_ViewProjection; + uniform mat4 u_Transform; + + out vec3 v_Position; + + void main() + { + v_Position = a_Position; + gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0); + } + )"; + + std::string flatColorShaderFragmentSrc = R"( + #version 330 core + + layout(location = 0) out vec4 color; + + in vec3 v_Position; + + uniform vec3 u_Color; + + void main() + { + color = vec4(u_Color, 1.0); + } + )"; + + m_FlatColorShader = Hazel::Shader::Create("FlatColor", flatColorShaderVertexSrc, flatColorShaderFragmentSrc); + + auto textureShader = m_ShaderLibrary.Load("assets/shaders/Texture.glsl"); + + m_Texture = Hazel::Texture2D::Create("assets/textures/Checkerboard.png"); + m_ChernoLogoTexture = Hazel::Texture2D::Create("assets/textures/ChernoLogo.png"); + + textureShader->Bind(); + textureShader->SetInt("u_Texture", 0); +} + +void ExampleLayer::OnAttach() +{ +} + +void ExampleLayer::OnDetach() +{ +} + +void ExampleLayer::OnUpdate(Hazel::Timestep ts) +{ + // Update + m_CameraController.OnUpdate(ts); + + // Render + Hazel::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 }); + Hazel::RenderCommand::Clear(); + + Hazel::Renderer::BeginScene(m_CameraController.GetCamera()); + + glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f)); + + m_FlatColorShader->Bind(); + m_FlatColorShader->SetFloat3("u_Color", m_SquareColor); + + for (int y = 0; y < 20; y++) + { + for (int x = 0; x < 20; x++) + { + glm::vec3 pos(x * 0.11f, y * 0.11f, 0.0f); + glm::mat4 transform = glm::translate(glm::mat4(1.0f), pos) * scale; + Hazel::Renderer::Submit(m_FlatColorShader, m_SquareVA, transform); + } + } + + auto textureShader = m_ShaderLibrary.Get("Texture"); + + m_Texture->Bind(); + Hazel::Renderer::Submit(textureShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f))); + m_ChernoLogoTexture->Bind(); + Hazel::Renderer::Submit(textureShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f))); + + // Triangle + // Hazel::Renderer::Submit(m_Shader, m_VertexArray); + + Hazel::Renderer::EndScene(); +} + +void ExampleLayer::OnImGuiRender() +{ + ImGui::Begin("Settings"); + ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor)); + ImGui::End(); +} + +void ExampleLayer::OnEvent(Hazel::Event& e) +{ + m_CameraController.OnEvent(e); +} diff --git a/Sandbox/src/ExampleLayer.h b/Sandbox/src/ExampleLayer.h new file mode 100644 index 000000000..ec98d3bef --- /dev/null +++ b/Sandbox/src/ExampleLayer.h @@ -0,0 +1,30 @@ +#pragma once + +#include "Hazel.h" + +class ExampleLayer : public Hazel::Layer +{ +public: + ExampleLayer(); + virtual ~ExampleLayer() = default; + + virtual void OnAttach() override; + virtual void OnDetach() override; + + void OnUpdate(Hazel::Timestep ts) override; + virtual void OnImGuiRender() override; + void OnEvent(Hazel::Event& e) override; +private: + Hazel::ShaderLibrary m_ShaderLibrary; + Hazel::Ref m_Shader; + Hazel::Ref m_VertexArray; + + Hazel::Ref m_FlatColorShader; + Hazel::Ref m_SquareVA; + + Hazel::Ref m_Texture, m_ChernoLogoTexture; + + Hazel::OrthographicCameraController m_CameraController; + glm::vec3 m_SquareColor = { 0.2f, 0.3f, 0.8f }; +}; + diff --git a/Sandbox/src/SandboxApp.cpp b/Sandbox/src/SandboxApp.cpp index 52cb51707..e88f309e8 100644 --- a/Sandbox/src/SandboxApp.cpp +++ b/Sandbox/src/SandboxApp.cpp @@ -1,202 +1,8 @@ #include #include -#include - -#include -#include - #include "Sandbox2D.h" - -class ExampleLayer : public Hazel::Layer -{ -public: - ExampleLayer() - : Layer("Example"), m_CameraController(1280.0f / 720.0f) - { - m_VertexArray = Hazel::VertexArray::Create(); - - float vertices[3 * 7] = { - -0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f, - 0.5f, -0.5f, 0.0f, 0.2f, 0.3f, 0.8f, 1.0f, - 0.0f, 0.5f, 0.0f, 0.8f, 0.8f, 0.2f, 1.0f - }; - - Hazel::Ref vertexBuffer = Hazel::VertexBuffer::Create(vertices, sizeof(vertices)); - Hazel::BufferLayout layout = { - { Hazel::ShaderDataType::Float3, "a_Position" }, - { Hazel::ShaderDataType::Float4, "a_Color" } - }; - vertexBuffer->SetLayout(layout); - m_VertexArray->AddVertexBuffer(vertexBuffer); - - uint32_t indices[3] = { 0, 1, 2 }; - Hazel::Ref indexBuffer = Hazel::IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t)); - m_VertexArray->SetIndexBuffer(indexBuffer); - - m_SquareVA = Hazel::VertexArray::Create(); - - 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 - }; - - Hazel::Ref squareVB = Hazel::VertexBuffer::Create(squareVertices, sizeof(squareVertices)); - squareVB->SetLayout({ - { Hazel::ShaderDataType::Float3, "a_Position" }, - { Hazel::ShaderDataType::Float2, "a_TexCoord" } - }); - m_SquareVA->AddVertexBuffer(squareVB); - - uint32_t squareIndices[6] = { 0, 1, 2, 2, 3, 0 }; - Hazel::Ref squareIB = Hazel::IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t)); - m_SquareVA->SetIndexBuffer(squareIB); - - std::string vertexSrc = R"( - #version 330 core - - layout(location = 0) in vec3 a_Position; - layout(location = 1) in vec4 a_Color; - - uniform mat4 u_ViewProjection; - uniform mat4 u_Transform; - - out vec3 v_Position; - out vec4 v_Color; - - void main() - { - v_Position = a_Position; - v_Color = a_Color; - gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0); - } - )"; - - std::string fragmentSrc = R"( - #version 330 core - - layout(location = 0) out vec4 color; - - in vec3 v_Position; - in vec4 v_Color; - - void main() - { - color = vec4(v_Position * 0.5 + 0.5, 1.0); - color = v_Color; - } - )"; - - m_Shader = Hazel::Shader::Create("VertexPosColor", vertexSrc, fragmentSrc); - - std::string flatColorShaderVertexSrc = R"( - #version 330 core - - layout(location = 0) in vec3 a_Position; - - uniform mat4 u_ViewProjection; - uniform mat4 u_Transform; - - out vec3 v_Position; - - void main() - { - v_Position = a_Position; - gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0); - } - )"; - - std::string flatColorShaderFragmentSrc = R"( - #version 330 core - - layout(location = 0) out vec4 color; - - in vec3 v_Position; - - uniform vec3 u_Color; - - void main() - { - color = vec4(u_Color, 1.0); - } - )"; - - m_FlatColorShader = Hazel::Shader::Create("FlatColor", flatColorShaderVertexSrc, flatColorShaderFragmentSrc); - - auto textureShader = m_ShaderLibrary.Load("assets/shaders/Texture.glsl"); - - m_Texture = Hazel::Texture2D::Create("assets/textures/Checkerboard.png"); - m_ChernoLogoTexture = Hazel::Texture2D::Create("assets/textures/ChernoLogo.png"); - - textureShader->Bind(); - textureShader->SetInt("u_Texture", 0); - } - - void OnUpdate(Hazel::Timestep ts) override - { - // Update - m_CameraController.OnUpdate(ts); - - // Render - Hazel::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 }); - Hazel::RenderCommand::Clear(); - - Hazel::Renderer::BeginScene(m_CameraController.GetCamera()); - - glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f)); - - m_FlatColorShader->Bind(); - m_FlatColorShader->SetFloat3("u_Color", m_SquareColor); - - for (int y = 0; y < 20; y++) - { - for (int x = 0; x < 20; x++) - { - glm::vec3 pos(x * 0.11f, y * 0.11f, 0.0f); - glm::mat4 transform = glm::translate(glm::mat4(1.0f), pos) * scale; - Hazel::Renderer::Submit(m_FlatColorShader, m_SquareVA, transform); - } - } - - auto textureShader = m_ShaderLibrary.Get("Texture"); - - m_Texture->Bind(); - Hazel::Renderer::Submit(textureShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f))); - m_ChernoLogoTexture->Bind(); - Hazel::Renderer::Submit(textureShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f))); - - // Triangle - // Hazel::Renderer::Submit(m_Shader, m_VertexArray); - - Hazel::Renderer::EndScene(); - } - - virtual void OnImGuiRender() override - { - ImGui::Begin("Settings"); - ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor)); - ImGui::End(); - } - - void OnEvent(Hazel::Event& e) override - { - m_CameraController.OnEvent(e); - } -private: - Hazel::ShaderLibrary m_ShaderLibrary; - Hazel::Ref m_Shader; - Hazel::Ref m_VertexArray; - - Hazel::Ref m_FlatColorShader; - Hazel::Ref m_SquareVA; - - Hazel::Ref m_Texture, m_ChernoLogoTexture; - - Hazel::OrthographicCameraController m_CameraController; - glm::vec3 m_SquareColor = { 0.2f, 0.3f, 0.8f }; -}; +#include "ExampleLayer.h" class Sandbox : public Hazel::Application {