Remove code duplication in CGLxxDevice

1008-fix
AbigailBuccaneer 2018-05-03 18:14:55 +01:00
parent 10fc47476b
commit 94b30c00a0
7 changed files with 152 additions and 470 deletions

View File

@ -92,6 +92,13 @@ struct VertexFormat
VertexAttribute tex2{}; VertexAttribute tex2{};
}; };
enum VertexType
{
VERTEX_TYPE_NORMAL,
VERTEX_TYPE_TEX2,
VERTEX_TYPE_COL,
};
/** /**
* \struct Vertex * \struct Vertex
* \brief Vertex of a primitive * \brief Vertex of a primitive
@ -105,6 +112,8 @@ struct VertexFormat
*/ */
struct Vertex struct Vertex
{ {
static constexpr VertexType VERTEX_TYPE = VERTEX_TYPE_NORMAL;
Math::Vector coord; Math::Vector coord;
Math::Vector normal; Math::Vector normal;
Math::Point texCoord; Math::Point texCoord;
@ -137,6 +146,8 @@ struct Vertex
*/ */
struct VertexCol struct VertexCol
{ {
static constexpr VertexType VERTEX_TYPE = VERTEX_TYPE_COL;
Math::Vector coord; Math::Vector coord;
Color color; Color color;
@ -166,6 +177,8 @@ struct VertexCol
*/ */
struct VertexTex2 struct VertexTex2
{ {
static constexpr VertexType VERTEX_TYPE = VERTEX_TYPE_TEX2;
Math::Vector coord; Math::Vector coord;
Math::Vector normal; Math::Vector normal;
Math::Point texCoord; Math::Point texCoord;

View File

@ -1635,7 +1635,8 @@ void CGL14Device::DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
} }
unsigned int CGL14Device::CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) template <typename Vertex>
unsigned int CGL14Device::CreateStaticBufferImpl(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount)
{ {
unsigned int id = 0; unsigned int id = 0;
if (m_vertexBufferType != VBT_DISPLAY_LIST) if (m_vertexBufferType != VBT_DISPLAY_LIST)
@ -1644,7 +1645,7 @@ unsigned int CGL14Device::CreateStaticBuffer(PrimitiveType primitiveType, const
VboObjectInfo info; VboObjectInfo info;
info.primitiveType = primitiveType; info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_NORMAL; info.vertexType = Vertex::VERTEX_TYPE;
info.vertexCount = vertexCount; info.vertexCount = vertexCount;
info.bufferId = 0; info.bufferId = 0;
@ -1669,75 +1670,8 @@ unsigned int CGL14Device::CreateStaticBuffer(PrimitiveType primitiveType, const
return id; return id;
} }
unsigned int CGL14Device::CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) template <typename Vertex>
{ void CGL14Device::UpdateStaticBufferImpl(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount)
unsigned int id = 0;
if (m_vertexBufferType != VBT_DISPLAY_LIST)
{
id = ++m_lastVboId;
VboObjectInfo info;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_TEX2;
info.vertexCount = vertexCount;
info.bufferId = 0;
m_glGenBuffers(1, &info.bufferId);
m_glBindBuffer(GL_ARRAY_BUFFER, info.bufferId);
m_glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(VertexTex2), vertices, GL_STATIC_DRAW);
m_glBindBuffer(GL_ARRAY_BUFFER, 0);
m_vboObjects[id] = info;
}
else
{
id = glGenLists(1);
glNewList(id, GL_COMPILE);
DrawPrimitive(primitiveType, vertices, vertexCount);
glEndList();
}
return id;
}
unsigned int CGL14Device::CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount)
{
unsigned int id = 0;
if (m_vertexBufferType != VBT_DISPLAY_LIST)
{
id = ++m_lastVboId;
VboObjectInfo info;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_COL;
info.vertexCount = vertexCount;
info.bufferId = 0;
m_glGenBuffers(1, &info.bufferId);
m_glBindBuffer(GL_ARRAY_BUFFER, info.bufferId);
m_glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(VertexCol), vertices, GL_STATIC_DRAW);
m_glBindBuffer(GL_ARRAY_BUFFER, 0);
m_vboObjects[id] = info;
}
else
{
id = glGenLists(1);
glNewList(id, GL_COMPILE);
DrawPrimitive(primitiveType, vertices, vertexCount);
glEndList();
}
return id;
}
void CGL14Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount)
{ {
if (m_vertexBufferType != VBT_DISPLAY_LIST) if (m_vertexBufferType != VBT_DISPLAY_LIST)
{ {
@ -1747,7 +1681,7 @@ void CGL14Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
VboObjectInfo& info = (*it).second; VboObjectInfo& info = (*it).second;
info.primitiveType = primitiveType; info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_NORMAL; info.vertexType = Vertex::VERTEX_TYPE;
info.vertexCount = vertexCount; info.vertexCount = vertexCount;
m_glBindBuffer(GL_ARRAY_BUFFER, info.bufferId); m_glBindBuffer(GL_ARRAY_BUFFER, info.bufferId);
@ -1764,60 +1698,6 @@ void CGL14Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
} }
} }
void CGL14Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount)
{
if (m_vertexBufferType != VBT_DISPLAY_LIST)
{
auto it = m_vboObjects.find(bufferId);
if (it == m_vboObjects.end())
return;
VboObjectInfo& info = (*it).second;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_TEX2;
info.vertexCount = vertexCount;
m_glBindBuffer(GL_ARRAY_BUFFER, info.bufferId);
m_glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(VertexTex2), vertices, GL_STATIC_DRAW);
m_glBindBuffer(GL_ARRAY_BUFFER, 0);
}
else
{
glNewList(bufferId, GL_COMPILE);
DrawPrimitive(primitiveType, vertices, vertexCount);
glEndList();
}
}
void CGL14Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount)
{
if (m_vertexBufferType != VBT_DISPLAY_LIST)
{
auto it = m_vboObjects.find(bufferId);
if (it == m_vboObjects.end())
return;
VboObjectInfo& info = (*it).second;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_COL;
info.vertexCount = vertexCount;
m_glBindBuffer(GL_ARRAY_BUFFER, info.bufferId);
m_glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(VertexCol), vertices, GL_STATIC_DRAW);
m_glBindBuffer(GL_ARRAY_BUFFER, 0);
}
else
{
glNewList(bufferId, GL_COMPILE);
DrawPrimitive(primitiveType, vertices, vertexCount);
glEndList();
}
}
void CGL14Device::DrawStaticBuffer(unsigned int bufferId) void CGL14Device::DrawStaticBuffer(unsigned int bufferId)
{ {
if (m_vertexBufferType != VBT_DISPLAY_LIST) if (m_vertexBufferType != VBT_DISPLAY_LIST)

View File

@ -139,12 +139,31 @@ public:
virtual void DrawPrimitives(PrimitiveType type, const VertexCol *vertices, virtual void DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
int first[], int count[], int drawCount) override; int first[], int count[], int drawCount) override;
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override; unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override; {
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override; return CreateStaticBufferImpl(primitiveType, vertices, vertexCount);
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override; }
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override; unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override; {
return CreateStaticBufferImpl(primitiveType, vertices, vertexCount);
}
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override
{
return CreateStaticBufferImpl(primitiveType, vertices, vertexCount);
}
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override
{
UpdateStaticBufferImpl(bufferId, primitiveType, vertices, vertexCount);
}
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override
{
UpdateStaticBufferImpl(bufferId, primitiveType, vertices, vertexCount);
}
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override
{
UpdateStaticBufferImpl(bufferId, primitiveType, vertices, vertexCount);
}
void DrawStaticBuffer(unsigned int bufferId) override; void DrawStaticBuffer(unsigned int bufferId) override;
void DestroyStaticBuffer(unsigned int bufferId) override; void DestroyStaticBuffer(unsigned int bufferId) override;
@ -214,6 +233,11 @@ private:
//! Disables shadows //! Disables shadows
void DisableShadows(); void DisableShadows();
template <typename Vertex>
unsigned int CreateStaticBufferImpl(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount);
template <typename Vertex>
void UpdateStaticBufferImpl(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount);
private: private:
//! Current config //! Current config
DeviceConfig m_config; DeviceConfig m_config;
@ -260,14 +284,6 @@ private:
//! Map of framebuffers //! Map of framebuffers
std::map<std::string, std::unique_ptr<CFramebuffer>> m_framebuffers; std::map<std::string, std::unique_ptr<CFramebuffer>> m_framebuffers;
//! Type of vertex structure
enum VertexType
{
VERTEX_TYPE_NORMAL,
VERTEX_TYPE_TEX2,
VERTEX_TYPE_COL,
};
//! Info about static VBO buffers //! Info about static VBO buffers
struct VboObjectInfo struct VboObjectInfo
{ {

View File

@ -1443,13 +1443,15 @@ void CGL21Device::DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
} }
unsigned int CGL21Device::CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount)
template <typename Vertex>
unsigned int CGL21Device::CreateStaticBufferImpl(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount)
{ {
unsigned int id = ++m_lastVboId; unsigned int id = ++m_lastVboId;
VboObjectInfo info; VboObjectInfo info;
info.primitiveType = primitiveType; info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_NORMAL; info.vertexType = Vertex::VERTEX_TYPE;
info.vertexCount = vertexCount; info.vertexCount = vertexCount;
info.bufferId = 0; info.bufferId = 0;
info.size = vertexCount * sizeof(Vertex); info.size = vertexCount * sizeof(Vertex);
@ -1463,47 +1465,8 @@ unsigned int CGL21Device::CreateStaticBuffer(PrimitiveType primitiveType, const
return id; return id;
} }
unsigned int CGL21Device::CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) template <typename Vertex>
{ void CGL21Device::UpdateStaticBufferImpl(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount)
unsigned int id = ++m_lastVboId;
VboObjectInfo info;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_TEX2;
info.vertexCount = vertexCount;
info.bufferId = 0;
info.size = vertexCount * sizeof(VertexTex2);
glGenBuffers(1, &info.bufferId);
BindVBO(info.bufferId);
glBufferData(GL_ARRAY_BUFFER, info.size, vertices, GL_STATIC_DRAW);
m_vboObjects[id] = info;
return id;
}
unsigned int CGL21Device::CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount)
{
unsigned int id = ++m_lastVboId;
VboObjectInfo info;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_COL;
info.vertexCount = vertexCount;
info.bufferId = 0;
info.size = vertexCount * sizeof(VertexCol);
glGenBuffers(1, &info.bufferId);
BindVBO(info.bufferId);
glBufferData(GL_ARRAY_BUFFER, info.size, vertices, GL_STATIC_DRAW);
m_vboObjects[id] = info;
return id;
}
void CGL21Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount)
{ {
auto it = m_vboObjects.find(bufferId); auto it = m_vboObjects.find(bufferId);
if (it == m_vboObjects.end()) if (it == m_vboObjects.end())
@ -1513,7 +1476,7 @@ void CGL21Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
VboObjectInfo& info = (*it).second; VboObjectInfo& info = (*it).second;
info.primitiveType = primitiveType; info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_NORMAL; info.vertexType = Vertex::VERTEX_TYPE;
info.vertexCount = vertexCount; info.vertexCount = vertexCount;
BindVBO(info.bufferId); BindVBO(info.bufferId);
@ -1529,58 +1492,6 @@ void CGL21Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
} }
} }
void CGL21Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount)
{
auto it = m_vboObjects.find(bufferId);
if (it == m_vboObjects.end())
return;
VboObjectInfo& info = (*it).second;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_TEX2;
info.vertexCount = vertexCount;
int newSize = vertexCount * sizeof(VertexTex2);
BindVBO(info.bufferId);
if (info.size < newSize)
{
glBufferData(GL_ARRAY_BUFFER, newSize, vertices, GL_STATIC_DRAW);
info.size = newSize;
}
else
{
glBufferSubData(GL_ARRAY_BUFFER, 0, newSize, vertices);
}
}
void CGL21Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount)
{
auto it = m_vboObjects.find(bufferId);
if (it == m_vboObjects.end())
return;
VboObjectInfo& info = (*it).second;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_COL;
info.vertexCount = vertexCount;
int newSize = vertexCount * sizeof(VertexCol);
BindVBO(info.bufferId);
if (info.size < newSize)
{
glBufferData(GL_ARRAY_BUFFER, newSize, vertices, GL_STATIC_DRAW);
info.size = newSize;
}
else
{
glBufferSubData(GL_ARRAY_BUFFER, 0, newSize, vertices);
}
}
void CGL21Device::DrawStaticBuffer(unsigned int bufferId) void CGL21Device::DrawStaticBuffer(unsigned int bufferId)
{ {
auto it = m_vboObjects.find(bufferId); auto it = m_vboObjects.find(bufferId);

View File

@ -120,12 +120,30 @@ public:
virtual void DrawPrimitives(PrimitiveType type, const VertexCol *vertices, virtual void DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
int first[], int count[], int drawCount) override; int first[], int count[], int drawCount) override;
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override; unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override; {
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override; return CreateStaticBufferImpl(primitiveType, vertices, vertexCount);
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override; }
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override; unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override; {
return CreateStaticBufferImpl(primitiveType, vertices, vertexCount);
}
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override
{
return CreateStaticBufferImpl(primitiveType, vertices, vertexCount);
}
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override
{
UpdateStaticBufferImpl(bufferId, primitiveType, vertices, vertexCount);
}
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override
{
UpdateStaticBufferImpl(bufferId, primitiveType, vertices, vertexCount);
}
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override
{
UpdateStaticBufferImpl(bufferId, primitiveType, vertices, vertexCount);
}
void DrawStaticBuffer(unsigned int bufferId) override; void DrawStaticBuffer(unsigned int bufferId) override;
void DestroyStaticBuffer(unsigned int bufferId) override; void DestroyStaticBuffer(unsigned int bufferId) override;
@ -192,6 +210,11 @@ private:
//! Binds texture //! Binds texture
inline void BindTexture(int index, GLuint texture); inline void BindTexture(int index, GLuint texture);
template <typename Vertex>
unsigned int CreateStaticBufferImpl(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount);
template <typename Vertex>
void UpdateStaticBufferImpl(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount);
private: private:
//! Current config //! Current config
DeviceConfig m_config; DeviceConfig m_config;
@ -232,14 +255,6 @@ private:
//! Map of framebuffers //! Map of framebuffers
std::map<std::string, std::unique_ptr<CFramebuffer>> m_framebuffers; std::map<std::string, std::unique_ptr<CFramebuffer>> m_framebuffers;
//! Type of vertex structure
enum VertexType
{
VERTEX_TYPE_NORMAL,
VERTEX_TYPE_TEX2,
VERTEX_TYPE_COL,
};
//! Info about static VBO buffers //! Info about static VBO buffers
struct VboObjectInfo struct VboObjectInfo
{ {

View File

@ -1358,27 +1358,12 @@ void CGL33Device::DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
glMultiDrawArrays(TranslateGfxPrimitive(type), first, count, drawCount); glMultiDrawArrays(TranslateGfxPrimitive(type), first, count, drawCount);
} }
unsigned int CGL33Device::CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) namespace
{ {
unsigned int id = 0; template <typename Vertex> void SetVertexAttributes();
id = ++m_lastVboId;
VertexBufferInfo info;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_NORMAL;
info.vertexCount = vertexCount;
info.size = vertexCount * sizeof(Vertex);
glGenVertexArrays(1, &info.vao);
BindVAO(info.vao);
glGenBuffers(1, &info.vbo);
BindVBO(info.vbo);
glBufferData(GL_ARRAY_BUFFER, info.size, vertices, GL_STATIC_DRAW);
m_vboMemory += info.size;
template <> void SetVertexAttributes<Vertex>()
{
// Vertex coordinate // Vertex coordinate
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, coord))); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, coord)));
@ -1398,33 +1383,10 @@ unsigned int CGL33Device::CreateStaticBuffer(PrimitiveType primitiveType, const
// Texture coordinate 1 // Texture coordinate 1
glDisableVertexAttribArray(4); glDisableVertexAttribArray(4);
glVertexAttrib2f(4, 0.0f, 0.0f); glVertexAttrib2f(4, 0.0f, 0.0f);
m_vboObjects[id] = info;
return id;
} }
unsigned int CGL33Device::CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) template <> void SetVertexAttributes<VertexTex2>()
{ {
unsigned int id = 0;
id = ++m_lastVboId;
VertexBufferInfo info;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_TEX2;
info.vertexCount = vertexCount;
info.size = vertexCount * sizeof(VertexTex2);
glGenVertexArrays(1, &info.vao);
BindVAO(info.vao);
glGenBuffers(1, &info.vbo);
BindVBO(info.vbo);
glBufferData(GL_ARRAY_BUFFER, info.size, vertices, GL_STATIC_DRAW);
m_vboMemory += info.size;
// Vertex coordinate // Vertex coordinate
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, coord))); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, coord)));
@ -1444,31 +1406,10 @@ unsigned int CGL33Device::CreateStaticBuffer(PrimitiveType primitiveType, const
// Texture coordinate 1 // Texture coordinate 1
glEnableVertexAttribArray(4); glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord2))); glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord2)));
m_vboObjects[id] = info;
return id;
} }
unsigned int CGL33Device::CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) template <> void SetVertexAttributes<VertexCol>()
{ {
unsigned int id = ++m_lastVboId;
VertexBufferInfo info;
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_COL;
info.vertexCount = vertexCount;
info.size = vertexCount * sizeof(VertexCol);
glGenVertexArrays(1, &info.vao);
BindVAO(info.vao);
glGenBuffers(1, &info.vbo);
BindVBO(info.vbo);
glBufferData(GL_ARRAY_BUFFER, info.size, vertices, GL_STATIC_DRAW);
m_vboMemory += info.size;
// Vertex coordinate // Vertex coordinate
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, coord))); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, coord)));
@ -1488,13 +1429,40 @@ unsigned int CGL33Device::CreateStaticBuffer(PrimitiveType primitiveType, const
// Texture coordinate 1 // Texture coordinate 1
glDisableVertexAttribArray(4); glDisableVertexAttribArray(4);
glVertexAttrib2f(4, 0.0f, 0.0f); glVertexAttrib2f(4, 0.0f, 0.0f);
}
} // namespace
template <typename Vertex>
unsigned int CGL33Device::CreateStaticBufferImpl(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount)
{
unsigned int id = 0;
id = ++m_lastVboId;
VertexBufferInfo info;
info.primitiveType = primitiveType;
info.vertexType = Vertex::VERTEX_TYPE;
info.vertexCount = vertexCount;
info.size = vertexCount * sizeof(Vertex);
glGenVertexArrays(1, &info.vao);
BindVAO(info.vao);
glGenBuffers(1, &info.vbo);
BindVBO(info.vbo);
glBufferData(GL_ARRAY_BUFFER, info.size, vertices, GL_STATIC_DRAW);
m_vboMemory += info.size;
SetVertexAttributes<Vertex>();
m_vboObjects[id] = info; m_vboObjects[id] = info;
return id; return id;
} }
void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) template <typename Vertex>
void CGL33Device::UpdateStaticBufferImpl(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount)
{ {
auto it = m_vboObjects.find(bufferId); auto it = m_vboObjects.find(bufferId);
if (it == m_vboObjects.end()) if (it == m_vboObjects.end())
@ -1504,12 +1472,12 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
unsigned int size = vertexCount * sizeof(Vertex); unsigned int size = vertexCount * sizeof(Vertex);
bool changed = (info.vertexType != VERTEX_TYPE_NORMAL) || (size > info.size); bool changed = (info.vertexType != Vertex::VERTEX_TYPE) || (size > info.size);
if (info.vertexType != VERTEX_TYPE_NORMAL) CLogger::GetInstance().Debug("Changing static buffer type\n"); if (info.vertexType != Vertex::VERTEX_TYPE) CLogger::GetInstance().Debug("Changing static buffer type\n");
info.primitiveType = primitiveType; info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_NORMAL; info.vertexType = Vertex::VERTEX_TYPE;
info.vertexCount = vertexCount; info.vertexCount = vertexCount;
BindVBO(info.vbo); BindVBO(info.vbo);
@ -1531,143 +1499,7 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
{ {
BindVAO(info.vao); BindVAO(info.vao);
// Vertex coordinate SetVertexAttributes<Vertex>();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, coord)));
// Normal
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));
// Color
glDisableVertexAttribArray(2);
glVertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
// Texture coordinate 0
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, texCoord)));
// Texture coordinate 1
glDisableVertexAttribArray(4);
glVertexAttrib2f(4, 0.0f, 0.0f);
}
}
void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount)
{
auto it = m_vboObjects.find(bufferId);
if (it == m_vboObjects.end())
return;
VertexBufferInfo& info = (*it).second;
unsigned int size = vertexCount * sizeof(VertexTex2);
bool changed = (info.vertexType != VERTEX_TYPE_TEX2) || (size > info.size);
if (info.vertexType != VERTEX_TYPE_TEX2) CLogger::GetInstance().Debug("Changing static buffer type\n");
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_TEX2;
info.vertexCount = vertexCount;
BindVBO(info.vbo);
if (info.size < size)
{
CLogger::GetInstance().Debug("Resizing static buffer: %d->%d\n", info.size, size);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
m_vboMemory -= info.size;
info.size = size;
m_vboMemory += info.size;
}
else
{
glBufferSubData(GL_ARRAY_BUFFER, 0, size, vertices);
}
if (changed) // Update vertex array bindings
{
BindVAO(info.vao);
// Vertex coordinate
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, coord)));
// Normal
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, normal)));
// Color
glDisableVertexAttribArray(2);
glVertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
// Texture coordinate 0
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord)));
// Texture coordinate 1
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord2)));
}
}
void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount)
{
auto it = m_vboObjects.find(bufferId);
if (it == m_vboObjects.end())
return;
VertexBufferInfo& info = (*it).second;
unsigned int size = vertexCount * sizeof(VertexCol);
bool changed = (info.vertexType != VERTEX_TYPE_COL) || (size > info.size);
if (info.vertexType != VERTEX_TYPE_NORMAL) CLogger::GetInstance().Debug("Changing static buffer type\n");
info.primitiveType = primitiveType;
info.vertexType = VERTEX_TYPE_COL;
info.vertexCount = vertexCount;
BindVBO(info.vbo);
if (info.size < size)
{
CLogger::GetInstance().Debug("Resizing static buffer: %d->%d\n", info.size, size);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
m_vboMemory -= info.size;
info.size = size;
m_vboMemory += info.size;
}
else
{
glBufferSubData(GL_ARRAY_BUFFER, 0, size, vertices);
}
if (changed) // Update vertex array bindings
{
BindVAO(info.vao);
// Vertex coordinate
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, coord)));
// Normal
glDisableVertexAttribArray(1);
glVertexAttrib3f(1, 0.0f, 0.0f, 1.0f);
// Color
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, color)));
// Texture coordinate 0
glDisableVertexAttribArray(3);
glVertexAttrib2f(3, 0.0f, 0.0f);
// Texture coordinate 1
glDisableVertexAttribArray(4);
glVertexAttrib2f(4, 0.0f, 0.0f);
} }
} }

