Skip to content
Permalink
Browse files

Renderer2D now uses a single shader to render both textured and non-t…

…extured quads
  • Loading branch information...
TheCherno committed Nov 7, 2019
1 parent 6791c41 commit 8c35857cc442706ec0196d0298fefa11dfcbe0bf
@@ -12,8 +12,8 @@ namespace Hazel {
struct Renderer2DStorage
{
Ref<VertexArray> QuadVertexArray;
Ref<Shader> FlatColorShader;
Ref<Shader> TextureShader;
Ref<Texture2D> WhiteTexture;
};

static Renderer2DStorage* s_Data;
@@ -43,7 +43,10 @@ namespace Hazel {
squareIB.reset(IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t)));
s_Data->QuadVertexArray->SetIndexBuffer(squareIB);

s_Data->FlatColorShader = Shader::Create("assets/shaders/FlatColor.glsl");
s_Data->WhiteTexture = Texture2D::Create(1, 1);
uint32_t whiteTextureData = 0xffffffff;
s_Data->WhiteTexture->SetData(&whiteTextureData, sizeof(uint32_t));

s_Data->TextureShader = Shader::Create("assets/shaders/Texture.glsl");
s_Data->TextureShader->Bind();
s_Data->TextureShader->SetInt("u_Texture", 0);
@@ -56,16 +59,12 @@ namespace Hazel {

void Renderer2D::BeginScene(const OrthographicCamera& camera)
{
s_Data->FlatColorShader->Bind();
s_Data->FlatColorShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix());

s_Data->TextureShader->Bind();
s_Data->TextureShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix());
}

void Renderer2D::EndScene()
{

}

void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color)
@@ -75,12 +74,11 @@ namespace Hazel {

void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color)
{
s_Data->FlatColorShader->Bind();
s_Data->FlatColorShader->SetFloat4("u_Color", color);
s_Data->TextureShader->SetFloat4("u_Color", color);
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 });
s_Data->FlatColorShader->SetMat4("u_Transform", transform);

s_Data->TextureShader->SetMat4("u_Transform", transform);
s_Data->QuadVertexArray->Bind();
RenderCommand::DrawIndexed(s_Data->QuadVertexArray);
}
@@ -92,13 +90,12 @@ namespace Hazel {

void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D>& texture)
{
s_Data->TextureShader->Bind();
s_Data->TextureShader->SetFloat4("u_Color", glm::vec4(1.0f));
texture->Bind();

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

texture->Bind();

s_Data->QuadVertexArray->Bind();
RenderCommand::DrawIndexed(s_Data->QuadVertexArray);
}
@@ -6,12 +6,24 @@

namespace Hazel {

Ref<Texture2D> Texture2D::Create(uint32_t width, uint32_t height)
{
switch (Renderer::GetAPI())
{
case RendererAPI::API::None: HZ_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr;
case RendererAPI::API::OpenGL: return CreateRef<OpenGLTexture2D>(width, height);
}

HZ_CORE_ASSERT(false, "Unknown RendererAPI!");
return nullptr;
}

Ref<Texture2D> Texture2D::Create(const std::string& path)
{
switch (Renderer::GetAPI())
{
case RendererAPI::API::None: HZ_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr;
case RendererAPI::API::OpenGL: return std::make_shared<OpenGLTexture2D>(path);
case RendererAPI::API::OpenGL: return CreateRef<OpenGLTexture2D>(path);
}

HZ_CORE_ASSERT(false, "Unknown RendererAPI!");
@@ -14,12 +14,15 @@ namespace Hazel {
virtual uint32_t GetWidth() const = 0;
virtual uint32_t GetHeight() const = 0;

virtual void SetData(void* data, uint32_t size) = 0;

virtual void Bind(uint32_t slot = 0) const = 0;
};

class Texture2D : public Texture
{
public:
static Ref<Texture2D> Create(uint32_t width, uint32_t height);
static Ref<Texture2D> Create(const std::string& path);
};

@@ -31,6 +31,7 @@ namespace Hazel {
void OpenGLRendererAPI::DrawIndexed(const std::shared_ptr<VertexArray>& vertexArray)
{
glDrawElements(GL_TRIANGLES, vertexArray->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
}

}
@@ -3,10 +3,24 @@

#include "stb_image.h"

#include <glad/glad.h>

namespace Hazel {

OpenGLTexture2D::OpenGLTexture2D(uint32_t width, uint32_t height)
: m_Width(width), m_Height(height)
{
m_InternalFormat = GL_RGBA8;
m_DataFormat = GL_RGBA;

glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);
glTextureStorage2D(m_RendererID, 1, m_InternalFormat, m_Width, m_Height);

glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_T, GL_REPEAT);
}

OpenGLTexture2D::OpenGLTexture2D(const std::string& path)
: m_Path(path)
{
@@ -29,6 +43,9 @@ namespace Hazel {
dataFormat = GL_RGB;
}

m_InternalFormat = internalFormat;
m_DataFormat = dataFormat;

HZ_CORE_ASSERT(internalFormat & dataFormat, "Format not supported!");

glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);
@@ -50,6 +67,13 @@ namespace Hazel {
glDeleteTextures(1, &m_RendererID);
}

void OpenGLTexture2D::SetData(void* data, uint32_t size)
{
uint32_t bpp = m_DataFormat == GL_RGBA ? 4 : 3;
HZ_CORE_ASSERT(size == m_Width * m_Height * bpp, "Data must be entire texture!");
glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, m_DataFormat, GL_UNSIGNED_BYTE, data);
}

void OpenGLTexture2D::Bind(uint32_t slot) const
{
glBindTextureUnit(slot, m_RendererID);
@@ -2,22 +2,28 @@

#include "Hazel/Renderer/Texture.h"

#include <glad/glad.h>

namespace Hazel {

class OpenGLTexture2D : public Texture2D
{
public:
OpenGLTexture2D(uint32_t width, uint32_t height);
OpenGLTexture2D(const std::string& path);
virtual ~OpenGLTexture2D();

virtual uint32_t GetWidth() const override { return m_Width; }
virtual uint32_t GetHeight() const override { return m_Height; }

virtual void SetData(void* data, uint32_t size) override;

virtual void Bind(uint32_t slot = 0) const override;
private:
std::string m_Path;
uint32_t m_Width, m_Height;
uint32_t m_RendererID;
GLenum m_InternalFormat, m_DataFormat;
};

}
@@ -24,9 +24,10 @@ layout(location = 0) out vec4 color;

in vec2 v_TexCoord;

uniform vec4 u_Color;
uniform sampler2D u_Texture;

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

0 comments on commit 8c35857

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