2019-08-09 04:18:08 +00:00
|
|
|
|
2019-08-10 00:32:08 +00:00
|
|
|
#include "Precomp.h"
|
2019-08-09 04:18:08 +00:00
|
|
|
#include "VertexBuffer.h"
|
2019-08-16 11:07:57 +00:00
|
|
|
#include "Shader.h"
|
2019-08-09 04:18:08 +00:00
|
|
|
|
2019-08-16 09:24:22 +00:00
|
|
|
VertexBuffer::VertexBuffer()
|
2019-08-09 04:18:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-10 05:46:29 +00:00
|
|
|
VertexBuffer::~VertexBuffer()
|
|
|
|
{
|
2019-12-18 01:27:49 +00:00
|
|
|
// To do: release its slot in RenderDevice (note: this might be called by a finalizer in a different thread)
|
2019-08-10 05:46:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 01:27:49 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
SharedVertexBuffer::SharedVertexBuffer(VertexFormat format, int64_t size) : Format(format), Size(size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint SharedVertexBuffer::GetBuffer()
|
2019-08-10 05:46:29 +00:00
|
|
|
{
|
|
|
|
if (mBuffer == 0)
|
|
|
|
glGenBuffers(1, &mBuffer);
|
|
|
|
return mBuffer;
|
|
|
|
}
|
|
|
|
|
2019-12-18 01:27:49 +00:00
|
|
|
GLuint SharedVertexBuffer::GetVAO()
|
2019-08-16 11:07:57 +00:00
|
|
|
{
|
|
|
|
if (!mVAO)
|
|
|
|
{
|
|
|
|
glGenVertexArrays(1, &mVAO);
|
|
|
|
glBindVertexArray(mVAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, GetBuffer());
|
|
|
|
if (Format == VertexFormat::Flat)
|
|
|
|
SetupFlatVAO();
|
|
|
|
else
|
|
|
|
SetupWorldVAO();
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
return mVAO;
|
|
|
|
}
|
|
|
|
|
2019-12-18 01:27:49 +00:00
|
|
|
void SharedVertexBuffer::SetupFlatVAO()
|
2019-08-16 11:07:57 +00:00
|
|
|
{
|
|
|
|
glEnableVertexAttribArray((int)DeclarationUsage::Position);
|
|
|
|
glEnableVertexAttribArray((int)DeclarationUsage::Color);
|
|
|
|
glEnableVertexAttribArray((int)DeclarationUsage::TextureCoordinate);
|
|
|
|
glVertexAttribPointer((int)DeclarationUsage::Position, 3, GL_FLOAT, GL_FALSE, FlatStride, (const void*)0);
|
|
|
|
glVertexAttribPointer((int)DeclarationUsage::Color, GL_BGRA, GL_UNSIGNED_BYTE, GL_TRUE, FlatStride, (const void*)12);
|
|
|
|
glVertexAttribPointer((int)DeclarationUsage::TextureCoordinate, 2, GL_FLOAT, GL_FALSE, FlatStride, (const void*)16);
|
|
|
|
}
|
|
|
|
|
2019-12-18 01:27:49 +00:00
|
|
|
void SharedVertexBuffer::SetupWorldVAO()
|
2019-08-16 11:07:57 +00:00
|
|
|
{
|
|
|
|
glEnableVertexAttribArray((int)DeclarationUsage::Position);
|
|
|
|
glEnableVertexAttribArray((int)DeclarationUsage::Color);
|
|
|
|
glEnableVertexAttribArray((int)DeclarationUsage::TextureCoordinate);
|
|
|
|
glEnableVertexAttribArray((int)DeclarationUsage::Normal);
|
|
|
|
glVertexAttribPointer((int)DeclarationUsage::Position, 3, GL_FLOAT, GL_FALSE, WorldStride, (const void*)0);
|
|
|
|
glVertexAttribPointer((int)DeclarationUsage::Color, GL_BGRA, GL_UNSIGNED_BYTE, GL_TRUE, WorldStride, (const void*)12);
|
|
|
|
glVertexAttribPointer((int)DeclarationUsage::TextureCoordinate, 2, GL_FLOAT, GL_FALSE, WorldStride, (const void*)16);
|
|
|
|
glVertexAttribPointer((int)DeclarationUsage::Normal, 3, GL_FLOAT, GL_FALSE, WorldStride, (const void*)24);
|
|
|
|
}
|
|
|
|
|
2019-08-09 04:18:08 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-08-30 08:30:28 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
2019-08-16 09:24:22 +00:00
|
|
|
VertexBuffer* VertexBuffer_New()
|
2019-08-09 04:18:08 +00:00
|
|
|
{
|
2019-08-16 09:24:22 +00:00
|
|
|
return new VertexBuffer();
|
2019-08-09 04:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexBuffer_Delete(VertexBuffer* buffer)
|
|
|
|
{
|
2019-08-17 01:08:34 +00:00
|
|
|
//delete buffer;
|
2019-08-09 04:18:08 +00:00
|
|
|
}
|
2019-08-30 08:30:28 +00:00
|
|
|
|
|
|
|
}
|