forked from TheCherno/Hazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Sandbox ExampleLayer to separate file
Cleaning up SandboxApp TheCherno#170
- Loading branch information
1 parent
95fda7f
commit a957b5b
Showing
3 changed files
with
219 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
#include "ExampleLayer.h" | ||
|
||
#include "imgui/imgui.h" | ||
|
||
#include <glm/gtc/matrix_transform.hpp> | ||
#include <glm/gtc/type_ptr.hpp> | ||
|
||
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<Hazel::VertexBuffer> 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<Hazel::IndexBuffer> 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<Hazel::VertexBuffer> 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<Hazel::IndexBuffer> 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Hazel::Shader> m_Shader; | ||
Hazel::Ref<Hazel::VertexArray> m_VertexArray; | ||
|
||
Hazel::Ref<Hazel::Shader> m_FlatColorShader; | ||
Hazel::Ref<Hazel::VertexArray> m_SquareVA; | ||
|
||
Hazel::Ref<Hazel::Texture2D> m_Texture, m_ChernoLogoTexture; | ||
|
||
Hazel::OrthographicCameraController m_CameraController; | ||
glm::vec3 m_SquareColor = { 0.2f, 0.3f, 0.8f }; | ||
}; | ||
|
Oops, something went wrong.