Skip to content

Commit

Permalink
Renderer2D consolidate DrawRotatedQaud functions (TheCherno#281)
Browse files Browse the repository at this point in the history
And fix error in Sandbox
  • Loading branch information
LovelySanta authored Jul 21, 2020
1 parent 5aff96f commit 72bc16e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 66 deletions.
72 changes: 7 additions & 65 deletions Hazel/src/Hazel/Renderer/Renderer2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,6 @@ namespace Hazel {
s_Data.Stats.QuadCount++;
}

void Renderer2D::DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const glm::vec4& color)
{
DrawRotatedQuad({ position.x, position.y, 0.0f }, size, rotation, color);
}

void Renderer2D::DrawQuad(const glm::mat4& transform, const Ref<Texture2D>& texture, float tilingFactor, const glm::vec4& tintColor)
{
HZ_PROFILE_FUNCTION();
Expand Down Expand Up @@ -263,35 +258,20 @@ namespace Hazel {
s_Data.Stats.QuadCount++;
}

void Renderer2D::DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const glm::vec4& color)
{
DrawRotatedQuad({ position.x, position.y, 0.0f }, size, rotation, color);
}

void Renderer2D::DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const glm::vec4& color)
{
HZ_PROFILE_FUNCTION();

constexpr size_t quadVertexCount = 4;
const float textureIndex = 0.0f; // White Texture
constexpr glm::vec2 textureCoords[] = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, { 0.0f, 1.0f } };
const float tilingFactor = 1.0f;

if (s_Data.QuadIndexCount >= Renderer2DData::MaxIndices)
FlushAndReset();

glm::mat4 transform = glm::translate(glm::mat4(1.0f), position)
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation), { 0.0f, 0.0f, 1.0f })
* glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });

for (size_t i = 0; i < quadVertexCount; i++)
{
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPositions[i];
s_Data.QuadVertexBufferPtr->Color = color;
s_Data.QuadVertexBufferPtr->TexCoord = textureCoords[i];
s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
s_Data.QuadVertexBufferPtr++;
}

s_Data.QuadIndexCount += 6;

s_Data.Stats.QuadCount++;
DrawQuad(transform, color);
}

void Renderer2D::DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const Ref<Texture2D>& texture, float tilingFactor, const glm::vec4& tintColor)
Expand All @@ -303,49 +283,11 @@ namespace Hazel {
{
HZ_PROFILE_FUNCTION();

constexpr size_t quadVertexCount = 4;
constexpr glm::vec2 textureCoords[] = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, { 0.0f, 1.0f } };

if (s_Data.QuadIndexCount >= Renderer2DData::MaxIndices)
FlushAndReset();

float textureIndex = 0.0f;
for (uint32_t i = 1; i < s_Data.TextureSlotIndex; i++)
{
if (*s_Data.TextureSlots[i].get() == *texture.get())
{
textureIndex = (float)i;
break;
}
}

if (textureIndex == 0.0f)
{
if (s_Data.TextureSlotIndex >= Renderer2DData::MaxTextureSlots)
FlushAndReset();

textureIndex = (float)s_Data.TextureSlotIndex;
s_Data.TextureSlots[s_Data.TextureSlotIndex] = texture;
s_Data.TextureSlotIndex++;
}

glm::mat4 transform = glm::translate(glm::mat4(1.0f), position)
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation), { 0.0f, 0.0f, 1.0f })
* glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });

for (size_t i = 0; i < quadVertexCount; i++)
{
s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPositions[i];
s_Data.QuadVertexBufferPtr->Color = tintColor;
s_Data.QuadVertexBufferPtr->TexCoord = textureCoords[i];
s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
s_Data.QuadVertexBufferPtr++;
}

s_Data.QuadIndexCount += 6;

s_Data.Stats.QuadCount++;
DrawQuad(transform, texture, tilingFactor, tintColor);
}

void Renderer2D::ResetStats()
Expand Down
3 changes: 2 additions & 1 deletion premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ project "Sandbox"
"Hazel/vendor/spdlog/include",
"Hazel/src",
"Hazel/vendor",
"%{IncludeDir.glm}"
"%{IncludeDir.glm}",
"%{IncludeDir.entt}"
}

links
Expand Down

0 comments on commit 72bc16e

Please sign in to comment.