DrawQuad Transform

- DraqQuadImpl functions now call a non-static DrawQuadFinal function with a mat4
      transform matrix
This commit is contained in:
Light 2021-07-30 12:49:48 +04:30
parent 931a3a168c
commit 98184d358f
10 changed files with 45 additions and 35 deletions

View file

@ -1,6 +1,6 @@
#version 440 core #version 440 core
layout(location = 0) in vec3 a_Position; layout(location = 0) in vec4 a_Position;
layout(location = 1) in vec4 a_Color; layout(location = 1) in vec4 a_Color;
layout(std140, binding = 0) uniform ub_ViewProjection layout(std140, binding = 0) uniform ub_ViewProjection
@ -12,6 +12,6 @@ out vec4 vso_FragmentColor;
void main() void main()
{ {
gl_Position = viewProjection * vec4(a_Position, 1.0); gl_Position = viewProjection * a_Position;
vso_FragmentColor = a_Color; vso_FragmentColor = a_Color;
} }

View file

@ -9,10 +9,10 @@ cbuffer cv_ViewProjection : register(b0)
row_major matrix viewProjection; row_major matrix viewProjection;
} }
VertexOut main(float3 InPosition : POSITION, float4 InColor : COLOR) VertexOut main(float4 InPosition : POSITION, float4 InColor : COLOR)
{ {
VertexOut vso; VertexOut vso;
vso.Position = mul(float4(InPosition.x, InPosition.y, InPosition.z, 1.0), viewProjection); vso.Position = mul(InPosition, viewProjection);
vso.Color = InColor; vso.Color = InColor;
return vso; return vso;

View file

@ -1,6 +1,6 @@
#version 440 core #version 440 core
layout(location = 0) in vec3 a_Position; layout(location = 0) in vec4 a_Position;
layout(location = 1) in vec2 a_TexCoord; layout(location = 1) in vec2 a_TexCoord;
layout(std140, binding = 0) uniform ub_ViewProjection layout(std140, binding = 0) uniform ub_ViewProjection
@ -12,6 +12,6 @@ out vec2 vso_TexCoord;
void main() void main()
{ {
gl_Position = u_ViewProjection * vec4(a_Position, 1.0); gl_Position = u_ViewProjection * a_Position;
vso_TexCoord = a_TexCoord; vso_TexCoord = a_TexCoord;
} }

View file

@ -9,10 +9,10 @@ cbuffer cb_ViewProjection : register(b0)
row_major matrix viewProjection; row_major matrix viewProjection;
} }
VertexOut main(float3 InPosition : POSITION, float2 InTexChoord : TEXCOORD) VertexOut main(float4 InPosition : POSITION, float2 InTexChoord : TEXCOORD)
{ {
VertexOut vso; VertexOut vso;
vso.Position = mul(float4(InPosition, 1.0), viewProjection); vso.Position = mul(float4(InPosition), viewProjection);
vso.TexChoord = InTexChoord; vso.TexChoord = InTexChoord;
return vso; return vso;

View file

@ -47,31 +47,42 @@ namespace Light {
m_RenderCommand->SetViewport(0u, 0u, event.GetSize().x, event.GetSize().y); m_RenderCommand->SetViewport(0u, 0u, event.GetSize().x, event.GetSize().y);
} }
//======================================== DRAW_QUAD_TINT ========================================// //======================================== DRAW_QUAD ========================================//
/* tint */
void Renderer::DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, const glm::vec4& tint) void Renderer::DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, const glm::vec4& tint)
{
DrawQuadFinal(glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }),
tint);
}
/* texture */
void Renderer::DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, Ref<Texture> texture)
{
DrawQuadFinal(glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }),
texture);
}
//======================================== DRAW_QUAD ========================================//
//==================== DRAW_QUAD_TINT ====================//
void Renderer::DrawQuadFinal(const glm::mat4& transform, const glm::vec4& tint)
{ {
// locals // locals
QuadRendererProgram::QuadVertexData* bufferMap = m_QuadRenderer.GetMapCurrent(); QuadRendererProgram::QuadVertexData* bufferMap = m_QuadRenderer.GetMapCurrent();
const float xMin = position.x;
const float yMin = position.y;
const float xMax = position.x + size.x;
const float yMax = position.y + size.y;
// top left // top left
bufferMap[0].position = { xMin, yMin, position.z }; bufferMap[0].position = transform * glm::vec4(-0.5f, -0.5f, 0.0f, 1.0f);
bufferMap[0].tint = tint; bufferMap[0].tint = tint;
// top right // top right
bufferMap[1].position = { xMax, yMin, position.z }; bufferMap[1].position = transform * glm::vec4( 0.5f, -0.5f, 0.0f, 1.0f);
bufferMap[1].tint = tint; bufferMap[1].tint = tint;
// bottom right // bottom right
bufferMap[2].position = { xMax, yMax, position.z }; bufferMap[2].position = transform * glm::vec4( 0.5f, 0.5f, 0.0f, 1.0f);
bufferMap[2].tint = tint; bufferMap[2].tint = tint;
// bottom left // bottom left
bufferMap[3].position = { xMin, yMax, position.z }; bufferMap[3].position = transform * glm::vec4(-0.5f, 0.5f, 0.0f, 1.0f);
bufferMap[3].tint = tint; bufferMap[3].tint = tint;
// advance // advance
@ -81,10 +92,10 @@ namespace Light {
FlushScene(); FlushScene();
} }
} }
//======================================== DRAW_QUAD_TINT ========================================// //==================== DRAW_QUAD_TINT ====================//
//============================== DRAW_QUAD_TEXTURE ==============================// //==================== DRAW_QUAD_TEXTURE ====================//
void Renderer::DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, Ref<Texture> texture) void Renderer::DrawQuadFinal(const glm::mat4& transform, Ref<Texture> texture)
{ {
// #todo: implement a proper binding // #todo: implement a proper binding
texture->Bind(); texture->Bind();
@ -92,25 +103,20 @@ namespace Light {
// locals // locals
TextureRendererProgram::TextureVertexData* bufferMap = m_TextureRenderer.GetMapCurrent(); TextureRendererProgram::TextureVertexData* bufferMap = m_TextureRenderer.GetMapCurrent();
const float xMin = position.x;
const float yMin = position.y;
const float xMax = position.x + size.x;
const float yMax = position.y + size.y;
// top left // top left
bufferMap[0].position = { xMin, yMin, position.z }; bufferMap[0].position = transform * glm::vec4(-0.5f, -0.5f, 0.0f, 1.0f);
bufferMap[0].texcoord = { 0.0f, 0.0f }; bufferMap[0].texcoord = { 0.0f, 0.0f };
// top right // top right
bufferMap[1].position = { xMax, yMin, position.z }; bufferMap[1].position = transform * glm::vec4( 0.5f, -0.5f, 0.0f, 1.0f);
bufferMap[1].texcoord = { 1.0f, 0.0f }; bufferMap[1].texcoord = { 1.0f, 0.0f };
// bottom right // bottom right
bufferMap[2].position = { xMax, yMax, position.z }; bufferMap[2].position = transform * glm::vec4( 0.5f, 0.5f, 0.0f, 1.0f);
bufferMap[2].texcoord = { 1.0f, 1.0f }; bufferMap[2].texcoord = { 1.0f, 1.0f };
// bottom left // bottom left
bufferMap[3].position = { xMin, yMax, position.z }; bufferMap[3].position = transform * glm::vec4(-0.5f, 0.5f, 0.0f, 1.0f);
bufferMap[3].texcoord = { 0.0f, 1.0f }; bufferMap[3].texcoord = { 0.0f, 1.0f };
// advance // advance
@ -120,7 +126,8 @@ namespace Light {
FlushScene(); FlushScene();
} }
} }
//============================== DRAW_QUAD_TEXTURE ==============================//
//==================== DRAW_QUAD_TEXTURE ====================//
void Renderer::BeginFrame() void Renderer::BeginFrame()
{ {

View file

@ -62,6 +62,9 @@ namespace Light {
void DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, const glm::vec4& tint); void DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, const glm::vec4& tint);
void DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, Ref<Texture> texture); void DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, Ref<Texture> texture);
void DrawQuadFinal(const glm::mat4& transform, const glm::vec4& tint);
void DrawQuadFinal(const glm::mat4& transform, Ref<Texture> texture);
void BeginSceneImpl(const Ref<Camera>& camera, const Ref<Framebuffer>& targetFrameBuffer = nullptr); void BeginSceneImpl(const Ref<Camera>& camera, const Ref<Framebuffer>& targetFrameBuffer = nullptr);
void FlushScene(); void FlushScene();
void EndSceneImpl(); void EndSceneImpl();

