hx3d  1
2D/3D Simple Game Framework
attribute_stream_buffer.cpp
1 /*
2  Attribute array buffer.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/buffers/attribute_stream_buffer.hpp"
22 
23 #include "hx3d/graphics/shader.hpp"
24 
25 namespace hx3d {
26 namespace graphics {
27 namespace buffers {
28 
30  ArrayBuffer<float>(), _attribute(), _stream_size(0)
31 {}
32 
33 AttributeStreamBuffer::AttributeStreamBuffer(const Attribute attribute, const unsigned int stream_size):
34  ArrayBuffer<float>(), _attribute(attribute), _stream_size(stream_size)
35 {}
36 
37 AttributeStreamBuffer::~AttributeStreamBuffer() {}
38 
39 void AttributeStreamBuffer::create(const Attribute attribute, const unsigned int stream_size) {
40  _attribute = attribute;
41  _stream_size = stream_size;
42 }
43 
45  return _attribute;
46 }
47 
49 
50  // if (_vector.size() > 0) {
51  // glBindBuffer(GL_ARRAY_BUFFER, _buf);
52  // glBufferData(GL_ARRAY_BUFFER, _vector.size() * sizeof(float), _vector.data(), GL_STATIC_DRAW);
53  // glBindBuffer(GL_ARRAY_BUFFER, 0);
54  // }
55 
56  glBindBuffer(GL_ARRAY_BUFFER, _buf);
57  glBufferData(GL_ARRAY_BUFFER, _stream_size * _attribute.getSize() * sizeof(_attribute.getType()), NULL, GL_STREAM_DRAW);
58  glBindBuffer(GL_ARRAY_BUFFER, 0);
59 }
60 
62 
63  GLint loc = shader->getAttribute(_attribute.getName());
64 
65  glBindBuffer(GL_ARRAY_BUFFER, _buf);
66  glEnableVertexAttribArray(loc);
67  glVertexAttribPointer(loc, _attribute.getSize(), _attribute.getType(), GL_FALSE, 0, BUFFER_OFFSET(0));
68 
69  glBindBuffer(GL_ARRAY_BUFFER, 0);
70 }
71 
73  if (_vector.size() > 0) {
74  GLint loc = shader->getAttribute(_attribute.getName());
75 
76  glDisableVertexAttribArray(loc);
77  }
78 }
79 
80 } /* buffers */
81 } /* graphics */
82 } /* hx3d */
virtual void upload() override
Upload the data to the GPU.
std::vector< float > _vector
Data.
Definition: buffer.hpp:114
AttributeStreamBuffer()
Construct an empty attribute array buffer.
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
virtual void begin(const Ptr< Shader > &shader) override
Begin the use with a shader.
void create(const Attribute attribute, const unsigned int stream_size)
Initialize the attribute array buffer.
const GLuint getSize() const
Get the attribute size.
Definition: attribute.cpp:44
virtual void end(const Ptr< Shader > &shader) override
End the use with a shader.
const std::string getName() const
Get the attribute name.
Definition: attribute.cpp:36
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
OpenGL GPU uploadable buffer.