Skip to content

Commit

Permalink
Fix prefab does not use new proj matrix construction
Browse files Browse the repository at this point in the history
  • Loading branch information
valkum committed May 18, 2019
1 parent 08eb53c commit e253645
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions amethyst_rendy/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ impl Projection {
let tan_half_fovy = (fov / 2.0).tan();

proj[(0, 0)] = 1.0 / (aspect * tan_half_fovy);

// TODO: Check if we can change this to 1.0 / tan_half_fovy
// as other vulkan render examples use this form of a matrix
proj[(1, 1)] = -1.0 / tan_half_fovy;
proj[(2, 2)] = z_far / (z_near - z_far);

proj[(2, 3)] = -(z_near * z_far) / (z_far - z_near);
proj[(3, 2)] = -1.0;
proj[(3, 2)] = - 1.0;
proj[(3, 3)] = 0.0;

// Important: nalgebra's methods on Perspective3 are not safe for use with RH matrices
Expand Down Expand Up @@ -92,6 +95,9 @@ impl Camera {
/// Bottom left corner is (-width/2.0, -height/2.0)
/// View transformation will be multiplicative identity.
pub fn standard_2d(width: f32, height: f32) -> Self {

// TODO: Check if bottom = height/2.0 is really the solution we want here.
// Maybe the same problem as with the perspective matrix.
Self::from(Projection::orthographic(
-width / 2.0,
width / 2.0,
Expand Down Expand Up @@ -196,7 +202,7 @@ mod serde_ortho {
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
};

use super::*;
use amethyst_core::math::Orthographic3;

#[derive(serde::Deserialize, serde::Serialize)]
Expand All @@ -214,14 +220,16 @@ mod serde_ortho {
D: Deserializer<'de>,
{
let values = Orthographic::deserialize(deserializer)?;
Ok(Orthographic3::new(
values.left,
values.right,
values.bottom,
values.top,
values.znear,
values.zfar,
))
let mut proj = Matrix4::<f32>::identity();

proj[(0, 0)] = 2.0 / (values.right - values.left);
proj[(1, 1)] = 2.0 / (values.bottom - values.top);
proj[(2, 2)] = -1.0 / (values.zfar - values.znear);
proj[(0, 3)] = -(values.right + values.left) / (values.right - values.left);
proj[(1, 3)] = -(values.bottom + values.top) / (values.bottom - values.top);
proj[(2, 3)] = -values.znear / (values.zfar - values.znear);

Ok(Orthographic3::from_matrix_unchecked(proj))
}

pub fn serialize<S>(proj: &Orthographic3<f32>, serializer: S) -> Result<S::Ok, S::Error>
Expand All @@ -248,6 +256,8 @@ mod serde_persp {
ser::{Serialize, Serializer},
};

use super::*;

use amethyst_core::math::Perspective3;

#[derive(serde::Deserialize, serde::Serialize)]
Expand All @@ -263,12 +273,21 @@ mod serde_persp {
D: Deserializer<'de>,
{
let values = Perspective::deserialize(deserializer)?;
Ok(Perspective3::new(
values.aspect,
values.fovy,
values.znear,
values.zfar,
))


let mut proj = Matrix4::<f32>::identity();

let tan_half_fovy = (values.fovy / 2.0).tan();

proj[(0, 0)] = 1.0 / (values.aspect * tan_half_fovy);
proj[(1, 1)] = -1.0 / tan_half_fovy;
proj[(2, 2)] = values.zfar / (values.znear - values.zfar);

proj[(2, 3)] = -(values.znear * values.zfar) / (values.zfar - values.znear);
proj[(3, 2)] = - 1.0;
proj[(3, 3)] = 0.0;

Ok(Perspective3::from_matrix_unchecked(proj))
}

pub fn serialize<S>(proj: &Perspective3<f32>, serializer: S) -> Result<S::Ok, S::Error>
Expand Down

0 comments on commit e253645

Please sign in to comment.