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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use cgmath::{Point3, Matrix4};
use collider::Aabb;
use std::io::{self, Read};
use byteorder::{ReadBytesExt, LittleEndian};
use {ModelHeader, MAGIC, Encode};
use scene::NodeData;
use std::borrow::Cow;

// 1.1
// 	Adds the TagPoints chunk
//  Adds the tag_points count in Quantities.
// 1.2
//  Adds the sub_models count in Quantities.
// 1.3
//  Adds the array to MiscChunk.

/// Expected: SSMF v1.3
pub const EXPECTED_MODEL_HEADER: ModelHeader = ModelHeader { magic: MAGIC, major: 1, minor: 3 };

#[derive(Debug)]
pub struct V1 {
	pub quantities: Quantities,
	pub center: Point3<f32>,
	pub unknown: u8,
	pub points: Vec<u32>,
	pub triangles: Vec<(Vertex, Vertex, Vertex)>,
	pub triangle_groups: Vec<TriangleGroup>,
	pub materials: Vec<Material>,
	pub vertices: Vec<(u32, f32)>,
	pub tag_points: Vec<String>,
	pub frames: Vec<Frame>
}

impl V1 {
	pub fn read<R>(r: &mut R) -> io::Result<(Self, NodeData)> where R: Read {
		let quantities = Quantities::read(r)?;

		let node = NodeData {
			additional_models: quantities.additional_models,
			name: Cow::Owned(String::read(r)?)
		};

		Ok((V1 {
			center: Point3::read(r)?,
			unknown: r.read_u8()?,
			points: {
				let mut points = Vec::with_capacity(quantities.vertex_points as usize);

				for _ in 0..quantities.vertex_points {
					points.push(r.read_u32::<LittleEndian>()?);
				}

				points
			},
			triangles: {
				let mut triangles = Vec::with_capacity(quantities.triangles as usize);

				for _ in 0..quantities.triangles {
					triangles.push((
						Vertex::read(r)?,
						Vertex::read(r)?,
						Vertex::read(r)?
					));
				}

				triangles
			},
			triangle_groups: {
				let mut triangle_groups = Vec::with_capacity(quantities.triangle_groups as usize);

				for _ in 0..quantities.triangle_groups {
					triangle_groups.push(TriangleGroup::read(r)?);
				}

				triangle_groups
			},
			materials: {
				let mut materials = Vec::with_capacity(quantities.materials as usize);

				for _ in 0..quantities.materials {
					materials.push(Material::read(r)?);
				}

				materials
			},
			vertices: {
				let mut vertices = Vec::with_capacity(quantities.vertices as usize);

				for _ in 0..quantities.vertices {
					vertices.push((
						r.read_u32::<LittleEndian>()?,
						r.read_f32::<LittleEndian>()?
					));
				}

				vertices
			},
			tag_points: {
				let mut tag_points = Vec::with_capacity(quantities.tag_points as usize);

				for _ in 0..quantities.tag_points {
					tag_points.push(String::read(r)?);
				}

				tag_points
			},
			frames: {
				let mut frames = Vec::with_capacity(quantities.frames as usize);

				for _ in 0..quantities.frames {
					frames.push(Frame::read(r, &quantities)?);
				}

				frames
			},
			quantities
		}, node ))
	}
}

/// Contains metadata about the quantities of certain things in this file.
/// Not useful on its own, but necessary to parse the rest of the file.
#[derive(Debug)]
pub struct Quantities {
	pub frames:  u32,
	pub materials:  u32,
	/// Number of individual vertex points. This only represents the count of unique position components.
	pub vertex_points:  u32,
	pub triangles: u32,
	pub triangle_groups:  u32,
	/// Count of unique vertices, including vertex normals and texture positions. Should always be >= vertex_points.
	pub vertices:  u32,
	pub tag_points:  u32,
	pub additional_models:  u32
}

impl Quantities {
	pub fn read<R>(r: &mut R) -> io::Result<Self> where R: Read {
		Ok(Quantities {
			frames:  r.read_u32::<LittleEndian>()?,
			materials:  r.read_u32::<LittleEndian>()?,
			vertex_points:  r.read_u32::<LittleEndian>()?,
			triangles: r.read_u32::<LittleEndian>()?,
			triangle_groups:  r.read_u32::<LittleEndian>()?,
			vertices:  r.read_u32::<LittleEndian>()?,
			tag_points:  r.read_u32::<LittleEndian>()?,
			additional_models:  r.read_u32::<LittleEndian>()?
		})
	}
}