View file

@ -26,7 +26,7 @@ namespace Light {
m_Shader = ResourceManager::GetShader("LT_ENGINE_RESOURCES_QUAD_SHADER"); m_Shader = ResourceManager::GetShader("LT_ENGINE_RESOURCES_QUAD_SHADER");
m_VertexBuffer = Ref<VertexBuffer>(VertexBuffer::Create(nullptr, sizeof(QuadVertexData), maxVertices, sharedContext)); m_VertexBuffer = Ref<VertexBuffer>(VertexBuffer::Create(nullptr, sizeof(QuadVertexData), maxVertices, sharedContext));
m_IndexBuffer = Ref<IndexBuffer>(IndexBuffer::Create(nullptr, (maxVertices / 4) * 6, sharedContext)); m_IndexBuffer = Ref<IndexBuffer>(IndexBuffer::Create(nullptr, (maxVertices / 4) * 6, sharedContext));
m_VertexLayout = Ref<VertexLayout>(VertexLayout::Create(m_VertexBuffer, m_Shader, { { "POSITION", VertexElementType::Float3 }, m_VertexLayout = Ref<VertexLayout>(VertexLayout::Create(m_VertexBuffer, m_Shader, { { "POSITION", VertexElementType::Float4 },
{ "COLOR" , VertexElementType::Float4 }}, sharedContext)); { "COLOR" , VertexElementType::Float4 }}, sharedContext));
} }

