// // Created by kam on 10/4/26. // #include "Geometry.h" #include using namespace Snail; Mesh Snail::Geometry::quad(f32 width, f32 height) { f32 hw = width / 2; f32 hh = height / 2; return Mesh { std::vector { -hw, -hh, 0.f, hw, -hh, 0.f, hw, hh, 0.f, -hw, hh, 0.f }, std::vector { 0, 1, 2, 2, 3, 0 }, std::vector {}, std::vector { 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f } }; } Mesh Snail::Geometry::cube(f32 size) { return prism(size, size, size); } Mesh Snail::Geometry::prism(f32 width, f32 height, f32 depth) { f32 hw = width / 2; f32 hh = height / 2; f32 hd = depth / 2; return Mesh { std::vector{ -hw, -hh, -hd, // 0 - L B F -hw, -hh, hd, // 1 - L B K -hw, hh, -hd, // 2 - L T F -hw, hh, hd, // 3 - L T K hw, -hh, -hd, // 4 - R B F hw, -hh, hd, // 5 - R B K hw, hh, -hd, // 6 - R T F hw, hh, hd, // 7 - R T K }, // (-1, 1) o----o ( 1, 1) // | .ยด | // (-1, -1) o----o ( 1,-1) std::vector{ // Bottom face 0, 1, 5, 0, 5, 4, // Front face 6, 0, 2, 6, 4, 0, // Left face 3, 0, 1, 3, 2, 0, // Back face 7, 1, 3, 7, 5, 1, // Right face 4, 5, 7, 4, 7, 6, // Top face 2, 3, 7, 2, 7, 6, }, std::vector{ 0, -1, 0, // 0 - L B F 0, 0, 0, // 1 - L B K 0, 1, 0, // 2 - L T F -1, 0, 0, // 3 - L T K 1, 0, 0, // 4 - R B F 0, 0, 0, // 5 - R B K 0, 0, -1, // 6 - R T F 0, 0, 1, // 7 - R T K }, std::vector { 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f } }; } Mesh Snail::Geometry::sphere(f32 radius, u32 samples) { std::vector vertices{}, normals{}, tex_coords{}; std::vector indices{}; vertices.reserve(samples * samples); indices.reserve(samples * samples); normals.reserve(samples * samples); tex_coords.reserve(samples * samples); const f32 hstep = M_PI / static_cast(samples - 1); const f32 step = 2.f * M_PI / static_cast(samples); const f32 uv_step = 1.f / static_cast(samples); for (u32 u = 0; u < samples; u++) { for (int v = 0; v < samples; ++v) { f32 uf = u * hstep; f32 vf = v * step; f32 x = cosf(vf) * sinf(uf); f32 y = -cosf(uf); f32 z = sinf(vf) * sinf(uf); vertices.push_back(x * radius); vertices.push_back(y * radius); vertices.push_back(z * radius); normals.push_back(x); normals.push_back(y); normals.push_back(z); tex_coords.push_back(uv_step * u); tex_coords.push_back(uv_step * v); } } for (int u = 0; u < samples; ++u) { for (int v = 0; v < samples - 1; ++v) { int i = u * samples + v; indices.push_back(i); indices.push_back(i + 1); indices.push_back(i + samples); indices.push_back(i + 1); indices.push_back(i + samples + 1); indices.push_back(i + samples); } indices.push_back((u + 1) * samples - 1); indices.push_back(u * samples); indices.push_back((u + 2) * samples - 1); indices.push_back(u * samples); indices.push_back((u + 1) * samples); indices.push_back((u + 2) * samples - 1); } return Mesh{ vertices, indices, normals, tex_coords }; }