diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-08-16 18:54:44 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-08-16 18:54:44 +1000 |
commit | a4074b0feb6b2194af7c175fe1826c8e550f10aa (patch) | |
tree | 91449852c57936780ae316ed7d4e8ff12e4e24a6 | |
parent | 938e3944fd7d55951e794224e6346488d3c701ea (diff) |
cone mesh
-rw-r--r-- | examples/game_demo/game_demo.c | 14 | ||||
-rw-r--r-- | src/maths/primitives.c | 37 | ||||
-rw-r--r-- | src/render/immdraw.c | 11 | ||||
-rw-r--r-- | src/render/immdraw.h | 2 |
4 files changed, 49 insertions, 15 deletions
diff --git a/examples/game_demo/game_demo.c b/examples/game_demo/game_demo.c index 919e306..2d0c11c 100644 --- a/examples/game_demo/game_demo.c +++ b/examples/game_demo/game_demo.c @@ -107,12 +107,14 @@ int main() { // Shadow_Run(entities, entity_count); // Quat rot = quat_from_axis_angle(VEC3_X, HALF_PI, true); - Immdraw_Sphere(transform_create(VEC3_ZERO, quat_ident(), vec3(1.0, 3.0, 1.0)), - vec4(1.0, 0.0, 0.0, 1.0), true); - Immdraw_Cuboid(transform_create(vec3(4.0, 0.0, 0.0), quat_ident(), vec3(3.0, 0.5, 3.0)), - 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_Sphere(transform_create(VEC3_ZERO, quat_ident(), vec3(1.0, 3.0, 1.0)), + // vec4(1.0, 0.0, 0.0, 1.0), true); + // Immdraw_Cuboid(transform_create(vec3(4.0, 0.0, 0.0), quat_ident(), vec3(3.0, 0.5, 3.0)), + // 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); if (draw_debug) { // draw the player model with shadows diff --git a/src/maths/primitives.c b/src/maths/primitives.c index 3387084..3820426 100644 --- a/src/maths/primitives.c +++ b/src/maths/primitives.c @@ -239,26 +239,47 @@ Geometry Geo_CreateCylinder(f32 radius, f32 height, u32 resolution) { } Geometry Geo_CreateCone(f32 radius, f32 height, u32 resolution) { - Vertex_darray* vertices = Vertex_darray_new(1); - u32_darray* indices = u32_darray_new(1); + Vertex_darray* vertices = Vertex_darray_new((resolution + 1) * 2); + u32_darray* indices = u32_darray_new(resolution * 2 * 3); + + // TODO: decide how UVs are unwrapped // tip - VERT_3D(vertices, vec3(0.0, height, 0.0), VEC3_Y, vec2(0, 0)); // TODO: fix uvs + VERT_3D(vertices, vec3(0.0, height, 0.0), VEC3_Y, vec2(0, 0)); + + // sides + f32 step = TAU / resolution; + + for (u32 i = 0; i < resolution; i++) { + f32 x = cos(step * i) * radius; + f32 z = sin(step * i) * radius; + Vec3 pos = vec3(x, 0.0, z); + Vec3 tip_to_vertex = vec3_sub(pos, vertices->data[0].static_3d.position); + Vec3 center_to_vertex = pos; + Vec3 tangent = vec3_cross(VEC3_Y, center_to_vertex); + Vec3 normal_dir = vec3_cross(tangent, tip_to_vertex); + Vec3 normal = vec3_normalise(normal_dir); + VERT_3D(vertices, pos, normal, vec2(0, 0)); + } + for (u32 i = 1; i < resolution; i++) { + push_triangle(indices, 0, i + 1, i); + } + push_triangle(indices, 0, 1, resolution); // base center - VERT_3D(vertices, VEC3_ZERO, VEC3_NEG_Y, vec2(0, 0)); // TODO: fix uvs + u32 center_idx = vertices->len; + VERT_3D(vertices, VEC3_ZERO, VEC3_NEG_Y, vec2(0, 0)); // base circle - f32 step = TAU / resolution; for (u32 i = 0; i < resolution; i++) { f32 x = cos(step * i) * radius; f32 z = sin(step * i) * radius; VERT_3D(vertices, vec3(x, 0.0, z), VEC3_NEG_Z, vec2(0, 0)); } - - // sides - for (u32 i = 0; i < resolution; i++) { + for (u32 i = 1; i < resolution; i++) { + push_triangle(indices, center_idx, center_idx + i, center_idx + i + 1); } + push_triangle(indices, center_idx, center_idx + resolution, center_idx + 1); Geometry geo = { .format = VERTEX_STATIC_3D, diff --git a/src/render/immdraw.c b/src/render/immdraw.c index c2589c3..d352ddf 100644 --- a/src/render/immdraw.c +++ b/src/render/immdraw.c @@ -25,6 +25,9 @@ void Immdraw_Init(Immdraw_Storage* storage) { Geometry plane_geo = Geo_CreatePlane(f32x2(1.0, 1.0), 1, 1); storage->plane = Mesh_Create(&plane_geo, true); + Geometry cone_geo = Geo_CreateCone(1.0, 1.0, 8); + storage->cone = Mesh_Create(&cone_geo, true); + storage->bbox = GenBboxMesh(); // Pipeline / material @@ -78,12 +81,18 @@ void Immdraw_Plane(Transform tf, Vec4 colour, bool wireframe) { Immdraw_Primitive(tf, CEL_TRI, 1.0, colour, wireframe, imm->plane); } -PUB void Immdraw_Bbox(Transform tf, Vec4 colour, bool wireframe) { +void Immdraw_Bbox(Transform tf, Vec4 colour, bool wireframe) { TRACE("Draw bbox"); Immdraw_Storage* imm = Render_GetImmdrawStorage(); Immdraw_Primitive(tf, CEL_LINE, 1.0, colour, wireframe, imm->bbox); } +void Immdraw_Cone(Transform tf, Vec4 colour, bool wireframe) { + TRACE("Draw cone"); + Immdraw_Storage* imm = Render_GetImmdrawStorage(); + Immdraw_Primitive(tf, CEL_TRI, 1.0, colour, wireframe, imm->cone); +} + void Immdraw_Primitive(Transform tf, PrimitiveTopology topology, f32 size, Vec4 colour, bool wireframe, Mesh mesh) { Immdraw_Storage* imm = Render_GetImmdrawStorage(); diff --git a/src/render/immdraw.h b/src/render/immdraw.h index b205129..cf0bed5 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 cone; Mesh bbox; GPU_Pipeline* colour_pipeline; /** @brief Pipeline for drawing geometry that has vertex colours */ } Immdraw_Storage; @@ -30,6 +31,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_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); |