Skip to content
Permalink
Browse files

Renderer2D API improvements

  • Loading branch information
TheCherno committed Mar 25, 2020
1 parent f823e2e commit 681c72fb1b929bf9a1e6fd823886a10158f4029f
@@ -83,27 +83,74 @@ namespace Hazel {
HZ_PROFILE_FUNCTION();

s_Data->TextureShader->SetFloat4("u_Color", color);
s_Data->TextureShader->SetFloat("u_TilingFactor", 1.0f);
s_Data->WhiteTexture->Bind();

glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
glm::mat4 transform = glm::translate(glm::mat4(1.0f), position)
* glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
s_Data->TextureShader->SetMat4("u_Transform", transform);
s_Data->QuadVertexArray->Bind();
RenderCommand::DrawIndexed(s_Data->QuadVertexArray);
}

void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref<Texture2D>& texture)
void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref<Texture2D>& texture, float tilingFactor, const glm::vec4& tintColor)
{
DrawQuad({ position.x, position.y, 0.0f }, size, texture);
DrawQuad({ position.x, position.y, 0.0f }, size, texture, tilingFactor, tintColor);
}

void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D>& texture)
void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D>& texture, float tilingFactor, const glm::vec4& tintColor)
{
HZ_PROFILE_FUNCTION();

s_Data->TextureShader->SetFloat4("u_Color", glm::vec4(1.0f));
s_Data->TextureShader->SetFloat4("u_Color", tintColor);
s_Data->TextureShader->SetFloat("u_TilingFactor", tilingFactor);
texture->Bind();

glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
glm::mat4 transform = glm::translate(glm::mat4(1.0f), position)
* glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
s_Data->TextureShader->SetMat4("u_Transform", transform);

s_Data->QuadVertexArray->Bind();
RenderCommand::DrawIndexed(s_Data->QuadVertexArray);
}

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();

s_Data->TextureShader->SetFloat4("u_Color", color);
s_Data->TextureShader->SetFloat("u_TilingFactor", 1.0f);
s_Data->WhiteTexture->Bind();

glm::mat4 transform = glm::translate(glm::mat4(1.0f), position)
* glm::rotate(glm::mat4(1.0f), rotation, { 0.0f, 0.0f, 1.0f })
* glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
s_Data->TextureShader->SetMat4("u_Transform", transform);
s_Data->QuadVertexArray->Bind();
RenderCommand::DrawIndexed(s_Data->QuadVertexArray);
}

void Renderer2D::DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const Ref<Texture2D>& texture, float tilingFactor, const glm::vec4& tintColor)
{
DrawRotatedQuad({ position.x, position.y, 0.0f }, size, rotation, texture, tilingFactor, tintColor);
}

void Renderer2D::DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const Ref<Texture2D>& texture, float tilingFactor, const glm::vec4& tintColor)
{
HZ_PROFILE_FUNCTION();

s_Data->TextureShader->SetFloat4("u_Color", tintColor);
s_Data->TextureShader->SetFloat("u_TilingFactor", tilingFactor);
texture->Bind();

glm::mat4 transform = glm::translate(glm::mat4(1.0f), position)
* glm::rotate(glm::mat4(1.0f), rotation, { 0.0f, 0.0f, 1.0f })
* glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
s_Data->TextureShader->SetMat4("u_Transform", transform);

s_Data->QuadVertexArray->Bind();
@@ -18,8 +18,14 @@ namespace Hazel {
// Primitives
static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color);
static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color);
static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref<Texture2D>& texture);
static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D>& texture);
static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref<Texture2D>& texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f));
static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D>& texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f));

static void DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const glm::vec4& color);
static void DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const glm::vec4& color);
static void DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const Ref<Texture2D>& texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f));
static void DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const Ref<Texture2D>& texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f));

};

}
@@ -16,6 +16,7 @@ namespace Hazel {
virtual void Unbind() const = 0;

virtual void SetInt(const std::string& name, int value) = 0;
virtual void SetFloat(const std::string& name, float value) = 0;
virtual void SetFloat3(const std::string& name, const glm::vec3& value) = 0;
virtual void SetFloat4(const std::string& name, const glm::vec4& value) = 0;
virtual void SetMat4(const std::string& name, const glm::mat4& value) = 0;
@@ -207,6 +207,13 @@ namespace Hazel {
UploadUniformInt(name, value);
}

void OpenGLShader::SetFloat(const std::string& name, float value)
{
HZ_PROFILE_FUNCTION();

UploadUniformFloat(name, value);
}

void OpenGLShader::SetFloat3(const std::string& name, const glm::vec3& value)
{
HZ_PROFILE_FUNCTION();
@@ -19,6 +19,7 @@ namespace Hazel {
virtual void Unbind() const override;

virtual void SetInt(const std::string& name, int value) override;
virtual void SetFloat(const std::string& name, float value) override;
virtual void SetFloat3(const std::string& name, const glm::vec3& value) override;
virtual void SetFloat4(const std::string& name, const glm::vec4& value) override;
virtual void SetMat4(const std::string& name, const glm::mat4& value) override;
@@ -25,9 +25,10 @@ layout(location = 0) out vec4 color;
in vec2 v_TexCoord;

uniform vec4 u_Color;
uniform float u_TilingFactor;
uniform sampler2D u_Texture;

void main()
{
color = texture(u_Texture, v_TexCoord * 10.0) * u_Color;
color = texture(u_Texture, v_TexCoord * u_TilingFactor) * u_Color;
}

0 comments on commit 681c72f

Please sign in to comment.
You can’t perform that action at this time.