Skip to content

Instantly share code, notes, and snippets.

@thetooth
Last active August 29, 2015 13:56
Show Gist options
  • Save thetooth/9288686 to your computer and use it in GitHub Desktop.
Save thetooth/9288686 to your computer and use it in GitHub Desktop.
OpenGL 4 Buffer Class
#pragma once
#include // Your lib that includes all the OpenGL bindings and maybe some other stuff i've forgotten like vector
namespace gl4 {
template<int BufferType, typename StorageClass> class glBuffer
{
public:
GLuint buffer;
glBuffer(){};
~glBuffer(){ glDeleteBuffers(1, &buffer); };
virtual void create(std::vector<StorageClass> v){
glGenBuffers(1, &buffer);
glBindBuffer(BufferType, buffer);
glBufferData(BufferType, v.size()*sizeof(StorageClass), &v.front(), GL_STATIC_DRAW);
};
};
template<typename StorageClass> class glObj
{
public:
GLuint vao;
glBuffer<GL_ARRAY_BUFFER, StorageClass> vbo;
glBuffer<GL_ELEMENT_ARRAY_BUFFER, GLuint> ebo;
int elementSize;
glObj(){};
glObj(std::vector<StorageClass> v, std::vector<GLuint> e) { create(v, e); };
~glObj(){ glDeleteVertexArrays(1, &vao); };
void create(std::vector<StorageClass> v, std::vector<GLuint> e){
// Create Vertex Array Object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Create two buffers, first stores the vertex data, second stores element index for draw order
vbo.create(v);
ebo.create(e);
// Retain raw element index length for drawing call
elementSize = e.size();
};
void draw(int mode = GL_TRIANGLE_STRIP){
glBindVertexArray(vao);
glDrawElements(mode, elementSize, GL_UNSIGNED_INT, 0);
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment