Added color and moved uniform data to uniform buffer block
parent
55513703b2
commit
12e696bf1c
|
@ -61,6 +61,8 @@ public:
|
||||||
virtual void SetProjection(float left, float right, float bottom, float top) = 0;
|
virtual void SetProjection(float left, float right, float bottom, float top) = 0;
|
||||||
//! Sets texture, setting texture 0 means using white texture
|
//! Sets texture, setting texture 0 means using white texture
|
||||||
virtual void SetTexture(const Texture& texture) = 0;
|
virtual void SetTexture(const Texture& texture) = 0;
|
||||||
|
//! Sets color
|
||||||
|
virtual void SetColor(const glm::vec4& color) = 0;
|
||||||
|
|
||||||
//! Draws primitive
|
//! Draws primitive
|
||||||
virtual void DrawPrimitive(PrimitiveType type, int count, const Vertex2D* vertices) = 0;
|
virtual void DrawPrimitive(PrimitiveType type, int count, const Vertex2D* vertices) = 0;
|
||||||
|
|
|
@ -1976,6 +1976,7 @@ void CEngine::SetState(int state, const Color& color)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_device->GetUIRenderer()->Flush();
|
m_device->GetUIRenderer()->Flush();
|
||||||
|
m_device->GetUIRenderer()->SetColor({ color.r, color.g, color.b, color.a });
|
||||||
|
|
||||||
m_lastState = state;
|
m_lastState = state;
|
||||||
m_lastColor = color;
|
m_lastColor = color;
|
||||||
|
|
|
@ -36,7 +36,7 @@ CGL33UIRenderer::CGL33UIRenderer(CGL33Device* device)
|
||||||
m_program = LinkProgram(2, shaders);
|
m_program = LinkProgram(2, shaders);
|
||||||
if (m_program == 0)
|
if (m_program == 0)
|
||||||
{
|
{
|
||||||
GetLogger()->Error("Cound not link shader program for normal rendering\n");
|
GetLogger()->Error("Cound not link shader program for interface renderer\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,9 +45,21 @@ CGL33UIRenderer::CGL33UIRenderer(CGL33Device* device)
|
||||||
|
|
||||||
glUseProgram(m_program);
|
glUseProgram(m_program);
|
||||||
|
|
||||||
m_projectionMatrix = glGetUniformLocation(m_program, "uni_ProjectionMatrix");
|
// Create uniform buffer
|
||||||
auto matrix = glm::ortho(0.0f, +1.0f, 0.0f, +1.0f);
|
glGenBuffers(1, &m_uniformBuffer);
|
||||||
glUniformMatrix4fv(m_projectionMatrix, 1, GL_FALSE, glm::value_ptr(matrix));
|
|
||||||
|
m_uniforms.projectionMatrix = glm::ortho(0.0f, +1.0f, 0.0f, +1.0f);
|
||||||
|
m_uniforms.color = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
|
|
||||||
|
m_uniformsDirty = true;
|
||||||
|
|
||||||
|
UpdateUniforms();
|
||||||
|
|
||||||
|
// Bind uniform block to uniform buffer binding
|
||||||
|
GLuint blockIndex = glGetUniformBlockIndex(m_program, "Uniforms");
|
||||||
|
|
||||||
|
glBindBufferBase(GL_UNIFORM_BUFFER, 0, m_uniformBuffer);
|
||||||
|
glUniformBlockBinding(m_program, blockIndex, 0);
|
||||||
|
|
||||||
// Set texture unit to 8th
|
// Set texture unit to 8th
|
||||||
auto texture = glGetUniformLocation(m_program, "uni_Texture");
|
auto texture = glGetUniformLocation(m_program, "uni_Texture");
|
||||||
|
@ -91,13 +103,8 @@ void CGL33UIRenderer::SetProjection(float left, float right, float bottom, float
|
||||||
{
|
{
|
||||||
Flush();
|
Flush();
|
||||||
|
|
||||||
glUseProgram(m_program);
|
m_uniforms.projectionMatrix = glm::ortho(left, right, bottom, top);
|
||||||
|
m_uniformsDirty = true;
|
||||||
auto matrix = glm::ortho(left, right, bottom, top);
|
|
||||||
|
|
||||||
glUniformMatrix4fv(m_projectionMatrix, 1, GL_FALSE, glm::value_ptr(matrix));
|
|
||||||
|
|
||||||
m_device->Restore();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGL33UIRenderer::SetTexture(const Texture& texture)
|
void CGL33UIRenderer::SetTexture(const Texture& texture)
|
||||||
|
@ -116,6 +123,14 @@ void CGL33UIRenderer::SetTexture(const Texture& texture)
|
||||||
glBindTexture(GL_TEXTURE_2D, m_currentTexture);
|
glBindTexture(GL_TEXTURE_2D, m_currentTexture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CGL33UIRenderer::SetColor(const glm::vec4& color)
|
||||||
|
{
|
||||||
|
Flush();
|
||||||
|
|
||||||
|
m_uniforms.color = color;
|
||||||
|
m_uniformsDirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
void CGL33UIRenderer::DrawPrimitive(PrimitiveType type, int count, const Vertex2D* vertices)
|
void CGL33UIRenderer::DrawPrimitive(PrimitiveType type, int count, const Vertex2D* vertices)
|
||||||
{
|
{
|
||||||
// If too much data would be buffered, flush
|
// If too much data would be buffered, flush
|
||||||
|
@ -137,8 +152,12 @@ void CGL33UIRenderer::Flush()
|
||||||
{
|
{
|
||||||
if (m_types.empty()) return;
|
if (m_types.empty()) return;
|
||||||
|
|
||||||
|
UpdateUniforms();
|
||||||
|
|
||||||
glUseProgram(m_program);
|
glUseProgram(m_program);
|
||||||
|
|
||||||
|
glBindBufferBase(GL_UNIFORM_BUFFER, 0, m_uniformBuffer);
|
||||||
|
|
||||||
// Increase buffer size if necessary
|
// Increase buffer size if necessary
|
||||||
size_t size = m_buffer.size() * sizeof(Vertex2D);
|
size_t size = m_buffer.size() * sizeof(Vertex2D);
|
||||||
|
|
||||||
|
@ -190,4 +209,14 @@ void CGL33UIRenderer::Flush()
|
||||||
m_device->Restore();
|
m_device->Restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CGL33UIRenderer::UpdateUniforms()
|
||||||
|
{
|
||||||
|
if (!m_uniformsDirty) return;
|
||||||
|
|
||||||
|
glBindBuffer(GL_COPY_WRITE_BUFFER, m_uniformBuffer);
|
||||||
|
glBufferData(GL_COPY_WRITE_BUFFER, sizeof(Uniforms), nullptr, GL_STREAM_DRAW);
|
||||||
|
glBufferSubData(GL_COPY_WRITE_BUFFER, 0, sizeof(Uniforms), &m_uniforms);
|
||||||
|
glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Gfx
|
} // namespace Gfx
|
||||||
|
|
|
@ -45,16 +45,29 @@ public:
|
||||||
|
|
||||||
virtual void SetProjection(float left, float right, float bottom, float top) override;
|
virtual void SetProjection(float left, float right, float bottom, float top) override;
|
||||||
virtual void SetTexture(const Texture& texture) override;
|
virtual void SetTexture(const Texture& texture) override;
|
||||||
|
virtual void SetColor(const glm::vec4& color) override;
|
||||||
|
|
||||||
virtual void DrawPrimitive(PrimitiveType type, int count, const Vertex2D* vertices) override;
|
virtual void DrawPrimitive(PrimitiveType type, int count, const Vertex2D* vertices) override;
|
||||||
|
|
||||||
virtual void Flush() override;
|
virtual void Flush() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void UpdateUniforms();
|
||||||
|
|
||||||
CGL33Device* const m_device;
|
CGL33Device* const m_device;
|
||||||
|
|
||||||
// location of uni_ProjectionMatrix uniform
|
// Uniform data
|
||||||
GLint m_projectionMatrix = -1;
|
struct Uniforms
|
||||||
|
{
|
||||||
|
glm::mat4 projectionMatrix;
|
||||||
|
glm::vec4 color;
|
||||||
|
};
|
||||||
|
Uniforms m_uniforms;
|
||||||
|
// true means uniforms need to be updated
|
||||||
|
bool m_uniformsDirty = false;
|
||||||
|
|
||||||
|
// Uniform buffer object
|
||||||
|
GLuint m_uniformBuffer = 0;
|
||||||
|
|
||||||
// Vertex buffer object
|
// Vertex buffer object
|
||||||
GLuint m_bufferVBO = 0;
|
GLuint m_bufferVBO = 0;
|
||||||
|
|
|
@ -20,12 +20,16 @@
|
||||||
// VERTEX SHADER - UI RENDERER
|
// VERTEX SHADER - UI RENDERER
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
uniform mat4 uni_ProjectionMatrix;
|
|
||||||
|
|
||||||
layout(location = 0) in vec4 in_VertexCoord;
|
layout(location = 0) in vec4 in_VertexCoord;
|
||||||
layout(location = 1) in vec2 in_TexCoord;
|
layout(location = 1) in vec2 in_TexCoord;
|
||||||
layout(location = 2) in vec4 in_Color;
|
layout(location = 2) in vec4 in_Color;
|
||||||
|
|
||||||
|
uniform Uniforms
|
||||||
|
{
|
||||||
|
mat4 uni_Matrix;
|
||||||
|
vec4 uni_Color;
|
||||||
|
};
|
||||||
|
|
||||||
out VertexData
|
out VertexData
|
||||||
{
|
{
|
||||||
vec4 Color;
|
vec4 Color;
|
||||||
|
@ -34,8 +38,8 @@ out VertexData
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = uni_ProjectionMatrix * in_VertexCoord;
|
gl_Position = uni_Matrix * in_VertexCoord;
|
||||||
|
|
||||||
data.Color = in_Color;
|
data.Color = uni_Color * in_Color;
|
||||||
data.TexCoord = in_TexCoord;
|
data.TexCoord = in_TexCoord;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue