From 7612051f814bf03bd092187a1db8224b9b86d4b5 Mon Sep 17 00:00:00 2001 From: Imanol Fernandez Date: Thu, 24 Jan 2019 17:21:14 +0100 Subject: [PATCH] Add support for rendering geometry range --- include/vrb/Geometry.h | 2 ++ src/Geometry.cpp | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/vrb/Geometry.h b/include/vrb/Geometry.h index 7c2995f..54e19c7 100644 --- a/include/vrb/Geometry.h +++ b/include/vrb/Geometry.h @@ -38,6 +38,8 @@ class Geometry : public Node, public Drawable, protected ResourceGL { VertexArrayPtr GetVertexArray() const; void SetVertexArray(const VertexArrayPtr& aVertexArray); void UpdateBuffers(); + void SetRenderRange(uint32_t aStartIndex, uint32_t aLength); + int32_t TriangleCount() const; void AddFace( const std::vector &aVerticies, diff --git a/src/Geometry.cpp b/src/Geometry.cpp index 10f7409..b64a263 100644 --- a/src/Geometry.cpp +++ b/src/Geometry.cpp @@ -48,8 +48,11 @@ struct Geometry::State : public Node::State, public ResourceGL::State, public Dr int triangleCount; GLuint vertexObjectId; GLuint indexObjectId; + uint32_t rangeStart; + uint32_t rangeLength; - State() : vertexCount(0), triangleCount(0), vertexObjectId(0), indexObjectId(0) {} + State() : vertexCount(0), triangleCount(0), vertexObjectId(0), indexObjectId(0), + rangeStart(0), rangeLength(0) {} GLsizei UVLength() const { @@ -121,7 +124,14 @@ Geometry::Draw(const Camera& aCamera, const Matrix& aModelTransform) { if (kUseTextureCoords) { VRB_GL_CHECK(glEnableVertexAttribArray((GLuint)m.renderState->AttributeUV())); } - VRB_GL_CHECK(glDrawElements(GL_TRIANGLES, m.triangleCount * 3, GL_UNSIGNED_SHORT, 0)); + const int32_t maxLength = m.triangleCount * 3; + if (m.rangeLength == 0) { + VRB_GL_CHECK(glDrawElements(GL_TRIANGLES, maxLength, GL_UNSIGNED_SHORT, 0)); + } else if (m.rangeStart >= maxLength || (m.rangeStart + m.rangeLength) > maxLength) { + VRB_WARN("Invalid geometry range (%u-%u). Max geometry length %d", m.rangeStart, m.rangeLength + m.rangeLength, maxLength); + } else { + VRB_GL_CHECK(glDrawElements(GL_TRIANGLES, m.rangeLength, GL_UNSIGNED_SHORT, (void*)(m.rangeStart * sizeof(GLushort)))); + } VRB_GL_CHECK(glDisableVertexAttribArray((GLuint)m.renderState->AttributePosition())); VRB_GL_CHECK(glDisableVertexAttribArray((GLuint)m.renderState->AttributeNormal())); if (kUseTextureCoords) { @@ -225,6 +235,17 @@ Geometry::UpdateBuffers() { VRB_GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0)); } +void +Geometry::SetRenderRange(uint32_t aStartIndex, uint32_t aLength) { + m.rangeStart = aStartIndex; + m.rangeLength = aLength; +} + +int32_t +Geometry::TriangleCount() const { + return m.triangleCount; +} + void Geometry::AddFace( const std::vector& aVertices,