History

Support triangle and hexagon shapes, add shape support to overlays.

Brad Davis committed 10 years ago

Commit 03cb6171 parent cf1c95410 files · +298 −12Browse files

Showing the changes to libraries/render-utils/src/GeometryCache.cpp (1 of 10 files). Show all

Unified · Split

libraries/render-utils/src/GeometryCache.cpp+91−2
@@ -60,6 +60,18 @@ static const uint SHAPE_NORMALS_OFFSET = sizeof(glm::vec3); · 59 unmodified lines
6060 static const gpu::Type SHAPE_INDEX_TYPE = gpu::UINT32;
6161 static const uint SHAPE_INDEX_SIZE = sizeof(gpu::uint32);
6262
63+template <size_t SIDES>
64+std::vector<vec3> polygon() {
65+ std::vector<vec3> result;
66+ result.reserve(SIDES);
67+ double angleIncrement = 2.0 * M_PI / SIDES;
68+ for (size_t i = 0; i < SIDES; ++i) {
69+ double angle = (double)i * angleIncrement;
70+ result.push_back(vec3{ cos(angle) * 0.5, 0.0, sin(angle) * 0.5 });
71+ }
72+ return result;
73+}
74+
6375 void GeometryCache::ShapeData::setupVertices(gpu::BufferPointer& vertexBuffer, const geometry::VertexVector& vertices) {
6476 vertexBuffer->append(vertices);
6577
@@ -239,6 +251,75 @@ void setupSmoothShape(GeometryCache::ShapeData& shapeData, const geometry::Solid · 173 unmodified lines
239251 shapeData.setupIndices(indexBuffer, solidIndices, wireIndices);
240252 }
241253
254+template <uint32_t N>
255+void extrudePolygon(GeometryCache::ShapeData& shapeData, gpu::BufferPointer& vertexBuffer, gpu::BufferPointer& indexBuffer) {
256+ using namespace geometry;
257+ Index baseVertex = (Index)(vertexBuffer->getSize() / SHAPE_VERTEX_STRIDE);
258+ VertexVector vertices;
259+ IndexVector solidIndices, wireIndices;
260+
261+ // Top and bottom faces
262+ std::vector<vec3> shape = polygon<N>();
263+ for (const vec3& v : shape) {
264+ vertices.push_back(vec3(v.x, 0.5f, v.z));
265+ vertices.push_back(vec3(0, 1, 0));
266+ }
267+ for (const vec3& v : shape) {
268+ vertices.push_back(vec3(v.x, -0.5f, v.z));
269+ vertices.push_back(vec3(0, -1, 0));
270+ }
271+ for (uint32_t i = 2; i < N; ++i) {
272+ solidIndices.push_back(baseVertex + 0);
273+ solidIndices.push_back(baseVertex + i);
274+ solidIndices.push_back(baseVertex + i - 1);
275+ solidIndices.push_back(baseVertex + N);
276+ solidIndices.push_back(baseVertex + i + N - 1);
277+ solidIndices.push_back(baseVertex + i + N);
278+ }
279+ for (uint32_t i = 1; i <= N; ++i) {
280+ wireIndices.push_back(baseVertex + (i % N));
281+ wireIndices.push_back(baseVertex + i - 1);
282+ wireIndices.push_back(baseVertex + (i % N) + N);
283+ wireIndices.push_back(baseVertex + (i - 1) + N);
284+ }
285+
286+ // Now do the sides
287+ baseVertex += 2 * N;
288+
289+ for (uint32_t i = 0; i < N; ++i) {
290+ vec3 left = shape[i];
291+ vec3 right = shape[(i + 1) % N];
292+ vec3 normal = glm::normalize(left + right);
293+ vec3 topLeft = vec3(left.x, 0.5f, left.z);
294+ vec3 topRight = vec3(right.x, 0.5f, right.z);
295+ vec3 bottomLeft = vec3(left.x, -0.5f, left.z);
296+ vec3 bottomRight = vec3(right.x, -0.5f, right.z);
297+
298+ vertices.push_back(topLeft);
299+ vertices.push_back(normal);
300+ vertices.push_back(bottomLeft);
301+ vertices.push_back(normal);
302+ vertices.push_back(topRight);
303+ vertices.push_back(normal);
304+ vertices.push_back(bottomRight);
305+ vertices.push_back(normal);
306+
307+ solidIndices.push_back(baseVertex + 0);
308+ solidIndices.push_back(baseVertex + 2);
309+ solidIndices.push_back(baseVertex + 1);
310+ solidIndices.push_back(baseVertex + 1);
311+ solidIndices.push_back(baseVertex + 2);
312+ solidIndices.push_back(baseVertex + 3);
313+ wireIndices.push_back(baseVertex + 0);
314+ wireIndices.push_back(baseVertex + 1);
315+ wireIndices.push_back(baseVertex + 3);
316+ wireIndices.push_back(baseVertex + 2);
317+ baseVertex += 4;
318+ }
319+
320+ shapeData.setupVertices(vertexBuffer, vertices);
321+ shapeData.setupIndices(indexBuffer, solidIndices, wireIndices);
322+}
242323
243324 // FIXME solids need per-face vertices, but smooth shaded
244325 // components do not. Find a way to support using draw elements
@@ -285,10 +366,13 @@ void GeometryCache::buildShapes() { · 40 unmodified lines
285366 // Not implememented yet:
286367
287368 //Triangle,
369+ extrudePolygon<3>(_shapes[Triangle], _shapeVertices, _shapeIndices);
370+ //Hexagon,
371+ extrudePolygon<6>(_shapes[Hexagon], _shapeVertices, _shapeIndices);
372+ //Octagon,
373+ extrudePolygon<8>(_shapes[Octagon], _shapeVertices, _shapeIndices);
288374 //Quad,
289375 //Circle,
290− //Octahetron,
291− //Dodecahedron,
292376 //Torus,
293377 //Cone,
294378 //Cylinder,
@@ -1757,6 +1841,11 @@ void GeometryCache::renderSolidShapeInstance(gpu::Batch& batch, GeometryCache::S · 1462 unmodified lines
17571841 renderInstances(batch, color, false, pipeline, shape);
17581842 }
17591843
1844+void GeometryCache::renderWireShapeInstance(gpu::Batch& batch, GeometryCache::Shape shape, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
1845+ renderInstances(batch, color, true, pipeline, shape);
1846+}
1847+
1848+
17601849 void GeometryCache::renderSolidSphereInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
17611850 renderInstances(batch, color, false, pipeline, GeometryCache::Sphere);
17621851 }