diff options
Diffstat (limited to 'Client/Source/Graphics/VertexBuffer.cpp')
-rw-r--r-- | Client/Source/Graphics/VertexBuffer.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/Client/Source/Graphics/VertexBuffer.cpp b/Client/Source/Graphics/VertexBuffer.cpp new file mode 100644 index 0000000..d95bf58 --- /dev/null +++ b/Client/Source/Graphics/VertexBuffer.cpp @@ -0,0 +1,109 @@ +#include "VertexBuffer.h" + +VertexBuffer::VertexBuffer(int vbSize, int ibSize, VertexBufferType type) +{ + m_VB = GPU::ClaimBuffer(vbSize, GL_STATIC_DRAW); + m_IB = GPU::ClaimBuffer(ibSize, GL_STATIC_DRAW); +} + +VertexBuffer::~VertexBuffer() +{ + GPU::ReleaseBuffer(m_VB); + GPU::ReleaseBuffer(m_IB); +} +// GetChunk +// <fill> +// FlushChunk +// DrawChunk +void VertexBuffer::GetChunk(uint sizePerVert, uint sizePerIndex, int maxVerts, int maxIndices, EPrimitive primitive, void **out_vb, void **out_ib) +{ + m_SizePerVertex = sizePerVert; + + uint vbufferSize = sizePerVert * maxVerts; + uint ibufferSize = sizePerIndex * maxIndices; + + GLenum usage = GL_STATIC_DRAW; + m_VB->Restore(vbufferSize, usage); + m_IB->Restore(ibufferSize, usage); + + const GLenum access = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_FLUSH_EXPLICIT_BIT; + + *out_vb = m_VB->MapRange(0, vbufferSize, access); + *out_ib = m_IB->MapRange(0, ibufferSize, access); + + m_Primitive = primitive; + + WipeGLError(); +} + +void VertexBuffer::FlushChunk(int actualVerts, int actualIndices) +{ + int actualVBufferSize = m_SizePerVertex * actualVerts; + int actualIBufferSize = VertexLayout::GetDefaultIndexSize() * actualIndices; + + m_CurIndexCount = actualIndices; + + m_VB->FlushMapedRange(0, actualVBufferSize); + m_IB->FlushMapedRange(0, actualIBufferSize); + + m_VB->UnMap(); + m_IB->UnMap(); + + WipeGLError(); +} + +void VertexBuffer::Draw(CustomVertexLayout& layout) +{ + const byte* basepointer = 0; + const GLuint buffer = m_VB->GetHandle(); + + FillCustomVertexLayout(layout); + + VertexLayout::SetupCustomVertexLayout(layout); + + layout.RestorePointer(); + + const void* indexPtr = 0; + + CheckGLError( + throw GLException(error); + ); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IB->GetHandle()); + + CheckGLError( + throw GLException(error); + ); + GLenum indexFormat = VertexLayout::GetDefaultIndexFormat(); + + switch (m_Primitive) + { + case Primitive_Triangle: + glDrawElements(GL_TRIANGLES, m_CurIndexCount, indexFormat, indexPtr); + break; + case Primitive_Line: + glDrawElements(GL_LINE, m_CurIndexCount, indexFormat, indexPtr); + break; + case Primitive_Point: + glDrawElements(GL_POINT, m_CurIndexCount, indexFormat, indexPtr); + break; + } + + CheckGLError( + throw GLException(error); + ); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} + +void VertexBuffer::FillCustomVertexLayout(CustomVertexLayout& dst) +{ + const byte* basepointer = 0; + const GLuint buffer = m_VB->GetHandle(); + + dst.buffer = buffer; + + for (int i = 0; i < dst.attributes.size(); ++i) + { + int offset = dst.attributes[i].startOffset; + dst.attributes[i].pointer = basepointer + offset; + } +}
\ No newline at end of file |