bheisler / raytracer Goto Github PK
View Code? Open in Web Editor NEWA toy raytracer in Rust
License: MIT License
A toy raytracer in Rust
License: MIT License
Quite impressive work. But haven't you noticed the lighting looks a bit wonky?
Please read this: http://blog.johnnovak.net/2016/09/21/what-every-coder-should-know-about-gamma/.
(Don't worry. Almost nobody gets this right.)
Shouldn't its normal be 0,0,1?
In your example scene, the back wall has a normal of 0,0,-1
Backstory: I'm using your code and implementing triangles using https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/ray-triangle-intersection-geometric-solution as reference. I got it to work and then realized my normals were going opposite than I thought (mind boggling). Anyway, I was under the impression that the normal was the direction an object's plane was facing.
Hi,
I'm trying to implement my own toy ray tracer in Rust, but I am having lot's of trouble with the Fresnel calculations. Out of frustration I just tried to just copy your fresnel function, but I only get 0.04 or 0.03999999 as a result. I isolated the function and wrote some tests but I am unable to get the function to work correctly. Can you help me?
I copied this function:
https://github.com/bheisler/raytracer/blob/master/src/rendering.rs#L267-L287
The test results for a 0 degree angle are correct (I think 0.04 is correct when the IOR = 1.5). I also test a 90 degree angle and smaller angles, but the result stays around 0.04.
---- helpers::tests::test_fresnel_ratio_half_at_around_80_deg_on_normal stdout ----
Normal Vector3 [0.0, 0.0, -1.0]
Angle of incidence: Vector3 [1.0, 0.0, 0.2]
ior: 1.5
ratio: 0.04000000000000001
0.04000000000000001
thread 'helpers::tests::test_fresnel_ratio_half_at_around_80_deg_on_normal' panicked at 'assertion failed: ratio > 0.499999', src\helpers.rs:184:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.
---- helpers::tests::test_fresnel_ratio_one_at_perpendicular_of_normal stdout ----
Normal Vector3 [0.0, 0.0, -1.0]
Angle of incidence: Vector3 [1.0, 0.0, 0.0]
ior: 1.5
ratio: 0.040000000000000015
0.040000000000000015
thread 'helpers::tests::test_fresnel_ratio_one_at_perpendicular_of_normal' panicked at 'assertion failed: ratio > 0.99999999', src\helpers.rs:171:9
Any ideas?
Edit:
I also tried the following. With this code I got values between 1 and 1000 or more...
let mut n1 = 1.0;
let mut n2 = ior;
let cos_theta_incidence = normal_normalized.dot(angle_of_incidence_normalized);
let sin_theta_incidence = (1.0 - cos_theta_incidence * cos_theta_incidence).sqrt();
if cos_theta_incidence > 0.0 {
n1 = ior;
n2 = 1.0;
}
let sin_theta_transmittance = n1 * (sin_theta_incidence / n2);
let cos_theta_transmittance = (1.0 - sin_theta_transmittance * sin_theta_transmittance).sqrt();
let n1_cos_theta_transmittance = n1 * cos_theta_transmittance;
let n2_cos_theta_transmittance = n2 * cos_theta_transmittance;
let n1_cos_theta_incidence = n1 * cos_theta_incidence;
let n2_cos_theta_incidence = n2 * cos_theta_incidence;
let s_polarized_sqrt = (n1_cos_theta_incidence - n2_cos_theta_transmittance)
/ (n1_cos_theta_incidence + n2_cos_theta_transmittance);
let s_polarized = s_polarized_sqrt * s_polarized_sqrt;
let p_polarized_sqrt = (n2_cos_theta_incidence - n1_cos_theta_transmittance)
/ (n2_cos_theta_incidence + n1_cos_theta_transmittance);
let p_polarized = p_polarized_sqrt * p_polarized_sqrt;
fresnel_ratio = (s_polarized + p_polarized) / 2.0;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.