diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/defines.h | 2 | ||||
-rw-r--r-- | src/maths/primitives.c | 60 | ||||
-rw-r--r-- | src/render/immdraw.c | 13 | ||||
-rw-r--r-- | src/render/immdraw.h | 2 |
4 files changed, 69 insertions, 8 deletions
diff --git a/src/defines.h b/src/defines.h index e9db543..0c0dcac 100644 --- a/src/defines.h +++ b/src/defines.h @@ -64,7 +64,7 @@ CORE_DEFINE_HANDLE( // NOTE: The below is now handled in xmake.lua // Platform will inform renderer backend (unless user overrides) #if defined(CEL_PLATFORM_LINUX) -// #define CEL_REND_BACKEND_OPENGL 1 +#define CEL_REND_BACKEND_OPENGL 1 // #define CEL_REND_BACKEND_VULKAN 1 #endif diff --git a/src/maths/primitives.c b/src/maths/primitives.c index 3820426..551a5df 100644 --- a/src/maths/primitives.c +++ b/src/maths/primitives.c @@ -234,10 +234,6 @@ Geometry Geo_CreateUVsphere(f32 radius, u32 north_south_lines, u32 east_west_lin return geo; } -Geometry Geo_CreateCylinder(f32 radius, f32 height, u32 resolution) { - TODO("implement cylinder meshing"); -} - Geometry Geo_CreateCone(f32 radius, f32 height, u32 resolution) { Vertex_darray* vertices = Vertex_darray_new((resolution + 1) * 2); u32_darray* indices = u32_darray_new(resolution * 2 * 3); @@ -289,4 +285,58 @@ Geometry Geo_CreateCone(f32 radius, f32 height, u32 resolution) { .indices = indices, }; return geo; -}
\ No newline at end of file +} + +Geometry Geo_CreateCylinder(f32 radius, f32 height, u32 resolution) { + Vertex_darray* vertices = Vertex_darray_new(1); + u32_darray* indices = u32_darray_new(1); + + f32 step = TAU / resolution; + + // bot cap + VERT_3D(vertices, VEC3_ZERO, VEC3_NEG_Y, vec2(0, 0)); + for (u32 i = 0; i < resolution; i++) { + VERT_3D(vertices, vec3(cos(step * i) * radius, 0.0, sin(step * i) * radius), VEC3_NEG_Y, vec2(0, 0)); + } + for (u32 i = 1; i < resolution; i++) { + push_triangle(indices, 0, i, i + 1); + } + push_triangle(indices, 0, resolution, 1); + + // top cap + u32 center_idx = vertices->len; + VERT_3D(vertices, vec3(0.0, height, 0.0), VEC3_Y, vec2(0, 0)); + for (u32 i = 0; i < resolution; i++) { + VERT_3D(vertices, vec3(cos(step * i) * radius, height, sin(step * i) * radius), VEC3_Y, + vec2(0, 0)); + } + for (u32 i = 1; i < resolution; i++) { + push_triangle(indices, center_idx, center_idx + i + 1, center_idx + i); + } + push_triangle(indices, center_idx, center_idx + 1, center_idx + resolution); + + // sides + u32 sides_start = vertices->len; + for (u32 i = 0; i < resolution; i++) { + f32 x = cos(step * i) * radius; + f32 z = sin(step * i) * radius; + // top then bottom + VERT_3D(vertices, vec3(x, height, z), vec3_normalise(vec3(x, 0.0, z)), vec2(0, 0)); + VERT_3D(vertices, vec3(x, 0.0, z), vec3_normalise(vec3(x, 0.0, z)), vec2(0, 0)); + } + for (u32 i = 0; i < resolution; i++) { + u32 current = sides_start + i * 2; + u32 next = sides_start + ((i + 1) % resolution) * 2; + push_triangle(indices, current, next, current + 1); + push_triangle(indices, current + 1, next, next + 1); + } + + Geometry geo = { + .format = VERTEX_STATIC_3D, + .vertices = vertices, + .has_indices = true, + .index_count = indices->len, + .indices = indices, + }; + return geo; +} diff --git a/src/render/immdraw.c b/src/render/immdraw.c index d352ddf..8a10c65 100644 --- a/src/render/immdraw.c +++ b/src/render/immdraw.c @@ -28,6 +28,9 @@ void Immdraw_Init(Immdraw_Storage* storage) { Geometry cone_geo = Geo_CreateCone(1.0, 1.0, 8); storage->cone = Mesh_Create(&cone_geo, true); + Geometry cyl_geo = Geo_CreateCylinder(1.0, 2.0, 8); + storage->cylinder = Mesh_Create(&cyl_geo, true); + storage->bbox = GenBboxMesh(); // Pipeline / material @@ -54,7 +57,7 @@ void Immdraw_Init(Immdraw_Storage* storage) { .vs = { .debug_name = "Immdraw Vertex Shader", .filepath = vert_path, .code = vert_shader }, .fs = { .debug_name = "Immdraw Fragment Shader", .filepath = frag_path, .code = frag_shader }, .depth_test = true, - .wireframe = false, + .wireframe = true, }; GPU_Renderpass* rpass = GPU_Renderpass_Create((GPU_RenderpassDesc){ .default_framebuffer = true }); @@ -87,6 +90,12 @@ void Immdraw_Bbox(Transform tf, Vec4 colour, bool wireframe) { Immdraw_Primitive(tf, CEL_LINE, 1.0, colour, wireframe, imm->bbox); } +void Immdraw_Cylinder(Transform tf, Vec4 colour, bool wireframe) { + TRACE("Draw cylinder"); + Immdraw_Storage* imm = Render_GetImmdrawStorage(); + Immdraw_Primitive(tf, CEL_TRI, 1.0, colour, wireframe, imm->cylinder); +} + void Immdraw_Cone(Transform tf, Vec4 colour, bool wireframe) { TRACE("Draw cone"); Immdraw_Storage* imm = Render_GetImmdrawStorage(); @@ -164,4 +173,4 @@ Mesh GenBboxMesh() { .indices = indices }; return Mesh_Create(&geo, true); -}
\ No newline at end of file +} diff --git a/src/render/immdraw.h b/src/render/immdraw.h index cf0bed5..2911350 100644 --- a/src/render/immdraw.h +++ b/src/render/immdraw.h @@ -13,6 +13,7 @@ typedef struct Immdraw_Storage { Mesh plane; Mesh cube; Mesh sphere; + Mesh cylinder; Mesh cone; Mesh bbox; GPU_Pipeline* colour_pipeline; /** @brief Pipeline for drawing geometry that has vertex colours */ @@ -31,6 +32,7 @@ PUB void Immdraw_Shutdown(Immdraw_Storage* storage); // These functions cause a pipeline switch and so aren't optimised for performance PUB void Immdraw_Plane(Transform tf, Vec4 colour, bool wireframe); PUB void Immdraw_Cuboid(Transform tf, Vec4 colour, bool wireframe); +PUB void Immdraw_Cylinder(Transform tf, Vec4 colour, bool wireframe); PUB void Immdraw_Cone(Transform tf, Vec4 colour, bool wireframe); PUB void Immdraw_Sphere(Transform tf, Vec4 colour, bool wireframe); PUB void Immdraw_Bbox(Transform tf, Vec4 colour, bool wireframe); |