All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
VertexBufferBindingHelpers.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Graphics.h"
6 #include "IdTypes.h"
7 #include "VertexBinding.h"
8 #include "VAOCache.h"
9 #include "Rendering.h"
10 
11 #ifdef EEGEO_IOS
12  #define BindVertexArray(va) Eegeo_GL(glBindVertexArrayOES(va))
13 #elif defined(EEGEO_OSX)
14  #define BindVertexArray(va) Eegeo_GL(glBindVertexArrayAPPLE(va))
15 #else
16  #define BindVertexArray(va)
17 #endif
18 
19 namespace Eegeo
20 {
21  namespace Rendering
22  {
23  namespace VertexBufferBindingHelpers
24  {
25  inline void UnbindVertexBuffers()
26  {
27 #ifdef EEGEO_IOS
28  Eegeo_GL(glBindVertexArrayOES(0));
29 #else
30  Eegeo_GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
31  Eegeo_GL(glBindBuffer(GL_ARRAY_BUFFER, 0));
32 #endif
33  }
34 
35  inline void BindVertexBuffersImmediate(const TVertexBufferId vertexBuffer,
36  const TIndexBufferId indexBuffer,
37  const VertexLayouts::VertexBinding* pVertexBinding,
38  Rendering::GLState& glState)
39  {
40  Eegeo_ASSERT(pVertexBinding != NULL);
41 
42  Eegeo_GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer));
43  Eegeo_GL(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer));
44 
45  pVertexBinding->BindVertices();
46  }
47 
48  inline void BindVertexBuffersImmediate(const TVertexBufferId vertexBuffer,
49  const VertexLayouts::VertexBinding* pVertexBinding,
50  Rendering::GLState& glState)
51  {
52  Eegeo_ASSERT(pVertexBinding != NULL);
53 
54  Eegeo_GL(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer));
55  Eegeo_GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
56 
57  pVertexBinding->BindVertices();
58  }
59 
60 #if defined(EEGEO_IOS)
61  inline void BindVertexBuffersVAO(const TVertexBufferId vertexBuffer,
62  const TIndexBufferId indexBuffer,
63  const VertexLayouts::VertexBinding* pVertexBinding,
64  Rendering::GLState& glState,
65  VAOCache& vaoCache)
66  {
67  GLuint vertexArrayObject;
68 
69  if(!vaoCache.TryGetVAOForVertexBinding(pVertexBinding, &vertexArrayObject))
70  {
71  vertexArrayObject = vaoCache.CreateVAOForVertexBinding(pVertexBinding);
72 
73  BindVertexArray(vertexArrayObject);
74 
75  BindVertexBuffersImmediate(vertexBuffer, indexBuffer, pVertexBinding, glState);
76  Eegeo_GL(glBindBuffer(GL_ARRAY_BUFFER, 0));
77 
78  BindVertexArray(0);
79  }
80  BindVertexArray(vertexArrayObject);
81  }
82 
83  inline void BindVertexBuffersVAO(const TVertexBufferId vertexBuffer,
84  const VertexLayouts::VertexBinding* pVertexBinding,
85  Rendering::GLState& glState,
86  VAOCache& vaoCache)
87  {
88  GLuint vertexArrayObject;
89 
90  if(!vaoCache.TryGetVAOForVertexBinding(pVertexBinding, &vertexArrayObject))
91  {
92  vertexArrayObject = vaoCache.CreateVAOForVertexBinding(pVertexBinding);
93 
94  BindVertexArray(vertexArrayObject);
95 
96  BindVertexBuffersImmediate(vertexBuffer, pVertexBinding, glState);
97  Eegeo_GL(glBindBuffer(GL_ARRAY_BUFFER, 0));
98 
99  BindVertexArray(0);
100  }
101  BindVertexArray(vertexArrayObject);
102  }
103 #endif
104  }
105  }
106 }