fixed template linking error

This commit is contained in:
Anselme 2016-04-26 23:32:19 +02:00
parent 48e1c4afdc
commit b682002ecd
2 changed files with 77 additions and 104 deletions

View File

@ -1,94 +0,0 @@
#include "buffer.h"
#include "sparrowrenderer.h"
#include <cstdio>
template <typename T>
GLenum TBuffer<T>::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 <typename T>
TBuffer<T>::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 <typename T>
TBuffer<T>::BufferEditor::~BufferEditor()
{
glUnmapBuffer(m_typeEnum);
glBindBuffer(m_typeEnum, 0);
}
template <typename T>
TBuffer<T>::TBuffer(const std::vector<T> &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 <typename T>
void TBuffer<T>::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 <typename T>
void TBuffer<T>::bind()
{
glBindBuffer(getGLEnum(m_type), m_id);
}
template <typename T>
void TBuffer<T>::unbind()
{
glBindBuffer(getGLEnum(m_type), 0);
}
template <typename T>
TBuffer<T>::~TBuffer()
{
glDeleteBuffers(1, m_id);
}

View File

@ -1,7 +1,8 @@
#ifndef BUFFER_H #ifndef BUFFER_H
#define BUFFER_H #define BUFFER_H
#include "glew.h" #include "sparrowrenderer.h"
#include <cstdio>
#include <vector> #include <vector>
#define BUFFER_OFFSET(i) ((char *)NULL + (i)) #define BUFFER_OFFSET(i) ((char *)NULL + (i))
@ -16,7 +17,7 @@ public:
UBO UBO
}; };
virtual ~Buffer(); virtual ~Buffer() {}
virtual void bind() = 0; virtual void bind() = 0;
virtual void unbind() = 0; virtual void unbind() = 0;
}; };
@ -31,19 +32,67 @@ public:
T *ptr; T *ptr;
public: public:
BufferEditor(TBuffer *b); BufferEditor(TBuffer *b)
~BufferEditor(); {
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(); T* getPointer();
}; };
TBuffer(const std::vector<T> &data, BufferType type, bool isDynamic = false); TBuffer(const std::vector<T> &data, BufferType type, bool isDynamic = false) :
~TBuffer(); 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 bind()
virtual void unbind(); {
glBindBuffer(getGLEnum(), m_id);
}
virtual void unbind()
{
glBindBuffer(getGLEnum(), 0);
}
GLuint getId() {return m_id;} GLuint getId() {return m_id;}
BufferType getType() {return m_type;} BufferType getType() {return m_type;}
@ -54,7 +103,25 @@ private:
BufferType m_type; BufferType m_type;
bool m_isDynamic; 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 #endif // BUFFER_H