23 #include "hx3d/graphics/buffers/multi_array_buffer.hpp" 24 #include "hx3d/utils/log.hpp" 26 #include "hx3d/graphics/shader.hpp" 32 MultiArrayBuffer::MultiArrayBuffer():
36 MultiArrayBuffer::~MultiArrayBuffer()
41 if (_attributes.find(name) != _attributes.end()) {
42 Log.
Error(
"MultiArrayBuffer: Attribute %s already exists.", name.c_str());
46 _attributes[name].create(attribute);
50 if (_attributes.find(name) != _attributes.end()) {
51 _attributes[name].set(data);
56 return _attributes[name];
61 unsigned int totalLength = 0;
62 for (
unsigned int i = 0; i < _attributes.size(); ++i) {
64 totalLength += buffer.
size();
70 unsigned int last = 0;
71 for (
unsigned int i = 0; i < _attributes.size(); ++i) {
73 for (
unsigned int j = 0; j < buffer.
size(); ++j) {
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);
89 glBindBuffer(GL_ARRAY_BUFFER,
_buf);
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();
100 glBindBuffer(GL_ARRAY_BUFFER, 0);
105 for (
auto& attr: _attributes) {
106 Attribute& attribute = attr.second.getAttribute();
108 GLint loc = shader->getAttribute(attribute.
getName());
109 glDisableVertexAttribArray(loc);
117 unsigned int count = 0;
118 for (
auto it = _attributes.begin(); it != _attributes.end(); ++it, ++count) {
124 throw std::out_of_range(
"Out of range");
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();
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.
virtual void upload() override
Upload the data to the GPU.
const GLenum getType() const
Get the attribute type.
void addAttribute(const std::string name, const Attribute attribute)
Add an empty initialized attribute array buffer.
Attribute specialized 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.
const GLuint getSize() const
Get the attribute size.
static hx3d::LogImpl Log
Current log implementation.
AttributeArrayBuffer & getAttribute(const std::string name)
Get the wanted attribute array buffer.
const std::string getName() const
Get the attribute name.
void generate()
Generate the vector using the attributes.
GLushort * data()
Get the buffer values.
T getValue(unsigned int i)
Get a value.
unsigned int size()
Get the buffer size.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.