add Buffer API to create a Buffer with an initializer_list of GLfloats/GLdoubles

This commit is contained in:
Josh Holtrop 2015-06-14 16:20:49 -04:00
parent cf06b6a945
commit 09cb80327f
2 changed files with 42 additions and 19 deletions

View File

@ -3,6 +3,7 @@
#include "glcxx/gl.hpp"
#include <memory>
#include <initializer_list>
namespace glcxx
{
@ -15,6 +16,18 @@ namespace glcxx
void set_buffer_data(GLenum target, GLenum usage, const void * ptr, size_t size);
void set_buffer_data(GLenum target, GLenum usage, std::initializer_list<GLfloat> data)
{
set_buffer_data(target, usage, &*data.begin(),
(uintptr_t)&*data.end() - (uintptr_t)&*data.begin());
}
void set_buffer_data_d(GLenum target, GLenum usage, std::initializer_list<GLdouble> data)
{
set_buffer_data(target, usage, &*data.begin(),
(uintptr_t)&*data.end() - (uintptr_t)&*data.begin());
}
GLuint id() const { return m_id; }
void bind() const { glBindBuffer(m_target, m_id); }
@ -26,6 +39,22 @@ namespace glcxx
return buffer;
}
static std::shared_ptr<Buffer> create(GLenum target, GLenum usage,
std::initializer_list<GLfloat> data)
{
std::shared_ptr<Buffer> buffer = std::make_shared<Buffer>();
buffer->set_buffer_data(target, usage, data);
return buffer;
}
static std::shared_ptr<Buffer> create_d(GLenum target, GLenum usage,
std::initializer_list<GLdouble> data)
{
std::shared_ptr<Buffer> buffer = std::make_shared<Buffer>();
buffer->set_buffer_data_d(target, usage, data);
return buffer;
}
protected:
GLuint m_id;

View File

@ -35,26 +35,20 @@ bool init(void)
"position", 0,
"color", 1);
GLfloat coords[] = {
-0.5, -0.5,
0.5, -0.5,
0.5, 0.5,
-0.5, 0.5,
};
buffer = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, &coords, sizeof(coords));
buffer = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW,
{-0.5, -0.5,
0.5, -0.5,
0.5, 0.5,
-0.5, 0.5});
GLfloat coords2[] = {
0.2, 0.2,
0.9, 0.2,
0.9, 0.9,
};
buffer2 = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, &coords2, sizeof(coords2));
GLfloat colors[] = {
1.0, 0.1, 0.1, 1.0,
0.1, 1.0, 0.1, 1.0,
0.1, 0.1, 1.0, 1.0,
};
buffer3 = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, &colors, sizeof(colors));
buffer2 = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW,
{0.2, 0.2,
0.9, 0.2,
0.9, 0.9});
buffer3 = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW,
{1.0, 0.1, 0.1, 1.0,
0.1, 1.0, 0.1, 1.0,
0.1, 0.1, 1.0, 1.0});
array1->bind();
glEnableVertexAttribArray(0);