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
// #![doc(html_logo_url = "https://rin.rs/logo.svg")]
// #![doc(html_favicon_url = "https://rin.rs/favicon.ico")]

#[cfg(feature="dds")]
pub extern crate dds;

#[doc(inline)]
pub use self::mesh::Mesh;
#[doc(inline)]
pub use self::mesh::IndexT;
#[doc(inline)]
pub use self::mesh::PrimitiveType;
#[doc(inline)]
pub use self::mesh::mesh;
#[doc(inline)]
pub use self::mesh::load_ply;
#[doc(inline)]
pub use self::primitives::*;
#[doc(inline)]
pub use self::path::Path2D;
#[doc(inline)]
pub use self::node::Node;
#[doc(inline)]
pub use self::node::{NodeRef, NodeMut};
#[doc(inline)]
pub use self::projection::{Projection,CoordinateOrigin};
#[doc(inline)]
pub use self::mvp::{Mvp, Model, CameraMatrices, ModelMatrices};
#[doc(inline)]
pub use self::gradient::{Gradient, LinearGradientDirection};
#[doc(inline)]
#[cfg(feature="dds")]
pub use self::dds::Dds;
#[doc(inline)]
pub use self::camera::{
    Camera, CameraExt, CameraPerspective, CameraOrthographic,
    arcball_camera::{self, ArcballCamera},
    ortho_camera::{self, OrthoCamera},
};
#[cfg(feature="ttf")]
#[doc(inline)]
pub use self::ttf::Ttf;

#[cfg(feature="ttf_rusttype")]
pub use self::ttf_rusttype as ttf;
#[cfg(feature="ttf_rusttype")]
#[doc(inline)]
pub use self::ttf::Ttf;

pub use self::vertex::*;

#[cfg(feature = "freeimage")]
pub mod freeimage;

#[cfg(feature = "freeimage")]
pub use self::freeimage as image;

#[doc(no_inline)]
#[cfg(feature = "freeimage")]
pub use self::freeimage::Image;

#[cfg(feature = "image")]
pub mod image;

#[doc(no_inline)]
#[cfg(feature = "image")]
pub use self::image::Image;


mod mesh;
mod primitives;
pub mod node;
pub mod path;
mod gradient;
pub mod vertex;
pub mod projection;
pub mod mvp;
pub mod camera;
#[cfg(feature="ttf")]
pub mod ttf;
#[cfg(all(feature="ttf_rusttype", feature="image"))]
pub mod ttf_rusttype;

use na::*;
use std::f32;
use math::Rect;
#[cfg(any(feature="gl", feature="gles", feature="webgl"))]
use glin::gl;

pub type Mesh2D = Mesh<Vertex2D>;
pub type Mesh2DColor = Mesh<Vertex2DColor>;
pub type Mesh2DTex = Mesh<Vertex2DTex>;
pub type Mesh2DTexColor = Mesh<Vertex2DTexColor>;
pub type Mesh2DTex3D = Mesh<Vertex2DTex3D>;
pub type Mesh3D = Mesh<Vertex3D>;
pub type Mesh3DColor = Mesh<Vertex3DColor>;
pub type Mesh3DColorNormal = Mesh<Vertex3DColorNormal>;
pub type Mesh3DNormal = Mesh<Vertex3DNormal>;
pub type Mesh3DTexColor = Mesh<Vertex3DTexColor>;
pub type Mesh3DTexNormal = Mesh<Vertex3DTexNormal>;
pub type Mesh3DTex = Mesh<Vertex3DTex>;

/// Z value for screen <-> world conversions
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ScreenZ{
    /// The z value is in world coordinates
    World(f32),
    /// The z value is in normalized depth
    Depth(f32),
}

/// Converts a 3d point to a projection in the screen in 2D + normalized coordinates of it's depth
pub fn world_to_screen(p: &Pnt3, screen_size: &Vec2, projection_view: &Mat4) -> (Pnt2, ScreenZ){
    let c = *projection_view * pnt4!(p, 1.0);
    let c = c.xyz() / c.w;
    let p = pnt2(((c.x + 1.0f32) / 2.0f32 * screen_size.x).round(),
         ((1.0f32 - c.y) / 2.0f32 * screen_size.y).round());
    (p, ScreenZ::Depth(c.z))
}

