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

extern crate na;
#[macro_use] extern crate color;
#[macro_use] extern crate glin;
extern crate gl;
pub extern crate dds;
extern crate angle;
extern crate alga;
extern crate num;
extern crate rinmath as math;
extern crate rinutil as util;
extern crate rinwindow as window;
extern crate rinio as io;
extern crate seitan as events;
#[macro_use] extern crate bitflags;

#[doc(inline)]
pub use mesh::Mesh;
#[doc(inline)]
pub use mesh::IndexT;
#[doc(inline)]
pub use mesh::PrimitiveType;
#[doc(inline)]
pub use mesh::mesh;
#[doc(inline)]
pub use mesh::load_ply;
#[doc(inline)]
pub use primitives::*;
#[doc(inline)]
pub use path::Path2D;
#[doc(inline)]
pub use node::Node;
#[doc(inline)]
pub use node::NodeT;
#[doc(inline)]
pub use projection::{Projection,CoordinateOrigin};
#[doc(inline)]
pub use mvp::{Mvp, Model};
#[doc(inline)]
pub use gradient::{Gradient, LinearGradientDirection};
#[doc(inline)]
pub use dds::Dds;
#[doc(inline)]
pub use camera::{
    Camera, CameraT,
    arcball_camera::{self, ArcballCamera}
};
#[doc(inline)]
pub use ttf::Ttf;
pub use vertex::*;

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

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

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

#[doc(no_inline)]
pub use image::Image;

mod mesh;
pub mod renderer;
mod primitives;
pub mod node;
pub mod path;
mod gradient;
pub mod vertex;
pub mod projection;
pub mod mvp;
pub mod camera;
pub mod ttf;

use na::*;
use std::f32;
use math::Rect;

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>;


pub fn world_to_camera(p: &Vec4, model_view_projection: &Mat4) -> Vec4{
     let c = *model_view_projection * *p;
     c / c.w
}

pub fn world_to_screen(p: &Vec4, screen_size: &Vec2, model_view_projection: &Mat4) -> Vec4{
    let c = world_to_camera(p, model_view_projection);
    vec4(((c.x + 1.0f32) / 2.0f32 * screen_size.x).round(),
         ((1.0f32 - c.y) / 2.0f32 * screen_size.y).round(),
         c.z, c.w)
}

pub fn screen_to_world(screen: &Vec2, model_view_projection: &Mat4, viewport: &Rect<i32>) -> Vec4 {

	//convert from screen to camera
	let mut camera_xyz = vec4(0.0,0.0,0.0,1.0);
	camera_xyz.x = 2.0f32 * (screen.x - viewport.pos.x as f32) / viewport.width as f32 - 1.0f32;
	camera_xyz.y = 1.0f32 - 2.0f32 *(screen.y - viewport.pos.y as f32) / viewport.height as f32;

	//convert camera to world
	model_view_projection.try_inverse().unwrap() * camera_xyz

}

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}
}

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}
    }
}


#[cfg(not(target_os="android"))]
pub fn primitive_type_to_gl(ty: ::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,
    }
}

#[cfg(target_os="android")]
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,
        _ => -1
    }
}