rust warms

This commit is contained in:
dengqn 2025-08-19 08:53:07 +08:00
parent ff4132edff
commit 0e62a20775
5 changed files with 13 additions and 14 deletions

View File

@ -70,8 +70,8 @@ impl<'a> Camera<'a> {
// viewport_width, // viewport_width,
// viewport_height, // viewport_height,
center, center,
look_at, // look_at,
v_up, // v_up,
// focal_length, // focal_length,
// viewport_u, // viewport_u,
// viewport_v, // viewport_v,
@ -99,7 +99,6 @@ impl<'a> Camera<'a> {
for i in 0..self.image_width { for i in 0..self.image_width {
// color // color
let mut color = Color::new(0.0, 0.0, 0.0); let mut color = Color::new(0.0, 0.0, 0.0);
let rng = &mut rng();
for _ in 0..self.sample_times { for _ in 0..self.sample_times {
let r = self.get_ray(i, j); let r = self.get_ray(i, j);
let sample_color = self.ray_color(&r, self.reflect_depth, world); let sample_color = self.ray_color(&r, self.reflect_depth, world);
@ -200,7 +199,7 @@ impl<'a> Camera<'a> {
// -> [x, y, 0] // -> [x, y, 0]
fn random_square(&self, rng: &mut ThreadRng) -> Vec3 { fn random_square(&self, rng: &mut ThreadRng) -> Vec3 {
Vec3::new(rng.gen_range(-0.5..0.1), rng.gen_range(-0.5..0.1), 0.0) Vec3::new(rng.random_range(-0.5..0.1), rng.random_range(-0.5..0.1), 0.0)
} }

View File

@ -31,7 +31,7 @@ impl Material for Lambertian {
let mut scatter_direction = r_in.direction + Vec3::random_unit_on_hemisphere(hit_record.normal); let mut scatter_direction = r_in.direction + Vec3::random_unit_on_hemisphere(hit_record.normal);
if (near_zero(scatter_direction)) { if near_zero(scatter_direction) {
scatter_direction = hit_record.normal; scatter_direction = hit_record.normal;
} }

View File

@ -8,7 +8,7 @@ pub fn random_in_unit_disk() -> Vec3 {
let rng = &mut rng(); let rng = &mut rng();
loop { loop {
let p = Vec3::new(rng.random_range(-1.0..1.0), rng.random_range(-1.0..1.0), 0.0); let p = Vec3::new(rng.random_range(-1.0..1.0), rng.random_range(-1.0..1.0), 0.0);
if (p.length_squared() < 1.0) { if p.length_squared() < 1.0 {
return p; return p;
} }
} }

View File

@ -1,5 +1,5 @@
use std::{fs::File, io::{BufWriter}}; use std::{fs::File, io::{BufWriter}};
use crate::{material::MaterialKind, vec3}; use crate::{material::MaterialKind};
use crate::ppm_writer::PPMWriter; use crate::ppm_writer::PPMWriter;
/* /*
@ -38,8 +38,8 @@ pub struct Camera<'a> {
// pub viewport_width: f32, // pub viewport_width: f32,
// pub viewport_height: f32, // pub viewport_height: f32,
pub center: Point, pub center: Point,
pub look_at: Point, // pub look_at: Point,
pub v_up: Vec3, // pub v_up: Vec3,
// pub focal_length: Vec3, // pub focal_length: Vec3,
// pub viewport_u: Vec3, // pub viewport_u: Vec3,
// pub viewport_v: Vec3, // pub viewport_v: Vec3,

View File

@ -1,7 +1,7 @@
use std::{fmt::Display, ops::{Add, AddAssign, Div, Mul, MulAssign, Sub}}; use std::{fmt::Display, ops::{Add, AddAssign, Div, Mul, MulAssign, Sub}};
use std::fmt; use std::fmt;
use rand::{thread_rng, Rng}; use rand::{rng, Rng};
use crate::types_defined::Vec3; use crate::types_defined::Vec3;
impl Display for Vec3 { impl Display for Vec3 {
@ -50,13 +50,13 @@ impl Vec3 {
} }
pub fn random_range(min: f32, max: f32) -> Self { pub fn random_range(min: f32, max: f32) -> Self {
let rng = &mut thread_rng(); let rng = &mut rng();
Vec3::new(rng.gen_range(min..max), rng.gen_range(min..max), rng.gen_range(min..max)) Vec3::new(rng.random_range(min..max), rng.random_range(min..max), rng.random_range(min..max))
} }
pub fn random() -> Self { pub fn random() -> Self {
let rng = &mut thread_rng(); let rng = &mut rng();
Vec3::new(rng.r#gen(), rng.r#gen(), rng.r#gen()) Vec3::new(rng.random(), rng.random(), rng.random())
} }
pub fn random_unit() -> Self { pub fn random_unit() -> Self {