Muto 0.0.1
Cross-Platform Application Template
GitHub Repo | Muto Updates | Muto's Author
Loading...
Searching...
No Matches
ExampleLayer Class Reference
Inheritance diagram for ExampleLayer:
Muto::Layer Muto::Layer

Public Member Functions

 ExampleLayer ()
 
 ExampleLayer ()
 
void OnEvent (Muto::Event &e) override
 Called when an event is dispatched to the layer.
 
void OnEvent (Muto::Event &e) override
 Called when an event is dispatched to the layer.
 
void OnImGuiRender () override
 Called when the layer should render its ImGui components.
 
void OnImGuiRender () override
 Called when the layer should render its ImGui components.
 
void OnUpdate (Muto::Timestep ts) override
 Called every frame to update the layer with the given timestep.
 
void OnUpdate (Muto::Timestep ts) override
 Called every frame to update the layer with the given timestep.
 
- Public Member Functions inherited from Muto::Layer
const std::string & GetName () const
 Retrieves the name of the layer for debugging purposes.
 
 Layer (const std::string &name="Layer")
 Constructs a Layer with an optional name for debugging purposes.
 
virtual void OnAttach ()
 Called when the layer is attached to the application.
 
virtual void OnDetach ()
 Called when the layer is detached from the application.
 
virtual void OnRender ()
 Called when the layer should render its contents.
 
virtual ~Layer ()
 

Private Attributes

Muto::OrthographicCameraController m_CameraController
 
Muto::Ref< Muto::Shader > m_FlatColorShader
 
Muto::Ref< Muto::Shader > m_Shader
 
glm::vec3 m_SquareColor = { 0.2f, 0.3f, 0.8f }
 
Muto::Ref< Muto::VertexArray > m_SquareVA
 
Muto::Ref< Muto::VertexArray > m_TriangleVertexArray
 

Additional Inherited Members

- Protected Attributes inherited from Muto::Layer
std::string m_DebugName
 The name of the layer assigned at creation, used for debugging.
 

Constructor & Destructor Documentation

◆ ExampleLayer() [1/2]

ExampleLayer::ExampleLayer ( )
inline

Traingle Setup

Todo:
Abstract triangle rendering into Renderer2D (Similar to QuadVertex struct) (will use this shape for boids)
19 : Layer("Example"), m_CameraController(1280.0f / 720.0f)
20 {
24 {
26
27 float triangleVertices[3 * 7] = {
28 -0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f, 0.5f, -0.5f, 0.0f, 0.2f,
29 0.3f, 0.8f, 1.0f, 0.0f, 0.5f, 0.0f, 0.8f, 0.8f, 0.2f, 1.0f,
30 };
31
33 vertexBuffer = (Muto::VertexBuffer::Create(triangleVertices,
34 sizeof(triangleVertices)));
37
38 };
39 vertexBuffer->SetLayout(layout);
40 m_TriangleVertexArray->AddVertexBuffer(vertexBuffer);
41 uint32_t indices[3] = { 0, 1, 2 };
43 indexBuffer = (Muto::IndexBuffer::Create(indices, sizeof(indices) /
44 sizeof(uint32_t)));
45 m_TriangleVertexArray->SetIndexBuffer(indexBuffer);
46
47 std::string triangleVertexSrc = R"(
48 #version 330 core
49
50 layout(location = 0) in vec3 a_Position;
51 layout(location = 1) in vec4 a_Color;
52
53 uniform mat4 u_ViewProjection;
54 uniform mat4 u_Transform;
55
56 out vec3 v_Position;
57 out vec4 v_Color;
58
59 void main()
60 {
61 v_Position = a_Position;
62 v_Color = a_Color;
63 gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0);
64 }
65 )";
66
67 std::string triangleFragmentSrc = R"(
68 #version 330 core
69
70 layout(location = 0) out vec4 color;
71
72 in vec3 v_Position;
73 in vec4 v_Color;
74
75 void main()
76 {
77 color = vec4(v_Position * 0.5 + 0.5, 1.0);
78 color = v_Color;
79 }
80 )";
81
82 m_Shader = Muto::Shader::Create("VertexPosColor", triangleVertexSrc,
83 triangleFragmentSrc);
84 }
85
87 float squareVertices[5 * 4] = { -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f,
88 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.0f, 1.0f,
89 1.0f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f };
90
92 squareVB =
93 (Muto::VertexBuffer::Create(squareVertices, sizeof(squareVertices)));
94 squareVB->SetLayout({ {Muto::ShaderDataType::SDT_Float3, "a_Position"},
95 {Muto::ShaderDataType::SDT_Float2, "a_TexCoord"} });
96 m_SquareVA->AddVertexBuffer(squareVB);
97
98 uint32_t squareIndices[6] = { 0, 1, 2, 2, 3, 0 };
100 squareIB = (Muto::IndexBuffer::Create(squareIndices, sizeof(squareIndices) /
101 sizeof(uint32_t)));
102 m_SquareVA->SetIndexBuffer(squareIB);
103
104 std::string blueShaderVertexSrc = R"(
105 #version 330 core
106
107 layout(location = 0) in vec3 a_Position;
108
109 uniform mat4 u_ViewProjection;
110 uniform mat4 u_Transform;
111
112 out vec3 v_Position;
113
114 void main()
115 {
116 v_Position = a_Position;
117 gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0);
118 }
119 )";
120
121 std::string flatColorShaderFragmentSrc = R"(
122 #version 330 core
123
124 layout(location = 0) out vec4 color;
125
126 in vec3 v_Position;
127
128 uniform vec3 u_Color;
129
130 void main()
131 {
132 color = vec4(u_Color, 1.0);
133 }
134 )";
Muto::Ref< Muto::Shader > m_Shader
Definition SandboxApp.cpp:170
Muto::Ref< Muto::VertexArray > m_TriangleVertexArray
Definition SandboxApp.cpp:171
Muto::OrthographicCameraController m_CameraController
Definition SandboxApp.cpp:174
Muto::Ref< Muto::VertexArray > m_SquareVA
Definition SandboxApp.cpp:173
Represents the layout of a buffer, consisting of multiple BufferElements.
Definition Buffer.h:87
static Ref< IndexBuffer > Create(uint32_t *indices, uint32_t count)
Definition Buffer.cpp:33
Layer(const std::string &name="Layer")
Constructs a Layer with an optional name for debugging purposes.
Definition Layer.cpp:6
static Ref< Shader > Create(const std::string &name, const std::string &vertexSrc, const std::string &fragmentSrc)
Creates a new shader from vertex and fragment shader source code.
Definition Shader.cpp:20
static Ref< VertexArray > Create()
Definition VertexArray.cpp:7
static Ref< VertexBuffer > Create(uint32_t size)
Definition Buffer.cpp:11
std::shared_ptr< T > Ref
A smart pointer type representing shared ownership of an object.
Definition Defines_Macros.h:62

