SuperTuxKart
stk_text_billboard.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 // Copyright (C) 2014-2015 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 STK_TEXT_BILLBOARD_HPP
19 #define STK_TEXT_BILLBOARD_HPP
20 
21 #include "font/font_with_face.hpp"
22 #include "graphics/gl_headers.hpp"
23 #include "graphics/sp/sp_instanced_data.hpp"
24 #include "utils/no_copy.hpp"
25 
26 #include <ISceneNode.h>
27 #include <array>
28 #include <unordered_map>
29 #include <vector>
30 
31 using namespace irr;
32 using namespace scene;
33 
34 namespace irr
35 {
36  namespace scene
37  {
38  class IMeshSceneNode;
39  class IMeshBuffer;
40  }
41 }
42 
43 class STKTextBillboard : public ISceneNode, public NoCopy,
45 {
46 public:
47  struct GLTB
48  {
49  core::vector3df m_position;
50  video::SColor m_color;
51  short m_uv[2];
52  };
53 
54 private:
56  {
57  video::ITexture* m_texture;
58 
59  core::rect<float> m_dest_rect;
60 
61  core::rect<s32> m_source_rect;
62 
63  // ------------------------------------------------------------------------
64  STKTextBillboardChar(video::ITexture* texture,
65  const core::rect<float>& dest_rect,
66  const core::rect<irr::s32>& source_rect,
67  const video::SColor* const colors)
68  {
69  m_texture = texture;
70  m_dest_rect = dest_rect;
71  m_source_rect = source_rect;
72  }
73  };
74 
75  SP::SPInstancedData m_instanced_data;
76 
77  GLuint m_instanced_array = 0;
78 
79  std::vector<STKTextBillboardChar>* m_chars = NULL;
80 
81  video::SColor m_color_top;
82 
83  video::SColor m_color_bottom;
84 
85  std::unordered_map<video::ITexture*, std::vector<std::array<GLTB, 4> > >
86  m_gl_tbs;
87 
88  std::unordered_map<video::ITexture*, std::pair<GLuint, GLuint> >
89  m_vao_vbos;
90 
91  std::unordered_map<video::ITexture*, scene::IMeshBuffer*> m_gl_mb;
92 
93  core::aabbox3df m_bbox;
94 
95  FontWithFace* m_face;
96 
97  core::stringw m_text;
98 
99  IMeshSceneNode* m_ge_node;
100 
101  // ------------------------------------------------------------------------
102  float getDefaultScale(FontWithFace* face);
103  // ------------------------------------------------------------------------
104  void removeGENode();
105 public:
106  // ------------------------------------------------------------------------
107  STKTextBillboard(const video::SColor& color_top,
108  const video::SColor& color_bottom, ISceneNode* parent,
109  ISceneManager* mgr, s32 id,
110  const core::vector3df& position,
111  const core::vector3df& scale = core::vector3df(1, 1, 1));
112  // ------------------------------------------------------------------------
114  {
115  removeGENode();
116  clearBuffer();
117  }
118  // ------------------------------------------------------------------------
119  void clearBuffer();
120  // ------------------------------------------------------------------------
121  void reload();
122  // ------------------------------------------------------------------------
123  virtual void collectChar(video::ITexture* texture,
124  const core::rect<float>& dest_rect,
125  const core::rect<irr::s32>& source_rect,
126  const video::SColor* const colors);
127  // ------------------------------------------------------------------------
128  virtual void updateAbsolutePosition();
129  // ------------------------------------------------------------------------
130  virtual void OnRegisterSceneNode();
131  // ------------------------------------------------------------------------
132  virtual void render();
133  // ------------------------------------------------------------------------
134  virtual const core::aabbox3df& getBoundingBox() const { return m_bbox; }
135  // ------------------------------------------------------------------------
136  void init(const core::stringw& text, FontWithFace* face);
137  // ------------------------------------------------------------------------
138  void initLegacy(const core::stringw& text, FontWithFace* face);
139  // ------------------------------------------------------------------------
140  void draw(video::ITexture* tex) const
141  {
142 #ifndef SERVER_ONLY
143  glBindVertexArray(m_vao_vbos.at(tex).first);
144  for (unsigned i = 0; i < m_gl_tbs.at(tex).size(); i++)
145  {
146  glDrawArraysInstanced(GL_TRIANGLE_STRIP, i * 4, 4, 1);
147  }
148 #endif
149  }
150  // ------------------------------------------------------------------------
151  std::vector<video::ITexture*> getAllTBTextures() const
152  {
153  std::vector<video::ITexture*> ret;
154  for (auto& p : m_vao_vbos)
155  {
156  ret.push_back(p.first);
157  }
158  return ret;
159  }
160  // ------------------------------------------------------------------------
161  void updateGLInstanceData() const
162  {
163 #ifndef SERVER_ONLY
164  glBindBuffer(GL_ARRAY_BUFFER, m_instanced_array);
165  glBufferSubData(GL_ARRAY_BUFFER, 0, 36, m_instanced_data.getData());
166  glBindBuffer(GL_ARRAY_BUFFER, 0);
167 #endif
168  }
169  // ------------------------------------------------------------------------
170  static void updateAllTextBillboards();
171 };
172 
173 #endif
A class for STKTextBillboard to get font info to render billboard text.
Definition: font_with_face.hpp:77
An abstract class which contains functions which convert vector fonts into bitmap and render them in ...
Definition: font_with_face.hpp:72
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Definition: sp_instanced_data.hpp:32
Definition: stk_text_billboard.hpp:45
Definition: stk_text_billboard.hpp:48
Definition: stk_text_billboard.hpp:56