fixed template linking error
This commit is contained in:
parent
48e1c4afdc
commit
b682002ecd
@ -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);
|
||||
}
|
87
src/buffer.h
87
src/buffer.h
@ -1,7 +1,8 @@
|
||||
#ifndef BUFFER_H
|
||||
#define BUFFER_H
|
||||
|
||||
#include "glew.h"
|
||||
#include "sparrowrenderer.h"
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
|
||||
#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<T> &data, BufferType type, bool isDynamic = false);
|
||||
~TBuffer();
|
||||
TBuffer(const std::vector<T> &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
|
||||
|
Loading…
x
Reference in New Issue
Block a user