extern crate cgmath;
extern crate euler;
extern crate awi;
use std::cmp::Ordering;
pub use awi::{
afi, afi::VFrame, Input, Window, WindowConnection
};
pub use euler::*;
pub use std::f32::consts::PI;
pub trait Display {
fn color(&mut self, color: (f32, f32, f32)) -> ();
fn fog(&mut self, fog: Option<(f32, f32)>) -> ();
fn update(&mut self) -> Option<Input>;
fn camera(&mut self, position: Vec3, rotation: Vec3) -> ();
fn model(&mut self, vertices: &[f32], fans: Vec<(u32, u32)>) -> Model;
fn texture(&mut self, wh: (u16,u16), graphic: &VFrame) -> Texture;
fn gradient(&mut self, colors: &[f32]) -> Gradient;
fn texcoords(&mut self, texcoords: &[f32]) -> TexCoords;
fn set_texture(&mut self, texture: &mut Texture, wh: (u16,u16),
graphic: &VFrame) -> ();
fn shape_solid(&mut self, model: &Model, transform: Transform,
color: [f32; 4], blending: bool, fog: bool, camera: bool)
-> Shape;
fn shape_gradient(&mut self, model: &Model, transform: Transform,
gradient: Gradient, blending: bool, fog: bool, camera: bool)
-> Shape;
fn shape_texture(&mut self, model: &Model, transform: Transform,
texture: &Texture, tc: TexCoords, blending: bool,
fog: bool, camera: bool) -> Shape;
fn shape_faded(&mut self, model: &Model, transform: Transform,
texture: &Texture, tc: TexCoords, alpha: f32,
fog: bool, camera: bool) -> Shape;
fn shape_tinted(&mut self, model: &Model, transform: Transform,
texture: &Texture, tc: TexCoords, tint: [f32; 4],
blending: bool, fog: bool, camera: bool) -> Shape;
fn shape_complex(&mut self, model: &Model, transform: Transform,
texture: &Texture, tc: TexCoords,
gradient: Gradient, blending: bool,
fog: bool, camera: bool) -> Shape;
fn drop_shape(&mut self, shape: &Shape);
fn transform(&mut self, shape: &Shape, transform: Transform);
fn resize(&mut self, wh: (u16, u16)) -> ();
fn wh(&self) -> (u16, u16);
}
#[derive(Clone)]
pub enum ShapeHandle {
Alpha(u32),
Opaque(u32),
Gui(u32),
}
pub struct Shape(ShapeHandle);
#[derive(Copy, Clone)]
pub struct Model(pub usize);
#[derive(Copy, Clone)]
pub struct Gradient(pub usize);
#[derive(Copy, Clone)]
pub struct TexCoords(pub usize);
pub struct Texture(pub usize, pub u16, pub u16);
pub fn new_shape(i: ShapeHandle) -> Shape {
Shape(i)
}
pub fn get_shape(s: &Shape) -> ShapeHandle {
s.0.clone()
}
pub fn projection(ratiox: f32, fovy: f32) -> Transform {
let a: [[f32;4];4] = cgmath::perspective(
cgmath::Rad(fovy),
ratiox,
0.1, 100.0, ).into();
Transform::IDENTITY
.m(mat4!(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, -1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
))
.m(Mat4::from(a))
}
pub trait Point {
fn point(&self) -> Vec3;
}
pub fn zsort<T: Point>(sorted: &mut Vec<u32>, points: &Vec<T>, nr: bool,
position: Vec3)
{
sorted.sort_unstable_by(|a, b| {
let p1 = points[*a as usize].point() - position;
let p2 = points[*b as usize].point() - position;
if p1.length() > p2.length() {
if nr { Ordering::Greater } else { Ordering::Less }
} else if p1.length() < p2.length() {
if nr { Ordering::Less } else { Ordering::Greater }
} else {
Ordering::Equal
}
});
}
#[derive(Copy, Clone)]
pub struct Transform(pub Mat4);
impl Transform {
pub const IDENTITY: Transform = Transform(Mat4 {
m00: 1.0, m01: 0.0, m02: 0.0, m03: 0.0,
m10: 0.0, m11: 1.0, m12: 0.0, m13: 0.0,
m20: 0.0, m21: 0.0, m22: 1.0, m23: 0.0,
m30: 0.0, m31: 0.0, m32: 0.0, m33: 1.0,
});
#[inline(always)]
pub fn srt(self, scale: Vec3, rotate: Vec3, translate: Vec3) -> Self {
self.s(scale).r(rotate).t(translate)
}
#[inline(always)]
pub fn rt(self, rotate: Vec3, translate: Vec3) -> Transform {
self.r(rotate).t(translate)
}
#[inline(always)]
pub fn st(self, scale: Vec3, translate: Vec3) -> Transform {
self.s(scale).t(translate)
}
#[inline(always)]
pub fn t(self, translate: Vec3) -> Transform {
self.m(Trs::new(
translate, quat!(), vec3!(1.0, 1.0, 1.0), ).matrix())
}
#[inline(always)]
pub fn s(self, scale: Vec3) -> Transform {
self.m(Trs::new(
vec3!(0.0, 0.0, 0.0), quat!(), scale, ).matrix())
}
#[inline(always)]
pub fn r(self, rotate: Vec3) -> Transform {
self.m(Trs::new(
vec3!(0.0, 0.0, 0.0), Quat::euler(rotate), vec3!(1.0, 1.0, 1.0), ).matrix())
}
#[inline(always)]
pub fn m(self, matrix: Mat4) -> Transform {
Transform(matrix * self.0)
}
}
impl Into<[f32;16]> for Transform {
fn into(self) -> [f32; 16] {
let matrix = self.0;
[
matrix.m00, matrix.m01, matrix.m02, matrix.m03,
matrix.m10, matrix.m11, matrix.m12, matrix.m13,
matrix.m20, matrix.m21, matrix.m22, matrix.m23,
matrix.m30, matrix.m31, matrix.m32, matrix.m33,
]
}
}