Giter Club home page Giter Club logo

rs-cpurenderer's Introduction

使用纯Rust编写的3D软渲染器。

B站视频教程

目前的显示

snapshot

工程运行

只有一个例子:examples/sandbox.rs,用

cargo run --example sandbox

运行。

使用WSAD前后左右移动相机,使用Q上升相机,E下降相机。

本工程由两种软渲染:纯粹为了在CPU上快速运行的CPU软渲染(./src/cpu_renderer.rs),以及模拟GPU原理的GPU软渲染(./src/gpu_renderer.rs)。使用features可以指定运行某种:

cargo run --example sandbox --features cpu
cargo run --example sandbox --features gpu

默认是CPU渲染。

参考

书籍:

  • 《3D游戏编程大师技巧》
  • 《Fundamentals of Computer Graphics》
  • 《线性代数及其应用》

视频:

齐次空间裁剪:

投影矩阵推导:

透视投影矫正:

其他:

rs-cpurenderer's People

Contributors

visualgmq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rs-cpurenderer's Issues

Cohen-Sutherland线段裁剪算法

            if outcode & TOP != 0 {
                // 这里是不是应该为 p.x = pt1.x + (pt2.x - pt1.x) * (rect_max.y - pt1.y) / (pt2.y - pt1.y);
                p.x = p1.x + (pt2.x - pt1.x) * (rect_max.y - pt1.y) / (pt2.y - pt1.y);
                p.y = rect_max.y;
            } else if outcode & BOTTOM != 0 {
                // 这里是不是应该为 p.x = pt1.x + (pt2.x - pt1.x) * (rect_min.y - pt1.y) / (pt2.y - pt1.y);
                p.x = p1.x + (pt2.x - pt1.x) * (rect_min.y - pt1.y) / (pt2.y - pt1.y);
                p.y = rect_min.y;
            } else if outcode & RIGHT != 0 {
                p.y = pt1.y + (pt2.y - pt1.y) * (rect_max.x - pt1.x) / (pt2.x - pt1.x);
                p.x = rect_max.x;
            } else if outcode & LEFT != 0 {
                p.y = pt1.y + (pt2.y - pt1.y) * (rect_min.x - pt1.x) / (pt2.x - pt1.x);
                p.x = rect_min.x;
            }

image.rs中DepthAttachment的初始值问题

impl PureElemImage<f32> {
    pub fn new(w: u32, h: u32) -> Self {
        Self {
            data: vec![std::f32::MAX; (w * h) as usize],
            w,
            h,
        }
    }

我理解的应该是看向Z的负方向,z值越大离相机越近(renderer.rs中深度测试也是保留深度大的值)。

            if depth_attachment.get(x, y) <= z {
                let mut attr = vertex.attributes;
                shader::attributes_foreach(&mut attr, |value| value * z);
                // call pixel shading function to get shading color
                let color = shading(&vertex.attributes, uniforms, texture_storage);
                color_attachment.set(x, y, &color);
                depth_attachment.set(x, y, z);
            }

那么初始值是否应该被设置为浮点最小值?(cpu_renderer.rsgpu_renderer.rs中的clear_depth也是这么做的)。

    fn clear_depth(&mut self) {
        self.depth_attachment.clear(f32::MIN);
    }

四元数的逆

    pub fn inverse(&self) -> Quaternion {
        self.conjugate() / self.length()
    }

是不是应该为self.conjugate() / self.length_square() ?

Frustum中GPU的矩阵问题

// camera.rs
mpl Frustum {
    #[rustfmt::skip]
    pub fn new(near: f32, far: f32, aspect: f32, fovy: f32) -> Self {

// ...

            } 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 / (near - far), 2.0 * far * near / (near - far),
                              0.0,           0.0,                      -1.0,                             0.0,
                ])

请问第三行第三列是否应该是(far + near) / (near - far)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.