View File

@ -135,12 +135,30 @@ public:
virtual void DrawPrimitives(PrimitiveType type, const VertexCol *vertices, virtual void DrawPrimitives(PrimitiveType type, const VertexCol *vertices,
int first[], int count[], int drawCount) override; int first[], int count[], int drawCount) override;
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override; unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override; {
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override; return CreateStaticBufferImpl(primitiveType, vertices, vertexCount);
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override; }
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override; unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override; {
return CreateStaticBufferImpl(primitiveType, vertices, vertexCount);
}
unsigned int CreateStaticBuffer(PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override
{
return CreateStaticBufferImpl(primitiveType, vertices, vertexCount);
}
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount) override
{
UpdateStaticBufferImpl(bufferId, primitiveType, vertices, vertexCount);
}
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount) override
{
UpdateStaticBufferImpl(bufferId, primitiveType, vertices, vertexCount);
}
void UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount) override
{
UpdateStaticBufferImpl(bufferId, primitiveType, vertices, vertexCount);
}
void DrawStaticBuffer(unsigned int bufferId) override; void DrawStaticBuffer(unsigned int bufferId) override;
void DestroyStaticBuffer(unsigned int bufferId) override; void DestroyStaticBuffer(unsigned int bufferId) override;
@ -215,6 +233,11 @@ private:
inline void UpdateVertexAttribute(int index, const VertexAttribute &attribute, int offset); inline void UpdateVertexAttribute(int index, const VertexAttribute &attribute, int offset);
template <typename Vertex>
unsigned int CreateStaticBufferImpl(PrimitiveType primitiveType, const Vertex* vertices, int vertexCount);
template <typename Vertex>
void UpdateStaticBufferImpl(unsigned int bufferId, PrimitiveType primitiveType, const Vertex* vertices, int vertexCount);
private: private:
//! Current config //! Current config
DeviceConfig m_config; DeviceConfig m_config;
@ -256,14 +279,6 @@ private:
//! Free texture unit //! Free texture unit
const int m_freeTexture = 3; const int m_freeTexture = 3;
//! Type of vertex structure
enum VertexType
{
VERTEX_TYPE_NORMAL,
VERTEX_TYPE_TEX2,
VERTEX_TYPE_COL,
};
//! Info about static VBO buffers //! Info about static VBO buffers
struct VertexBufferInfo struct VertexBufferInfo
{ {