2019-12-22 23:13:09 +00:00
|
|
|
/*
|
|
|
|
** BuilderNative Renderer
|
|
|
|
** Copyright (c) 2019 Magnus Norddahl
|
|
|
|
**
|
|
|
|
** This software is provided 'as-is', without any express or implied
|
|
|
|
** warranty. In no event will the authors be held liable for any damages
|
|
|
|
** arising from the use of this software.
|
|
|
|
**
|
|
|
|
** Permission is granted to anyone to use this software for any purpose,
|
|
|
|
** including commercial applications, and to alter it and redistribute it
|
|
|
|
** freely, subject to the following restrictions:
|
|
|
|
**
|
|
|
|
** 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
** claim that you wrote the original software. If you use this software
|
|
|
|
** in a product, an acknowledgment in the product documentation would be
|
|
|
|
** appreciated but is not required.
|
|
|
|
** 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
** misrepresented as being the original software.
|
|
|
|
** 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
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-12-22 22:44:58 +00:00
|
|
|
#include "RenderDevice.h"
|
2019-08-09 04:18:08 +00:00
|
|
|
|
2019-12-22 22:44:58 +00:00
|
|
|
SharedVertexBuffer::SharedVertexBuffer(VertexFormat format, int size) : Format(format), Size(size)
|
2019-12-18 01:27:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12-22 22:44:58 +00:00
|
|
|
VertexBuffer::~VertexBuffer()
|
|
|
|
{
|
|
|
|
if (Device)
|
|
|
|
{
|
|
|
|
Device->mSharedVertexBuffers[(int)Format]->VertexBuffers.erase(ListIt);
|
|
|
|
Device = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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-12-22 22:44:58 +00:00
|
|
|
RenderDevice::DeleteObject(buffer);
|
2019-08-09 04:18:08 +00:00
|
|
|
}
|
2019-08-30 08:30:28 +00:00
|
|
|
|
|
|
|
}
|