Skip to content

Commit

Permalink
(fix): fixed GPU project matrix by #5; fixed perspect correct in line…
Browse files Browse the repository at this point in the history
… draw
  • Loading branch information
VisualGMQ committed Jun 28, 2023
1 parent c12c94e commit 5a93435
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
10 changes: 6 additions & 4 deletions examples/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,12 @@ fn main() {
let mut renderer = create_renderer(WINDOW_WIDTH, WINDOW_HEIGHT, camera);
renderer.set_front_face(FrontFace::CCW);
renderer.set_face_cull(FaceCull::Back);
// renderer.enable_framework();
let mut texture_storage = TextureStorage::default();

// data prepare, from OBJ model
const MODEL_ROOT_DIR: &str = "./resources/Red";
const MODEL_ROOT_DIR: &str = "./resources/Son Goku";
let (meshes, mtllibs) = model::load_from_file(
&format!("{}/{}", MODEL_ROOT_DIR, "Red.obj"),
&format!("{}/{}", MODEL_ROOT_DIR, "Goku.obj"),
model::PreOperation::None,
)
.unwrap();
Expand Down Expand Up @@ -157,14 +156,17 @@ fn main() {
if event_key_down(Key::from_char('e')) {
camera.move_offset(math::Vec3::new(0.0, -0.01, 0.0));
}
if event_key_down(Key::from_char('t')) {
renderer.toggle_framework();
}
}

// render
renderer.clear(&math::Vec4::new(0.2, 0.2, 0.2, 1.0));
renderer.clear_depth();

let model = math::create_translate(&math::Vec3::new(0.0, 0.0, -4.0))
* math::create_eular_rotate_y(rotation.to_radians());
* math::create_eular_rotate_x(rotation.to_radians());

for data in &vertex_datas {
// set data into uniform
Expand Down
2 changes: 1 addition & 1 deletion src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Frustum {
math::Mat4::from_row(&[
near / half_w, 0.0, 0.0, 0.0,
0.0, near / half_h, 0.0, 0.0,
0.0, 0.0, far + near / (near - far), 2.0 * far * near / (near - far),
0.0, 0.0,(far + near) / (near - far), 2.0 * far * near / (near - far),
0.0, 0.0, -1.0, 0.0,
])
},
Expand Down
11 changes: 8 additions & 3 deletions src/cpu_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ impl renderer::RendererInterface for Renderer {
fn disable_framework(&mut self) {
self.enable_framework = false;
}

fn toggle_framework(&mut self) {
self.enable_framework = !self.enable_framework;
}
}

impl Renderer {
Expand Down Expand Up @@ -228,11 +232,12 @@ impl Renderer {
for i in 0..3 {
let mut v1 = vertices[i];
let mut v2 = vertices[(i + 1) % 3];
v1.position.z = 1.0 / v1.position.z;
v2.position.z = 1.0 / v2.position.z;

shader::vertex_rhw_init(&mut v1);
shader::vertex_rhw_init(&mut v2);

rasterize_line(
&Line::new(v1, v2),
&mut Line::new(v1, v2),
&self.shader.pixel_shading,
&self.uniforms,
texture_storage,
Expand Down
19 changes: 12 additions & 7 deletions src/gpu_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
line::Line,
math::{self, Berycentric},
renderer::*,
shader::*,
shader::{*, self},
texture::TextureStorage,
};

Expand Down Expand Up @@ -74,9 +74,9 @@ impl RendererInterface for Renderer {
}

// set truely z
/* NOTIC: in OpenGL, after MVP transform, z in [-1, 1], then OpenGL do `z = (z + 1) / 2` to make z in [0, 1],
/* NOTIC: in OpenGL, after MVP & Perspective divide, z in [-1, 1], then OpenGL do `z = (z + 1) / 2` to make z in [0, 1],
then, use `1 / z` to test depth.
But here we replace transformed z to it's original z which transformed after MV.
But here we replace transformed z to it's original z which transformed after MVP.
Traditionally we will save `-1.0 / v.position.w` into v.rhw and use it interpolate attributes.
But here I don't do it(because I'm lazy :D, maybe do it later).
*/
Expand All @@ -88,6 +88,7 @@ impl RendererInterface for Renderer {
for v in &mut vertices {
v.position.x /= v.position.w;
v.position.y /= v.position.w;
v.position.w = 1.0;
}

// Viewport transform
Expand All @@ -97,7 +98,6 @@ impl RendererInterface for Renderer {
v.position.y = self.viewport.h as f32
- (v.position.y + 1.0) * 0.5 * (self.viewport.h as f32 - 1.0)
+ self.viewport.y as f32;
v.position.w = (v.position.w + 1.0) / 2.0;
}

// find AABB for triangle
Expand Down Expand Up @@ -153,11 +153,12 @@ impl RendererInterface for Renderer {
for i in 0..3 {
let mut v1 = vertices[i];
let mut v2 = vertices[(i + 1) % 3];
v1.position.z = 1.0 / v1.position.z;
v2.position.z = 1.0 / v2.position.z;

shader::vertex_rhw_init(&mut v1);
shader::vertex_rhw_init(&mut v2);

rasterize_line(
&Line::new(v1, v2),
&mut Line::new(v1, v2),
&self.shader.pixel_shading,
&self.uniforms,
texture_storage,
Expand Down Expand Up @@ -243,6 +244,10 @@ impl RendererInterface for Renderer {
fn disable_framework(&mut self) {
self.enable_framework = false;
}

fn toggle_framework(&mut self) {
self.enable_framework = !self.enable_framework;
}
}

#[rustfmt::skip]
Expand Down
7 changes: 4 additions & 3 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub trait RendererInterface {
fn get_face_cull(&self) -> FaceCull;
fn enable_framework(&mut self);
fn disable_framework(&mut self);
fn toggle_framework(&mut self);
}

pub fn texture_sample(texture: &Texture, texcoord: &math::Vec2) -> math::Vec4 {
Expand Down Expand Up @@ -78,7 +79,7 @@ pub(crate) fn should_cull(
}

pub(crate) fn rasterize_line(
line: &Line,
line: &mut Line,
shading: &shader::PixelShading,
uniforms: &shader::Uniforms,
texture_storage: &TextureStorage,
Expand Down Expand Up @@ -108,9 +109,9 @@ pub(crate) fn rasterize_line(
let y = y as u32;
if depth_attachment.get(x, y) <= z {
let mut attr = vertex.attributes;
shader::attributes_foreach(&mut attr, |value| value * z);
shader::attributes_foreach(&mut attr, |value| value / rhw);
// call pixel shading function to get shading color
let color = shading(&vertex.attributes, uniforms, texture_storage);
let color = shading(&attr, uniforms, texture_storage);
color_attachment.set(x, y, &color);
depth_attachment.set(x, y, z);
}
Expand Down

0 comments on commit 5a93435

Please sign in to comment.