inline Mesh* Mesh::Sphere(std::string name, float32 radius, uint32 segs, uint32 rgs) { Mesh* mesh = new Mesh(name); uint32 segments = Mathf.Max(segs, 3) + 1, rings = Mathf.Max(rgs, 3) + 2; float32 PI = Mathf.PI, TWO_PI = PI * 2.0f, HALF_PI = PI * 0.5f, R = 1.0f / (rings - 1), S = 1.0f / (segments - 1), x, y, z; uint32 r, s, a, b, c, d; Array<Vec3f*>& vertices = mesh->vertices; Array<Vec3f*>& normals = mesh->normals; Array<Vec2f*>& uv = mesh->uv; Array<Colorf*>& colors = mesh->colors; Array<uint32>& indices = mesh->indices; for (r = 0; r < rings; r++) { for (s = 0; s < segments; s++) { x = Mathf.Cos(TWO_PI * s * S) * Mathf.Sin(PI * r * R); y = Mathf.Sin(TWO_PI * s * S) * Mathf.Sin(PI * r * R); z = Mathf.Sin(-HALF_PI + PI * r * R); vertices.Push(new Vec3f(x * radius, y * radius, z * radius)); normals.Push(new Vec3f(x, y, z)); uv.Push(new Vec2f(s * S, r * R)); colors.Push(new Colorf(s * S, r * R, 0.0f)); } } for (r = 0; r < rings - 1; r++) { for (s = 0; s < segments - 1; s++) { a = r * segments + s; b = r * segments + (s + 1); c = (r + 1) * segments + (s + 1); d = (r + 1) * segments + s; indices.Push(a); indices.Push(b); indices.Push(c); indices.Push(a); indices.Push(c); indices.Push(d); } } mesh->CalculateTangents(); mesh->CalculateAABB(); return mesh; }
inline Mesh* Mesh::Plane(std::string name, float32 width, float32 height, int32 widthSegments, int32 heightSegments) { Mesh* mesh = new Mesh(name); Mesh::m_BuildPlane(mesh, 0, 1, 1, 1, width, widthSegments, height, heightSegments, 0, 0); mesh->CalculateTangents(); mesh->CalculateAABB(); return mesh; }
inline Mesh* Mesh::Cube(std::string name, float32 width, float32 height, float32 depth, int32 widthSegments, int32 heightSegments, int32 depthSegments) { float32 hw = width * 0.5, hh = height * 0.5, hd = depth * 0.5; Mesh* mesh = new Mesh(name); Mesh::m_BuildPlane(mesh, 2, 1, -1, 1, depth, depthSegments, height, heightSegments, hw, widthSegments); Mesh::m_BuildPlane(mesh, 2, 1, 1, 1, depth, depthSegments, height, heightSegments, -hw, widthSegments); Mesh::m_BuildPlane(mesh, 0, 2, 1, -1, width, widthSegments, depth, depthSegments, hh, heightSegments); Mesh::m_BuildPlane(mesh, 0, 2, 1, 1, width, widthSegments, depth, depthSegments, -hh, heightSegments); Mesh::m_BuildPlane(mesh, 0, 1, 1, 1, width, widthSegments, height, heightSegments, hd, depthSegments); Mesh::m_BuildPlane(mesh, 0, 1, -1, 1, width, widthSegments, height, heightSegments, -hd, depthSegments); mesh->CalculateTangents(); mesh->CalculateAABB(); return mesh; }