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
use std::rc::Rc;
use std::sync::Arc;
use std::cell::RefCell;

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

use math::*;
use util::uuid::OsRandNewV4;

#[derive(Debug)]
pub struct PlaneGeometry
{
	uuid: uuid::Uuid,
	width:f32,
	height:f32,
	width_segments:u32,
	height_segments:u32,
	vertices:float3s,
	normals:float3s,
	texcoords:float2s,
	indices:Vec<u16>,
}

impl PlaneGeometry
{
	pub fn new(width:f32, height:f32, width_segments:u32, height_segments:u32) -> Self 
	{
		let mut _vertices = Vec::new();
		let mut _indices = Vec::new();
		let mut _normals = Vec::new();
		let mut _texcoords = Vec::new();

		let grid = (width_segments, height_segments);
		let grid1 = (grid.0 + 1, grid.1 + 1);

		let width_half = width * 0.5;
		let height_half = height * 0.5;
		let segment_width = width / grid.0 as f32;
		let segment_height = height / grid.1 as f32;

		let normal = float!(0.0,1.0,0.0);

		for iz in 0..grid1.1
		{
			for ix in 0..grid1.0
			{
				let x = ix as f32 * segment_width - width_half;
				let z = iz as f32 * segment_height - height_half;

				_vertices.push(float!(x, 0.0, z));
				_normals.push(normal);
			}
		}

		for iy in 0..grid.1
		{
			for ix in 0..grid.0
			{
				let fx = ix as f32;
				let fy = iy as f32;

				let a = ix + grid1.0 * iy;
				let b = ix + grid1.0 * (iy + 1);
				let c = ix + grid1.0 * (iy + 1) + 1;
				let d = ix + grid1.0 * iy + 1;

				_texcoords.push(float!((fx      ) / grid.0 as f32, (fy + 1.0) / grid.1 as f32));
				_texcoords.push(float!((fx + 1.0) / grid.0 as f32, (fy + 1.0) / grid.1 as f32));
				_texcoords.push(float!((fx      ) / grid.0 as f32, (fy      ) / grid.1 as f32));
				_texcoords.push(float!((fx + 1.0) / grid.0 as f32, (fy      ) / grid.1 as f32));

				_indices.push(a as u16);
				_indices.push(b as u16);
				_indices.push(c as u16);

				_indices.push(c as u16);
				_indices.push(d as u16);
				_indices.push(a as u16);
			}
		}

		Self
		{
			uuid:uuid::Uuid::new_v4_osrng(),
			width:width,
			height:height,
			width_segments:width_segments,
			height_segments:height_segments,
			vertices:_vertices,
			normals:_normals,
			texcoords:_texcoords,
			indices:_indices
		}
	}

	pub fn width(&self) -> f32
	{
		return self.width;
	}

	pub fn height(&self) -> f32
	{
		return self.height;
	}

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

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

impl Geometry for PlaneGeometry
{
	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 PlaneGeometry
{
	#[inline]
	fn uuid(&self) -> &uuid::Uuid
	{
		&self.uuid
	}
}

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

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

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

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