shading
This commit is contained in:
parent
a205928068
commit
f7bb59cfed
|
@ -10,7 +10,6 @@ use sdl2::event::Event;
|
|||
use sdl2::keyboard::Keycode;
|
||||
use sdl2::pixels::PixelFormatEnum;
|
||||
use sdl2::surface::Surface;
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() {
|
||||
let sdl_context = sdl2::init().unwrap();
|
||||
|
@ -31,7 +30,7 @@ fn main() {
|
|||
// let surface = Surface::from_data(&mut data, 800, 600, 3, PixelFormatEnum::RGB24).unwrap();
|
||||
let texture_creator = canvas.texture_creator();
|
||||
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||
let mut i = 0;
|
||||
// let mut i = 0;
|
||||
'running: loop {
|
||||
render(&mut *data);
|
||||
// put_pixel(
|
||||
|
@ -67,7 +66,5 @@ fn main() {
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
// The rest of the game loop goes here...
|
||||
// ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,5 +2,5 @@ use super::ray::Ray;
|
|||
|
||||
pub trait Object {
|
||||
fn get_intersection(&self, ray: Ray) -> Option<f64>;
|
||||
fn get_normal(&self) -> [f64; 3];
|
||||
fn get_normal(&self, n: [f64; 3]) -> [f64; 3];
|
||||
}
|
||||
|
|
|
@ -6,11 +6,6 @@ use crate::ray::Ray;
|
|||
use crate::sphere::Sphere;
|
||||
use sdl2::pixels::Color;
|
||||
|
||||
struct Camera {
|
||||
rv: [f64; 3],
|
||||
uv: [f64; 3],
|
||||
}
|
||||
|
||||
fn put_pixel(data: &mut [u8], x: usize, y: usize, color: Color) {
|
||||
data[y * 3 * 800 + x * 3] = color.r;
|
||||
data[y * 3 * 800 + x * 3 + 1] = color.g;
|
||||
|
@ -36,10 +31,6 @@ pub fn render(data: &mut [u8]) {
|
|||
&mat4::new_identity(),
|
||||
&vec3::new(1., 1., 0.),
|
||||
);
|
||||
let camera = Camera {
|
||||
rv: [1., 0., 0.],
|
||||
uv: [0., 1., 0.],
|
||||
};
|
||||
for x in 0..800 {
|
||||
for y in 0..600 {
|
||||
let mut ray = Ray {
|
||||
|
@ -54,21 +45,36 @@ pub fn render(data: &mut [u8]) {
|
|||
ray.direction[0] /= n;
|
||||
ray.direction[1] /= n;
|
||||
ray.direction[2] /= n;
|
||||
let ray2 = Box::clone(&Box::new(ray));
|
||||
// let ray2 = Box::clone(&Box::new(ray));
|
||||
// vec3::transform_mat4(&mut ray.direction, &ray2.direction, &projection_matrix);
|
||||
let ray2 = Box::clone(&Box::new(ray));
|
||||
vec3::transform_mat4(&mut ray.origin, &ray2.origin, &view_matrix);
|
||||
let ray2 = Box::clone(&Box::new(ray));
|
||||
vec3::transform_mat4(&mut ray.origin, &ray2.origin, &model_matrix);
|
||||
match sphere.get_intersection(ray) {
|
||||
Some(v) => {
|
||||
Some(dist) => {
|
||||
let n = [
|
||||
ray.origin[0] + ray.direction[0] * dist,
|
||||
ray.origin[1] + ray.direction[1] * dist,
|
||||
ray.origin[2] + ray.direction[2] * dist,
|
||||
];
|
||||
let normal = sphere.get_normal(n);
|
||||
let light = [n[0] - 10., n[1] - 10., n[2] - 10.];
|
||||
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 mut diffuse =
|
||||
normal[0] * light_d[0] + normal[1] * light_d[1] + normal[2] * light_d[2];
|
||||
if diffuse < 0. {
|
||||
diffuse = 0.;
|
||||
}
|
||||
put_pixel(
|
||||
&mut *data,
|
||||
x,
|
||||
y,
|
||||
Color {
|
||||
r: 255,
|
||||
g: 0,
|
||||
b: 0,
|
||||
r: (diffuse * 255.) as u8,
|
||||
g: (diffuse * 255.) as u8,
|
||||
b: (diffuse * 255.) as u8,
|
||||
a: 0,
|
||||
},
|
||||
);
|
||||
|
|
|
@ -31,7 +31,8 @@ impl Object for Sphere {
|
|||
None
|
||||
}
|
||||
}
|
||||
fn get_normal(&self) -> [f64; 3] {
|
||||
vec3::new_zero()
|
||||
fn get_normal(&self, n: [f64; 3]) -> [f64; 3] {
|
||||
let scalar = (n[0].powi(2) + n[1].powi(2) + n[2].powi(2)).sqrt();
|
||||
[n[0] / scalar, n[1] / scalar, n[2] / scalar]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue