From: Daniel Karbach Date: Mon, 27 Jul 2015 14:52:42 +0000 (+0200) Subject: move font color from texture to uniform X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=1bc6f085c53cdeaa08e2c00e821d4e2e25cae1c8;p=blank.git move font color from texture to uniform --- diff --git a/TODO b/TODO index 8c52ca1..49fac3b 100644 --- a/TODO +++ b/TODO @@ -12,10 +12,11 @@ textures do I need to say anything? :) -font colours +font rendering - set font fg and bg colour as either uniform or vertex attribute - and lerp between them based on the texture's alpha component + should combine all that's needed to render a text into some struct + also, with background nw being a thing, a padding might be nice + or could separate bg from fg rendering it may also be feasible to get rid of SDL_ttf and use freetype directly to eliminate the unneccessary surface creation diff --git a/src/graphics/BlendedSprite.hpp b/src/graphics/BlendedSprite.hpp index 167b013..a15d8a1 100644 --- a/src/graphics/BlendedSprite.hpp +++ b/src/graphics/BlendedSprite.hpp @@ -25,6 +25,8 @@ public: void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept; void SetTexture(Texture &) noexcept; + void SetFG(const glm::vec4 &) noexcept; + void SetBG(const glm::vec4 &) noexcept; const glm::mat4 &Projection() const noexcept { return projection; } const glm::mat4 &View() const noexcept { return view; } @@ -39,6 +41,8 @@ private: GLuint mvp_handle; GLuint sampler_handle; + GLuint fg_handle; + GLuint bg_handle; }; diff --git a/src/graphics/BlockLighting.hpp b/src/graphics/BlockLighting.hpp index 8f7f01d..321cf79 100644 --- a/src/graphics/BlockLighting.hpp +++ b/src/graphics/BlockLighting.hpp @@ -31,8 +31,6 @@ public: private: Program program; - float fog_density; - glm::mat4 projection; glm::mat4 view; glm::mat4 vp; diff --git a/src/graphics/DirectionalLighting.hpp b/src/graphics/DirectionalLighting.hpp index df89053..e42d2cf 100644 --- a/src/graphics/DirectionalLighting.hpp +++ b/src/graphics/DirectionalLighting.hpp @@ -17,6 +17,7 @@ public: void Activate() noexcept; void SetLightDirection(const glm::vec3 &) noexcept; + void SetLightColor(const glm::vec3 &) noexcept; void SetFogDensity(float) noexcept; @@ -33,11 +34,6 @@ public: private: Program program; - glm::vec3 light_direction; - glm::vec3 light_color; - - float fog_density; - glm::mat4 projection; glm::mat4 view; glm::mat4 vp; diff --git a/src/graphics/Font.hpp b/src/graphics/Font.hpp index 1d4ff17..45ea928 100644 --- a/src/graphics/Font.hpp +++ b/src/graphics/Font.hpp @@ -59,8 +59,8 @@ public: glm::tvec2 TextSize(const char *) const; - Texture Render(const char *, SDL_Color) const; - void Render(const char *, SDL_Color, Texture &) const; + Texture Render(const char *) const; + void Render(const char *, Texture &) const; private: TTF_Font *handle; diff --git a/src/graphics/Program.hpp b/src/graphics/Program.hpp index a67a0b3..ff01f9d 100644 --- a/src/graphics/Program.hpp +++ b/src/graphics/Program.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace blank { @@ -28,6 +29,12 @@ public: GLint AttributeLocation(const GLchar *name) const noexcept; GLint UniformLocation(const GLchar *name) const noexcept; + void Uniform(GLint, GLint) noexcept; + void Uniform(GLint, float) noexcept; + void Uniform(GLint, const glm::vec3 &) noexcept; + void Uniform(GLint, const glm::vec4 &) noexcept; + void Uniform(GLint, const glm::mat4 &) noexcept; + void Use() const noexcept { glUseProgram(handle); } private: diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index 7e04840..1dc9b2b 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -107,13 +107,14 @@ glm::tvec2 Font::TextSize(const char *text) const { return size; } -Texture Font::Render(const char *text, SDL_Color color) const { +Texture Font::Render(const char *text) const { Texture tex; + Render(text, tex); return tex; } -void Font::Render(const char *text, SDL_Color color, Texture &tex) const { - SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, color); +void Font::Render(const char *text, Texture &tex) const { + SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, { 0xFF, 0xFF, 0xFF, 0xFF }); if (!srf) { throw std::runtime_error(TTF_GetError()); } diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index de994ce..03b2edc 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace { @@ -146,10 +147,29 @@ GLint Program::UniformLocation(const GLchar *name) const noexcept { } +void Program::Uniform(GLint loc, GLint val) noexcept { + glUniform1i(loc, val); +} + +void Program::Uniform(GLint loc, float val) noexcept { + glUniform1f(loc, val); +} + +void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept { + glUniform3fv(loc, 1, glm::value_ptr(val)); +} + +void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept { + glUniform4fv(loc, 1, glm::value_ptr(val)); +} + +void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept { + glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val)); +} + + DirectionalLighting::DirectionalLighting() : program() -, light_direction(1.0f, 3.0f, 2.0f) -, light_color(0.9f, 0.9f, 0.9f) , vp(1.0f) , m_handle(0) , mv_handle(0) @@ -211,32 +231,34 @@ DirectionalLighting::DirectionalLighting() light_direction_handle = program.UniformLocation("light_direction"); light_color_handle = program.UniformLocation("light_color"); fog_density_handle = program.UniformLocation("fog_density"); + + Activate(); + program.Uniform(light_direction_handle, glm::vec3(1.0f, 3.0f, 2.0f)); + program.Uniform(light_color_handle, glm::vec3(1.0f)); + program.Uniform(fog_density_handle, 0.0f); } void DirectionalLighting::Activate() noexcept { program.Use(); - - glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z); - glUniform3f(light_color_handle, light_color.x, light_color.y, light_color.z); } void DirectionalLighting::SetM(const glm::mat4 &m) noexcept { - glm::mat4 mv(view * m); - glm::mat4 mvp(vp * m); - glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]); - glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]); - glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); + program.Uniform(m_handle, m); + program.Uniform(mv_handle, view * m); + program.Uniform(mvp_handle, vp * m); } void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept { - light_direction = -dir; - glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z); + program.Uniform(light_direction_handle, -dir); +} + +void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept { + program.Uniform(light_color_handle, col); } void DirectionalLighting::SetFogDensity(float f) noexcept { - fog_density = f; - glUniform1f(fog_density_handle, fog_density); + program.Uniform(fog_density_handle, f); } void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept { @@ -321,15 +343,12 @@ void BlockLighting::Activate() noexcept { } void BlockLighting::SetM(const glm::mat4 &m) noexcept { - glm::mat4 mv(view * m); - glm::mat4 mvp(vp * m); - glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]); - glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); + program.Uniform(mv_handle, view * m); + program.Uniform(mvp_handle, vp * m); } void BlockLighting::SetFogDensity(float f) noexcept { - fog_density = f; - glUniform1f(fog_density_handle, fog_density); + program.Uniform(fog_density_handle, f); } void BlockLighting::SetProjection(const glm::mat4 &p) noexcept { @@ -376,9 +395,14 @@ BlendedSprite::BlendedSprite() "#version 330 core\n" "in vec2 frag_tex_uv;\n" "uniform sampler2D tex_sampler;\n" + "uniform vec4 fg_factor;\n" + "uniform vec4 bg_factor;\n" "out vec4 color;\n" "void main() {\n" - "color = texture(tex_sampler, frag_tex_uv);\n" + "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n" + "vec4 factor = mix(bg_factor, fg_factor, tex_color.a);\n" + "color = tex_color * factor;\n" + "color.a = factor.a;\n" "}\n" ); program.Link(); @@ -389,6 +413,12 @@ BlendedSprite::BlendedSprite() mvp_handle = program.UniformLocation("MVP"); sampler_handle = program.UniformLocation("tex_sampler"); + fg_handle = program.UniformLocation("fg_factor"); + bg_handle = program.UniformLocation("bg_factor"); + + Activate(); + SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); + SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f)); } @@ -397,8 +427,7 @@ void BlendedSprite::Activate() noexcept { } void BlendedSprite::SetM(const glm::mat4 &m) noexcept { - glm::mat4 mvp(vp * m); - glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); + program.Uniform(mvp_handle, vp * m); } void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept { @@ -425,7 +454,15 @@ void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::ma void BlendedSprite::SetTexture(Texture &tex) noexcept { glActiveTexture(GL_TEXTURE0); tex.Bind(); - glUniform1i(sampler_handle, 0); + program.Uniform(sampler_handle, GLint(0)); +} + +void BlendedSprite::SetFG(const glm::vec4 &v) noexcept { + program.Uniform(fg_handle, v); +} + +void BlendedSprite::SetBG(const glm::vec4 &v) noexcept { + program.Uniform(bg_handle, v); } } diff --git a/src/ui/HUD.hpp b/src/ui/HUD.hpp index 4b60127..55f3afb 100644 --- a/src/ui/HUD.hpp +++ b/src/ui/HUD.hpp @@ -39,7 +39,6 @@ private: Texture block_label; SpriteModel label_sprite; glm::mat4 label_transform; - SDL_Color label_color; bool block_visible; diff --git a/src/ui/Interface.hpp b/src/ui/Interface.hpp index f3b1252..1e2e883 100644 --- a/src/ui/Interface.hpp +++ b/src/ui/Interface.hpp @@ -9,7 +9,6 @@ #include "../model/OutlineModel.hpp" #include "../world/Block.hpp" -#include #include @@ -93,7 +92,6 @@ private: SpriteModel counter_sprite; glm::mat4 counter_transform; float counter_x; - SDL_Color counter_color; Config config; diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 80fbbcd..ee26989 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -28,7 +28,6 @@ HUD::HUD(const BlockTypeRegistry &types, const Font &font) , block_label() , label_sprite() , label_transform(1.0f) -, label_color{0xFF, 0xFF, 0xFF, 0xFF} , block_visible(false) , crosshair() { block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f)); @@ -55,7 +54,7 @@ void HUD::Display(const Block &b) { type.FillModel(block_buf, b.Transform()); block.Update(block_buf); - font.Render(type.label.c_str(), label_color, block_label); + font.Render(type.label.c_str(), block_label); glm::vec2 size(font.TextSize(type.label.c_str())); label_sprite.LoadRect(size.x, size.y); label_transform = glm::translate(glm::vec3( @@ -88,6 +87,8 @@ void HUD::Render(Viewport &viewport) noexcept { BlendedSprite &sprite_prog = viewport.SpriteProgram(); sprite_prog.SetM(label_transform); sprite_prog.SetTexture(block_label); + sprite_prog.SetFG(glm::vec4(1.0f)); + sprite_prog.SetBG(glm::vec4(0.5f)); label_sprite.Draw(); } } @@ -114,7 +115,6 @@ Interface::Interface( , counter_sprite() , counter_transform(1.0f) , counter_x(935.0f) -, counter_color{0xFF, 0xFF, 0xFF, 0xFF} , config(config) , place_timer(256) , remove_timer(256) @@ -295,7 +295,7 @@ void Interface::UpdateCounter() { std::stringstream s; s << std::setprecision(3) << counter.AvgRunning() << "ms"; std::string text = s.str(); - font.Render(text.c_str(), counter_color, counter_tex); + font.Render(text.c_str(), counter_tex); glm::vec2 size(font.TextSize(text.c_str())); counter_sprite.LoadRect(size.x, size.y); counter_transform = glm::translate(glm::vec3(