References Muto::SDT_Float2, Muto::SDT_Float3, and Muto::SDT_Float4.

◆ ExampleLayer() [2/2]

ExampleLayer::ExampleLayer ( )
inline

Traingle Setup

Todo:
Abstract triangle rendering into Renderer2D (Similar to QuadVertex struct) (will use this shape for boids)
19 : Layer("Example"), m_CameraController(1280.0f / 720.0f)
20 {
24 {
26
27 float triangleVertices[3 * 7] = {
28 -0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f, 0.5f, -0.5f, 0.0f, 0.2f,
29 0.3f, 0.8f, 1.0f, 0.0f, 0.5f, 0.0f, 0.8f, 0.8f, 0.2f, 1.0f,
30 };
31
33 vertexBuffer = (Muto::VertexBuffer::Create(triangleVertices,
34 sizeof(triangleVertices)));
37
38 };
39 vertexBuffer->SetLayout(layout);
40 m_TriangleVertexArray->AddVertexBuffer(vertexBuffer);
41 uint32_t indices[3] = { 0, 1, 2 };
43 indexBuffer = (Muto::IndexBuffer::Create(indices, sizeof(indices) /
44 sizeof(uint32_t)));
45 m_TriangleVertexArray->SetIndexBuffer(indexBuffer);
46
47 std::string triangleVertexSrc = R"(
48 #version 330 core
49
50 layout(location = 0) in vec3 a_Position;
51 layout(location = 1) in vec4 a_Color;
52
53 uniform mat4 u_ViewProjection;
54 uniform mat4 u_Transform;
55
56 out vec3 v_Position;
57 out vec4 v_Color;
58
59 void main()
60 {
61 v_Position = a_Position;
62 v_Color = a_Color;
63 gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0);
64 }
65 )";
66
67 std::string triangleFragmentSrc = R"(
68 #version 330 core
69
70 layout(location = 0) out vec4 color;
71
72 in vec3 v_Position;
73 in vec4 v_Color;
74
75 void main()
76 {
77 color = vec4(v_Position * 0.5 + 0.5, 1.0);
78 color = v_Color;
79 }
80 )";
81
82 m_Shader = Muto::Shader::Create("VertexPosColor", triangleVertexSrc,
83 triangleFragmentSrc);
84 }
85
87 float squareVertices[5 * 4] = { -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f,
88 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.0f, 1.0f,
89 1.0f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f };
90
92 squareVB =
93 (Muto::VertexBuffer::Create(squareVertices, sizeof(squareVertices)));
94 squareVB->SetLayout({ {Muto::ShaderDataType::SDT_Float3, "a_Position"},
95 {Muto::ShaderDataType::SDT_Float2, "a_TexCoord"} });
96 m_SquareVA->AddVertexBuffer(squareVB);
97
98 uint32_t squareIndices[6] = { 0, 1, 2, 2, 3, 0 };
100 squareIB = (Muto::IndexBuffer::Create(squareIndices, sizeof(squareIndices) /
101 sizeof(uint32_t)));
102 m_SquareVA->SetIndexBuffer(squareIB);
103
104 std::string blueShaderVertexSrc = R"(
105 #version 330 core
106
107 layout(location = 0) in vec3 a_Position;
108
109 uniform mat4 u_ViewProjection;
110 uniform mat4 u_Transform;
111
112 out vec3 v_Position;
113
114 void main()
115 {
116 v_Position = a_Position;
117 gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0);
118 }
119 )";
120
121 std::string flatColorShaderFragmentSrc = R"(
122 #version 330 core
123
124 layout(location = 0) out vec4 color;
125
126 in vec3 v_Position;
127
128 uniform vec3 u_Color;
129
130 void main()
131 {
132 color = vec4(u_Color, 1.0);
133 }
134 )";

