This commit is contained in:
Thomas Forgione 2018-06-14 22:03:43 +02:00
parent 654a2df6d9
commit 881914f549
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
6 changed files with 26 additions and 33 deletions

View File

@ -9,6 +9,7 @@ glium = "*"
image = "*" image = "*"
byteorder = "*" byteorder = "*"
clap = "*" clap = "*"
nalgebra = "*"
verbose-log = { git = "https://gitea.tforgione.fr/dash-3d/verbose-log" } verbose-log = { git = "https://gitea.tforgione.fr/dash-3d/verbose-log" }
[[bin]] [[bin]]

View File

@ -1,37 +1,24 @@
//! This module contains everything to deal with cameras. //! This module contains everything to deal with cameras.
use nalgebra::Matrix4;
use math::vector::Vector3; use math::vector::Vector3;
use math::frustum::Frustum; use math::frustum::Frustum;
/// Multiplies two matrices 4x4
fn multiply_matrices(a: [[f32; 4]; 4], b: [[f32; 4]; 4]) -> [[f32; 4]; 4] {
let mut res = [[0.0; 4]; 4];
for i in 0..4 {
for j in 0..4 {
for k in 0..4 {
res[i][j] += a[i][k] + b[k][j];
}
}
}
res
}
/// The trait that a render camera should implement. /// The trait that a render camera should implement.
/// ///
/// It allows the renderer to use it. /// It allows the renderer to use it.
pub trait RenderCamera { pub trait RenderCamera {
/// Returns the view matrix of the camera. /// Returns the view matrix of the camera.
fn get_view_matrix(&self) -> [[f32; 4]; 4]; fn get_view_matrix(&self) -> Matrix4<f32>;
/// Returns the perspective matrix of the camera. /// Returns the perspective matrix of the camera.
fn get_perspective_matrix(&self) -> [[f32; 4]; 4]; fn get_perspective_matrix(&self) -> Matrix4<f32>;
/// Returns the product of the perspective matrix and the view matrix. /// Returns the product of the perspective matrix and the view matrix.
fn get_model_view_matrix(&self) -> [[f32; 4]; 4] { fn get_model_view_matrix(&self) -> Matrix4<f32> {
multiply_matrices(self.get_perspective_matrix(), self.get_view_matrix()) self.get_perspective_matrix() * self.get_view_matrix().try_inverse().unwrap()
} }
/// Returns the frustum of the camera. /// Returns the frustum of the camera.
@ -42,7 +29,7 @@ pub trait RenderCamera {
} }
/// Creates a look at matrix from the center, the target pointed by the camera and the up vector. /// Creates a look at matrix from the center, the target pointed by the camera and the up vector.
pub fn look_at_matrix(position: [f32; 3], target: [f32; 3], up: [f32; 3]) -> [[f32; 4]; 4] { pub fn look_at_matrix(position: [f32; 3], target: [f32; 3], up: [f32; 3]) -> Matrix4<f32> {
let f = { let f = {
let f = [ let f = [
target[0] - position[0], target[0] - position[0],
@ -77,12 +64,12 @@ pub fn look_at_matrix(position: [f32; 3], target: [f32; 3], up: [f32; 3]) -> [[f
[-s_norm[1], u[1], f[1], 0.0], [-s_norm[1], u[1], f[1], 0.0],
[-s_norm[2], u[2], f[2], 0.0], [-s_norm[2], u[2], f[2], 0.0],
[-p[0], p[1], p[2], 1.0], [-p[0], p[1], p[2], 1.0],
] ].into()
} }
/// Creates a perspective matrix of a camera. /// Creates a perspective matrix of a camera.
pub fn perspective_matrix(aspect_ratio: f32, z_near: f32, z_far: f32) -> [[f32; 4]; 4] { pub fn perspective_matrix(aspect_ratio: f32, z_near: f32, z_far: f32) -> Matrix4<f32> {
let fov = 3.141592 / 3.0; let fov = 3.141592 / 3.0;
use num::Float; use num::Float;
@ -93,7 +80,7 @@ pub fn perspective_matrix(aspect_ratio: f32, z_near: f32, z_far: f32) -> [[f32;
[ 0.0 , f , 0.0 , 0.0], [ 0.0 , f , 0.0 , 0.0],
[ 0.0 , 0.0, (z_far+z_near)/(z_far-z_near) , 1.0], [ 0.0 , 0.0, (z_far+z_near)/(z_far-z_near) , 1.0],
[ 0.0 , 0.0, -(2.0*z_far*z_near)/(z_far-z_near), 0.0], [ 0.0 , 0.0, -(2.0*z_far*z_near)/(z_far-z_near), 0.0],
] ].into()
} }
/// A simple camera with its position, target and up vector. /// A simple camera with its position, target and up vector.
@ -139,11 +126,11 @@ impl Camera {
} }
impl RenderCamera for Camera { impl RenderCamera for Camera {
fn get_view_matrix(&self) -> [[f32; 4]; 4] { fn get_view_matrix(&self) -> Matrix4<f32> {
look_at_matrix(self.position.into(), self.target.into(), self.up.into()) look_at_matrix(self.position.into(), self.target.into(), self.up.into())
} }
fn get_perspective_matrix(&self) -> [[f32; 4]; 4] { fn get_perspective_matrix(&self) -> Matrix4<f32> {
perspective_matrix(self.aspect_ratio, self.z_near, self.z_far) perspective_matrix(self.aspect_ratio, self.z_near, self.z_far)
} }
} }

View File

@ -4,6 +4,7 @@
extern crate num; extern crate num;
extern crate image; extern crate image;
extern crate nalgebra;
#[macro_use] extern crate verbose_log; #[macro_use] extern crate verbose_log;
#[macro_use] extern crate glium; #[macro_use] extern crate glium;

View File

@ -3,6 +3,8 @@
//! The frustum is the field of view of a camera. It allows you to make computation to know what is //! The frustum is the field of view of a camera. It allows you to make computation to know what is
//! visible or not in an efficient manner, though it doesn't take occlusions into account. //! visible or not in an efficient manner, though it doesn't take occlusions into account.
use nalgebra::Matrix4;
use math::vector::Vector3; use math::vector::Vector3;
use math::plane::Plane; use math::plane::Plane;
use math::bounding_box::BoundingBox3; use math::bounding_box::BoundingBox3;
@ -18,12 +20,12 @@ impl Frustum {
/// Creates a frustum from the matrix of a camera. /// Creates a frustum from the matrix of a camera.
/// ///
/// This is *ahem...* slightly inspired from THREE.js Frustum /// This is *ahem...* slightly inspired from THREE.js Frustum
pub fn from_matrix(m: &[[f32; 4]; 4]) -> Frustum { pub fn from_matrix(m: &Matrix4<f32>) -> Frustum {
let m0 = m[0][0]; let m1 = m[0][1]; let m2 = m[0][2]; let m3 = m[0][3]; let m0 = m[(0, 0)]; let m1 = m[(0, 1)]; let m2 = m[(0, 2)]; let m3 = m[(0, 3)];
let m4 = m[1][0]; let m5 = m[1][1]; let m6 = m[1][2]; let m7 = m[1][3]; let m4 = m[(1, 0)]; let m5 = m[(1, 1)]; let m6 = m[(1, 2)]; let m7 = m[(1, 3)];
let m8 = m[2][0]; let m9 = m[2][1]; let m10 = m[2][2]; let m11 = m[2][3]; let m8 = m[(2, 0)]; let m9 = m[(2, 1)]; let m10 = m[(2, 2)]; let m11 = m[(2, 3)];
let m12 = m[3][0]; let m13 = m[3][1]; let m14 = m[3][2]; let m15 = m[3][3]; let m12 = m[(3, 0)]; let m13 = m[(3, 1)]; let m14 = m[(3, 2)]; let m15 = m[(3, 3)];
Frustum { Frustum {
planes: [ planes: [

View File

@ -285,6 +285,8 @@ impl Model {
new_part.faces.push(face.clone()); new_part.faces.push(face.clone());
new_faces.push(face); new_faces.push(face);
} }
new_part.needs_update = true;
} }
for face in new_faces { for face in new_faces {

View File

@ -156,9 +156,9 @@ impl Renderer {
&self.program, &self.program,
&uniform!( &uniform!(
tex: texture, tex: texture,
perspective: perspective, perspective: Into::<[[f32; 4]; 4]>::into(perspective),
view: view, view: Into::<[[f32; 4]; 4]>::into(view),
), ),
&params, &params,
).unwrap(); ).unwrap();
} }