Skip to content

Commit

Permalink
(fix): fix GPU viewport and perspective matrix as opengl specific
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualGMQ committed May 29, 2023
1 parent 976529b commit 04817dd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ cargo run --example sandbox --features gpu
* [RenderHelp项目](https://github.com/skywind3000/RenderHelp)
* [mini3d项目](https://github.com/skywind3000/mini3d)
* [光栅化实现](https://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/overview-rasterization-algorithm.html)
* [图形学|shader|用一篇文章理解半透明渲染、透明度测试和混合、提前深度测试并彻底理清渲染顺序。](https://zhuanlan.zhihu.com/p/263566318)
6 changes: 4 additions & 2 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ impl Frustum {
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, -1.0 / near, 0.0,
])
} else {
} else { // when in GPU, [we use opengl matrix](http://www.songho.ca/opengl/gl_projectionmatrix.html)
let half_w = near * fovy.tan();
let half_h = half_w / aspect;
let near = near.abs();
let far = far.abs();
// with far plane, clamp x,y,z in [-1, 1]
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 / (far - near), 2.0 * far * near / (far - near),
0.0, 0.0, far + near / (near - far), 2.0 * far * near / (near - far),
0.0, 0.0, -1.0, 0.0,
])
},
Expand Down
1 change: 1 addition & 0 deletions src/gpu_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ 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

0 comments on commit 04817dd

Please sign in to comment.