From 4e9c0f3c3e04e5edaef2364fc37090e21d86c0b4 Mon Sep 17 00:00:00 2001 From: codereader Date: Sat, 29 Jan 2022 04:49:19 +0100 Subject: [PATCH] #5893: Code cleanup --- .../rendersystem/backend/GeometryRenderer.h | 67 +++---------------- 1 file changed, 11 insertions(+), 56 deletions(-) diff --git a/radiantcore/rendersystem/backend/GeometryRenderer.h b/radiantcore/rendersystem/backend/GeometryRenderer.h index d9b2689b7b..5a5772f481 100644 --- a/radiantcore/rendersystem/backend/GeometryRenderer.h +++ b/radiantcore/rendersystem/backend/GeometryRenderer.h @@ -55,7 +55,6 @@ class GeometryRenderer : glMultiDrawElementsBaseVertex(_mode, _sizes.data(), GL_UNSIGNED_INT, &_firstIndices.front(), static_cast(_sizes.size()), &_firstVertices.front()); - //glDrawElements(_mode, static_cast(_indices.size()), GL_UNSIGNED_INT, &_indices.front()); } void renderSurface(std::size_t surfaceIndex) const @@ -63,9 +62,7 @@ class GeometryRenderer : glVertexPointer(3, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &_vertices.front().vertex); glTexCoordPointer(2, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &_vertices.front().texcoord); glNormalPointer(GL_DOUBLE, sizeof(ArbitraryMeshVertex), &_vertices.front().normal); -#if 0 - glDrawElements(_mode, static_cast(numIndices), GL_UNSIGNED_INT, &_indices[firstIndex]); -#endif + glDrawElementsBaseVertex(_mode, _sizes[surfaceIndex], GL_UNSIGNED_INT, _firstIndices[surfaceIndex], _firstVertices[surfaceIndex]); } @@ -74,36 +71,25 @@ class GeometryRenderer : std::size_t addSurface(const std::vector& vertices, const std::vector& indices) { -#if 0 - auto vertexOffset = _vertices.size(); - auto indexOffset = _indices.size(); -#endif - auto numIndices = indices.size(); - _sizes.push_back(static_cast(numIndices)); + _sizes.push_back(static_cast(indices.size())); _firstVertices.push_back(static_cast(_vertices.size())); std::copy(vertices.begin(), vertices.end(), std::back_inserter(_vertices)); std::copy(indices.begin(), indices.end(), std::back_inserter(_indices)); // Rebuild the index array to point at the (most likely) new memory locations - _firstIndices.resize(_sizes.size()); + auto numSurfaces = _sizes.size(); + _firstIndices.resize(numSurfaces); auto currentLocation = const_cast(&_indices.front()); - for (auto i = 0; i < _sizes.size(); ++i) + for (auto i = 0; i < numSurfaces; ++i) { _firstIndices[i] = currentLocation; currentLocation += _sizes[i]; } - return _sizes.size() - 1; -#if 0 - for (auto index : indices) - { - _indices.push_back(index + static_cast(vertexOffset)); - } - return { vertexOffset, indexOffset }; -#endif + return numSurfaces - 1; } void updateSurface(std::size_t surfaceIndex, @@ -119,20 +105,10 @@ class GeometryRenderer : // Copy the data to the correct slot in the array std::copy(vertices.begin(), vertices.end(), _vertices.begin() + firstVertex); std::copy(indices.begin(), indices.end(), _indices.begin() + firstIndex); -#if 0 - // Before assignment, the indices need to be shifted to match the array offset of the vertices - auto targetIndex = _indices.begin() + firstIndex; - auto indexShift = static_cast(firstVertex); - - for (auto index : indices) - { - *targetIndex++ = index + indexShift; - } -#endif } - // Cuts out the vertices and indices, adjusts all indices located to the right of the cut - void removeSurface(std::size_t surfaceIndex, /*std::size_t firstVertex, */std::size_t numVertices/*, std::size_t firstIndex, std::size_t numIndices*/) + // Cuts out the vertices and indices, adjusts all metadata + void removeSurface(std::size_t surfaceIndex, std::size_t numVertices) { auto firstVertex = _firstVertices.at(surfaceIndex); @@ -147,7 +123,7 @@ class GeometryRenderer : auto firstIndexToRemove = _indices.begin() + firstIndex; _indices.erase(firstIndexToRemove, firstIndexToRemove + _sizes.at(surfaceIndex)); - // Adjust the metadata + // Adjust the metadata needed for glMultiDrawElementsBaseVertex // Shift the vertex starts by the amount of removed vertices auto firstVertexToAdjust = _firstVertices.begin() + surfaceIndex; @@ -159,7 +135,7 @@ class GeometryRenderer : _firstVertices.erase(_firstVertices.begin() + surfaceIndex); - // Shift the index start pointers + // Shift the index start pointers, assuming that the buffer has not been reallocated auto firstIndexToAdjust = _firstIndices.begin() + surfaceIndex; auto numIndicesRemoved = _sizes.at(surfaceIndex); @@ -172,22 +148,6 @@ class GeometryRenderer : // Index size is reduced by one _sizes.erase(_sizes.begin() + surfaceIndex); - -#if 0 - // Shift all indices to the left, offsetting their values by the number of removed vertices - auto offsetToApply = -static_cast(numVertices); - - auto targetIndex = _indices.begin() + firstIndex; - auto indexToMove = targetIndex + numIndices; - - auto indexEnd = _indices.end(); - while (indexToMove != indexEnd) - { - *targetIndex++ = *indexToMove++ + offsetToApply; - } - // Cut off the tail of the indices - _indices.resize(_indices.size() - numIndices); -#endif } }; @@ -202,9 +162,7 @@ class GeometryRenderer : { std::uint8_t bucketIndex; std::size_t surfaceIndex; - //std::size_t firstVertex; std::size_t numVertices; - //std::size_t firstIndex; std::size_t numIndices; }; std::vector _slots; @@ -245,9 +203,7 @@ class GeometryRenderer : slot.surfaceIndex = buffer.addSurface(vertices, indices); slot.bucketIndex = bufferIndex; - //slot.firstVertex = vertexOffset; slot.numVertices = vertices.size(); - //slot.firstIndex = indexOffset; slot.numIndices = indices.size(); return newSlotIndex; @@ -274,8 +230,6 @@ class GeometryRenderer : // Invalidate the slot slotInfo.numVertices = 0; slotInfo.surfaceIndex = InvalidSurfaceIndex; - //slotInfo.firstVertex = InvalidVertexIndex; - //slotInfo.firstIndex = 0; slotInfo.numIndices = 0; if (slot < _freeSlotMappingHint) @@ -314,6 +268,7 @@ class GeometryRenderer : buffer.render(info.checkFlag(RENDER_BUMP)); } + glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); }