Minor optimization
This commit is contained in:
parent
99ad9ea260
commit
8f74621937
2 changed files with 41 additions and 16 deletions
33
src/main.rs
33
src/main.rs
|
@ -18,11 +18,28 @@ mod view;
|
|||
use view::View;
|
||||
|
||||
fn main() {
|
||||
let input_data = [Triangle::new(
|
||||
let input_data = [
|
||||
Triangle::new(
|
||||
vec3(0.0, 0.0, 0.0),
|
||||
vec3(1.0, 0.0, 0.0),
|
||||
vec3(1.0, 1.0, 0.0),
|
||||
)];
|
||||
),
|
||||
Triangle::new(
|
||||
vec3(1.0, 1.0, 0.0),
|
||||
vec3(0.0, 1.0, 0.0),
|
||||
vec3(0.0, 0.0, 0.0),
|
||||
),
|
||||
Triangle::new(
|
||||
vec3(1.0, 1.0, 0.0),
|
||||
vec3(2.0, 1.0, 0.0),
|
||||
vec3(2.0, 2.0, 0.0),
|
||||
),
|
||||
Triangle::new(
|
||||
vec3(2.0, 2.0, 0.0),
|
||||
vec3(1.0, 2.0, 0.0),
|
||||
vec3(1.0, 1.0, 0.0),
|
||||
),
|
||||
];
|
||||
|
||||
let view = View {
|
||||
position: vec3(0.0, -4.0, 4.0),
|
||||
|
@ -31,6 +48,8 @@ fn main() {
|
|||
fov: 45.0,
|
||||
};
|
||||
|
||||
let debug_ray = Ray::primary_ray(640.0, 360.0, 1280.0, 720.0, &view);
|
||||
|
||||
debug_raytracer(1280, 720, &view, &input_data);
|
||||
}
|
||||
|
||||
|
@ -95,12 +114,12 @@ fn look_at_test(eye: Vector3<f32>, center: Vector3<f32>, up: Vector3<f32>) -> Ma
|
|||
fn debug_raytracer(dim_x: u32, dim_y: u32, view: &View, data: &[Triangle]) {
|
||||
let mut imgbuf = ImageBuffer::new(dim_x, dim_y);
|
||||
|
||||
for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
|
||||
let ray = Ray::primary_ray(x, y, dim_x, dim_y, view);
|
||||
|
||||
let acceleration_data = create_acceleration_data(data);
|
||||
|
||||
let color = pixel_color(&ray, data, acceleration_data);
|
||||
for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
|
||||
let ray = Ray::primary_ray(x as f32, y as f32, dim_x as f32, dim_y as f32, view);
|
||||
|
||||
let color = pixel_color(&ray, data, &acceleration_data);
|
||||
|
||||
*pixel = image::Rgb([f_to_u(color.x), f_to_u(color.y), f_to_u(color.z)]);
|
||||
}
|
||||
|
@ -157,7 +176,7 @@ fn create_acceleration_data(input_data: &[Triangle]) -> Vec<AABB> {
|
|||
acceleration_data
|
||||
}
|
||||
|
||||
fn pixel_color(ray: &Ray, data: &[Triangle], acceleration_data: Vec<AABB>) -> Vector3<f32> {
|
||||
fn pixel_color(ray: &Ray, data: &[Triangle], acceleration_data: &Vec<AABB>) -> Vector3<f32> {
|
||||
let mut final_color = vec3(0.0, 1.0, 0.0);
|
||||
let mut closest_value = MAX;
|
||||
let mut closest_index = -1;
|
||||
|
|
18
src/ray.rs
18
src/ray.rs
|
@ -25,17 +25,23 @@ impl Ray {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn primary_ray(x: u32, y: u32, dim_y: u32, dim_x: u32, view: &View) -> Ray {
|
||||
let aspect_ratio = dim_x as f32 / dim_y as f32;
|
||||
pub fn primary_ray(x: f32, y: f32, dim_x: f32, dim_y: f32, view: &View) -> Ray {
|
||||
let aspect_ratio = dim_x / dim_y;
|
||||
|
||||
let uv = vec2(x as f32, y as f32);
|
||||
let uv = vec2((x + 0.5) / dim_x, (y + 0.5) / dim_y);
|
||||
|
||||
let (up, right) = view.axises();
|
||||
|
||||
let trans = 2.0 * uv - vec2(1.0, 1.0);
|
||||
let mut dir = view.look_at + right * trans.x + up * trans.y;
|
||||
dir = vec3(dir.x * aspect_ratio, dir.y, dir.z * aspect_ratio);
|
||||
let raw_dir = view.look_at + right * trans.x + up * trans.y;
|
||||
|
||||
return Self::new(view.position, dir.normalize());
|
||||
let dir = vec3(
|
||||
raw_dir.x * aspect_ratio,
|
||||
raw_dir.y,
|
||||
raw_dir.z * aspect_ratio,
|
||||
)
|
||||
.normalize();
|
||||
|
||||
return Self::new(view.position, dir);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue