From 8d75fd9488b3a9d192edb64d2c43c6edd9bc07e8 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Thu, 18 Aug 2022 16:48:39 -0400 Subject: [PATCH] DrawSprite implemented (just calls DrawQuad) --- docs/tasks/refactoring.todo | 21 ++++++-- src/core/types.h | 2 + src/renderer/renderer2D.cpp | 52 ++++--------------- src/renderer/renderer2D.h | 43 ++++++++++----- .../testbed/scenes/simple_render_scene.cpp | 1 + .../testbed/scenes/simple_render_scene.h | 2 +- 6 files changed, 61 insertions(+), 60 deletions(-) diff --git a/docs/tasks/refactoring.todo b/docs/tasks/refactoring.todo index 5bb7917..a7d4bff 100644 --- a/docs/tasks/refactoring.todo +++ b/docs/tasks/refactoring.todo @@ -7,11 +7,24 @@ Renderer rewrite: ✔ Batch rendering minimally working @done(22-08-12 19:19) ✔ See if it's possible/better to move the matrix math into the shaders @high @done(22-08-15 14:23) - ☐ Textures in batch renderer @high - ☐ DrawSprite method - ☐ Sprite shader - ☐ Text Renderer + ✔ Textures in batch renderer @high @done(22-08-18 16:09) + ✔ Add sprite sheet logic to DrawQuad @done(22-08-18 16:13) + + ✔ DrawSprite method @done(22-08-18 16:42) + - Just calls DrawQuad + ✘ Sprite shader @cancelled(22-08-18 16:09) + - Moved the "Sprite" logic into the DrawQuad method instead + + ✔ Text Renderer @done(22-08-18 16:10) + ✔ Font loading @done(22-08-18 16:13) + ✔ Generate single conposite texture of all font bitmap data @done(22-08-18 16:13) + ✔ DrawString @done(22-08-18 16:14) + ☐ DrawElipse @low ☐ Elipse Shader ☐ Elipse Batch data + + ☐ DrawLine @low + ☐ Line Shader + ☐ Line Batch data \ No newline at end of file diff --git a/src/core/types.h b/src/core/types.h index 44ae27f..875ad21 100644 --- a/src/core/types.h +++ b/src/core/types.h @@ -106,6 +106,8 @@ namespace lunarium float top() const { return Y - HalfHeight; } float right() const { return X + HalfWidth; } float bottom() const { return Y + HalfHeight; } + float width() const { return HalfWidth * 2; } + float height() const { return HalfHeight * 2; } glm::vec2 CenterPoint() const; Sizef FullSize() const; diff --git a/src/renderer/renderer2D.cpp b/src/renderer/renderer2D.cpp index 5d160b9..aa1d883 100644 --- a/src/renderer/renderer2D.cpp +++ b/src/renderer/renderer2D.cpp @@ -168,6 +168,7 @@ namespace lunarium { mFrameStats.DrawCalls = 0; mFrameStats.NumTris = 0; + mFrameStats.NumVerts = 0; } Renderer2D::FrameStats Renderer2D::GetFrameStats() const @@ -194,6 +195,13 @@ namespace lunarium ///////////////////////////////////////////////////////////////////// // DRAW METHODS ///////////////////////////////////////////////////////////////////// + + void Renderer2D::DrawSprite(Texture* texture, glm::vec2 position, Rectangle sub_texture_area, Color color, float angle) + { + Rectangle quad = Rectangle::MakeFromTopLeft(position.x, position.y, sub_texture_area.width(), sub_texture_area.height()); + DrawQuad(quad, color, texture, angle, sub_texture_area); + } + void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle, Rectangle sub_texture_area) { int texture_slot = -1; @@ -327,52 +335,10 @@ namespace lunarium mQuadData.mNumQuads++; mFrameStats.NumTris += 2; + mFrameStats.NumVerts += 4; } - - void Renderer2D::DrawSprite(Texture& image, glm::vec2 position, Color color, float angle) - { - Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawSprite is not yet implemented"); - // float half_width = image.GetWidth() / 2; - // float half_height = image.GetHeight() / 2; - // DrawImage(image, Rectangle::MakeFromTopLeft(0, 0, image.GetWidth(), image.GetHeight()), - // Rectangle(position.x + half_width, position.y + half_height, half_width, half_height), color, angle); - } - - void Renderer2D::DrawSprite(Texture& image, Rectangle source, Rectangle destination, Color color, float angle) - { - Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawSprite is not yet implemented"); - // glm::mat4 id = glm::mat4(1.0f); - // glm::vec3 pos = glm::vec3(destination.X, destination.Y, 0.0f); - // glm::mat4 trans = glm::translate(id, pos); - // trans = glm::rotate(trans, angle, glm::vec3(0.0f, 0.0f, 1.0f)); - // trans = glm::scale(trans, glm::vec3(destination.HalfWidth * 2, destination.HalfHeight * 2, 1.0f)); - - - // mImageShader.MakeActive(); - - // float xScale = (source.HalfWidth * 2) / image.GetWidth(); - // float xOffset = source.left() / image.GetWidth(); - // float yScale = (source.HalfHeight * 2) / image.GetHeight(); - // float yOffset = source.top() / image.GetHeight(); - - - // // * -1.0f on yScale will flip the image vertically - // mImageShader.SetUniformf("uvManip", { xScale, xOffset, yScale * -1.0f, yOffset}); - // mImageShader.SetUniformMatrix("model", 1, glm::value_ptr(trans)); - // mImageShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection)); - // mImageShader.SetUniformf("spriteColor", { color.R, color.G, color.B, color.A }); - - - // glActiveTexture(GL_TEXTURE0); - // glBindTexture(GL_TEXTURE_2D, image.GetGLTextureID()); - - // glBindVertexArray(mImageVAO); - // glDrawArrays(GL_TRIANGLES, 0, 6); - // glBindVertexArray(0); - } - void Renderer2D::DrawString(const char* string, Rectangle bounding_box, Color color, float scale, int font) { //Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawString is not yet implemented"); diff --git a/src/renderer/renderer2D.h b/src/renderer/renderer2D.h index ff598d8..defa043 100644 --- a/src/renderer/renderer2D.h +++ b/src/renderer/renderer2D.h @@ -29,6 +29,7 @@ namespace lunarium public: struct FrameStats { + int NumVerts = 0; int NumTris = 0; int DrawCalls = 0; }; @@ -44,11 +45,15 @@ namespace lunarium // Draw methods + /// sub_texture_area is the area of the texture to draw + /// position is the top-left corner of the quad + void DrawSprite(Texture* texture, glm::vec2 position, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f); + /// sub_texture_area is the area of the texture to draw void DrawQuad(Rectangle quad, Color color, Texture* texture = nullptr, float angle = 0.0f, Rectangle sub_texture_area = Rectangle(-1, -1, -1, -1)); - void DrawQuads(Rectangle* quads, u32 num_quads, Color* pColors); - void DrawSprite(Texture& image, glm::vec2 position, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float angle = 0); - void DrawSprite(Texture& image, Rectangle source, Rectangle destination, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float angle = 0); + void DrawLine(glm::vec2 point_a, glm::vec2 point_b, Color color, float thickness = 1.0f, float angle = 0.0f); + void DrawBox(Rectangle box, Color color, float thickness = 1.0f, float angle = 0.0f); + void DrawEllipse(glm::vec2 center_point, glm::vec2 radii, Color color, bool filled = false, float thickness = 1.0f, float angle = 0.0f); void DrawString(const char* string, Rectangle bounding_box, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float scale = 1.0f, int font = -1); // DEBUG: @@ -76,13 +81,6 @@ namespace lunarium const int MaxIndices = MaxQuads * 6; const int TextureSlots = 32; - // const glm::vec4 vert_pos[4] = { - // { -0.5f, 0.5f, 0.0f, 1.0f }, // TOP LEFT - // { 0.5f, 0.5f, 0.0f, 1.0f }, // TOP RIGHT - // { 0.5f, -0.5f, 0.0f, 1.0f }, // BOTTOM RIGHT - // { -0.5f, -0.5f, 0.0f, 1.0f } // BOTTOM LEFT - // }; - const glm::vec4 vert_pos[4] = { { -0.5f, -0.5f, 0.0f, 1.0f }, { 0.5f, -0.5f, 0.0f, 1.0f }, @@ -109,7 +107,6 @@ namespace lunarium float tex_is_grey_scale; // 0 or 1, this is mostly used for text rendering }; - //const u32 indices[6] = { 0, 1, 2, 0, 2, 3 }; const u32 indices[6] = { 0, 1, 2, 2, 3, 0 }; Vertex* mRawVertexBuffer; @@ -117,10 +114,32 @@ namespace lunarium BufferLayout mBufferLayout; VertexBuffer* mVertexBuffer; - //IndexBuffer* mIndexBuffer; Shader* mQuadShader; } mQuadData; + struct EllipseData + { + + + } mEllipseData; + + struct LineData + { + + struct Vertex + { + glm::vec2 pos; + glm::vec4 color; + }; + + Vertex* mRawVertexBuffer; + int mRawBufferIndex; + + BufferLayout mBufferLayout; + VertexBuffer* mVertexBuffer; + Shader* mShader; + } mLineData; + TextRenderer mTextAPI; int mDefaultFont; diff --git a/src/run_modes/testbed/scenes/simple_render_scene.cpp b/src/run_modes/testbed/scenes/simple_render_scene.cpp index d9ef506..900b9f0 100644 --- a/src/run_modes/testbed/scenes/simple_render_scene.cpp +++ b/src/run_modes/testbed/scenes/simple_render_scene.cpp @@ -318,6 +318,7 @@ namespace lunarium //g.DrawQuad(Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 0.0f, 1.0f), nullptr, angle); g.DrawQuad(Rectangle(100, 100, 16, 16), Color::White(), mpTestImageLoad2, 0.0f, mSubTex); + g.DrawSprite(mpTestImageLoad2, glm::vec2(600, 200), Rectangle::MakeFromTopLeft(255, 255, 255, 255)); // g.DrawQuad(Rectangle(200, 200, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f));//, nullptr, box_angle); // g.DrawQuad(Rectangle(400, 300, 64.0f, 64.0f), Color(0.0f, 1.0f, 1.0f, 1.0f), nullptr, box_angle); diff --git a/src/run_modes/testbed/scenes/simple_render_scene.h b/src/run_modes/testbed/scenes/simple_render_scene.h index b23de54..540ccdc 100644 --- a/src/run_modes/testbed/scenes/simple_render_scene.h +++ b/src/run_modes/testbed/scenes/simple_render_scene.h @@ -44,6 +44,7 @@ namespace lunarium Texture* mpTestImageLoad; Texture* mpTestImageLoad2; Texture* mpTestImageLoad3; + Rectangle mSubTex; FrameBuffer* mFrameBufferOne; FrameBuffer* mFrameBufferTwo; float angle; @@ -52,7 +53,6 @@ namespace lunarium float mSrcWidth; float mSrcHeight; i32 mTextDebugPosX; - Rectangle mSubTex; HighResTimer mTimer; double mFrameTime;