From b682002ecdd222323da62839b9d668c1a1e02fb9 Mon Sep 17 00:00:00 2001 From: Anselme Date: Tue, 26 Apr 2016 23:32:19 +0200 Subject: [PATCH] fixed template linking error --- src/buffer.cpp | 94 -------------------------------------------------- src/buffer.h | 87 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 104 deletions(-) delete mode 100644 src/buffer.cpp diff --git a/src/buffer.cpp b/src/buffer.cpp deleted file mode 100644 index d8b2758..0000000 --- a/src/buffer.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include "buffer.h" -#include "sparrowrenderer.h" - -#include - -template -GLenum TBuffer::getGLEnum(BufferType type) -{ - GLenum typeEnum; - - switch(m_type) - { - case VBO : - typeEnum = GL_ARRAY_BUFFER; - break; - case EBO : - typeEnum = GL_ELEMENT_ARRAY_BUFFER; - break; - case UBO : - typeEnum = GL_UNIFORM_BUFFER; - break; - } - - return typeEnum; -} - -template -TBuffer::BufferEditor::BufferEditor(TBuffer *b) -{ - if(b->isDynamic()) - { - GLenum m_typeEnum = getGLEnum(b->getType()); - glBindBuffer(m_typeEnum, b->getId()); - ptr = (T*)glMapBuffer(m_typeEnum, GL_WRITE_ONLY); - } - else - { - fprintf(stderr, "Buffer data can't be edited, this buffer is static\n"); - ptr = NULL; - } -} - -template -TBuffer::BufferEditor::~BufferEditor() -{ - glUnmapBuffer(m_typeEnum); - glBindBuffer(m_typeEnum, 0); -} - -template -TBuffer::TBuffer(const std::vector &data, BufferType type, bool isDynamic) : - m_type(type), - m_isDynamic(isDynamic) -{ - // TODO : allow stream draw - GLenum draw_type = isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW; - GLenum typeEnum = getGLEnum(m_type); - glGenBuffers(1, m_id); - glBindBuffer(typeEnum, m_id); - glBufferData(typeEnum, data.size() * sizeof(T), data.data(), draw_type); - glBindBuffer(typeEnum, 0); -} - -template -void TBuffer::setVertexAttrib(int location, int nbComponents, int offset, int instanceDivisor) -{ - if(m_type == VBO && SparrowRenderer::isModernOpenGLAvailable()) - { - glBindBuffer(GL_ARRAY_BUFFER, m_id); - glEnableVertexAttribArray(location); - if(instanceDivisor) - glVertexAttribDivisor(location, instanceDivisor); - glVertexAttribPointer(location, nbComponents, GL_FLOAT, GL_FALSE, sizeof(T), BUFFER_OFFSET(offset)); - glBindBuffer(GL_ARRAY_BUFFER, 0); - } -} - -template -void TBuffer::bind() -{ - glBindBuffer(getGLEnum(m_type), m_id); -} - -template -void TBuffer::unbind() -{ - glBindBuffer(getGLEnum(m_type), 0); -} - -template -TBuffer::~TBuffer() -{ - glDeleteBuffers(1, m_id); -} diff --git a/src/buffer.h b/src/buffer.h index e16b993..a9381bb 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -1,7 +1,8 @@ #ifndef BUFFER_H #define BUFFER_H -#include "glew.h" +#include "sparrowrenderer.h" +#include #include #define BUFFER_OFFSET(i) ((char *)NULL + (i)) @@ -16,7 +17,7 @@ public: UBO }; - virtual ~Buffer(); + virtual ~Buffer() {} virtual void bind() = 0; virtual void unbind() = 0; }; @@ -31,19 +32,67 @@ public: T *ptr; public: - BufferEditor(TBuffer *b); - ~BufferEditor(); + BufferEditor(TBuffer *b) + { + if(b->isDynamic()) + { + GLenum m_typeEnum = b->getGLEnum(); + glBindBuffer(m_typeEnum, b->getId()); + ptr = (T*)glMapBuffer(m_typeEnum, GL_WRITE_ONLY); + } + else + { + fprintf(stderr, "Buffer data can't be edited, this buffer is static\n"); + ptr = NULL; + } + } + ~BufferEditor() + { + glUnmapBuffer(m_typeEnum); + glBindBuffer(m_typeEnum, 0); + } T* getPointer(); }; - TBuffer(const std::vector &data, BufferType type, bool isDynamic = false); - ~TBuffer(); + TBuffer(const std::vector &data, BufferType type, bool isDynamic = false) : + m_type(type), + m_isDynamic(isDynamic) + { + // TODO : allow stream draw + GLenum draw_type = isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW; + GLenum typeEnum = getGLEnum(); + glGenBuffers(1, &m_id); + glBindBuffer(typeEnum, m_id); + glBufferData(typeEnum, data.size() * sizeof(T), data.data(), draw_type); + glBindBuffer(typeEnum, 0); + } + virtual ~TBuffer() + { + glDeleteBuffers(1, &m_id); + } - virtual void setVertexAttrib(int location, int nbComponents, int offset = 0, int instanceDivisor = 0); + virtual void setVertexAttrib(int location, int nbComponents, int offset = 0, int instanceDivisor = 0) + { + if(m_type == VBO && SparrowRenderer::isModernOpenGLAvailable()) + { + glBindBuffer(GL_ARRAY_BUFFER, m_id); + glEnableVertexAttribArray(location); + if(instanceDivisor) + glVertexAttribDivisor(location, instanceDivisor); + glVertexAttribPointer(location, nbComponents, GL_FLOAT, GL_FALSE, sizeof(T), BUFFER_OFFSET(offset)); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + } - virtual void bind(); - virtual void unbind(); + virtual void bind() + { + glBindBuffer(getGLEnum(), m_id); + } + virtual void unbind() + { + glBindBuffer(getGLEnum(), 0); + } GLuint getId() {return m_id;} BufferType getType() {return m_type;} @@ -54,7 +103,25 @@ private: BufferType m_type; bool m_isDynamic; - GLenum getGLEnum(BufferType type); + GLenum getGLEnum() + { + GLenum typeEnum; + + switch(m_type) + { + case VBO : + typeEnum = GL_ARRAY_BUFFER; + break; + case EBO : + typeEnum = GL_ELEMENT_ARRAY_BUFFER; + break; + case UBO : + typeEnum = GL_UNIFORM_BUFFER; + break; + } + + return typeEnum; + } }; #endif // BUFFER_H