0192-画背景图片
环境
- Time 2022-11-15
- WSL-Ubuntu 22.04
- Rust 1.65.0
前言
说明
参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html
目标
在之前的基础上,画一副背景图片。
线的表示
用一个点和一个方向来表示射线。
use crate::vector3::{Point3, Vector3};
pub struct Ray {
origin: Point3,
direction: Vector3,
}
impl Ray {
pub fn new(direction: Vector3) -> Ray {
Ray {
origin: Point3::default(),
direction,
}
}
pub fn direction(&self) -> Vector3 {
self.direction
}
pub fn at(&self, t: f64) -> Point3 {
self.origin + t * self.direction
}
}
线的颜色
线性插值,根据不同的光线得到下面定义的两个颜色之间的渐变色。
类似天空的颜色,来作为背景。
// 光线的颜色计算
fn ray_color(ray: Ray) -> Color {
// 射线的单位向量
let unit = ray.direction().unit();
// 因为需要得到上下渐变的背景图,所以需要对 y 进行插值。
// 因为不考虑反向,所以将其加 1 乘 0.5
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)
}
图片和视角
定义了图片的宽和高,也定义相机需要显示的区域,它们等比例,是为了刚好能显示。
// 图片的比例,和宽高
const RATIO: f64 = 16.0 / 9.0;
const WIDTH: u64 = 400;
const HEIGHT: u64 = (WIDTH as f64 / RATIO) as u64;
// 相机
let view_height = 2.0;
// 图片的比例应该和照射的显示的图形比例一致
let view_width = RATIO * view_height;
// 将其固定在 z 轴远离原点一个单位的距离
let focal_length = 1.0;
移动视角到正中
将相机移动到正中。
// 水平向量
let horizontal = Vector3::new(view_width, 0.0, 0.0);
// 垂直向量
let vertical = Vector3::new(0.0, view_height, 0.0);
// 将视角移动到水平和垂直向量构成的面的正中间,并且 z 为 1 的距离
let lower_left_corner =
origin - horizontal / 2.0 - vertical / 2.0 - Vector3::new(0.0, 0.0, focal_length);
输出图片
其中的 u 和 v 是在视角平面上的向量,随着它们的改变射线的终点在显示的区域上变化,然后得到不同的射线。
// 输出图片,第一行输出 P3,表示像素图
let mut content = String::from("P3");
// 输出宽和高,和最大颜色值
content.push_str(&format!("\n{WIDTH} {HEIGHT}\n255\n"));
for j in (0..HEIGHT).rev() {
// 进度
eprintln!("Scan lines remaining: {j}");
for i in 0..WIDTH {
let u = i as f64 / (WIDTH - 1) as f64;
let v = j as f64 / (HEIGHT - 1) as f64;
// 从原点到屏幕显示上一个点的射线,然后计算它的颜色。
// 因为在循环里,所以每个像素都会计算一次
let ray = Ray::new(lower_left_corner + u * horizontal + v * vertical - origin);
let color = ray_color(ray);
content.push_str(&color.format_str());
}
}
println!("{}", content);
eprintln!("Done.");
效果
背景图,天空颜色效果。
总结
画出了背景图,类似天空的效果。
附录
main.rs
use ray::Ray;
use crate::vector3::{Color, Point3, Vector3};
mod ray;
mod vector3;
fn main() {
// 图片的比例,和宽高
const RATIO: f64 = 16.0 / 9.0;
const WIDTH: u64 = 400;
const HEIGHT: u64 = (WIDTH as f64 / RATIO) as u64;
// 相机
let view_height = 2.0;
// 图片的比例应该和照射的显示的图形比例一致
let view_width = RATIO * view_height;
// 将其固定在 z 轴远离原点一个单位的距离
let focal_length = 1.0;
let origin = Point3::default();
// 水平向量
let horizontal = Vector3::new(view_width, 0.0, 0.0);
// 垂直向量
let vertical = Vector3::new(0.0, view_height, 0.0);
// 将视角移动到水平和垂直向量构成的面的正中间,并且 z 为 1 的距离
let lower_left_corner =
origin - horizontal / 2.0 - vertical / 2.0 - Vector3::new(0.0, 0.0, focal_length);
// 输出图片,第一行输出 P3,表示像素图
let mut content = String::from("P3");
// 输出宽和高,和最大颜色值
content.push_str(&format!("\n{WIDTH} {HEIGHT}\n255\n"));
for j in (0..HEIGHT).rev() {
// 进度
eprintln!("Scan lines remaining: {j}");
for i in 0..WIDTH {
let u = i as f64 / (WIDTH - 1) as f64;
let v = j as f64 / (HEIGHT - 1) as f64;
// 从原点到屏幕显示上一个点的射线,然后计算它的颜色。
// 因为在循环里,所以每个像素都会计算一次
let ray = Ray::new(lower_left_corner + u * horizontal + v * vertical - origin);
let color = ray_color(ray);
content.push_str(&color.format_str());
}
}
println!("{}", content);
eprintln!("Done.");
}
// 光线的颜色计算
fn ray_color(ray: Ray) -> Color {
// 射线的单位向量
let unit = ray.direction().unit();
// 因为需要得到上下渐变的背景图,所以需要对 y 进行插值。
// 因为不考虑反方向,所以将其加 1 乘 0.5
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)
}
ray.rs
use crate::vector3::{Point3, Vector3};
pub struct Ray {
origin: Point3,
direction: Vector3,
}
impl Ray {
pub fn new(direction: Vector3) -> Ray {
Ray {
origin: Point3::default(),
direction,
}
}
pub fn direction(&self) -> Vector3 {
self.direction
}
pub fn at(&self, t: f64) -> Point3 {
self.origin + t * self.direction
}
}
vector3.rs
use std::ops::{Add, Div, Mul, Sub};
pub type Color = Vector3;
pub type Point3 = Vector3;
#[derive(Default, Clone, Copy)]
pub struct Vector3 {
x: f64,
y: f64,
z: f64,
}
impl Vector3 {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z }
}
pub fn format_str(&self) -> String {
let x = (255.999 * self.x) as u64;
let y = (255.999 * self.y) as u64;
let z = (255.999 * self.z) as u64;
format!("{x} {y} {z}\n")
}
pub fn y(&self) -> f64 {
self.y
}
/// 向量的长度
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,
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-07-17 【JavaScript】对象基础