From 57efea40ee805c0916fd176ac0e6cb6fc15cf94a Mon Sep 17 00:00:00 2001 From: Mark Peng Date: Mon, 5 Aug 2013 19:52:02 -0700 Subject: [PATCH] Add untested renderHalfCylinder function to GeometryCache/ --- interface/src/renderer/GeometryCache.cpp | 68 ++++++++++++++++++++++++ interface/src/renderer/GeometryCache.h | 2 + 2 files changed, 70 insertions(+) diff --git a/interface/src/renderer/GeometryCache.cpp b/interface/src/renderer/GeometryCache.cpp index 8331a5deb9..07c383c1c1 100644 --- a/interface/src/renderer/GeometryCache.cpp +++ b/interface/src/renderer/GeometryCache.cpp @@ -163,3 +163,71 @@ void GeometryCache::renderSquare(int xDivisions, int yDivisions) { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } + +void GeometryCache::renderHalfCylinder(int slices, int stacks, float radius, float height) { + VerticesIndices& vbo = _halfCylinderVBOs[IntPair(slices, stacks)]; + int vertices = slices * (stacks - 1) + 1; + int indices = slices * 2 * 3 * (stacks - 2) + slices * 3; + if (vbo.first == 0) { + GLfloat* vertexData = new GLfloat[vertices * 3]; + GLfloat* vertex = vertexData; + for (int i = 0; i < stacks; i++) { + float z = i * height / stacks; + + for (int j = 0; j < slices; j++) { + float theta = PIf * j / slices; + + *(vertex++) = sinf(theta) * radius; + *(vertex++) = cosf(theta) * radius; + *(vertex++) = z; + } + } + + glGenBuffers(1, &vbo.first); + glBindBuffer(GL_ARRAY_BUFFER, vbo.first); + const int BYTES_PER_VERTEX = 3 * sizeof(GLfloat); + glBufferData(GL_ARRAY_BUFFER, vertices * BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW); + delete[] vertexData; + + GLushort* indexData = new GLushort[indices]; + GLushort* index = indexData; + for (int i = 0; i < stacks - 1; i++) { + GLushort bottom = i * slices; + GLushort top = bottom + slices; + for (int j = 0; j < slices; j++) { + int next = (j + 1) % slices; + + *(index++) = bottom + j; + *(index++) = top + next; + *(index++) = top + j; + + *(index++) = bottom + j; + *(index++) = bottom + next; + *(index++) = top + next; + } + } + + glGenBuffers(1, &vbo.second); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + const int BYTES_PER_INDEX = sizeof(GLushort); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * BYTES_PER_INDEX, indexData, GL_STATIC_DRAW); + delete[] indexData; + + } else { + glBindBuffer(GL_ARRAY_BUFFER, vbo.first); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + } + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + + glVertexPointer(3, GL_FLOAT, 0, 0); + glNormalPointer(GL_FLOAT, 0, 0); + + glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} \ No newline at end of file diff --git a/interface/src/renderer/GeometryCache.h b/interface/src/renderer/GeometryCache.h index bfce642554..a0764db887 100644 --- a/interface/src/renderer/GeometryCache.h +++ b/interface/src/renderer/GeometryCache.h @@ -20,6 +20,7 @@ public: void renderHemisphere(int slices, int stacks); void renderSquare(int xDivisions, int yDivisions); + void renderHalfCylinder(int slices, int stacks, float radius, float height); private: @@ -28,6 +29,7 @@ private: QHash _hemisphereVBOs; QHash _squareVBOs; + QHash _halfCylinderVBOs; }; #endif /* defined(__interface__GeometryCache__) */