diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-12 00:16:51 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-12 00:16:51 +1000 |
commit | 26fb1fcafa552c600a70b2680c868f7355b138e4 (patch) | |
tree | 2fb77c8b14a5610584a316e57fd8db2795847a0f /src/maths/primitives.c | |
parent | 76216f91aee936bc57d7e1a1b2c2b63a03ce976a (diff) |
wip: uvsphere mesh gen and pbr shader first pass
Diffstat (limited to 'src/maths/primitives.c')
-rw-r--r-- | src/maths/primitives.c | 64 |
1 files changed, 61 insertions, 3 deletions
diff --git a/src/maths/primitives.c b/src/maths/primitives.c index c0af140..3931b93 100644 --- a/src/maths/primitives.c +++ b/src/maths/primitives.c @@ -112,7 +112,7 @@ geometry_data geo_create_cuboid(f32x3 extents) { .vertices = vertices, .has_indices = true, .indices = indices, // FIXME: make darray methods that return stack allocated struct - .colour = vec3(0, 0, 0), + .colour = (rgba){ 0, 0, 0, 1 } }; return geo; @@ -133,6 +133,64 @@ geometry_data geo_create_cuboid(f32x3 extents) { // --- Spheres -geometry_data geo_create_uvsphere(f32 radius, f32 north_south_lines, f32 east_west_lines) { - // TODO +vec3 spherical_to_cartesian_coords(f32 rho, f32 theta, f32 phi) { + f32 x = rho * sin(phi) * cos(theta); + f32 y = rho * sin(phi) * sin(theta); + f32 z = rho * cos(phi); + return vec3(x, y, z); +} + +geometry_data geo_create_uvsphere(f32 radius, u32 north_south_lines, u32 east_west_lines) { + assert(east_west_lines >= 3); // sphere will be degenerate and look gacked without at least 3 + assert(north_south_lines >= 3); + + vertex_darray* vertices = vertex_darray_new(2 + (east_west_lines - 1) * north_south_lines); + + // Create a UV sphere with spherical coordinates + // a point P on the unit sphere can be represented P(r, theta, phi) + // for each vertex we must convert that to a cartesian R3 coordinate + + // Top point + vertex top = { .static_3d = { .position = vec3(0, 0, radius), + .normal = vec3(0, 0, radius), + .tex_coords = vec2(0, 0) } }; + + // parallels + for (u32 i = 0; i < (east_west_lines - 1); i++) { + // phi should range from 0 to pi + f32 phi = PI * ((i + 1) / east_west_lines); + + // meridians + for (u32 j = 0; j < east_west_lines; j++) { + // theta should range from 0 to 2PI + f32 theta = TAU * (j / north_south_lines); + vec3 position = spherical_to_cartesian_coords(radius, theta, phi); + f32 d = vec3_len(position); + assert(d == radius); // all points on the sphere should be 'radius' away from the origin + vertex v = { .static_3d = { + .position = position, + .normal = position, // normal vector on sphere is same as position + .tex_coords = vec2(0, 0) // TODO + } }; + vertex_darray_push(vertices, v); + } + } + + // Bottom point + vertex bot = { .static_3d = { .position = vec3(0, 0, -radius), + .normal = vec3(0, 0, -radius), + .tex_coords = vec2(0, 0) } }; + + // TODO: generate indices for for each flat quad on the UV sphere and triangles + // where they meet the north and south poles + u32_darray* indices = u32_darray_new(1); + + geometry_data geo = { + .format = VERTEX_STATIC_3D, + .vertices = vertices, + .has_indices = true, + .indices = indices, + .colour = RED_800, + }; + return geo; } |