]> git.localhorst.tv Git - blank.git/commitdiff
move font color from texture to uniform
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 27 Jul 2015 14:52:42 +0000 (16:52 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 27 Jul 2015 14:52:42 +0000 (16:52 +0200)
TODO
src/graphics/BlendedSprite.hpp
src/graphics/BlockLighting.hpp
src/graphics/DirectionalLighting.hpp
src/graphics/Font.hpp
src/graphics/Program.hpp
src/graphics/render.cpp
src/graphics/shader.cpp
src/ui/HUD.hpp
src/ui/Interface.hpp
src/ui/ui.cpp

diff --git a/TODO b/TODO
index 8c52ca1a6bf7008bf72f8ed94f819da9d6d862a2..49fac3b69c76fbd38211eccaed92a37cedb85f4e 100644 (file)
--- 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
index 167b0136e322c789bfafd4245ae54790b7b47e60..a15d8a1cb2b41c077c498f92ed86f377defdeeed 100644 (file)
@@ -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;
 
 };
 
index 8f7f01dca1e722cd44b8442cf365dbf7b51b3206..321cf79bdb598bf8b54fb61854c24e39e206c156 100644 (file)
@@ -31,8 +31,6 @@ public:
 private:
        Program program;
 
-       float fog_density;
-
        glm::mat4 projection;
        glm::mat4 view;
        glm::mat4 vp;
index df89053cd7e342df9d6bfcfd8ef6197fa3decb92..e42d2cfa61e64f23b2b5f20e4451879a55b19030 100644 (file)
@@ -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;
index 1d4ff17f619823b14efabe3fa8ac6a1a1452bee6..45ea9284b233bc7d7cb1e2a01666d5708f6b141d 100644 (file)
@@ -59,8 +59,8 @@ public:
 
        glm::tvec2<int> 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;
index a67a0b374eb793681b4502c79ce3b68573e4cc81..ff01f9dafd1e78b17b2b3980264cf603b6f25a04 100644 (file)
@@ -4,6 +4,7 @@
 #include <iosfwd>
 #include <list>
 #include <GL/glew.h>
+#include <glm/glm.hpp>
 
 
 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:
index 7e048408b5091239ca10e35e89a8dfe2fcec4035..1dc9b2be4e47646efe11806581dcff23db58feba 100644 (file)
@@ -107,13 +107,14 @@ glm::tvec2<int> 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());
        }
index de994ce2a170437e07b9a37d647f8da571c85ae4..03b2edc3c774c36705ab5ff0c85073ba0ef55a36 100644 (file)
@@ -13,6 +13,7 @@
 #include <ostream>
 #include <stdexcept>
 #include <string>
+#include <glm/gtc/type_ptr.hpp>
 
 
 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);
 }
 
 }
index 4b60127e685d7eb45462a9b3453b4c43a3251a44..55f3afb806cb56d73a01f27d881a0adb8d76816a 100644 (file)
@@ -39,7 +39,6 @@ private:
        Texture block_label;
        SpriteModel label_sprite;
        glm::mat4 label_transform;
-       SDL_Color label_color;
 
        bool block_visible;
 
index f3b1252643c93f02c8065c347a90ce7229bc3dcb..1e2e8834ba7a507f369661e4f68ca84eba583749 100644 (file)
@@ -9,7 +9,6 @@
 #include "../model/OutlineModel.hpp"
 #include "../world/Block.hpp"
 
-#include <SDL.h>
 #include <glm/glm.hpp>
 
 
@@ -93,7 +92,6 @@ private:
        SpriteModel counter_sprite;
        glm::mat4 counter_transform;
        float counter_x;
-       SDL_Color counter_color;
 
        Config config;
 
index 80fbbcdb5a63e265ff678eef1861e58a23104abf..ee26989a80187c5600dfaab2be12cff71873eeed 100644 (file)
@@ -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(