Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Add support for rendering geometry range
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed Jan 24, 2019
1 parent 056a9f9 commit 7612051
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/vrb/Geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> &aVerticies,
Expand Down
25 changes: 23 additions & 2 deletions src/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<int>& aVertices,
Expand Down

0 comments on commit 7612051

Please sign in to comment.