All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
VertexLayout.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "VertexLayoutElement.h"
7 #include "VertexSemantics.h"
8 #include <vector>
9 #include <algorithm>
10 
11 namespace Eegeo
12 {
13  namespace Rendering
14  {
15  namespace VertexLayouts
16  {
17  namespace
18  {
19  bool VertexLayoutElementComparator(VertexLayoutElement a, VertexLayoutElement b)
20  {
21  return a.GetSemanticId() < b.GetSemanticId();
22  }
23  }
24 
28  class VertexLayout : protected Eegeo::NonCopyable
29  {
30  public:
31  VertexLayout(int vertexSize)
32  : m_vertexSize(vertexSize)
33  , m_semanticFlags(VertexSemanticFlags::None)
34  {
35  }
36 
37  int GetVertexSize() const
38  {
39  return m_vertexSize;
40  }
41 
42  void AddElement(const VertexLayoutElement& element)
43  {
44  Eegeo_ASSERT(!ContainsSemantic(element.GetSemanticId()), "Attempt to add the same semantic to VertexAttribs twice.");
45 
46  m_elements.push_back(element);
47  m_semanticFlags = VertexSemanticFlags::SetFlagForSemantic(m_semanticFlags, element.GetSemanticId());
48 
49  std::sort(m_elements.begin(), m_elements.end(), VertexLayoutElementComparator);
50  }
51 
52  int GetNumOfElements() const
53  {
54  return static_cast<int>(m_elements.size());
55  }
56 
57  VertexSemanticFlags::FlagValues GetSemanticFlags() const
58  {
59  return m_semanticFlags;
60  }
61 
62  const VertexLayoutElement& GetElement(size_t elementIndex)
63  {
64  Eegeo_ASSERT(elementIndex < m_elements.size(), "Requested element index is out of range.");
65  return m_elements.at(elementIndex);
66  }
67 
68  bool ContainsSemantic(const VertexSemanticId::IdValues semantic) const
69  {
70  return VertexSemanticFlags::FlagsContainSemantic(m_semanticFlags, semantic);
71  }
72 
73  const VertexLayoutElement& GetElementBySemantic(const VertexSemanticId::IdValues semantic) const
74  {
75  for (std::vector<VertexLayoutElement>::const_iterator iter = m_elements.begin();
76  iter != m_elements.end();
77  iter++)
78  {
79  const VertexLayoutElement& element = (*iter);
80  if (element.GetSemanticId() == semantic)
81  {
82  return element;
83  }
84  }
85 
86  Eegeo_ASSERT(false, "No element found for semantic");
87  return *m_elements.end();
88  }
89 
90  private:
91  int m_vertexSize;
92  VertexSemanticFlags::FlagValues m_semanticFlags;
93 
94  // Probs need to keep a bitfield of used semantics here.
95  std::vector<VertexLayoutElement> m_elements;
96  };
97  }
98  }
99 }