e2rcore/implement/file/
wavefrontcomp.rs

1#![allow(non_snake_case)]
2#![allow(unused_variables)]
3
4extern crate pretty_env_logger;
5extern crate nom;
6
7use std::str;
8use std::str::FromStr;
9use std::f32;
10
11use self::nom::digit;
12
13use interface::i_wavefront::{ obj, compute };
14
15pub fn process( input: & obj::Collection ) -> Result< compute::ComputeCollection, & 'static str > {
16
17    let mut batch_vert = vec![];
18    let mut batch_normal = vec![];
19    let mut batch_tc = vec![];
20    let mut bbox_upper = [ 0f32; 3 ];
21    let mut bbox_lower = [ 0f32; 3 ];
22
23    let mut verts = vec![];
24    let mut normals = vec![];
25    let mut texture_coords = vec![];
26    
27    input._groups.iter().for_each( |x| {
28        verts.extend_from_slice( &x._verts[..] );
29        normals.extend_from_slice( &x._vert_normals[..] );
30        texture_coords.extend_from_slice( &x._texture_coords[..] );
31    } );
32
33    for i in input._groups.iter() {
34        for j in i._faces.iter() {
35            for k in 0..3 {
36                let v_idx = j._vert_index[ k ] - 1;
37                if let None = j._normal_index {
38                    return Err( "normal index not present")
39                }
40                let n_idx = j._normal_index.unwrap()[ k ] - 1;
41
42
43                let tc = match j._tc_index {
44                    Some(x) => {
45                        let tc_idx = j._tc_index.unwrap()[ k ] - 1;
46                        assert!( tc_idx < texture_coords.len() );
47                        &texture_coords[ tc_idx ][..]
48                    },
49                    None => {
50                        &[ 0., 0. ][..]
51                    }
52                    // return Err( "texture coord index not present")
53                };
54
55                if v_idx >= verts.len() {
56                    return Err( "vertex index out of range" )
57                }
58                assert!( v_idx < verts.len() );
59                assert!( n_idx < normals.len() );
60                
61                let v = &verts[ v_idx ][..];
62                let n = &normals[ n_idx ][..];
63                
64                for l in 0..3 {
65                    bbox_upper[l] = bbox_upper[l].max( v[l] );
66                    bbox_lower[l] = bbox_lower[l].min( v[l] );
67                }
68                
69                batch_vert.extend_from_slice( v );
70                batch_normal.extend_from_slice( n );
71                batch_tc.extend_from_slice( tc );
72            }
73        }
74    }
75
76    Ok(
77        compute::ComputeCollection {
78            _bbox_upper: bbox_upper,
79            _bbox_lower: bbox_lower,
80            _batch_vert: batch_vert,
81            _batch_normal: batch_normal,
82            _batch_tc: batch_tc,
83        }
84    )
85}