All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BatchedTextRenderable.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "RenderableBase.h"
6 #include "Rendering.h"
7 #include "Fonts.h"
8 #include "AllVertexTypes.h"
9 #include "BatchedTextRangeBuilder.h"
10 #include "GlBuffer.h"
11 
12 #include <deque>
13 #include <vector>
14 
15 namespace Eegeo
16 {
17  namespace Text
18  {
20  {
22 
23  const Text::BatchedTextAppearance* pTextAppearance;
24  bool IsHaloLayer;
25  bool IsStencilMaskPass;
26  bool NeedsShaderSetup;
27  };
28 
29 
31  {
32  public:
33  static Rendering::SortKey MakeSortKeyForLayer(Rendering::LayerIds::Values layerId, int subLayer, bool isHaloLayer);
34 
35  static Rendering::SortKey MakeSortKey(const Rendering::Materials::IMaterial& material, const Rendering::SortKey& sortKeyForLayer);
36 
38  const Rendering::VertexLayouts::VertexBinding* pVertexBinding,
39  const Rendering::SortKey& sortKey,
40  int vertexStrideBytes,
41  const GlBuffer<u16>& quadIndexBuffer
42  );
43 
45 
46  virtual void SetTextAppearance(const Text::BatchedTextAppearance& textAppearance);
47 
48  virtual const Text::BatchedTextAppearance& GetTextAppearanceForCurrentRange() const;
49 
50  virtual bool ShouldRender() const
51  {
52  return GetVertexCount() > 0;
53  }
54 
55  virtual void Finalize();
56 
57  virtual void Upload() = 0;
58 
59  virtual void Reset();
60 
61  bool IsHaloLayer() const
62  {
63  return m_pDrawState->IsHaloLayer;
64  }
65 
66  bool IsStencilMaskPass() const
67  {
68  return m_pDrawState->IsStencilMaskPass;
69  }
70 
71  bool NeedsShaderSetup() const
72  {
73  return m_pDrawState->NeedsShaderSetup;
74  }
75 
76  protected:
77  void Draw(Rendering::GLState& glState, int batchQuadOffset) const;
78 
79  virtual int GetVertexCount() const = 0;
80 
81  virtual const void* GetVertexData(int quadOffset, int quadCount) const = 0;
82 
83  private:
84  void EndRange(int currentIndexCount) const;
85 
86  int GetCurrentQuadCount() const;
87 
88  Text::BatchedTextRangeBuilder* m_pRangeBuilder;
89 
90  typedef std::deque<Text::BatchedTextRange> TAppearanceRanges;
91  TAppearanceRanges* m_pAppearanceRanges;
92  BatchedTextRenderableDrawState* m_pDrawState;
93 
94  const GlBuffer<u16>& m_quadIndexBuffer;
95 
96  };
97 
98  template <typename TVertex>
100  {
101  public:
102  typedef TVertex VertexType;
104  typedef std::vector<VertexType> VertexListType;
106 
108  const Rendering::VertexLayouts::VertexBinding* pVertexBinding,
109  const Rendering::SortKey& sortKey,
110  const TBatchedTextRenderable* pPrimary,
111  const GlBuffer<u16>& quadIndexBuffer,
112  VertexBufferType& vertexBuffer)
113  : BatchedTextRenderable(pMaterial, pVertexBinding, sortKey, static_cast<int>(sizeof(VertexType)), quadIndexBuffer)
114  , m_pPrimary(pPrimary)
115  , m_vertexBuffer(vertexBuffer)
116  , m_pVertexList((pPrimary == NULL) ? Eegeo_NEW(VertexListType)() : NULL)
117  , m_vertexBufferElementOffset(0)
118  {
119  }
120 
122  {
123  Eegeo_DELETE m_pVertexList;
124  }
125 
126  void Upload()
127  {
128  if (IsPrimary())
129  {
130  m_vertexBufferElementOffset = m_vertexBuffer.GetElementOffset();
131  m_vertexBuffer.Upload(*m_pVertexList);
132  }
133  else
134  {
135  m_vertexBufferElementOffset = -1;
136  }
137  }
138 
139  void Reset()
140  {
141  BatchedTextRenderable::Reset();
142  if (m_pVertexList) m_pVertexList->clear();
143  }
144 
145  void Render(Rendering::GLState& glState) const
146  {
147  const int quadOffset = GetVertexBufferElementOffset() / 4;
148 
149  m_vertexBuffer.Bind();
150 
151  BatchedTextRenderable::Draw(glState, quadOffset);
152 
153  m_vertexBuffer.Unbind();
154 
155  if (IsPrimary())
156  {
157  m_pVertexList->clear();
158  }
159  }
160 
161  VertexListType& GetVertexList() const
162  {
163  // Replica delegates to its Primary
164  return IsPrimary()
165  ? *m_pVertexList
166  : m_pPrimary->GetVertexList();
167  }
168 
169  bool IsPrimary() const
170  {
171  return m_pPrimary == NULL;
172  }
173 
174  bool IsReplica() const
175  {
176  return !IsPrimary();
177  }
178 
179  private:
180 
181  int GetVertexBufferElementOffset() const
182  {
183  const int vertexBufferElementOffset = IsPrimary()
184  ? m_vertexBufferElementOffset
185  : m_pPrimary->GetVertexBufferElementOffset();
186 
187  return vertexBufferElementOffset;
188  }
189 
190  virtual int GetVertexCount() const
191  {
192  return static_cast<int>(GetVertexList().size());
193  }
194 
195  virtual const void* GetVertexData(int quadOffset, int quadCount) const
196  {
197  const VertexListType& vertexList = GetVertexList();
198  Eegeo_ASSERT((quadOffset+quadCount)*4 <= vertexList.size());
199 
200  const VertexType* pVertexData = &(vertexList[quadOffset*4]);
201 
202  return static_cast<const void*>(pVertexData);
203  }
204 
205  const TBatchedTextRenderable* m_pPrimary;
206  VertexBufferType& m_vertexBuffer;
207  VertexListType* m_pVertexList;
208  int m_vertexBufferElementOffset;
209 
210  };
211 
212  class BatchedScreenTextRenderable : public TBatchedTextRenderable<Rendering::VertexTypes::ScreenTextVertex>
213  {
214  public:
216  const Rendering::VertexLayouts::VertexBinding* pVertexBinding,
217  const Rendering::SortKey& sortKey,
218  const TBatchedTextRenderable* pPrimary,
219  const GlBuffer<u16>& quadIndexBuffer,
221  )
222  : ThisType::ThisType(pMaterial, pVertexBinding, sortKey, pPrimary, quadIndexBuffer, screenTextVertexBuffer)
223  {
224 
225  }
226  };
227 
228 
229  class BatchedWorldTextRenderable : public TBatchedTextRenderable<Rendering::VertexTypes::WorldTextVertex>
230  {
231  public:
233  const Rendering::VertexLayouts::VertexBinding* pVertexBinding,
234  const Rendering::SortKey& sortKey,
235  const TBatchedTextRenderable* pPrimary,
236  const GlBuffer<u16>& quadIndexBuffer,
238  )
239  : ThisType::ThisType(pMaterial, pVertexBinding, sortKey, pPrimary, quadIndexBuffer, worldTextVertexBuffer)
240  {
241 
242  }
243  };
244  }
245 }