Skip to content

Commit

Permalink
Use object oriented version of vertex buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
massile committed Nov 15, 2016
1 parent 8a171f5 commit c33bc2c
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 50 deletions.
60 changes: 29 additions & 31 deletions src/Engine2D/graphics/batchRenderer2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ namespace FlowEngine { namespace Graphics {
mScreenQuad(MeshFactory::generateQuad(0, 0, screenSize.x, screenSize.y)),
mPostEffectBuffer(new FrameBuffer(screenSize.x, screenSize.y))
{
mVAO->bind();
API::bindBuffer(GL_ARRAY_BUFFER, mVBO);
API::setBufferData(GL_ARRAY_BUFFER, RENDERER_BUFFER_SIZE, nullptr, GL_DYNAMIC_DRAW);

for(int i=ShaderIndex::FIRST; i<=ShaderIndex::LAST; i++)
API::enableVertexAttribute(i);
API::setVertexAttributePointer(POSITION, 3, GL_FLOAT, false, RENDERER_VERTEX_SIZE, offsetof(VertexData, position));
API::setVertexAttributePointer(COLOR, 4, GL_UNSIGNED_BYTE, true, RENDERER_VERTEX_SIZE, offsetof(VertexData, color));
API::setVertexAttributePointer(UV, 2, GL_FLOAT, false, RENDERER_VERTEX_SIZE, offsetof(VertexData, uv));
API::setVertexAttributePointer(MASK_UV, 2, GL_FLOAT, false, RENDERER_VERTEX_SIZE, offsetof(VertexData, maskUv));
API::setVertexAttributePointer(TID, 1, GL_FLOAT, false, RENDERER_VERTEX_SIZE, offsetof(VertexData, tid));
API::setVertexAttributePointer(MID, 1, GL_FLOAT, false, RENDERER_VERTEX_SIZE, offsetof(VertexData, mid));
API::unbindBuffers(GL_ARRAY_BUFFER);
mVBO->resize(RENDERER_BUFFER_SIZE);

mVBO->setAttribute<glm::vec3>(POSITION);
mVBO->setAttribute<uint>(COLOR, 4, true);
mVBO->setAttribute<glm::vec2>(UV);
mVBO->setAttribute<glm::vec2>(MASK_UV);
mVBO->setAttribute<float>(TID);
mVBO->setAttribute<float>(MID);

mVAO->addBuffer(mVBO);

GLuint* indices = new GLuint[RENDERER_INDICES_SIZE];
for(int i=0, offset=0; i < RENDERER_INDICES_SIZE; i+=6, offset+=4)
Expand All @@ -36,7 +33,6 @@ namespace FlowEngine { namespace Graphics {
}

mIBO = new IndexBuffer(indices, RENDERER_INDICES_SIZE);
mVAO->unbind();

mFramebufferShader->enable();
mFramebufferShader->uniform("tex", 0);
Expand All @@ -48,7 +44,7 @@ namespace FlowEngine { namespace Graphics {
{
delete mIBO;
delete mVAO;
API::freeBuffer(mVBO);
delete mVBO;
}

void BatchRenderer2D::begin()
Expand All @@ -59,7 +55,7 @@ namespace FlowEngine { namespace Graphics {
mFrameBuffer->bind();
mFrameBuffer->clean();

API::bindBuffer(GL_ARRAY_BUFFER, mVBO);
mVBO->bind();
mBuffer = (VertexData*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
}

Expand Down Expand Up @@ -149,35 +145,37 @@ namespace FlowEngine { namespace Graphics {

void BatchRenderer2D::flush()
{
for(int i=0; i<mTextureSlots.size(); i++) {
for (int i = 0; i < mTextureSlots.size(); i++) {
API::setActiveTexture(GL_TEXTURE0 + i);
API::bindTexture(GL_TEXTURE_2D, mTextureSlots[i]);
}

mVAO->bind();
mIBO->bind();
API::drawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_INT, nullptr);
mIBO->unbind();
mVAO->unbind();
{
mVAO->bind();
mIBO->bind();
API::drawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_INT, nullptr);
mIBO->unbind();
mVAO->unbind();
}

mIndexCount = 0;
mTextureSlots.clear();

mPostEffect->render(mFrameBuffer, mPostEffectBuffer, mScreenQuad, mIBO);

API::bindFrameBuffer(GL_FRAMEBUFFER, 0);
API::setViewport(0, 0, mScreenSize.x, mScreenSize.y);

API::setActiveTexture(GL_TEXTURE0);
mPostEffectBuffer->getTexture()->bind();

mFramebufferShader->enable();
mScreenQuad->bind();
mIBO->bind();
API::drawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
mIBO->unbind();
API::bindVertexArray(0);
mFramebufferShader->disable();
{
mFramebufferShader->enable();
mScreenQuad->bind();
mIBO->bind();
API::drawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
mIBO->unbind();
mScreenQuad->unbind();
mFramebufferShader->disable();
}
}

}}
4 changes: 1 addition & 3 deletions src/Engine2D/graphics/batchRenderer2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ namespace FlowEngine { namespace Graphics {
#define RENDERER_BUFFER_SIZE RENDERER_SPRITE_SIZE * RENDERER_MAX_SPRITES
#define RENDERER_INDICES_SIZE RENDERER_MAX_SPRITES * 6

enum ShaderIndex { POSITION, COLOR, UV, MASK_UV, TID, MID, FIRST=0, LAST=MID };

class BatchRenderer2D : public Renderer2D
{
private:
VertexArray* mVAO = new VertexArray;
VertexArray* mScreenQuad;

uint mVBO = API::createBuffer();
VertexBuffer* mVBO = new VertexBuffer(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
IndexBuffer* mIBO;
uint mIndexCount;
VertexData* mBuffer;
Expand Down
13 changes: 8 additions & 5 deletions src/Engine2D/graphics/buffer/vertexArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ namespace FlowEngine { namespace Graphics {
delete mBuffers[i];
}

void VertexArray::addBuffer(VertexBuffer* buffer, GLuint index)
void VertexArray::addBuffer(VertexBuffer* buffer)
{
mBuffers.push_back(buffer);

bind();
buffer->bind();

API::enableVertexAttribute(index);
API::setVertexAttributePointer(index, buffer->getComponentCount(), GL_FLOAT, GL_FALSE, 0, 0);
for(BufferAttribute attribute : buffer->getAttributes()) {
API::enableVertexAttribute(attribute.index);
API::setVertexAttributePointer(attribute.index, attribute.componentCount, attribute.type,
attribute.normalized, buffer->getAttributeStride(), attribute.offset);
}

buffer->unbind();
unbind();

mBuffers.push_back(buffer);
}

void VertexArray::bind() const
Expand Down
2 changes: 1 addition & 1 deletion src/Engine2D/graphics/buffer/vertexArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace FlowEngine { namespace Graphics {
VertexArray();
~VertexArray();

void addBuffer(VertexBuffer* buffer, GLuint index);
void addBuffer(VertexBuffer* buffer);
void bind() const;
void unbind() const;
};
Expand Down
47 changes: 44 additions & 3 deletions src/Engine2D/graphics/buffer/vertexBuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
#include "vertexBuffer.h"
#include "../../api/API.h"
#include "../renderable2d.h"

namespace FlowEngine { namespace Graphics {

VertexBuffer::VertexBuffer(GLfloat* data, GLsizei count, GLuint componentCount)
: mComponentCount(componentCount), mBufferID(API::createBuffer())
VertexBuffer::VertexBuffer(uint target, uint usage)
: mTarget(target), mUsage(usage)
{
mBufferID = API::createBuffer();
}

VertexBuffer::~VertexBuffer()
{
API::freeBuffer(mBufferID);
}

void VertexBuffer::resize(uint size)
{
setData(size, nullptr);
}

void VertexBuffer::setData(uint size, const void *data)
{
bind();
API::setBufferData(GL_ARRAY_BUFFER, count * sizeof(GLfloat), data, GL_STATIC_DRAW);
mSize = size;
API::setBufferData(mTarget, size, data, mUsage);
unbind();
}

Expand All @@ -21,4 +38,28 @@ namespace FlowEngine { namespace Graphics {
API::unbindBuffers(GL_ARRAY_BUFFER);
}

template <> void VertexBuffer::setAttribute<glm::vec3>(ShaderIndex index, uint count, bool normalized)
{
mAttributes.push_back({index, 3*count, GL_FLOAT, normalized, mStride});
mStride += 3*sizeof(float);
}

template <> void VertexBuffer::setAttribute<glm::vec2>(ShaderIndex index, uint count, bool normalized)
{
mAttributes.push_back({index, 2*count, GL_FLOAT, normalized, mStride});
mStride += 2*sizeof(float);
}

template <> void VertexBuffer::setAttribute<float>(ShaderIndex index, uint count, bool normalized)
{
mAttributes.push_back({index, count, GL_FLOAT, normalized, mStride});
mStride += sizeof(float);
}

template <> void VertexBuffer::setAttribute<uint>(ShaderIndex index, uint count, bool normalized)
{
mAttributes.push_back({index, count, GL_UNSIGNED_BYTE, normalized, mStride});
mStride += sizeof(uint);
}

}}
32 changes: 28 additions & 4 deletions src/Engine2D/graphics/buffer/vertexBuffer.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
#pragma once

#include <GL/glew.h>
#include <glm/glm.hpp>
#include <vector>
#include "../../api/API.h"

namespace FlowEngine { namespace Graphics {

enum ShaderIndex { POSITION, COLOR, UV, MASK_UV, TID, MID };

struct BufferAttribute
{
ShaderIndex index;
uint componentCount;
uint type;
bool normalized;
uint offset;
};

class VertexBuffer
{
private:
GLuint mBufferID;
GLuint mComponentCount;
uint mBufferID;
uint mTarget, mUsage;
uint mSize, mStride = 0;

std::vector<BufferAttribute> mAttributes;
public:
VertexBuffer(GLfloat* data, GLsizei count, GLuint componentCount);
VertexBuffer(uint target, uint usage);
~VertexBuffer();

void bind() const;
void unbind() const;
void resize(uint size);

void setData(uint size, const void* data);

inline GLuint getComponentCount() const { return mComponentCount; }
template<typename T>
void setAttribute(ShaderIndex index, uint count = 1, bool normalized = false);
inline std::vector<BufferAttribute> getAttributes() { return mAttributes; }
inline uint getAttributeStride() const { return mStride; }
};

}}
5 changes: 2 additions & 3 deletions src/Engine2D/graphics/mask.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
#include "texture.h"

namespace FlowEngine { namespace Graphics {
using namespace glm;
struct Mask
{
Texture* texture;
glm::mat4 transform;

Mask(Texture* texture, const mat4& transform = mat4(1.0))
Mask(Texture* texture, const glm::mat4& transform = glm::mat4(1.0))
: texture(texture),
transform(scale(mat4(), vec3((float)texture->getWidth()/texture->getHeight(), 1.0f, 1.0f)))
transform(glm::scale(glm::mat4(), glm::vec3((float)texture->getWidth()/texture->getHeight(), 1.0f, 1.0f)))
{

}
Expand Down

0 comments on commit c33bc2c

Please sign in to comment.