All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BatchedIconRenderableCache.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "BatchedIconAppearance.h"
7 #include "BatchedIconRenderable.h"
8 #include "SortKey.h"
9 #include "IBatchedIconRenderableFactory.h"
10 #include <map>
11 #include <vector>
12 
13 namespace Eegeo
14 {
15  namespace Icons
16  {
17  template <typename TBatchedIconRenderable, typename TBatchedIconRenderableFactory>
19  {
20  public:
22  typedef TBatchedIconRenderableFactory BatchedIconRenderableFactoryType;
25  private:
26 
27  typedef std::map<Rendering::SortKey, BatchedIconRenderableType*> TSortKeyToIconRenderable;
28 
29  public:
30 
31  typedef typename TSortKeyToIconRenderable::const_iterator IconRenderableIterator;
32 
34  TBatchedIconRenderableFactory& batchedIconRenderableFactory,
35  VertexBufferType& vertexBuffer)
36  : m_batchedIconRenderableFactory(batchedIconRenderableFactory)
37  , m_vertexBuffer(vertexBuffer)
38  {}
39 
41  {
42  for (typename TSortKeyToIconRenderable::const_iterator iter = m_sortKeyToIconRenderable.begin(); iter != m_sortKeyToIconRenderable.end(); ++iter)
43  {
44  BatchedIconRenderable* pIconRenderable = iter->second;
45  Eegeo_DELETE pIconRenderable;
46  }
47  }
48 
49  void SetupCurrentIconSet(const std::vector<Rendering::Materials::IMaterial*>& materialSet, Rendering::LayerIds::Values layerId, int subLayer)
50  {
51  m_iconRenderables.clear();
52  m_iconRenderables.reserve(materialSet.size());
53 
54  for (std::vector<Rendering::Materials::IMaterial*>::const_iterator materialIter = materialSet.begin(); materialIter != materialSet.end(); ++materialIter)
55  {
56  Rendering::Materials::IMaterial& material = **materialIter;
57 
58  BatchedIconRenderableType* pBatchedTextRenderable = GetRenderableForMaterial(material, layerId, subLayer, NULL);
59  m_iconRenderables.push_back(pBatchedTextRenderable);
60  }
61  }
62 
63  void ClearCurrentIconSet()
64  {
65  m_iconRenderables.clear();
66  }
67 
68  typename BatchedIconRenderableType::VertexListType& GetVertexList(int texturePage) const
69  {
70  return m_iconRenderables[texturePage]->GetVertexList();
71  }
72 
73  void UpdateIconAppearanceOnCurrentRenderables(const BatchedIconAppearance& batchedAppearance)
74  {
75  UpdateAppearanceOnRenderables(batchedAppearance, m_iconRenderables);
76  }
77 
78 
79  IconRenderableIterator RenderablesBegin() const { return m_sortKeyToIconRenderable.begin(); }
80  IconRenderableIterator RenderablesEnd() const { return m_sortKeyToIconRenderable.end(); }
81 
82  void Finalize()
83  {
84  m_vertexBuffer.Reset();
85 
86  int totalVertexCount = 0;
87  for (IconRenderableIterator iter = RenderablesBegin(); iter != RenderablesEnd(); ++iter)
88  {
89  BatchedIconRenderableType& renderable = *(iter->second);
90  renderable.Finalize();
91 
92  if (renderable.IsPrimary())
93  {
94  totalVertexCount += renderable.GetVertexList().size();
95  }
96  }
97 
98  m_vertexBuffer.Reserve(totalVertexCount);
99  }
100 
101  void Upload()
102  {
103  m_vertexBuffer.Bind();
104 
105  for (IconRenderableIterator iter = RenderablesBegin(); iter != RenderablesEnd(); ++iter)
106  {
107  BatchedIconRenderableType& renderable = *(iter->second);
108  renderable.Upload();
109  }
110 
111  m_vertexBuffer.Unbind();
112  }
113 
114  private:
115 
116 
117  void UpdateAppearanceOnRenderables(const BatchedIconAppearance& batchedAppearance, typename std::vector<TBatchedIconRenderable*>& renderables)
118  {
119  for (typename std::vector<TBatchedIconRenderable*>::const_iterator iter = renderables.begin(); iter != renderables.end(); ++iter)
120  {
121  BatchedIconRenderable* pRenderable = *iter;
122  pRenderable->SetAppearance(batchedAppearance);
123  }
124  }
125 
126 
127  BatchedIconRenderableType* GetRenderableForMaterial(Rendering::Materials::IMaterial& material, Rendering::LayerIds::Values layerId, int subLayer, BatchedIconRenderableType* pPrimaryRenderable)
128  {
129  const Rendering::SortKey& sortKey = BatchedIconRenderable::MakeSortKey(material, layerId, subLayer);
130 
131  typename TSortKeyToIconRenderable::const_iterator iter = m_sortKeyToIconRenderable.find(sortKey);
132 
133  if (iter == m_sortKeyToIconRenderable.end())
134  {
135  m_sortKeyToIconRenderable.insert(std::make_pair(sortKey, m_batchedIconRenderableFactory.Create(material, sortKey, pPrimaryRenderable)));
136  iter = m_sortKeyToIconRenderable.find(sortKey);
137  }
138 
139  BatchedIconRenderableType* pBatchedIconRenderable = iter->second;
140  Eegeo_ASSERT(pBatchedIconRenderable->GetLayer() == layerId);
141  Eegeo_ASSERT(pBatchedIconRenderable->GetUserDefined() == subLayer);
142  return pBatchedIconRenderable;
143  }
144 
145 
146  TBatchedIconRenderableFactory& m_batchedIconRenderableFactory;
147  VertexBufferType& m_vertexBuffer;
148 
149  std::vector<TBatchedIconRenderable*> m_iconRenderables;
150 
151  TSortKeyToIconRenderable m_sortKeyToIconRenderable;
152 
153 
154  };
155  }
156 }