Member Function Documentation

◆ OnEvent() [1/2]

void ExampleLayer::OnEvent ( Muto::Event &  event)
inlineoverridevirtual

Called when an event is dispatched to the layer.

Parameters
eventThe event being dispatched.

Reimplemented from Muto::Layer.

◆ OnEvent() [2/2]

void ExampleLayer::OnEvent ( Muto::Event &  event)
inlineoverridevirtual

Called when an event is dispatched to the layer.

Parameters
eventThe event being dispatched.

Reimplemented from Muto::Layer.

◆ OnImGuiRender() [1/2]

void ExampleLayer::OnImGuiRender ( )
inlineoverridevirtual

Called when the layer should render its ImGui components.

Reimplemented from Muto::Layer.

◆ OnImGuiRender() [2/2]

void ExampleLayer::OnImGuiRender ( )
inlineoverridevirtual

Called when the layer should render its ImGui components.

Reimplemented from Muto::Layer.

◆ OnUpdate() [1/2]

void ExampleLayer::OnUpdate ( Muto::Timestep  ts)
inlineoverridevirtual

Called every frame to update the layer with the given timestep.

Parameters
tsThe timestep representing the time elapsed since the last update.

Reimplemented from Muto::Layer.

141 {
142 // Update
144
145 // Render
146 Muto::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1.0f });
148
150 // Squares
151 static glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));
152 std::dynamic_pointer_cast<Muto::OpenGLShader>(m_FlatColorShader)->Bind();
153 std::dynamic_pointer_cast<Muto::OpenGLShader>(m_FlatColorShader)
154 ->UploadUniformFloat3("u_Color", m_SquareColor);
155 for (int y = 0; y < 20; y++)
156 {
157 for (int x = 0; x < 20; x++)
158 {
159 glm::vec3 pos(x * 0.11f, y * 0.11f, 0.0f);
160 glm::mat4 transform = glm::translate(glm::mat4(1.0f), pos) * scale;
162 }
163 }
glm::vec3 m_SquareColor
Definition SandboxApp.cpp:175
Muto::Ref< Muto::Shader > m_FlatColorShader
Definition SandboxApp.cpp:172
void OnUpdate(Timestep ts)
Definition OrthographicCameraController.cpp:21
OrthographicCamera & GetCamera()
Definition OrthographicCameraController.h:38
static void SetClearColor(const glm::vec4 &color)
Sets the clear color for the renderer.
Definition RenderCommand.h:29
static void Clear()
Clears the rendering buffers.
Definition RenderCommand.h:35
static void BeginScene(OrthographicCamera &camera)
Begins a new scene with the given orthographic camera.
Definition Renderer.cpp:30
static void Submit(const Ref< Shader > &shader, const Ref< VertexArray > &vertexArray, const glm::mat4 &transform=glm::mat4(1.0f))
Submits a draw call with the specified shader, vertex array, and transform.
Definition Renderer.cpp:42

◆ OnUpdate() [2/2]

void ExampleLayer::OnUpdate ( Muto::Timestep  ts)
inlineoverridevirtual

Called every frame to update the layer with the given timestep.

Parameters
tsThe timestep representing the time elapsed since the last update.

Reimplemented from Muto::Layer.

141 {
142 // Update
144
145 // Render
146 Muto::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1.0f });
148
150 // Squares
151 static glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));
152 std::dynamic_pointer_cast<Muto::OpenGLShader>(m_FlatColorShader)->Bind();
153 std::dynamic_pointer_cast<Muto::OpenGLShader>(m_FlatColorShader)
154 ->UploadUniformFloat3("u_Color", m_SquareColor);
155 for (int y = 0; y < 20; y++)
156 {
157 for (int x = 0; x < 20; x++)
158 {
159 glm::vec3 pos(x * 0.11f, y * 0.11f, 0.0f);
160 glm::mat4 transform = glm::translate(glm::mat4(1.0f), pos) * scale;
162 }
163 }

Member Data Documentation

◆ m_CameraController

Muto::OrthographicCameraController ExampleLayer::m_CameraController
private

◆ m_FlatColorShader

Muto::Ref< Muto::Shader > ExampleLayer::m_FlatColorShader
private

◆ m_Shader

Muto::Ref< Muto::Shader > ExampleLayer::m_Shader
private

◆ m_SquareColor

glm::vec3 ExampleLayer::m_SquareColor = { 0.2f, 0.3f, 0.8f }
private

◆ m_SquareVA

Muto::Ref< Muto::VertexArray > ExampleLayer::m_SquareVA
private

◆ m_TriangleVertexArray

Muto::Ref< Muto::VertexArray > ExampleLayer::m_TriangleVertexArray
private

The documentation for this class was generated from the following file: