Vivid
Loading...
Searching...
No Matches
Renderer2D.cpp
1#include "Renderer2D.h"
2#include "editor/Application.h"
3#include "common/maths/Vec.h"
4
5namespace Vivid
6{
7 Storage Renderer2D::s_Storage;
8
9 void Renderer2D::Init(int reserveVertices)
10 {
11 // Initialize shaders
12 s_Storage.quadShader = MakeRef<Shader>("./../assets/shaders/quad.vertexShader.glsl",
13 "./../assets/shaders/quad.pixelShader.glsl");
14 s_Storage.lineShader = MakeRef<Shader>("./../assets/shaders/quad.vertexShader.glsl",
15 "./../assets/shaders/line.pixelShader.glsl");
16 s_Storage.ellipseShader = MakeRef<Shader>("./../assets/shaders/quad.vertexShader.glsl",
17 "./../assets/shaders/ellipse.pixelShader.glsl");
18 // Create vertices of a square
19 Vertex squareVert1 = { { 0.0f, 100.0f, -20.0f },
20 { 1.0f, 1.0f },
21 { 0.0f, 1.0f, 0.0f },
22 { 1.0f, 1.0f, 1.0f } };
23 Vertex squareVert2 = { { 100.0f, 100.0f, -20.0f },
24 { 1.0f, 1.0f },
25 { 0.0f, 1.0f, 0.0f },
26 { 1.0f, 1.0f, 1.0f } };
27 Vertex squareVert3 = { { 100.0f, 0.0f, -20.0f },
28 { 1.0f, 1.0f },
29 { 0.0f, 1.0f, 0.0f },
30 { 1.0f, 1.0f, 1.0f } };
31 Vertex squareVert4 = { { 0.0f, 0.0f, -20.0f },
32 { 1.0f, 1.0f },
33 { 0.0f, 1.0f, 0.0f },
34 { 1.0f, 1.0f, 1.0f } };
35
36 Vector<Vertex> verts = { squareVert1, squareVert2, squareVert3, squareVert4 };
37
38 Vector<unsigned int> indices = {
39 0, 1, 2,
40 2, 3, 0
41 };
42
43 VertexBuffer vb(verts);
44 VertexBufferLayout layout;
45 layout.AddFloat(3); // Position
46 layout.AddFloat(2); // Texcoord
47 layout.AddFloat(3); // Color
48 layout.AddFloat(3); // Normal
49
50 IndexBuffer ib(indices);
51
52 s_Storage.vao = MakeRef<VertexArray>();
53 s_Storage.vao->AddVertexBuffer(vb, layout, verts);
54 s_Storage.vao->AddIndexBuffer(ib);
55
56 s_Storage.quadVertices.reserve(reserveVertices);
57 s_Storage.quadIndices.reserve(reserveVertices);
58 s_Storage.ellipseVertices.reserve(reserveVertices);
59 s_Storage.ellipseIndices.reserve(reserveVertices);
60 s_Storage.lineVertices.reserve(reserveVertices);
61 s_Storage.lineIndices.reserve(reserveVertices);
62
63 Camera* camera = Application::GetInstance()->GetCamera();
64 camera->SetPerspective(glm::ortho(-960.0f, 960.0f, -540.0f, 540.0f, 0.1f, 100.0f));
65 }
66
68 {
69 Camera* camera = Application::GetInstance()->GetCamera();
70
71 s_Storage.quadShader->Bind();
72 s_Storage.quadShader->SetUniformMat4f("u_Model", glm::mat4(1.0f));
73 s_Storage.quadShader->SetUniformMat4f("u_View", camera->GetViewMatrix());
74 s_Storage.quadShader->SetUniformMat4f("u_Proj", camera->GetProjectionMatrix());
75
76 s_Storage.lineShader->Bind();
77 s_Storage.lineShader->SetUniformMat4f("u_Model", glm::mat4(1.0f));
78 s_Storage.lineShader->SetUniformMat4f("u_View", camera->GetViewMatrix());
79 s_Storage.lineShader->SetUniformMat4f("u_Proj", camera->GetProjectionMatrix());
80
81 s_Storage.ellipseShader->Bind();
82 s_Storage.ellipseShader->SetUniformMat4f("u_Model", glm::mat4(1.0f));
83 s_Storage.ellipseShader->SetUniformMat4f("u_View", camera->GetViewMatrix());
84 s_Storage.ellipseShader->SetUniformMat4f("u_Proj", camera->GetProjectionMatrix());
85 }
86
87 void Renderer2D::DrawQuad(float x, float y, float width, float height, const Maths::Vec3& color)
88 {
89 drawQuad(x, y, width, height, color);
90 }
91
92 void Renderer2D::DrawQuad(const Maths::Vec2& vertex1, const Maths::Vec2& vertex2, const Maths::Vec2& vertex3,
93 const Maths::Vec2& vertex4, const Maths::Vec3& color)
94 {
95 drawQuad(vertex1, vertex2, vertex3, vertex4, color);
96 }
97
98 void Renderer2D::DrawLine(Maths::Vec2 start, Maths::Vec2 end, float thickness, Maths::Vec3 color)
99 {
100 Maths::Vec2 vertex1 = start + (end - start).Perpendicular().Normalize() * thickness;
101 Maths::Vec2 vertex2 = end + (end - start).Perpendicular().Normalize() * thickness;
102 Maths::Vec2 vertex3 = end - (end - start).Perpendicular().Normalize() * thickness;
103 Maths::Vec2 vertex4 = start - (end - start).Perpendicular().Normalize() * thickness;
104
105 drawLine(vertex1, vertex2, vertex3, vertex4, color);
106 }
107
108 void Renderer2D::DrawEllipse(Maths::Vec2 center, float radiusX, float radiusY, Maths::Vec3 color)
109 {
110 drawEllipse(center, radiusX, radiusY, color);
111 }
112
113 void Renderer2D::DrawCircle(Maths::Vec2 center, float radius, Maths::Vec3 color)
114 {
115 drawEllipse(center, radius, radius, color);
116 }
117
118 // Todo: The order doesn't matter
120 {
121 if (!s_Storage.quadVertices.empty())
122 {
123 VertexBuffer vb(s_Storage.quadVertices);
124 IndexBuffer ib(s_Storage.quadIndices);
125
126 VertexBufferLayout layout;
127 layout.AddFloat(3);
128 layout.AddFloat(2);
129 layout.AddFloat(3);
130 layout.AddFloat(3);
131
132 s_Storage.vao->AddVertexBuffer(vb, layout, s_Storage.quadVertices);
133 s_Storage.vao->AddIndexBuffer(ib);
134
135 // Draw call
136 s_Storage.quadShader->Bind();
137 Renderer::Draw(s_Storage.vao, ib.GetCount());
138
139 s_Storage.quadVertices.clear();
140 s_Storage.quadIndices.clear();
141 }
142
143 if (!s_Storage.ellipseVertices.empty())
144 {
145 VertexBuffer vb(s_Storage.ellipseVertices);
146 IndexBuffer ib(s_Storage.ellipseIndices);
147
148 VertexBufferLayout layout;
149 layout.AddFloat(3);
150 layout.AddFloat(2);
151 layout.AddFloat(3);
152 layout.AddFloat(3);
153
154 s_Storage.vao->AddVertexBuffer(vb, layout, s_Storage.ellipseVertices);
155 s_Storage.vao->AddIndexBuffer(ib);
156
157 // Draw call
158 s_Storage.ellipseShader->Bind();
159 Renderer::Draw(s_Storage.vao, ib.GetCount());
160
161 s_Storage.ellipseVertices.clear();
162 s_Storage.ellipseIndices.clear();
163 }
164
165 if (!s_Storage.lineVertices.empty())
166 {
167 VertexBuffer vb(s_Storage.lineVertices);
168 IndexBuffer ib(s_Storage.lineIndices);
169
170 VertexBufferLayout layout;
171 layout.AddFloat(3);
172 layout.AddFloat(2);
173 layout.AddFloat(3);
174 layout.AddFloat(3);
175
176 s_Storage.vao->AddVertexBuffer(vb, layout, s_Storage.lineVertices);
177 s_Storage.vao->AddIndexBuffer(ib);
178
179 // Draw Call
180 s_Storage.lineShader->Bind();
181 Renderer::Draw(s_Storage.vao, ib.GetCount());
182
183 s_Storage.lineVertices.clear();
184 s_Storage.lineIndices.clear();
185 }
186 }
187
188 // All private implementations
189 void Renderer2D::drawQuad(float x, float y, float width, float height, const Maths::Vec3& color)
190 {
191 drawQuad(Maths::Vec2(x - width / 2, y - height / 2), Maths::Vec2(x - width / 2, y + height / 2),
192 Maths::Vec2(x + width / 2, y + height / 2), Maths::Vec2(x + width / 2, y - height / 2), color);
193 }
194
195 // Winding order is clockwise
196 void Renderer2D::drawQuad(Maths::Vec2 vertex1, Maths::Vec2 vertex2, Maths::Vec2 vertex3, Maths::Vec2 vertex4, Maths::Vec3 color)
197 {
198 Vertex quadVert1 = { { vertex1.x, vertex1.y, -20.0f },
199 { 0.0f, 0.0f },
200 { color.x, color.y, color.z },
201 { 1.0f, 1.0f, 1.0f } };
202 Vertex quadVert2 = { { vertex2.x, vertex2.y, -20.0f },
203 { 1.0f, 0.0f },
204 { color.x, color.y, color.z },
205 { 1.0f, 1.0f, 1.0f } };
206 Vertex quadVert3 = { { vertex3.x, vertex3.y, -20.0f },
207 { 1.0f, 1.0f },
208 { color.x, color.y, color.z },
209 { 1.0f, 1.0f, 1.0f } };
210 Vertex quadVert4 = { { vertex4.x, vertex4.y, -20.0f },
211 { 0.0f, 1.0f },
212 { color.x, color.y, color.z },
213 { 1.0f, 1.0f, 1.0f } };
214
215 Vector<unsigned int> indices = {
216 0, 1, 2,
217 2, 3, 0
218 };
219
220 // Indices
221 s_Storage.quadIndices.emplace_back(s_Storage.quadVertices.size()); // 0
222 s_Storage.quadIndices.emplace_back(s_Storage.quadVertices.size() + 1); // 1
223 s_Storage.quadIndices.emplace_back(s_Storage.quadVertices.size() + 2); // 2
224 s_Storage.quadIndices.emplace_back(s_Storage.quadVertices.size() + 2); // 2
225 s_Storage.quadIndices.emplace_back(s_Storage.quadVertices.size() + 3); // 3
226 s_Storage.quadIndices.emplace_back(s_Storage.quadVertices.size()); // 0
227
228 // Vertices
229 s_Storage.quadVertices.emplace_back(quadVert1);
230 s_Storage.quadVertices.emplace_back(quadVert2);
231 s_Storage.quadVertices.emplace_back(quadVert3);
232 s_Storage.quadVertices.emplace_back(quadVert4);
233 }
234
235 void Renderer2D::drawEllipse(Maths::Vec2 center, float radiusX, float radiusY, Maths::Vec3 color)
236 {
237 Vertex vertex1 = { { center.x - radiusX, center.y - radiusY, -20.0f },
238 { 0.0f, 0.0f },
239 { color.x, color.y, color.z },
240 { 1.0f, 1.0f, 1.0f } };
241 Vertex vertex2 = { { center.x + radiusX, center.y - radiusY, -20.0f },
242 { 1.0f, 0.0f },
243 { color.x, color.y, color.z },
244 { 1.0f, 1.0f, 1.0f } };
245 Vertex vertex3 = { { center.x + radiusX, center.y + radiusY, -20.0f },
246 { 1.0f, 1.0f },
247 { color.x, color.y, color.z },
248 { 1.0f, 1.0f, 1.0f } };
249 Vertex vertex4 = { { center.x - radiusX, center.y + radiusY, -20.0f },
250 { 0.0f, 1.0f },
251 { color.x, color.y, color.z },
252 { 1.0f, 1.0f, 1.0f } };
253
254 Vector<unsigned int> indices = {
255 0, 1, 2,
256 2, 3, 0
257 };
258
259 // Indices
260 s_Storage.ellipseIndices.push_back(s_Storage.ellipseVertices.size()); // 0
261 s_Storage.ellipseIndices.push_back(s_Storage.ellipseVertices.size() + 1); // 1
262 s_Storage.ellipseIndices.push_back(s_Storage.ellipseVertices.size() + 2); // 2
263 s_Storage.ellipseIndices.push_back(s_Storage.ellipseVertices.size() + 2); // 2
264 s_Storage.ellipseIndices.push_back(s_Storage.ellipseVertices.size() + 3); // 3
265 s_Storage.ellipseIndices.push_back(s_Storage.ellipseVertices.size()); // 0
266
267 // Vertices
268 s_Storage.ellipseVertices.push_back(vertex1);
269 s_Storage.ellipseVertices.push_back(vertex2);
270 s_Storage.ellipseVertices.push_back(vertex3);
271 s_Storage.ellipseVertices.push_back(vertex4);
272 }
273
274 void Renderer2D::drawLine(Maths::Vec2 vertex1, Maths::Vec2 vertex2, Maths::Vec2 vertex3, Maths::Vec2 vertex4, Maths::Vec3 color)
275 {
276 Vertex lineVert1 = { { vertex1.x, vertex1.y, -20.0f },
277 { 0.0f, 0.0f },
278 { color.x, color.y, color.z },
279 { 1.0f, 1.0f, 1.0f } };
280 Vertex lineVert2 = { { vertex2.x, vertex2.y, -20.0f },
281 { 1.0f, 0.0f },
282 { color.x, color.y, color.z },
283 { 1.0f, 1.0f, 1.0f } };
284 Vertex lineVert3 = { { vertex3.x, vertex3.y, -20.0f },
285 { 1.0f, 1.0f },
286 { color.x, color.y, color.z },
287 { 1.0f, 1.0f, 1.0f } };
288 Vertex lineVert4 = { { vertex4.x, vertex4.y, -20.0f },
289 { 0.0f, 1.0f },
290 { color.x, color.y, color.z },
291 { 1.0f, 1.0f, 1.0f } };
292
293 Vector<unsigned int> indices = {
294 0, 1, 2,
295 2, 3, 0
296 };
297
298 // Indices
299 s_Storage.lineIndices.push_back(s_Storage.lineVertices.size()); // 0
300 s_Storage.lineIndices.push_back(s_Storage.lineVertices.size() + 1); // 1
301 s_Storage.lineIndices.push_back(s_Storage.lineVertices.size() + 2); // 2
302 s_Storage.lineIndices.push_back(s_Storage.lineVertices.size() + 2); // 2
303 s_Storage.lineIndices.push_back(s_Storage.lineVertices.size() + 3); // 3
304 s_Storage.lineIndices.push_back(s_Storage.lineVertices.size()); // 0
305
306 // Vertices
307 s_Storage.lineVertices.push_back(lineVert1);
308 s_Storage.lineVertices.push_back(lineVert2);
309 s_Storage.lineVertices.push_back(lineVert3);
310 s_Storage.lineVertices.push_back(lineVert4);
311 }
312
313}
A class that represents the camera.
Definition: Camera.h:25
Manages the IndexBuffer. For more information on IndexBuffers, visit https://learnopengl....
Definition: IndexBuffer.h:14
static void BeginScene()
Begins the scene.
Definition: Renderer2D.cpp:67
static void DrawEllipse(Maths::Vec2 center, float radiusX, float radiusY, Maths::Vec3 color)
Draws an ellipse.
Definition: Renderer2D.cpp:108
static void Init(int reserveVertices=10000)
Initializes the Renderer2D.
Definition: Renderer2D.cpp:9
static void EndScene()
Ends the scene.
Definition: Renderer2D.cpp:119
static void DrawLine(Maths::Vec2 start, Maths::Vec2 end, float thickness, Maths::Vec3 color)
Draws a line.
Definition: Renderer2D.cpp:98
static void DrawQuad(float x, float y, float width, float height, const Maths::Vec3 &color)
Draws a quad.
Definition: Renderer2D.cpp:87
static void DrawCircle(Maths::Vec2 center, float radius, Maths::Vec3 color)
Draws a circle.
Definition: Renderer2D.cpp:113
static void Draw(Ref< VertexArray > va, unsigned int indexCount, unsigned int instanceCount=1)
Initializes the Renderer.
Definition: Renderer.cpp:5
A class for the VertexBuffer.
Definition: VertexBuffer.h:31
Contains a 2D vector.
Definition: Vec.h:108
Contains a 3D vector.
Definition: Vec.h:51
A struct for the Vertex.
Definition: VertexBuffer.h:18