diff --git a/src/Engine2D/graphics/batchRenderer2d.cpp b/src/Engine2D/graphics/batchRenderer2d.cpp index 14da838..6df0b5e 100644 --- a/src/Engine2D/graphics/batchRenderer2d.cpp +++ b/src/Engine2D/graphics/batchRenderer2d.cpp @@ -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(POSITION); + mVBO->setAttribute(COLOR, 4, true); + mVBO->setAttribute(UV); + mVBO->setAttribute(MASK_UV); + mVBO->setAttribute(TID); + mVBO->setAttribute(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) @@ -36,7 +33,6 @@ namespace FlowEngine { namespace Graphics { } mIBO = new IndexBuffer(indices, RENDERER_INDICES_SIZE); - mVAO->unbind(); mFramebufferShader->enable(); mFramebufferShader->uniform("tex", 0); @@ -48,7 +44,7 @@ namespace FlowEngine { namespace Graphics { { delete mIBO; delete mVAO; - API::freeBuffer(mVBO); + delete mVBO; } void BatchRenderer2D::begin() @@ -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); } @@ -149,35 +145,37 @@ namespace FlowEngine { namespace Graphics { void BatchRenderer2D::flush() { - for(int i=0; ibind(); - 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(); + } } }} \ No newline at end of file diff --git a/src/Engine2D/graphics/batchRenderer2d.h b/src/Engine2D/graphics/batchRenderer2d.h index c2693a7..f629a8b 100644 --- a/src/Engine2D/graphics/batchRenderer2d.h +++ b/src/Engine2D/graphics/batchRenderer2d.h @@ -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; diff --git a/src/Engine2D/graphics/buffer/vertexArray.cpp b/src/Engine2D/graphics/buffer/vertexArray.cpp index ad48f10..951604e 100644 --- a/src/Engine2D/graphics/buffer/vertexArray.cpp +++ b/src/Engine2D/graphics/buffer/vertexArray.cpp @@ -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 diff --git a/src/Engine2D/graphics/buffer/vertexArray.h b/src/Engine2D/graphics/buffer/vertexArray.h index f67ea51..a730a48 100644 --- a/src/Engine2D/graphics/buffer/vertexArray.h +++ b/src/Engine2D/graphics/buffer/vertexArray.h @@ -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; }; diff --git a/src/Engine2D/graphics/buffer/vertexBuffer.cpp b/src/Engine2D/graphics/buffer/vertexBuffer.cpp index 359dbbe..0703b4c 100644 --- a/src/Engine2D/graphics/buffer/vertexBuffer.cpp +++ b/src/Engine2D/graphics/buffer/vertexBuffer.cpp @@ -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(); } @@ -21,4 +38,28 @@ namespace FlowEngine { namespace Graphics { API::unbindBuffers(GL_ARRAY_BUFFER); } + template <> void VertexBuffer::setAttribute(ShaderIndex index, uint count, bool normalized) + { + mAttributes.push_back({index, 3*count, GL_FLOAT, normalized, mStride}); + mStride += 3*sizeof(float); + } + + template <> void VertexBuffer::setAttribute(ShaderIndex index, uint count, bool normalized) + { + mAttributes.push_back({index, 2*count, GL_FLOAT, normalized, mStride}); + mStride += 2*sizeof(float); + } + + template <> void VertexBuffer::setAttribute(ShaderIndex index, uint count, bool normalized) + { + mAttributes.push_back({index, count, GL_FLOAT, normalized, mStride}); + mStride += sizeof(float); + } + + template <> void VertexBuffer::setAttribute(ShaderIndex index, uint count, bool normalized) + { + mAttributes.push_back({index, count, GL_UNSIGNED_BYTE, normalized, mStride}); + mStride += sizeof(uint); + } + }} diff --git a/src/Engine2D/graphics/buffer/vertexBuffer.h b/src/Engine2D/graphics/buffer/vertexBuffer.h index 63841a1..8fc3280 100644 --- a/src/Engine2D/graphics/buffer/vertexBuffer.h +++ b/src/Engine2D/graphics/buffer/vertexBuffer.h @@ -1,21 +1,45 @@ #pragma once #include +#include +#include +#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 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 + void setAttribute(ShaderIndex index, uint count = 1, bool normalized = false); + inline std::vector getAttributes() { return mAttributes; } + inline uint getAttributeStride() const { return mStride; } }; }} diff --git a/src/Engine2D/graphics/mask.h b/src/Engine2D/graphics/mask.h index dc527d8..fa502a2 100644 --- a/src/Engine2D/graphics/mask.h +++ b/src/Engine2D/graphics/mask.h @@ -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))) { }