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
60static const gpu::Type SHAPE_INDEX_TYPE = gpu::UINT32;60static const gpu::Type SHAPE_INDEX_TYPE = gpu::UINT32;
61static const uint SHAPE_INDEX_SIZE = sizeof(gpu::uint32);61static const uint SHAPE_INDEX_SIZE = sizeof(gpu::uint32);
62 62
63template <size_t SIDES>
64std::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
63void GeometryCache::ShapeData::setupVertices(gpu::BufferPointer& vertexBuffer, const geometry::VertexVector& vertices) {75void GeometryCache::ShapeData::setupVertices(gpu::BufferPointer& vertexBuffer, const geometry::VertexVector& vertices) {
64 vertexBuffer->append(vertices);76 vertexBuffer->append(vertices);
65 77
@@ -239,6 +251,75 @@ void setupSmoothShape(GeometryCache::ShapeData& shapeData, const geometry::Solid · 173 unmodified lines
239 shapeData.setupIndices(indexBuffer, solidIndices, wireIndices);251 shapeData.setupIndices(indexBuffer, solidIndices, wireIndices);
240}252}
241 253
254template <uint32_t N>
255void 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}
242 323
243// FIXME solids need per-face vertices, but smooth shaded324// FIXME solids need per-face vertices, but smooth shaded
244// components do not. Find a way to support using draw elements325// components do not. Find a way to support using draw elements
@@ -285,10 +366,13 @@ void GeometryCache::buildShapes() { · 40 unmodified lines
285 // Not implememented yet:366 // Not implememented yet:
286 367
287 //Triangle,368 //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);
288 //Quad,374 //Quad,
289 //Circle,375 //Circle,
290 //Octahetron,
291 //Dodecahedron,
292 //Torus,376 //Torus,
293 //Cone,377 //Cone,
294 //Cylinder,378 //Cylinder,
@@ -1757,6 +1841,11 @@ void GeometryCache::renderSolidShapeInstance(gpu::Batch& batch, GeometryCache::S · 1462 unmodified lines
1757 renderInstances(batch, color, false, pipeline, shape);1841 renderInstances(batch, color, false, pipeline, shape);
1758}1842}
1759 1843
1844void 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
1760void GeometryCache::renderSolidSphereInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {1849void GeometryCache::renderSolidSphereInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
1761 renderInstances(batch, color, false, pipeline, GeometryCache::Sphere);1850 renderInstances(batch, color, false, pipeline, GeometryCache::Sphere);
1762}1851}