/// Converts a screen position + a z value in either world coordinates
/// or normalized depth into a world 3d position
pub fn screen_to_world(screen: &Pnt2, depth: ScreenZ, projection: &Mat4, view: &Mat4, viewport: &Rect<i32>) -> Pnt3 {
	//convert from screen to camera
	let x = 2.0f32 * (screen.x - viewport.pos.x as f32) / viewport.width as f32 - 1.0f32;
	let y = 1.0f32 - 2.0f32 * (screen.y - viewport.pos.y as f32) / viewport.height as f32;
    let z = match depth{
        ScreenZ::Depth(depth) => depth,
        ScreenZ::World(z) => {
            // let far = projection.column(2)[3] / (projection.column(2)[2] - 1.);
            // let near  = projection.column(2)[3] / (projection.column(2)[2] + 1.);
            // let depth = ((far + near - z / (2. * near * far)) / (far - near) + 1.) / (far - near) / 2.;
            // depth

            let w = (projection * view) * vec4(0., 0., z, 1.);
            let w = w.xyz() / w.w;
            w.z
        }
    };
    let camera_xyz = vec4(x, y, z, 1.);

	//convert camera to world
	let v4 = (projection * view).try_inverse().unwrap() * camera_xyz;
    (v4.xyz() / v4.w).to_pnt()

}

fn num_partial_min<T:PartialOrd>(a: T, b: T) -> T{
    if a<b {a} else {b}
}

fn num_partial_max<T:PartialOrd>(a: T, b: T) -> T{
    if a>b {a} else {b}
}

/// Bounding box of a set of 2d points
pub fn bounding_box<T: BaseNum>(mesh: &[Vec2<T>]) -> Rect<T>{
    if mesh.is_empty(){
        return Rect{pos: pnt2(zero(), zero()), width: zero(), height: zero()}
    }else{
        let mut iter = mesh.iter();
        let (max, min) = iter.next()
            .map(|&head| iter.fold((head, head), |(max, min), v| {
                let max_x = num_partial_max(max.x, v.x);
                let max_y = num_partial_max(max.y, v.y);
                let min_x = num_partial_min(min.x, v.x);
                let min_y = num_partial_min(min.y, v.y);
                (vec2(max_x, max_y), vec2(min_x, min_y))
            }))
            .unwrap();
        Rect{ pos: pnt2(min.x, min.y), width: max.x - min.x, height: max.y - min.y}
    }
}

/// Mesh primitive type to gl primitive type
#[cfg(feature="gl")]
pub fn primitive_type_to_gl(ty: mesh::PrimitiveType) -> u32{
    match ty{
        PrimitiveType::Triangles => gl::TRIANGLES,
        PrimitiveType::TriangleStrip => gl::TRIANGLE_STRIP,
        PrimitiveType::TriangleFan => gl::TRIANGLE_FAN,
        PrimitiveType::Lines => gl::LINES,
        PrimitiveType::LineStrip => gl::LINE_STRIP,
        PrimitiveType::LineLoop => gl::LINE_LOOP,
        PrimitiveType::LinesAdjacency => gl::LINES_ADJACENCY,
        PrimitiveType::LineStripAdjacency => gl::LINE_STRIP_ADJACENCY,
        PrimitiveType::Points => gl::POINTS,
        PrimitiveType::Patches => gl::PATCHES,
    }
}

/// Mesh primitive type to gles primitive type
#[cfg(any(feature="gles", feature="webgl"))]
pub fn primitive_type_to_gl(ty: mesh::PrimitiveType) -> u32{
    match ty{
        PrimitiveType::Triangles => gl::TRIANGLES,
        PrimitiveType::TriangleStrip => gl::TRIANGLE_STRIP,
        PrimitiveType::TriangleFan => gl::TRIANGLE_FAN,
        PrimitiveType::Lines => gl::LINES,
        PrimitiveType::LineStrip => gl::LINE_STRIP,
        PrimitiveType::LineLoop => gl::LINE_LOOP,
        PrimitiveType::Points => gl::POINTS,
        _ => unimplemented!()
    }
}