From 855da86feae1a5cc14dc2d486ccf115f484dbc2e Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 25 Apr 2022 20:22:07 -0700 Subject: Changeset: 16 Initial work on rendering sprites to screen --- source/Mesh.cpp | 148 ++++++++++++++++++++------------------------------------ 1 file changed, 52 insertions(+), 96 deletions(-) (limited to 'source/Mesh.cpp') diff --git a/source/Mesh.cpp b/source/Mesh.cpp index 5b4f708..244e2e3 100644 --- a/source/Mesh.cpp +++ b/source/Mesh.cpp @@ -1,98 +1,54 @@ #include "Mesh.hpp" -#include - -GpuVertexBuffer::GpuVertexBuffer() { - glGenBuffers(1, &handle); -} - -GpuVertexBuffer::~GpuVertexBuffer() { - glDeleteBuffers(1, &handle); -} - -void GpuVertexBuffer::Upload(const std::byte* data, size_t sizeInBytes) { - glBindBuffer(GL_ARRAY_BUFFER, handle); - glBufferData(GL_ARRAY_BUFFER, sizeInBytes, data, GL_DYNAMIC_DRAW); -} - -GpuIndexBuffer::GpuIndexBuffer() { - glGenBuffers(1, &handle); -} - -GpuIndexBuffer::~GpuIndexBuffer() { - glDeleteBuffers(1, &handle); -} - -void GpuIndexBuffer::Upload(const std::byte* data, size_t count) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * Tags::SizeOf(indexType), data, GL_DYNAMIC_DRAW); -} - -void GpuIndexBuffer::Upload(const std::byte* data, Tags::IndexType type, size_t count) { - indexType = type; - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * Tags::SizeOf(type), data, GL_DYNAMIC_DRAW); -} - -int BufferBindings::GetMaxBindingIndex() const { - return bindings.size() - 1; -} - -GpuVertexBuffer* BufferBindings::GetBinding(int index) const { - if (index >= 0 && index < bindings.size()) { - return bindings[index].Get(); - } else { - return nullptr; - } -} - -void BufferBindings::SetBinding(int index, GpuVertexBuffer* buffer) { - int maxBindingIndex = GetMaxBindingIndex(); - if (index > maxBindingIndex) { - int countDelta = index - maxBindingIndex; - bindings.resize(bindings.size() + countDelta); - } - - bindings[index].Attach(buffer); - if (index == maxBindingIndex && buffer == nullptr) { - bindings.pop_back(); - } -} - -void BufferBindings::Clear() { - bindings.clear(); -} - -int VertexElementFormat::GetStride() const { - return Tags::SizeOf(type); -} - -void VertexFormat::AddElement(VertexElementFormat element) { - vertexSize += element.GetStride(); - - int lastIdx = (int)elements.size() - 1; - if (lastIdx >= 0) { - auto& last = elements[lastIdx]; - element.offset = last.offset + last.GetStride(); - } else { - element.offset = 0; - } - - elements.push_back(std::move(element)); -} - -void VertexFormat::RemoveElement(int index) { - auto& element = elements[index]; - vertexSize -= element.GetStride(); - elements.erase(elements.begin() + index); -} - -GpuMesh::GpuMesh() { -} - -GpuMesh::~GpuMesh() { -} - -bool GpuMesh::IsEmpty() const { - return vertFormat == nullptr || indexBuf == nullptr; -} +#include + +// StandardCpuMesh::StandardCpuMesh() +// : mGpuMesh(new GpuMesh()) { +// mGpuMesh->vertFormat = gVformatStandard; +// mGpuMesh->vertBufBindings.SetBinding(0, new GpuVertexBuffer()); +// mGpuMesh->vertBufBindings.SetBinding(1, new GpuVertexBuffer()); +// mGpuMesh->indexBuf.Attach(new GpuIndexBuffer()); +// } + +// StandardCpuMesh::~StandardCpuMesh() { +// delete mData; +// } + +// void StandardCpuMesh::CreateCpuData() { +// if (!mData) { +// mData = new StandardCpuMeshData(); +// } +// } + +// GpuVertexBuffer* StandardCpuMesh::GetPosBuffer() const { +// return mGpuMesh->vertBufBindings.bindings[0].Get(); +// } + +// GpuVertexBuffer* StandardCpuMesh::GetExtraBuffer() const { +// return mGpuMesh->vertBufBindings.bindings[1].Get(); +// } + +// bool StandardCpuMesh::UpdatePositions(glm::vec3* pos, size_t count, size_t startVertIndex) { +// if (mData) { +// std::memcpy(&mData->vertPositions[startVertIndex], pos, count * sizeof(glm::vec3)); +// } +// auto posBuf = GetPosBuffer(); +// glBindBuffer(GL_ARRAY_BUFFER, posBuf->handle); +// glBufferSubData(GL_ARRAY_BUFFER, startVertIndex * mGpuMesh->vertFormat->vertexSize, count * sizeof(glm::vec3), pos); +// return true; +// } + +// bool StandardCpuMesh::UpdateColors(RgbaColor* color, size_t count, size_t starVertIndex) { +// if (!mData) return false; +// // TODO +// } + +// bool StandardCpuMesh::UpdateNormals(glm::vec2* normals, size_t count, size_t startVertIndex) { +// if (!mData) return false; +// // TODO +// } + +// bool StandardCpuMesh::UpdateIndices(uint32_t* indices, size_t count, size_t startVertIndex) { +// if (!mData) return false; +// // TODO +// } -- cgit v1.2.3-70-g09d2