Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162//// Created by kam on 10/4/26.//
#include "Geometry.h"#include <cmath>
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<u32> { 0, 1, 2, 2, 3, 0 }, std::vector<f32> {}, 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<f32>{ -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<u32>{ // 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<f32>{ 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<f32> { 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<f32> vertices{}, normals{}, tex_coords{}; std::vector<u32> 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<f32>(samples - 1); const f32 step = 2.f * M_PI / static_cast<f32>(samples); const f32 uv_step = 1.f / static_cast<f32>(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 };}