Skip to content

Commit

Permalink
#5893: Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 29, 2022
1 parent a249291 commit 4e9c0f3
Showing 1 changed file with 11 additions and 56 deletions.
67 changes: 11 additions & 56 deletions radiantcore/rendersystem/backend/GeometryRenderer.h
Expand Up @@ -55,17 +55,14 @@ class GeometryRenderer :

glMultiDrawElementsBaseVertex(_mode, _sizes.data(), GL_UNSIGNED_INT,
&_firstIndices.front(), static_cast<GLsizei>(_sizes.size()), &_firstVertices.front());
//glDrawElements(_mode, static_cast<GLsizei>(_indices.size()), GL_UNSIGNED_INT, &_indices.front());
}

void renderSurface(std::size_t surfaceIndex) const
{
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<GLsizei>(numIndices), GL_UNSIGNED_INT, &_indices[firstIndex]);
#endif

glDrawElementsBaseVertex(_mode, _sizes[surfaceIndex], GL_UNSIGNED_INT,
_firstIndices[surfaceIndex], _firstVertices[surfaceIndex]);
}
Expand All @@ -74,36 +71,25 @@ class GeometryRenderer :
std::size_t addSurface(const std::vector<ArbitraryMeshVertex>& vertices,
const std::vector<unsigned int>& indices)
{
#if 0
auto vertexOffset = _vertices.size();
auto indexOffset = _indices.size();
#endif
auto numIndices = indices.size();
_sizes.push_back(static_cast<GLsizei>(numIndices));
_sizes.push_back(static_cast<GLsizei>(indices.size()));
_firstVertices.push_back(static_cast<GLint>(_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<unsigned int*>(&_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<unsigned int>(vertexOffset));
}
return { vertexOffset, indexOffset };
#endif
return numSurfaces - 1;
}

void updateSurface(std::size_t surfaceIndex,
Expand All @@ -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<unsigned int>(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);

Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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<int>(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
}
};

Expand All @@ -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<SlotInfo> _slots;
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 4e9c0f3

Please sign in to comment.