View file

@ -22,7 +22,7 @@ namespace Light {
public: public:
struct QuadVertexData struct QuadVertexData
{ {
glm::vec3 position; glm::vec4 position;
glm::vec4 tint; glm::vec4 tint;
}; };

View file

@ -26,7 +26,7 @@ namespace Light {
m_Shader = ResourceManager::GetShader("LT_ENGINE_RESOURCES_TEXTURE_SHADER"); m_Shader = ResourceManager::GetShader("LT_ENGINE_RESOURCES_TEXTURE_SHADER");
m_VertexBuffer = Ref<VertexBuffer>(VertexBuffer::Create(nullptr, sizeof(TextureVertexData), maxVertices, sharedContext)); m_VertexBuffer = Ref<VertexBuffer>(VertexBuffer::Create(nullptr, sizeof(TextureVertexData), maxVertices, sharedContext));
m_IndexBuffer = Ref<IndexBuffer>(IndexBuffer::Create(nullptr, (maxVertices / 4) * 6, sharedContext)); m_IndexBuffer = Ref<IndexBuffer>(IndexBuffer::Create(nullptr, (maxVertices / 4) * 6, sharedContext));
m_VertexLayout = Ref<VertexLayout>(VertexLayout::Create(m_VertexBuffer, m_Shader, { { "POSITION", VertexElementType::Float3 }, m_VertexLayout = Ref<VertexLayout>(VertexLayout::Create(m_VertexBuffer, m_Shader, { { "POSITION", VertexElementType::Float4 },
{ "TEXCOORD", VertexElementType::Float2 }}, sharedContext)); { "TEXCOORD", VertexElementType::Float2 }}, sharedContext));
} }

View file

@ -22,7 +22,7 @@ namespace Light {
public: public:
struct TextureVertexData struct TextureVertexData
{ {
glm::vec3 position; glm::vec4 position;
glm::vec2 texcoord; glm::vec2 texcoord;
}; };