1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use math::*;

use std::rc::Rc;
use std::sync::Arc;
use std::cell::RefCell;

use crate::core::geometry::*;
use crate::core::resource::*;

use util::uuid::OsRandNewV4;

#[derive(Debug)]
pub struct SphereGeometry
{
	uuid: uuid::Uuid,
	radius:f32,
	width_segments:u32,
	height_segments:u32,
	phi_start:f32,
	phi_length:f32,
	theta_start:f32,
	theta_length:f32,
	vertices:float3s,
	normals:float3s,
	texcoords:float2s,
	indices:Vec<u16>,
}

impl SphereGeometry 
{
	pub fn new(radius:f32, width_segments:u32, height_segments:u32, phi_start:f32, phi_length:f32, theta_start:f32, theta_length:f32) -> Self 
	{
		let mut _vertices = Vec::new();
		let mut _indices = Vec::new();
		let mut _normals = Vec::new();
		let mut _texcoords = Vec::new();
		let mut vertices = Vec::new();

		for y in 0.. height_segments + 1
		{
			for x in 0.. width_segments + 1
			{
				let u = (x as f32) / (width_segments as f32);
				let v = (y as f32) / (height_segments as f32);

				let mut vertex = float!(
					-radius * (theta_start + v * theta_length).sin() * (phi_start + u * phi_length).cos(),
					radius * (theta_start + v * theta_length).cos(),
					radius * (theta_start + v * theta_length).sin() * (phi_start + u * phi_length).sin()
				);

				let mut normal = vertex.normalize();

				_vertices.push(vertex);
				_normals.push(normal);
				_texcoords.push(float2::new(u, v));

				vertices.push((_vertices.len() - 1) as u16);
			}
		}

		for y in 0..height_segments
		{
			for x in 0..width_segments
			{
				let v1 = vertices[(y * (width_segments + 1) + x) as usize];
				let v2 = vertices[(y * (width_segments + 1) + x + 1) as usize];
				let v3 = vertices[((y + 1) * (width_segments + 1) + x) as usize];
				let v4 = vertices[((y + 1) * (width_segments + 1) + x + 1) as usize];

				if _vertices[v2 as usize].y.abs() == radius
				{
					_indices.push(v2);
					_indices.push(v3);
					_indices.push(v4);
				}
				else if _vertices[v3 as usize].y.abs() == radius
				{
					_indices.push(v2);
					_indices.push(v1);
					_indices.push(v3);
				}
				else
				{
					_indices.push(v2);
					_indices.push(v3);
					_indices.push(v4);

					_indices.push(v2);
					_indices.push(v1);
					_indices.push(v3);
				}
			}
		}

		Self
		{
			uuid:uuid::Uuid::new_v4_osrng(),
			radius:radius,
			width_segments:width_segments,
			height_segments:height_segments,
			phi_start:phi_start,
			phi_length:phi_length,
			theta_start:theta_start,
			theta_length:theta_length,
			vertices:_vertices,
			normals:_normals,
			texcoords:_texcoords,
			indices:_indices,
		}
	}

	pub fn new_1(radius:f32, width_segments:u32, height_segments:u32) -> Self 
	{
		SphereGeometry::new(radius, width_segments, height_segments, 0., 2. * f32::pi(), 0., f32::pi())
	}

	pub fn new_2(radius:f32, width_segments:u32, height_segments:u32, phi_start:f32, phi_length:f32, theta_start:f32, theta_length:f32) -> Self 
	{
		SphereGeometry::new(radius, width_segments, height_segments, phi_start, phi_length, theta_start, theta_length)
	}

	pub fn radius(&self) -> f32
	{
		self.radius
	}

	pub fn width_segments(&self) -> u32
	{
		self.width_segments
	}

	pub fn height_segments(&self) -> u32
	{
		self.height_segments
	}

	pub fn phi_start(&self) -> f32
	{
		self.phi_start
	}

	pub fn phi_length(&self) -> f32
	{
		self.phi_length
	}

	pub fn theta_start(&self) -> f32
	{
		self.theta_start
	}

	pub fn theta_length(&self) -> f32
	{
		self.theta_length
	}
}

impl Geometry for SphereGeometry
{
	fn vertices(&self) -> &[float3]
	{
		&self.vertices[..]
	}

	fn normals(&self) -> &[float3]
	{
		&self.normals[..]
	}

	fn texcoords(&self) -> &[float2]
	{
		&self.texcoords[..]
	}

	fn indices(&self) -> &[u16]
	{
		&self.indices[..]
	}
}

impl Resource for SphereGeometry
{
	#[inline]
	fn uuid(&self) -> &uuid::Uuid
	{
		&self.uuid
	}
}

impl From<SphereGeometry> for Rc<Geometry + 'static>
{
	fn from(sphere:SphereGeometry) -> Self
	{
		Rc::new(sphere)
	}
}

impl From<SphereGeometry> for Arc<Geometry + 'static>
{
	fn from(sphere:SphereGeometry) -> Self
	{
		Arc::new(sphere)
	}
}

impl From<SphereGeometry> for Rc<RefCell<Geometry + 'static>>
{
	fn from(sphere:SphereGeometry) -> Self
	{
		Rc::new(RefCell::new(sphere))
	}
}

impl From<SphereGeometry> for Arc<RefCell<Geometry + 'static>>
{
	fn from(sphere:SphereGeometry) -> Self
	{
		Arc::new(RefCell::new(sphere))
	}
}