hx3d  1
2D/3D Simple Game Framework
attribute_array_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_array_buffer.hpp"
22 
23 #include "hx3d/graphics/shader.hpp"
24 
25 namespace hx3d {
26 namespace graphics {
27 namespace buffers {
28 
30  ArrayBuffer<float>(), _attribute()
31 {}
32 
34  ArrayBuffer<float>(), _attribute(attribute)
35 {}
36 
37 AttributeArrayBuffer::AttributeArrayBuffer(const Attribute attribute, const std::vector<float> values):
38  AttributeArrayBuffer(attribute)
39 {
40  set(values);
41 }
42 
43 AttributeArrayBuffer::~AttributeArrayBuffer() {}
44 
45 void AttributeArrayBuffer::create(const Attribute attribute) {
46  _attribute = attribute;
47 }
48 
50  return _attribute;
51 }
52 
54 
55  if (_vector.size() > 0) {
56  glBindBuffer(GL_ARRAY_BUFFER, _buf);
57  glBufferData(GL_ARRAY_BUFFER, _vector.size() * sizeof(float), _vector.data(), GL_STATIC_DRAW);
58  glBindBuffer(GL_ARRAY_BUFFER, 0);
59  }
60 }
61 
63 
64  if (_vector.size() > 0) {
65  const GLint loc = shader->getAttribute(_attribute.getName());
66 
67  glBindBuffer(GL_ARRAY_BUFFER, _buf);
68  glEnableVertexAttribArray(loc);
69  glVertexAttribPointer(loc, _attribute.getSize(), _attribute.getType(), GL_FALSE, 0, BUFFER_OFFSET(0));
70 
71  glBindBuffer(GL_ARRAY_BUFFER, 0);
72  }
73 }
74 
76  if (_vector.size() > 0) {
77  const GLint loc = shader->getAttribute(_attribute.getName());
78 
79  glDisableVertexAttribArray(loc);
80  }
81 }
82 
83 } /* buffers */
84 } /* graphics */
85 } /* hx3d */
virtual void begin(const Ptr< Shader > &shader) override
Begin the use with a shader.
void create(const Attribute attribute)
Initialize the attribute array buffer.
std::vector< float > _vector
Data.
Definition: buffer.hpp:114
virtual void end(const Ptr< Shader > &shader) override
End the use with a shader.
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
const GLuint getSize() const
Get the attribute size.
Definition: attribute.cpp:44
virtual void upload() override
Upload the data to the GPU.
const std::string getName() const
Get the attribute name.
Definition: attribute.cpp:36
AttributeArrayBuffer()
Construct an empty attribute array buffer.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
OpenGL GPU uploadable buffer.