From 6dc5c41fe93ed0cb4f9051387b5551182bc5df41 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 29 Mar 2021 01:28:06 +0300 Subject: [PATCH] Make Text more flexible about which texture slot it uses Different material types have different names for their textures so using a fixed name here does not cut it anymore. --- source/render/text.cpp | 32 ++++++++++++++++++++++++++------ source/render/text.h | 9 +++++---- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/source/render/text.cpp b/source/render/text.cpp index b408bbe1..639beac1 100644 --- a/source/render/text.cpp +++ b/source/render/text.cpp @@ -1,4 +1,5 @@ #include "meshbuilder.h" +#include "program.h" #include "text.h" #include "texture2d.h" @@ -7,7 +8,7 @@ using namespace std; namespace Msp { namespace GL { -Text::Text(const Font &f, const Technique *tech): +Text::Text(const Font &f, const Technique *tech, const string &tex_slot): font(f), mesh((TEXCOORD2, VERTEX2)), horz_align(0.0f), @@ -16,16 +17,35 @@ Text::Text(const Font &f, const Technique *tech): { object.set_mesh(&mesh); if(tech) - set_technique(tech); + set_technique(tech, tex_slot); } -void Text::set_technique(const Technique *tech) +void Text::set_technique(const Technique *tech, const string &ts) { if(tech) { - technique = *tech; - technique.replace_texture("diffuse_map", font.get_texture()); - object.set_technique(&technique); + const char *tex_slot = ts.c_str(); + if(!*tex_slot && tech->has_pass(Tag())) + if(const Program *shprog = tech->get_pass(Tag()).get_shader_program()) + { + if(shprog->get_uniform_location("font_tex")>=0) + tex_slot = "font_tex"; + else if(shprog->get_uniform_location("color_tex")>=0) + tex_slot = "color_tex"; + else if(shprog->get_uniform_location("diffuse_map")>=0) + tex_slot = "diffuse_map"; + else if(shprog->get_uniform_location("base_color_map")>=0) + tex_slot = "base_color_map"; + } + + if(tex_slot) + { + technique = *tech; + technique.replace_texture(tex_slot, font.get_texture()); + object.set_technique(&technique); + } + else + object.set_technique(tech); } else object.set_technique(0); diff --git a/source/render/text.h b/source/render/text.h index 40c9b850..cbcef957 100644 --- a/source/render/text.h +++ b/source/render/text.h @@ -41,13 +41,14 @@ private: float width; public: - Text(const Font &, const Technique * = 0); + Text(const Font &, const Technique * = 0, const std::string & = std::string()); const Mesh *get_mesh() const { return &mesh; } - /** Sets technique to render with. It should have a texture slot named - "diffuse_map", which will be replaced with the font's texture. */ - void set_technique(const Technique *); + /** Sets technique to render with, replacing the given texture slot with + the font texture. If no texture slot is specified, heuristics are used to + choose a suitable one. */ + void set_technique(const Technique *, const std::string & = std::string()); const Technique *get_technique() const { return object.get_technique(); } -- 2.45.2