Last active
August 29, 2015 13:56
-
-
Save thetooth/9288686 to your computer and use it in GitHub Desktop.
OpenGL 4 Buffer Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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