diff options
Diffstat (limited to 'shared_cpp')
-rw-r--r-- | shared_cpp/Renderer2d.cpp | 47 | ||||
-rw-r--r-- | shared_cpp/Renderer2d.h | 9 |
2 files changed, 54 insertions, 2 deletions
diff --git a/shared_cpp/Renderer2d.cpp b/shared_cpp/Renderer2d.cpp index f91ed08..adef4de 100644 --- a/shared_cpp/Renderer2d.cpp +++ b/shared_cpp/Renderer2d.cpp @@ -53,6 +53,7 @@ void Renderer2d::unload() { void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, Renderer2d* renderer, GLenum loadType) { + mode = MeshMode::Vertex; numVertices = inNumVertices; useShader(renderer->shader); @@ -73,15 +74,57 @@ void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, Renderer2d* render glBindVertexArray(0); } +void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, GLuint* inIndices, uint32 inNumIndices, Renderer2d* renderer, GLenum loadType) { + mode = MeshMode::Index; + numVertices = inNumVertices; + numIndices = inNumIndices; + useShader(renderer->shader); + + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(Vertex2d), &inVertices[0], loadType); + + glEnableVertexAttribArray(renderer->attributes.position); + glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0); + + glEnableVertexAttribArray(renderer->attributes.color); + glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)offsetof(Vertex2d, color)); + + glGenBuffers(1, &ebo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * numIndices, inIndices, loadType); + + glBindVertexArray(0); +} + void Mesh2d::render(Renderer2d* renderer, GLenum drawType) { setShaderMat4(renderer->uniforms.model, model); glBindVertexArray(vao); - glDrawArrays(drawType, 0, numVertices); + + switch (mode) { + case MeshMode::Vertex: + glDrawArrays(drawType, 0, numVertices); + break; + case MeshMode::Index: + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glDrawElements(drawType, numIndices, GL_UNSIGNED_INT, 0); + break; + } + glBindVertexArray(0); -} +} + void Mesh2d::unload() { glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); + + if (mode == MeshMode::Index) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glDeleteBuffers(1, &ebo); + } } diff --git a/shared_cpp/Renderer2d.h b/shared_cpp/Renderer2d.h index afde20e..a942fa6 100644 --- a/shared_cpp/Renderer2d.h +++ b/shared_cpp/Renderer2d.h @@ -31,13 +31,22 @@ struct Vertex2d { Vector4 color; }; +enum MeshMode { + Vertex = 0, + Index = 1 +}; + struct Mesh2d { + MeshMode mode = MeshMode::Vertex; uint32 vao; uint32 vbo; + uint32 ebo; uint32 numVertices = 0; + uint32 numIndices = 0; Mat4x4 model; void load(Vertex2d* vertices, uint32 numVertices, Renderer2d* renderer, GLenum loadType = GL_STATIC_DRAW); + void load(Vertex2d* vertices, uint32 numVertices, GLuint* indices, uint32 numIndices, Renderer2d* renderer, GLenum loadType = GL_STATIC_DRAW); void render(Renderer2d* renderer, GLenum drawType = GL_TRIANGLES); void unload(); }; |