Skip to content

Commit

Permalink
optimize vao & vbo
Browse files Browse the repository at this point in the history
  • Loading branch information
mohlek committed Aug 13, 2017
1 parent 830cc0d commit 6bec39b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/engine/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ void Buffer::bind() {
glBindBuffer(this->bufferType, this->bufferId);
}

void Buffer::unbind() {
glBindBuffer(this->bufferType, 0);
}

void* Buffer::map(GLenum access = GL_READ_WRITE) {
if (glMapNamedBuffer) {
if (access != GL_READ_ONLY) {
Expand Down
5 changes: 5 additions & 0 deletions src/engine/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ namespace Engine {
GLint size;
GLenum dataType = GL_FLOAT;
GLsizei stride;
GLenum valuesPerIndex = 3;

Buffer(GLenum bufferType);
Buffer() : Buffer(GL_ARRAY_BUFFER) {};
virtual ~Buffer();

void bind();
void unbind();

void pushData(void* data, int size);

void* map(GLenum access);
bool unmap();

GLuint getId() { return bufferId; }

};
}

Expand Down
8 changes: 5 additions & 3 deletions src/engine/VertexArrayObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ void VertexArrayObject::addBuffer(std::shared_ptr<Buffer>& buffer) {

if (glEnableVertexArrayAttrib) {
glEnableVertexArrayAttrib(this->vaoId, index);
glVertexArrayVertexBuffer(this->vaoId, index, buffer->getId(), 0, buffer->stride);
glVertexArrayAttribFormat(this->vaoId, index, buffer->valuesPerIndex, GL_FLOAT, GL_FALSE, 0);
} else {
glEnableVertexAttribArray(index);
buffer->bind();
bind();
glVertexAttribPointer(index, buffer->valuesPerIndex, buffer->dataType, GL_FALSE, buffer->stride, 0);
}

buffer->bind();
bind();
glVertexAttribPointer(index, 3, buffer->dataType, GL_FALSE, buffer->stride, 0);
this->buffers.push_back(buffer);
}

Expand Down
4 changes: 2 additions & 2 deletions src/engine/model/3ds/Model3DS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using namespace Engine;

Model3DS::Model3DS(std::string&& filename) {
this->vertexVBO = std::make_shared<Buffer>();
this->normalsVBO = std::make_shared<Buffer>();
this->vertexVBO = std::make_shared<Buffer>(Buffer());
this->normalsVBO = std::make_shared<Buffer>(Buffer());
this->model = lib3ds_file_load(filename.c_str());

if (!model) {
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int main(int argc, char** argv) {
vbo->pushData(points, 9 * sizeof(float));
vbo->stride = 3 * sizeof(float);

//vao.addBuffer(dragon.vertexVBO);
vao.addBuffer(vbo);

while (win->loop()) {
Expand Down

0 comments on commit 6bec39b

Please sign in to comment.