1 //  SuperTuxKart - a fun racing game with go-kart
2 //  Copyright (C) 2018 SuperTuxKart-Team
3 //
4 //  This program is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU General Public License
6 //  as published by the Free Software Foundation; either version 3
7 //  of the License, or (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 
18 #ifndef SERVER_ONLY
19 
20 #include "graphics/stk_text_billboard.hpp"
21 #include "graphics/texture_shader.hpp"
22 
23 #include <unordered_set>
24 
25 namespace TextBillboardDrawer
26 {
27 // ----------------------------------------------------------------------------
28 std::unordered_map<video::ITexture*, std::vector<STKTextBillboard*> > g_tbs;
29 // ----------------------------------------------------------------------------
30 std::unordered_set<STKTextBillboard*> g_tbs_update;
31 // ============================================================================
32 /** A Shader to render text billboard.
33 */
34 class TBRenderer : public TextureShader<TBRenderer, 1>
35 {
36 public:
TBRenderer()37     TBRenderer()
38     {
39         loadProgram(PARTICLES_RENDERING,
40                     GL_VERTEX_SHADER,   "sp_pass.vert",
41                     GL_FRAGMENT_SHADER, "sp_text_billboard.frag");
42         assignUniforms();
43         assignSamplerNames(0, "font_texture",
44             ST_TRILINEAR_ANISOTROPIC_FILTERED);
45     }   // TBRenderer
46 };   // TBRenderer
47 
48 // ============================================================================
addTextBillboard(STKTextBillboard * tb)49 void addTextBillboard(STKTextBillboard* tb)
50 {
51     g_tbs_update.insert(tb);
52     const auto& tex = tb->getAllTBTextures();
53     for (video::ITexture* t : tex)
54     {
55         g_tbs[t].push_back(tb);
56     }
57 }   // addTextBillboard
58 
59 // ----------------------------------------------------------------------------
drawAll()60 void drawAll()
61 {
62     if (g_tbs.empty())
63     {
64         return;
65     }
66     glEnable(GL_DEPTH_TEST);
67     glDepthMask(GL_TRUE);
68     glEnable(GL_CULL_FACE);
69     glDisable(GL_BLEND);
70     TBRenderer::getInstance()->use();
71     for (auto& p : g_tbs)
72     {
73         TBRenderer::getInstance()
74             ->setTextureUnits(p.first->getOpenGLTextureName());
75         for (auto* q : p.second)
76         {
77             q->draw(p.first);
78         }
79     }
80 }   // drawAll
81 
82 // ----------------------------------------------------------------------------
reset()83 void reset()
84 {
85     g_tbs.clear();
86     g_tbs_update.clear();
87 }   // reset
88 
89 // ----------------------------------------------------------------------------
updateAll()90 void updateAll()
91 {
92     for (STKTextBillboard* tb : g_tbs_update)
93     {
94         tb->updateGLInstanceData();
95     }
96 }   // updateAll
97 
98 }
99 
100 #endif
101