From d17131c421d134757aca47796f06a51b26d1351e Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 17 Jul 2022 16:38:00 -0700 Subject: [PATCH] Pass:cone; --- src/api/l_graphics_pass.c | 10 ++++++ src/modules/graphics/graphics.c | 58 +++++++++++++++++++++++++++++++-- src/modules/graphics/graphics.h | 1 + 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/api/l_graphics_pass.c b/src/api/l_graphics_pass.c index 31fcd3e3..5ae8a638 100644 --- a/src/api/l_graphics_pass.c +++ b/src/api/l_graphics_pass.c @@ -559,6 +559,15 @@ static int l_lovrPassCylinder(lua_State* L) { return 0; } +static int l_lovrPassCone(lua_State* L) { + Pass* pass = luax_checktype(L, 1, Pass); + float transform[16]; + int index = luax_readmat4(L, 2, transform, -2); + uint32_t segments = luax_optu32(L, index, 64); + lovrPassCone(pass, transform, segments); + return 0; +} + static int l_lovrPassCapsule(lua_State* L) { Pass* pass = luax_checktype(L, 1, Pass); float transform[16]; @@ -958,6 +967,7 @@ const luaL_Reg lovrPass[] = { { "circle", l_lovrPassCircle }, { "sphere", l_lovrPassSphere }, { "cylinder", l_lovrPassCylinder }, + { "cone", l_lovrPassCone }, { "capsule", l_lovrPassCapsule }, { "torus", l_lovrPassTorus }, { "text", l_lovrPassText }, diff --git a/src/modules/graphics/graphics.c b/src/modules/graphics/graphics.c index 8571da4f..b1ef85ea 100644 --- a/src/modules/graphics/graphics.c +++ b/src/modules/graphics/graphics.c @@ -261,6 +261,7 @@ enum { SHAPE_CIRCLE, SHAPE_SPHERE, SHAPE_CYLINDER, + SHAPE_CONE, SHAPE_CAPSULE, SHAPE_TORUS, SHAPE_MONKEY @@ -1348,7 +1349,7 @@ ShaderSource lovrGraphicsCompileShader(ShaderStage stage, ShaderSource* source) [STAGE_VERTEX] = "" "void main() {" "Color = PassColor * VertexColor;" - "Normal = normalize(NormalMatrix * VertexNormal);" + "Normal = NormalMatrix * VertexNormal;" "UV = VertexUV;" "Position = lovrmain();" "}", @@ -4324,6 +4325,59 @@ void lovrPassCylinder(Pass* pass, float* transform, bool capped, float angle1, f } } +void lovrPassCone(Pass* pass, float* transform, uint32_t segments) { + uint32_t key[] = { SHAPE_CONE, segments }; + uint32_t vertexCount = 2 * segments + 1; + uint32_t indexCount = 3 * (segments - 2) + 3 * segments; + ShapeVertex* vertices; + uint16_t* indices; + + lovrPassDraw(pass, &(Draw) { + .hash = hash64(key, sizeof(key)), + .mode = MESH_TRIANGLES, + .transform = transform, + .vertex.pointer = (void**) &vertices, + .vertex.count = vertexCount, + .index.pointer = (void**) &indices, + .index.count = indexCount + }); + + if (!vertices) { + return; + } + + for (uint32_t i = 0; i < segments; i++) { + float theta = i * 2.f * (float) M_PI / segments; + float x = cosf(theta); + float y = sinf(theta); + float rsqrt3 = .57735f; + float nx = cosf(theta) * rsqrt3; + float ny = sinf(theta) * rsqrt3; + float nz = -rsqrt3; + float u = x + .5f; + float v = .5f - y; + vertices[segments * 0] = (ShapeVertex) { { x, y, 0.f }, { 0.f, 0.f, 1.f }, { u, v } }; + vertices[segments * 1] = (ShapeVertex) { { x, y, 0.f }, { nx, ny, nz }, { u, v } }; + vertices++; + } + + vertices[segments] = (ShapeVertex) { { 0.f, 0.f, -1.f }, { 0.f, 0.f, 0.f }, { .5f, .5f } }; + + // Base + for (uint32_t i = 0; i < segments - 2; i++) { + uint16_t tri[] = { 0, i + 1, i + 2 }; + memcpy(indices, tri, sizeof(tri)); + indices += COUNTOF(tri); + } + + // Sides + for (uint32_t i = 0; i < segments; i++) { + uint16_t tri[] = { segments + i, segments + (i + 1) % segments, vertexCount - 1 }; + memcpy(indices, tri, sizeof(tri)); + indices += COUNTOF(tri); + } +} + void lovrPassCapsule(Pass* pass, float* transform, uint32_t segments) { float sx = vec3_length(transform + 0); float sy = vec3_length(transform + 4); @@ -4349,7 +4403,7 @@ void lovrPassCapsule(Pass* pass, float* transform, uint32_t segments) { .vertex.pointer = (void**) &vertices, .vertex.count = vertexCount, .index.pointer = (void**) &indices, - .index.count = indexCount, + .index.count = indexCount }); if (!vertices) { diff --git a/src/modules/graphics/graphics.h b/src/modules/graphics/graphics.h index a0f3f718..aafe4e94 100644 --- a/src/modules/graphics/graphics.h +++ b/src/modules/graphics/graphics.h @@ -612,6 +612,7 @@ void lovrPassBox(Pass* pass, float* transform, DrawStyle style); void lovrPassCircle(Pass* pass, float* transform, DrawStyle style, float angle1, float angle2, uint32_t segments); void lovrPassSphere(Pass* pass, float* transform, uint32_t segmentsH, uint32_t segmentsV); void lovrPassCylinder(Pass* pass, float* transform, bool capped, float angle1, float angle2, uint32_t segments); +void lovrPassCone(Pass* pass, float* transform, uint32_t segments); void lovrPassCapsule(Pass* pass, float* transform, uint32_t segments); void lovrPassTorus(Pass* pass, float* transform, uint32_t segmentsT, uint32_t segmentsP); void lovrPassText(Pass* pass, Font* font, ColoredString* strings, uint32_t count, float* transform, float wrap, HorizontalAlign halign, VerticalAlign valign);