1#![deny(missing_docs)]
20
21extern crate piston;
22extern crate vecmath;
23extern crate wavefront_obj;
24extern crate image;
25
26mod gl_backend;
27
28pub use gl_backend::Scene;
29
30use std::path::Path;
31use std::io;
32
33use vecmath::*;
34
35#[derive(Copy, Clone, Debug, PartialEq)]
37pub enum Command {
38 UseProgram(Program),
40 SetModelViewProjection(Matrix4Uniform),
42 SetModel(Matrix4Uniform),
44 SetView(Matrix4Uniform),
46 SetTexture(Texture),
48 SetF32(F32Uniform, f32),
50 SetVector2(Vector2Uniform, Vector2<f32>),
52 SetVector3(Vector3Uniform, Vector3<f32>),
54 SetMatrix4(Matrix4Uniform, Matrix4<f32>),
56 EnableFrameBufferSRGB,
58 DisableFrameBufferSRGB,
60 EnableBlend,
62 DisableBlend,
64 EnableCullFace,
66 DisableCullFace,
68 CullFaceFront,
70 CullFaceBack,
72 CullFaceFrontAndBack,
74 DrawTriangles(VertexArray, usize),
76 DrawTriangleStrip(VertexArray, usize),
78 DrawLines(VertexArray, usize),
80 DrawPoints(VertexArray, usize),
82 Translate(Vector3<f32>),
84 TranslateGlobal(Vector3<f32>),
86 Scale(Vector3<f32>),
88 RotateXDeg(f32),
90 RotateYDeg(f32),
92 RotateZDeg(f32),
94 RotateAxisDeg(Vector3<f32>, f32),
96 PushTransform,
98 PopTransform,
100 Draw(CommandList),
102}
103
104#[derive(Debug)]
106pub struct FrameGraph {
107 command_lists: Vec<Vec<Command>>,
108}
109
110impl FrameGraph {
111 pub fn new() -> FrameGraph {
113 FrameGraph {
114 command_lists: vec![]
115 }
116 }
117
118 pub fn command_list(&mut self, commands: Vec<Command>) -> CommandList {
120 let id = self.command_lists.len();
121 self.command_lists.push(commands);
122 CommandList(id)
123 }
124}
125
126#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
128pub struct VertexShader(usize);
129#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
131pub struct FragmentShader(usize);
132#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
134pub struct Program(usize);
135#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
137pub struct Matrix4Uniform(usize);
138#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
140pub struct Vector2Uniform(usize);
141#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
143pub struct Vector3Uniform(usize);
144#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
146pub struct F32Uniform(usize);
147#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
149pub struct VertexArray(usize);
150#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
152pub struct ColorBuffer(usize, usize);
153#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
155pub struct VertexBuffer3(usize, usize);
156#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
158pub struct VertexBuffer2(usize, usize);
159#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
161pub struct UVBuffer(usize, usize);
162#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
164pub struct NormalBuffer(usize, usize);
165#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
167pub struct CommandList(usize);
168#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
170pub struct Texture(usize);
171
172impl ColorBuffer {
173 pub fn len(&self) -> usize {self.1}
175}
176
177impl VertexBuffer3 {
178 pub fn len(&self) -> usize {self.1}
180}
181
182impl VertexBuffer2 {
183 pub fn len(&self) -> usize {self.1}
185}
186
187pub struct ObjMesh {
189 pub vertices: Vec<f32>,
191 pub uvs: Vec<f32>,
193 pub normals: Vec<f32>,
195}
196
197impl ObjMesh {
198 pub fn load<P: AsRef<Path>>(path: P) -> Result<ObjMesh, io::Error> {
200 use std::fs::File;
201 use std::io::Read;
202
203 let mut obj_file = File::open(path)?;
204 let mut data = String::new();
205 obj_file.read_to_string(&mut data)?;
206 let obj_set = wavefront_obj::obj::parse(data).unwrap();
207 let obj = &obj_set.objects[0];
208 let temp_vertices = {
209 let mut res = vec![];
210 for v in &obj.vertices {
211 res.push(v.x as f32);
212 res.push(v.y as f32);
213 res.push(v.z as f32);
214 }
215 res
216 };
217 let temp_uvs = {
218 let mut res = vec![];
219 for uv in &obj.tex_vertices {
220 res.push(uv.u as f32);
221 res.push(1.0 - uv.v as f32);
222 }
223 res
224 };
225 let temp_normals = {
226 let mut res = vec![];
227 for normal in &obj.normals {
228 res.push(normal.x as gl::types::GLfloat);
229 res.push(normal.y as gl::types::GLfloat);
230 res.push(normal.z as gl::types::GLfloat);
231 }
232 res
233 };
234 let mut vertices = vec![];
235 let mut uvs = vec![];
236 let mut normals = vec![];
237 for geom in &obj.geometry {
238 for shape in &geom.shapes {
239 use wavefront_obj::obj::Primitive;
240
241 if let Primitive::Triangle(
242 (a_v, Some(a_uv), Some(a_n)),
243 (b_v, Some(b_uv), Some(b_n)),
244 (c_v, Some(c_uv), Some(c_n))
245 ) = shape.primitive {
246 vertices.push(temp_vertices[a_v * 3 + 0]);
247 vertices.push(temp_vertices[a_v * 3 + 1]);
248 vertices.push(temp_vertices[a_v * 3 + 2]);
249
250 vertices.push(temp_vertices[b_v * 3 + 0]);
251 vertices.push(temp_vertices[b_v * 3 + 1]);
252 vertices.push(temp_vertices[b_v * 3 + 2]);
253
254 vertices.push(temp_vertices[c_v * 3 + 0]);
255 vertices.push(temp_vertices[c_v * 3 + 1]);
256 vertices.push(temp_vertices[c_v * 3 + 2]);
257
258 uvs.push(temp_uvs[a_uv * 2 + 0]);
259 uvs.push(temp_uvs[a_uv * 2 + 1]);
260
261 uvs.push(temp_uvs[b_uv * 2 + 0]);
262 uvs.push(temp_uvs[b_uv * 2 + 1]);
263
264 uvs.push(temp_uvs[c_uv * 2 + 0]);
265 uvs.push(temp_uvs[c_uv * 2 + 1]);
266
267 normals.push(temp_normals[a_n * 3 + 0]);
268 normals.push(temp_normals[a_n * 3 + 1]);
269 normals.push(temp_normals[a_n * 3 + 2]);
270
271 normals.push(temp_normals[b_n * 3 + 0]);
272 normals.push(temp_normals[b_n * 3 + 1]);
273 normals.push(temp_normals[b_n * 3 + 2]);
274
275 normals.push(temp_normals[c_n * 3 + 0]);
276 normals.push(temp_normals[c_n * 3 + 1]);
277 normals.push(temp_normals[c_n * 3 + 2]);
278 }
279 }
280 }
281 Ok(ObjMesh {
282 vertices,
283 uvs,
284 normals
285 })
286 }
287}
288
289#[derive(Clone)]
291pub struct SceneSettings {
292 clear_depth_buffer: bool,
293 clear_enable_depth_test: bool,
294}
295
296impl SceneSettings {
297 pub fn new() -> SceneSettings {
299 SceneSettings {
300 clear_depth_buffer: true,
301 clear_enable_depth_test: true,
302 }
303 }
304
305 pub fn clear_depth_buffer(mut self, val: bool) -> Self {
307 self.clear_depth_buffer = val;
308 self
309 }
310
311 pub fn clear_enable_depth_test(mut self, val: bool) -> Self {
315 self.clear_enable_depth_test = val;
316 self
317 }
318}
319
320impl Default for SceneSettings {
321 fn default() -> Self {SceneSettings::new()}
322}
323
324#[cfg(test)]
325mod tests {
326 #[test]
327 fn it_works() {
328 assert_eq!(2 + 2, 4);
329 }
330}