12.1.Camera Viewing Geometry

This commit is contained in:
dengqn
2025-08-13 17:16:53 +08:00
parent 5dfdf5e32e
commit 5c1c9a3edd
5 changed files with 60 additions and 23 deletions
+12 -6
View File
@@ -2,24 +2,30 @@ use std::fs::File;
use std::io::{BufWriter};
use crate::hittable::HittableList;
use crate::math_utils::{clamp, near_zero};
use crate::math_utils::{clamp, degrees_to_radians};
use crate::ppm_writer::PPMWriter;
use crate::types_defined::{Camera, Color, HitRecord, Point, Ray, Vec3};
use rand::rngs::ThreadRng;
use rand::{Rng, thread_rng};
use rand::{Rng, rng};
use crate::material::{Material, MaterialKind};
impl<'a> Camera<'a> {
pub fn new(
fov: f32,
image_width: i32,
aspect_ratio: f32,
viewport_height: f32,
camera_center: Point,
focal_length: Vec3,
focal_length: f32,
sample_times: i8,
reflect_depth: i8,
ppm_file_writer: &'a mut PPMWriter<BufWriter<File>>
) -> Self {
let theta = degrees_to_radians(fov);
let h = f32::tan(theta / 2.);
let viewport_height = 2. * h * focal_length;
let image_height = ((image_width as f32 / aspect_ratio) as i32).max(1);
let viewport_width = viewport_height * (image_width as f32 / image_height as f32);
@@ -34,7 +40,7 @@ impl<'a> Camera<'a> {
// height per pix
let viewport_v_delta = viewport_v / (image_height as f32);
let viewport_top_left_pixel =
camera_center - focal_length - viewport_u / 2.0 - viewport_v / 2.0;
camera_center - Vec3::new(0., 0., focal_length) - viewport_u / 2.0 - viewport_v / 2.0;
// padding 0.5* delta u/v
let viewport_top_left_pixel_center =
viewport_top_left_pixel - viewport_u_delta / 2.0 - viewport_v_delta / 2.0;
@@ -69,7 +75,7 @@ impl<'a> Camera<'a> {
for i in 0..self.image_width {
// color
let mut color = Color::new(0.0, 0.0, 0.0);
let rng = &mut thread_rng();
let rng = &mut rng();
for _ in 0..self.sample_times {
let bias = self.random_square(rng);
let pix_sample = self.viewport_top_left_pixel_center
+5 -4
View File
@@ -22,11 +22,12 @@ fn main() {
fn camera_render() {
let fov: f32 = 90.;
let scale = 1;
let width: i32 = 800/scale;
let height: i32 = 600/scale;
let sample_times = 127;
let reflect_depth = 127;
let sample_times = 20;
let reflect_depth = 20;
let pw_r = PPMWriter::new(
@@ -40,11 +41,11 @@ fn camera_render() {
let mut camera: Camera = Camera::new(
fov,
width,
width as f32 / height as f32,
2.0,
Point::new(0.0, -0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
1.0,
sample_times,
reflect_depth,
pw
+6
View File
@@ -1,5 +1,11 @@
use std::f32::consts::PI;
use crate::types_defined::{Ray, Vec3};
pub fn degrees_to_radians(deg: f32) -> f32 {
deg * PI / 180.
}
pub fn clamp(value: f32, low: f32, high: f32) -> f32 {
if value < low {
return low;