diff options
author | Omni <omniscient.oce@gmail.com> | 2024-08-16 22:44:45 +1000 |
---|---|---|
committer | Omni <omniscient.oce@gmail.com> | 2024-08-16 22:44:45 +1000 |
commit | cfd7266c21a43cbd37fa712725cea85cdd1f7aab (patch) | |
tree | d09e6b819497c7823c062136142d9c1c9cfda3fc | |
parent | a4074b0feb6b2194af7c175fe1826c8e550f10aa (diff) |
cyl mesh and poor mans translation gizmo
-rw-r--r-- | examples/game_demo/game_demo.c | 24 | ||||
-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 |
5 files changed, 89 insertions, 12 deletions
diff --git a/examples/game_demo/game_demo.c b/examples/game_demo/game_demo.c index 2d0c11c..88de681 100644 --- a/examples/game_demo/game_demo.c +++ b/examples/game_demo/game_demo.c @@ -77,8 +77,8 @@ int main() { MaterialHandle crate_mat_handle = Material_pool_insert(Render_GetMaterialPool(), &crate_mat); // ModelHandle cube_handle = ModelLoad_gltf("assets/models/gltf/Cube/glTF/Cube.gltf", false); ModelHandle cube_handle = - // ModelLoad_gltf("assets/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf", false); - ModelLoad_gltf("../../assets/prototyper/prototyper_m.gltf", false); + ModelLoad_gltf("assets/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf", false); + // ModelLoad_gltf("../../assets/prototyper/prototyper_m.gltf", false); RenderEnt_darray* render_entities = RenderEnt_darray_new(1); @@ -113,8 +113,24 @@ int main() { // vec4(1.0, 0.0, 0.0, 1.0), true); // Immdraw_Bbox(transform_create(vec3(0.0, 0.0, 0.0), quat_ident(), vec3(2.0, 2.0, 2.0)), // vec4(0.0, 1.0, 0.0, 1.0), true); - Immdraw_Cone(transform_create(VEC3_ZERO, quat_ident(), vec3(1.0, 2.0, 1.0)), - vec4(1.0, 1.0, 1.0, 1.0), true); + f32 thickness = 0.1; + // Y Axis (blue) + Immdraw_Cone(transform_create(vec3(0.0, 2.0, 0.0), quat_ident(), vec3(0.3, 1.0, 0.3)), + vec4(0.0, 0.0, 1.0, 1.0), false); + Immdraw_Cylinder(transform_create(VEC3_ZERO, quat_ident(), vec3(thickness, 1.1, thickness)), vec4(0.0, 0.0, 1.0, 1.0), + false); + + // X Axis (green) + Immdraw_Cone(transform_create(vec3(2.0, 0.0, 0.0), quat_from_axis_angle(VEC3_Z, HALF_PI, true), vec3(0.3, 1.0, 0.3)), + vec4(0.0, 1.0, 0.0, 1.0), false); + Immdraw_Cylinder(transform_create(VEC3_ZERO, quat_from_axis_angle(VEC3_Z, HALF_PI, true), vec3(thickness, 1.1, thickness)), vec4(0.0, 1.0, 0.0, 1.0), + false); + + // Z Axis (red) + Immdraw_Cone(transform_create(vec3(0.0, 0.0, 2.0), quat_from_axis_angle(VEC3_X, -HALF_PI, true), vec3(0.3, 1.0, 0.3)), + vec4(1.0, 0.0, 0.0, 1.0), false); + Immdraw_Cylinder(transform_create(VEC3_ZERO, quat_from_axis_angle(VEC3_X, -HALF_PI, true), vec3(thickness, 1.1, thickness)), vec4(1.0, 0.0, 0.0, 1.0), + false); if (draw_debug) { // draw the player model with shadows 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); |