#[derive(Debug)]
pub struct Vertex {
	pub unknown0: u32,
	pub uv: (f32, f32),
	pub rgb: (f32, f32, f32),
	// Unknown, seems to be constant throughout the file
	pub unknown1: [f32; 4]
}

impl Vertex {
	pub fn read<R>(r: &mut R) -> io::Result<Self> where R: Read {
		Ok(Vertex {
			unknown0: r.read_u32::<LittleEndian>()?,
			uv: (
				r.read_f32::<LittleEndian>()?,
				r.read_f32::<LittleEndian>()?
			),
			rgb: (
				r.read_f32::<LittleEndian>()?,
				r.read_f32::<LittleEndian>()?,
				r.read_f32::<LittleEndian>()?
			),
			unknown1: [
				r.read_f32::<LittleEndian>()?,
				r.read_f32::<LittleEndian>()?,
				r.read_f32::<LittleEndian>()?,
				r.read_f32::<LittleEndian>()?
			]
		})
	}
}

#[derive(Debug)]
pub struct TriangleGroup {
	name: String,
	indices: Vec<u32>
}

impl TriangleGroup {
	pub fn read<R>(r: &mut R) -> io::Result<Self> where R: Read {
		Ok(TriangleGroup {
			name: String::read(r)?,
			indices: {
				let len = r.read_u32::<LittleEndian>()?;
				let mut indices = Vec::with_capacity(len as usize);

				for _ in 0..len {
					indices.push(r.read_u32::<LittleEndian>()?);
				}

				indices
			}
		})
	}
}

#[derive(Debug)]
pub struct Material {
	pub indices: Vec<u32>,
	/// Second value has an unknown meaning.
	pub texture: Option<(String, u32)>
}

impl Material {
	pub fn read<R>(r: &mut R) -> io::Result<Self> where R: Read {
		Ok(Material {
			indices: {
				let len = r.read_u32::<LittleEndian>()?;
				let mut indices = Vec::with_capacity(len as usize);

				for _ in 0..len {
					indices.push(r.read_u32::<LittleEndian>()?);
				}

				indices
			},
			texture: match r.read_u8()? {
				0 => None,
				1 => Some((String::read(r)?, r.read_u32::<LittleEndian>()?)),
				x => return Err(io::Error::new(io::ErrorKind::InvalidData, format!("A boolean must be 0 or 1, got {}", x)))
			}
		})
	}
}

#[derive(Debug)]
pub struct Frame {
	pub radius:           f32,
	pub points:           Vec<Point3<f32>>,
	/// Quantized normal vector index. 10086 to choose from.
	pub normals:          Vec<u16>,
	// pub triangle_normals: Vec<Point3<f32>>, // Removed in v1.3
	pub tag_points:       Vec<Point3<f32>>,
	pub transform:        Matrix4<f32>,
	pub bound:            Aabb
}

impl Frame {
	pub fn read<R>(r: &mut R, quantities: &Quantities) -> io::Result<Self> where R: Read {
		Ok(Frame {
			radius: r.read_f32::<LittleEndian>()?,
			points: {
				let mut points = Vec::with_capacity(quantities.vertex_points as usize);

				for _ in 0..quantities.vertex_points {
					points.push(Point3::read(r)?);
				}

				points
			},
			normals: {
				let mut normals = Vec::with_capacity(quantities.vertices as usize);

				for _ in 0..quantities.vertices {
					normals.push(r.read_u16::<LittleEndian>()?);
				}

				normals
			},
			/*triangle_normals: {
				let mut triangle_normals = Vec::with_capacity(quantities.triangles as usize);

				for _ in 0..quantities.triangles {
					triangle_normals.push(Point3::read(r)?);
				}

				triangle_normals
			},*/
			tag_points: {
				let mut tag_points = Vec::with_capacity(quantities.tag_points as usize);

				for _ in 0..quantities.tag_points {
					tag_points.push(Point3::read(r)?);
				}

				tag_points
			},
			transform: Matrix4::read(r)?,
			bound: Aabb::read(r)?
		})
	}
}