sculpto
sphere.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file sphere.cpp
3 * \brief Topology sphere object class implementation class.
4 *
5 * \author Sabitov Kirill
6 * \date 30 June 2022
7 *********************************************************************/
8
9#include "sclpch.h"
10#include "sphere.h"
11
12scl::topology::sphere::sphere(const vec3 &Center, float Radius, int Slices) :
13 grid(Slices, Slices)
14{
15 for (int i = 0; i <= Slices; i++)
16 {
17 float theta = (float)i / Slices * math::PI;
18 for (int j = 0; j <= Slices; j++)
19 {
20 float phi = (float)j / Slices * 2 * math::PI;
21 float x = sin(phi) * sin(theta);
22 float y = cos(phi);
23 float z = sin(phi) * cos(theta);
24 float x_tbn = sin(phi) * cos(theta);
25 float y_tbn = cos(phi) * cos(theta);
26 vec3 position { x, y, z };
27 vec3 tangent = position.Cross({ x_tbn, 0, y_tbn }).Normalized();
28 vec3 bitangent = tangent.Cross(position).Normalized();
29 vec2 texture_coordinates { (float)i / Slices, (float)j / Slices };
30
31 Vertices.emplace_back(
32 position * Radius + Center, position,
33 tangent, bitangent,
34 texture_coordinates
35 );
36 }
37 }
38
39 Min = -vec3(Radius), Max = vec3(Radius);
40}
vec3 Normalized() const
Definition: vec3.h:190
vec3 Cross(const vec3 &Other) const
Definition: vec3.h:224
std::vector< vertex > Vertices
Definition: basis.h:22
sphere(const vec3 &Center, float Radius, int Slices)
Definition: sphere.cpp:12
const float PI
Definition: math_common.h:33
math::vec3< float > vec3
Definition: base.h:38
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.
Topology sphere object class implementation module.