ready for refacto

This commit is contained in:
gbrochar 2020-12-17 11:26:42 +01:00
parent c8f1a5928b
commit b26fc2ae06
1 changed files with 26 additions and 18 deletions

View File

@ -15,12 +15,23 @@ fn put_pixel(data: &mut [u8], x: usize, y: usize, color: Color) {
pub fn render(data: &mut [u8], i: i32) { pub fn render(data: &mut [u8], i: i32) {
let sphere = Sphere {}; let sphere = Sphere {};
let forward = *vec3::transform_mat4(
&mut vec3::new_zero(),
&vec3::new(0. as f64, 0. as f64, -1. as f64),
mat4::rotate(
&mut mat4::new_zero(),
&mat4::new_identity(),
&0.,
&(3.141592 / 4.),
&0.,
),
);
let view_matrix = *mat4::inv( let view_matrix = *mat4::inv(
&mut mat4::new_zero(), &mut mat4::new_zero(),
mat4::look_at( mat4::look_at(
&mut mat4::new_zero(), &mut mat4::new_zero(),
&[0., 0., 20.], &[100., 0., 100.],
&[0., 0., 19.], &[100. + forward[0], 0. + forward[1], 100. + forward[2]],
&[0., 1., 0.], &[0., 1., 0.],
), ),
); );
@ -33,7 +44,7 @@ pub fn render(data: &mut [u8], i: i32) {
let scale = *mat4::scale( let scale = *mat4::scale(
&mut mat4::new_zero(), &mut mat4::new_zero(),
&mat4::new_identity(), &mat4::new_identity(),
&vec3::new(1. / 1., 1. / 3., 1. / 1.), &vec3::new(1. / 3., 1. / 3., 1. / 3.),
); );
let translate = *mat4::translate( let translate = *mat4::translate(
&mut mat4::new_zero(), &mut mat4::new_zero(),
@ -42,35 +53,32 @@ pub fn render(data: &mut [u8], i: i32) {
); );
let rotate = *mat4::transpose( let rotate = *mat4::transpose(
&mut mat4::new_zero(), &mut mat4::new_zero(),
mat4::rotate( mat4::rotate(&mut mat4::new_zero(), &mat4::new_identity(), &0., &0., &0.),
&mut mat4::new_zero(),
&mat4::new_identity(),
&(3.141592 / 4.),
&0.,
&(3.141592 / 4.),
),
); );
let model_matrix_tmp = *mat4::mul(&mut mat4::new_zero(), &scale, &rotate); let model_matrix_tmp = *mat4::mul(&mut mat4::new_zero(), &scale, &rotate);
let model_matrix1 = *mat4::mul(&mut mat4::new_zero(), &model_matrix_tmp, &translate); let model_matrix1 = *mat4::mul(&mut mat4::new_zero(), &model_matrix_tmp, &translate);
let mut origin = [0., 0., 0.];
let test = origin;
vec3::transform_mat4(&mut origin, &test, &view_matrix);
let test = origin;
vec3::transform_mat4(&mut origin, &test, &model_matrix1);
let mut prelight = [-10., 10., 0.];
let test = prelight;
vec3::transform_mat4(&mut prelight, &test, &model_matrix1);
for x in 0..800 { for x in 0..800 {
for y in 0..800 { for y in 0..800 {
let pi_4: f64 = 3.141592 / 3.; let pi_4: f64 = 3.141592 / 8.;
let dirx = (2. * ((x as f64 + 0.5) / 800.) - 1.) * pi_4.tan(); let dirx = (2. * ((x as f64 + 0.5) / 800.) - 1.) * pi_4.tan();
let diry = (1. - 2. * ((y as f64 + 0.5) / 800.)) * pi_4.tan(); let diry = (1. - 2. * ((y as f64 + 0.5) / 800.)) * pi_4.tan();
let scalar = (dirx.powi(2) + diry.powi(2) + 1.).sqrt(); let scalar = (dirx.powi(2) + diry.powi(2) + 1.).sqrt();
let mut ray = Ray { let mut ray = Ray {
origin: [0., 0., 0.], origin: origin,
direction: [dirx / scalar, diry / scalar, -1. / scalar], direction: [dirx / scalar, diry / scalar, -1. / scalar],
}; };
let test = ray.direction; let test = ray.direction;
vec3::transform_mat4(&mut ray.direction, &test, &view_matrix); vec3::transform_mat4(&mut ray.direction, &test, &view_matrix);
let test = ray.direction; let test = ray.direction;
vec3::transform_mat4(&mut ray.direction, &test, &model_matrix1); vec3::transform_mat4(&mut ray.direction, &test, &model_matrix1);
let test = ray.origin;
vec3::transform_mat4(&mut ray.origin, &test, &view_matrix);
let test = ray.origin;
vec3::transform_mat4(&mut ray.origin, &test, &model_matrix1);
ray.direction[0] = ray.direction[0] - ray.origin[0]; ray.direction[0] = ray.direction[0] - ray.origin[0];
ray.direction[1] = ray.direction[1] - ray.origin[1]; ray.direction[1] = ray.direction[1] - ray.origin[1];
ray.direction[2] = ray.direction[2] - ray.origin[2]; ray.direction[2] = ray.direction[2] - ray.origin[2];
@ -99,7 +107,7 @@ pub fn render(data: &mut [u8], i: i32) {
ray.origin[2] + ray.direction[2] * dist, ray.origin[2] + ray.direction[2] * dist,
]; ];
let normal = sphere.get_normal(n); let normal = sphere.get_normal(n);
let light = [n[0] - 10., n[1] - 10., n[2] + 10.]; let light = [prelight[0] - n[0], prelight[1] - n[1], prelight[2] - n[2]];
let scalar = (light[0].powi(2) + light[1].powi(2) + light[2].powi(2)).sqrt(); let scalar = (light[0].powi(2) + light[1].powi(2) + light[2].powi(2)).sqrt();
let light_d = [light[0] / scalar, light[1] / scalar, light[2] / scalar]; let light_d = [light[0] / scalar, light[1] / scalar, light[2] / scalar];
let mut diffuse = let mut diffuse =