2 个版本
0.1.1 | 2021 年 11 月 20 日 |
---|---|
0.1.0 | 2021 年 4 月 5 日 |
#517 在 图形 API 中
用于 2 个库
185KB
5.5K SLoC
库 ray_tracing_core
GitHub 页面 rabbid76.github.io/ray-tracing-with-rust
GitHub 仓库 Rabbid76/ray-tracing-with-rust
基于 Peter Shirley 的书籍
“我尽量避免使用 C++ 的大多数“现代特性”,但继承和运算符重载对于光线追踪器来说过于有用,不能放弃。”
― Peter Shirley, 一周之内学会光线追踪
示例
use ray_tracing_core::random;
use ray_tracing_core::test::TestSceneSimple;
use ray_tracing_core::types::ColorRGB;
use ray_tracing_core::types::FSize;
fn main() {
let cx = 40;
let cy = 20;
let samples = 10;
let scene = TestSceneSimple::new().scene;
let mut pixel_data: Vec<u8> = Vec::with_capacity(cx * cy * 4);
pixel_data.resize(cx * cy * 4, 0);
for x in 0..cx {
for y in 0..cy {
let mut c = ColorRGB::new(0.0, 0.0, 0.0);
for _ in 0..samples {
let u = (x as FSize + random::generate_size()) / cx as FSize;
let v = 1.0 - (y as FSize + random::generate_size()) / cy as FSize;
c = c + scene.ray_trace_color(u, v);
}
c = c / samples as FSize;
let i = (y * cx) + x;
pixel_data[i * 4] = (c[0].sqrt() * 255.0).round() as u8;
pixel_data[i * 4 + 1] = (c[1].sqrt() * 255.0).round() as u8;
pixel_data[i * 4 + 2] = (c[2].sqrt() * 255.0).round() as u8;
pixel_data[i * 4 + 3] = 255;
}
}
// [...]
}
依赖
~5MB
~95K SLoC