hx3d  1
2D/3D Simple Game Framework
multi_array_buffer.cpp
1 /*
2  Multi array buffer.
3  One buffer for multiple attributes.
4 
5  Copyright (C) 2015 Denis BOURGE
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  USA
21 */
22 
23 #include "hx3d/graphics/buffers/multi_array_buffer.hpp"
24 #include "hx3d/utils/log.hpp"
25 
26 #include "hx3d/graphics/shader.hpp"
27 
28 namespace hx3d {
29 namespace graphics {
30 namespace buffers {
31 
32 MultiArrayBuffer::MultiArrayBuffer():
33  ArrayBuffer<float>()
34 {}
35 
36 MultiArrayBuffer::~MultiArrayBuffer()
37 {}
38 
39 void MultiArrayBuffer::addAttribute(const std::string name, const Attribute attribute) {
40 
41  if (_attributes.find(name) != _attributes.end()) {
42  Log.Error("MultiArrayBuffer: Attribute %s already exists.", name.c_str());
43  return;
44  }
45 
46  _attributes[name].create(attribute);
47 }
48 
49 void MultiArrayBuffer::setAttribute(const std::string name, const std::vector<float> data) {
50  if (_attributes.find(name) != _attributes.end()) {
51  _attributes[name].set(data);
52  }
53 }
54 
56  return _attributes[name];
57 }
58 
60 {
61  unsigned int totalLength = 0;
62  for (unsigned int i = 0; i < _attributes.size(); ++i) {
63  AttributeArrayBuffer& buffer = getMapAttribute(i);
64  totalLength += buffer.size();
65  }
66 
67  _vector.clear();
68  _vector.resize(totalLength);
69 
70  unsigned int last = 0;
71  for (unsigned int i = 0; i < _attributes.size(); ++i) {
72  AttributeArrayBuffer& buffer = getMapAttribute(i);
73  for (unsigned int j = 0; j < buffer.size(); ++j) {
74  _vector[last++] = buffer.getValue(j);
75  }
76  }
77 }
78 
80  generate();
81 
82  glBindBuffer(GL_ARRAY_BUFFER, _buf);
83  glBufferData(GL_ARRAY_BUFFER, _vector.size() * sizeof(float), _vector.data(), GL_STATIC_DRAW);
84  glBindBuffer(GL_ARRAY_BUFFER, 0);
85 }
86 
88 {
89  glBindBuffer(GL_ARRAY_BUFFER, _buf);
90 
91  unsigned int lastSize = 0;
92  for (auto& attr: _attributes) {
93  Attribute& attribute = attr.second.getAttribute();
94  GLint loc = shader->getAttribute(attribute.getName());
95  glEnableVertexAttribArray(loc);
96  glVertexAttribPointer(loc, attribute.getSize(), attribute.getType(), GL_FALSE, 0, BUFFER_OFFSET(lastSize * sizeof(float)));
97  lastSize += attr.second.size();
98  }
99 
100  glBindBuffer(GL_ARRAY_BUFFER, 0);
101 }
102 
104 {
105  for (auto& attr: _attributes) {
106  Attribute& attribute = attr.second.getAttribute();
107 
108  GLint loc = shader->getAttribute(attribute.getName());
109  glDisableVertexAttribArray(loc);
110  }
111 }
112 
114 
115 AttributeArrayBuffer& MultiArrayBuffer::getMapAttribute(const unsigned int i) {
116 
117  unsigned int count = 0;
118  for (auto it = _attributes.begin(); it != _attributes.end(); ++it, ++count) {
119  if (count == i) {
120  return it->second;
121  }
122  }
123 
124  throw std::out_of_range("Out of range");
125 }
126 
127 unsigned int MultiArrayBuffer::attributeTotalSize() {
128  unsigned int totalsize = 0;
129  for (auto& attr: _attributes) {
130  Attribute& attribute = attr.second.getAttribute();
131  totalsize += attribute.getSize();
132  }
133 
134  return totalsize;
135 }
136 
137 } /* buffers */
138 } /* graphics */
139 } /* hx3d */
virtual void begin(const Ptr< Shader > &shader) override
Begin the use with a shader.
virtual void end(const Ptr< Shader > &shader) override
End the use with a shader.
std::vector< GLushort > _vector
Data.
Definition: buffer.hpp:114
virtual void upload() override
Upload the data to the GPU.
const GLenum getType() const
Get the attribute type.
Definition: attribute.cpp:40
hx3d framework namespace
Definition: audio.hpp:26
OpenGL buffer attribute.
Definition: attribute.hpp:35
void addAttribute(const std::string name, const Attribute attribute)
Add an empty initialized attribute array buffer.
void setAttribute(const std::string name, const std::vector< float > data)
Set the wanted attribute array buffer content.
void Error(const std::string fmt,...)
Write an error message.
Definition: log.cpp:72
const GLuint getSize() const
Get the attribute size.
Definition: attribute.cpp:44
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
AttributeArrayBuffer & getAttribute(const std::string name)
Get the wanted attribute array buffer.
const std::string getName() const
Get the attribute name.
Definition: attribute.cpp:36
void generate()
Generate the vector using the attributes.
GLushort * data()
Get the buffer values.
T getValue(unsigned int i)
Get a value.
Definition: buffer.inl.hpp:72
unsigned int size()
Get the buffer size.
Definition: buffer.inl.hpp:62
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34