0198-增加抗锯齿

环境

  • Time 2022-11-16
  • WSL-Ubuntu 22.04
  • Rust 1.65.0

前言

说明

参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html

目标

之前生成的版本,在交界处,能很明显看到锯齿,增加采样和抗锯齿。

颜色显示函数

pub fn format_str(&self, samples: f64) -> String {
    let ir = (256.0 * (self.x / (samples)).clamp(0.0, 0.999)) as u64;
    let ig = (256.0 * (self.y / (samples)).clamp(0.0, 0.999)) as u64;
    let ib = (256.0 * (self.z / (samples)).clamp(0.0, 0.999)) as u64;

    format!("{ir} {ig} {ib}\n")
}

main.rs

use camera::Camera;
use hittable::{Hit, World};
use rand::Rng;
use ray::Ray;
use sphere::Sphere;
use vector3::{Color, Point3};

mod camera;
mod hittable;
mod ray;
mod sphere;
mod vector3;

fn main() {
    // 图片的比例,和宽高
    const WIDTH: u64 = 400;
    const HEIGHT: u64 = (WIDTH as f64 / camera::RATIO) as u64;
    const SAMPLES_PER_PIXEL: u64 = 100;

    // 相机
    let camera = Camera::new();

    // 输出图片,第一行输出 P3,表示像素图
    let mut content = String::from("P3");
    // 输出宽和高,和最大颜色值
    content.push_str(&format!("\n{WIDTH} {HEIGHT}\n255\n"));

    let world: World = vec![
        Box::new(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5)),
        Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)),
    ];

    let mut rng = rand::thread_rng();
    for j in (0..HEIGHT).rev() {
        // 进度
        eprintln!("Scan lines remaining: {j}");
        for i in 0..WIDTH {
            let mut color = Color::default();
            for _ in 0..SAMPLES_PER_PIXEL {
                let random_u: f64 = rng.gen();
                let random_v: f64 = rng.gen();

                let u = ((i as f64) + random_u) / ((WIDTH - 1) as f64);
                let v = ((j as f64) + random_v) / ((HEIGHT - 1) as f64);

                color = ray_color(&camera.get_ray(u, v), &world) + color;
            }
            content.push_str(&color.format_str(SAMPLES_PER_PIXEL as f64));
        }
    }
    println!("{}", content);
    eprintln!("Done.");
}

// 光线的颜色计算
fn ray_color(ray: &Ray, hittable: &dyn Hit) -> Color {
    let option = hittable.hit(ray, 0.0, f64::INFINITY);

    if let Some(record) = option {
        return 0.5 * (record.normal + Color::new(1.0, 1.0, 1.0));
    }

    // 射线的单位向量
    let unit = ray.direction().unit();
    // 因为需要得到上下渐变的背景图,所以需要对 y 进行插值。
    let t = 0.5 * (unit.y + 1.0);
    // 线性插值,根据不同的光线得到在下面这个范围里的不同的颜色,并且是渐变色。
    (1.0 - t) * Color::new(1.0, 1.0, 1.0) + t * Color::new(0.5, 0.7, 1.0)
}

效果

抗锯齿

总结

给图片增加了抗锯齿的功能。

附录

vector3.rs

use std::ops::{Add, Div, Mul, Sub};

pub type Color = Vector3;
pub type Point3 = Vector3;

#[derive(Default, Clone, Copy)]
pub struct Vector3 {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Vector3 {
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    pub fn format_str(&self, samples: f64) -> String {
        let ir = (256.0 * (self.x / (samples)).clamp(0.0, 0.999)) as u64;
        let ig = (256.0 * (self.y / (samples)).clamp(0.0, 0.999)) as u64;
        let ib = (256.0 * (self.z / (samples)).clamp(0.0, 0.999)) as u64;

        format!("{ir} {ig} {ib}\n")
    }

    /// 向量的长度
    pub fn length(self) -> f64 {
        self.dot(self).sqrt()
    }

    /// 向量的点乘
    pub fn dot(self, other: Vector3) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    ///  单位向量
    pub fn unit(self) -> Vector3 {
        self / self.length()
    }
}

// 向量的加法
impl Add for Vector3 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Vector3 {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z,
        }
    }
}

// 向量的减法
impl Sub for Vector3 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z,
        }
    }
}

// 向量和数字的乘法
impl Mul<f64> for Vector3 {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self {
        Self {
            x: self.x * rhs,
            y: self.y * rhs,
            z: self.z * rhs,
        }
    }
}

// 向量和数字的乘法
impl Mul<Vector3> for f64 {
    type Output = Vector3;

    fn mul(self, rhs: Vector3) -> Vector3 {
        rhs * self
    }
}

// 向量的除法
impl Div<f64> for Vector3 {
    type Output = Self;

    fn div(self, rhs: f64) -> Self {
        Self {
            x: self.x / rhs,
            y: self.y / rhs,
            z: self.z / rhs,
        }
    }
}
posted @   jiangbo4444  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-07-19 【JavaScript】对象原型
点击右上角即可分享
微信分享提示