Allolib  1.0
C++ Components For Interactive Multimedia
al_EasyVAO.hpp
1 #ifndef INCLUDE_AL_EASYVAO_HPP
2 #define INCLUDE_AL_EASYVAO_HPP
3 
4 /* VAO wrapper ready to receive mesh data
5 
6  - wraps a vao for render call and vertex buffer handles for attributes
7  - This class does not store cpu side data
8  - Any user data format could be used
9  as long as they are stored as contiguous memory
10 
11  Keehong Youn, 2017, younkeehong@gmail.com
12 */
13 
14 #include "al/graphics/al_BufferObject.hpp"
15 #include "al/graphics/al_VAO.hpp"
16 
17 namespace al {
18 
23 struct EasyVAO : VAO {
24  // attribute locations specified in default shader
25  // layout (location = n) in vertex shader
26  enum AttribLayout : unsigned int {
27  LAYOUT_POSITION = 0,
28  LAYOUT_COLOR = 1,
29  LAYOUT_TEXCOORD = 2,
30  LAYOUT_NORMAL = 3,
31  };
32 
33  // attribute type specified in default shader
34  // 3 -> vec3, etc ...
35  enum AttribDimension : unsigned int {
36  DIMENSION_POSITION = 3,
37  DIMENSION_COLOR = 4,
38  DIMENSION_TEXCOORD = 2,
39  DIMENSION_NORMAL = 3,
40  };
41 
42  // buffer to hold attribute data (position, color, texcoord, ...)
43  struct MeshAttrib {
44  unsigned int layoutIndex; // attrib layout?
45  unsigned int dataDimension; // vec3? vec4? ...
46  BufferObject buffer;
47  MeshAttrib(unsigned int i, unsigned int d)
48  : layoutIndex(i), dataDimension(d) {}
49  };
50 
51  void update(void* data, size_t typeSize, size_t arraySize, MeshAttrib& attrib,
52  unsigned int dataType = GL_FLOAT);
53 
54  template <typename T>
55  void updatePosition(T* data, int arraySize,
56  unsigned int dataType = GL_FLOAT) {
57  mNumVertices = static_cast<int>(arraySize);
58  update(data, sizeof(T), arraySize, mPositionAtt, dataType);
59  }
60 
61  template <typename T>
62  void updateColor(T* data, size_t arraySize,
63  unsigned int dataType = GL_FLOAT) {
64  update(data, sizeof(T), arraySize, mColorAtt, dataType);
65  }
66 
67  template <typename T>
68  void updateTexcoord(T* data, size_t arraySize,
69  unsigned int dataType = GL_FLOAT) {
70  update(data, sizeof(T), arraySize, mTexcoord2dAtt, dataType);
71  }
72 
73  template <typename T>
74  void updateNormal(T* data, size_t arraySize,
75  unsigned int dataType = GL_FLOAT) {
76  update(data, sizeof(T), arraySize, mNormalAtt, dataType);
77  }
78 
79  // indices must be unsigned int
80  void updateIndices(const unsigned int* data, size_t size);
81 
82  // GL_TRIANGLES, GL_TRIANGLE_STRIP, ...
83  void primitive(unsigned int prim);
84 
85  void draw();
86 
87  void updateWithoutBinding(const void* data, size_t typeSize, size_t arraySize,
88  MeshAttrib& attrib,
89  unsigned int dataType = GL_FLOAT);
90 
91  unsigned int mGLPrimMode = GL_TRIANGLES;
92  int mNumVertices = 0;
93  int mNumIndices = 0;
94  MeshAttrib mPositionAtt{LAYOUT_POSITION, DIMENSION_POSITION},
95  mColorAtt{LAYOUT_COLOR, DIMENSION_COLOR},
96  mTexcoord2dAtt{LAYOUT_TEXCOORD, DIMENSION_TEXCOORD},
97  mNormalAtt{LAYOUT_NORMAL, DIMENSION_NORMAL};
98 
99  BufferObject mIndexBuffer;
100 };
101 
102 } // namespace al
103 
104 #endif
VAO class.
Definition: al_VAO.hpp:18
Definition: al_App.hpp:23
EasyVAO class.
Definition: al_EasyVAO.hpp:23