From 1f68fe78e84f25c8ddacdc37a293a5de31725bab Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 1 Jan 2022 13:01:08 +0000 Subject: First iteration with font/text support --- gfx/gl/shaders/uiShaderFont.fs | 12 ++++++++++++ gfx/gl/uiShader.cpp | 30 +++++++++++++++++++++++------- gfx/gl/uiShader.h | 6 ++++-- 3 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 gfx/gl/shaders/uiShaderFont.fs (limited to 'gfx') diff --git a/gfx/gl/shaders/uiShaderFont.fs b/gfx/gl/shaders/uiShaderFont.fs new file mode 100644 index 0000000..bee455d --- /dev/null +++ b/gfx/gl/shaders/uiShaderFont.fs @@ -0,0 +1,12 @@ +#version 130 + +in vec2 texCoord0; + +uniform sampler2D sampler; +uniform vec3 colour; + +void +main() +{ + gl_FragColor = vec4(colour, texture(sampler, texCoord0).r); +} diff --git a/gfx/gl/uiShader.cpp b/gfx/gl/uiShader.cpp index 1c2a0e1..fbcdd15 100644 --- a/gfx/gl/uiShader.cpp +++ b/gfx/gl/uiShader.cpp @@ -2,20 +2,36 @@ #include #include #include +#include #include #include #include +#include -UIShader::UIShader(size_t width, size_t height) : program {uiShader_vs.compile(), uiShader_fs.compile()} +UIShader::UIShader(size_t width, size_t height) : + progDefault {uiShader_vs.compile(), uiShader_fs.compile()}, progText {uiShader_vs.compile(), + uiShaderFont_fs.compile()} { - if (auto loc = glGetUniformLocation(program.m_program, "uiProjection"); loc >= 0) { - glUseProgram(program.m_program); - const auto uiProjection = glm::ortho(0, static_cast(width), 0, static_cast(height)); - glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(uiProjection)); + for (const auto prog : {&progDefault, &progText}) { + if (auto loc = glGetUniformLocation(prog->m_program, "uiProjection"); loc >= 0) { + glUseProgram(prog->m_program); + const auto uiProjection = glm::ortho(0, static_cast(width), 0, static_cast(height)); + glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(uiProjection)); + } } } + +void +UIShader::useDefault() const +{ + glUseProgram(progDefault.m_program); +} + void -UIShader::use() const +UIShader::useText(glm::vec3 colour) const { - glUseProgram(program.m_program); + glUseProgram(progText.m_program); + if (auto loc = glGetUniformLocation(progText.m_program, "colour"); loc >= 0) { + glUniform3fv(loc, 1, glm::value_ptr(colour)); + } } diff --git a/gfx/gl/uiShader.h b/gfx/gl/uiShader.h index 45ccc23..502ba13 100644 --- a/gfx/gl/uiShader.h +++ b/gfx/gl/uiShader.h @@ -4,17 +4,19 @@ #include "programHandle.h" #include #include +#include class UIShader { public: UIShader(std::size_t width, std::size_t height); - void use() const; + void useDefault() const; + void useText(glm::vec3) const; private: class UIProgramHandle : public ProgramHandleBase { using ProgramHandleBase::ProgramHandleBase; }; - UIProgramHandle program; + UIProgramHandle progDefault, progText; }; #endif -- cgit v1.2.3