Per-thread RNGs and RNG bugfix
This commit is contained in:
parent
a45ae025d6
commit
52806e4457
6 changed files with 70 additions and 39 deletions
14
material.hpp
14
material.hpp
|
|
@ -4,7 +4,7 @@
|
|||
#include "rtweekend.hpp"
|
||||
|
||||
struct material {
|
||||
virtual bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered) const = 0;
|
||||
virtual bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered, int32_t thread_id = 0) const = 0;
|
||||
};
|
||||
|
||||
struct lambertian : material {
|
||||
|
|
@ -15,9 +15,9 @@ struct lambertian : material {
|
|||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
virtual bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered) const override
|
||||
virtual bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered, int32_t thread_id = 0) const override
|
||||
{
|
||||
vec3 scatter_direction = rec.normal + random_unit_vector();
|
||||
vec3 scatter_direction = rec.normal + random_unit_vector(thread_id);
|
||||
|
||||
/* NOTE: it is possible that the random vector we generate is exactly opposite to the normal vector,
|
||||
in which case it will sum to a near-zero scatter vector and generate degenerate results.
|
||||
|
|
@ -45,10 +45,10 @@ struct metal : material {
|
|||
fuzz = f;
|
||||
};
|
||||
|
||||
virtual bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered) const override
|
||||
virtual bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered, int32_t thread_id) const override
|
||||
{
|
||||
vec3 reflected = reflect(normalize(r_in.direction), rec.normal);
|
||||
scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere());
|
||||
scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere(thread_id));
|
||||
attenuation = albedo;
|
||||
return (dot(scattered.direction, rec.normal) > 0);
|
||||
}
|
||||
|
|
@ -74,7 +74,7 @@ struct dielectric : material
|
|||
|
||||
|
||||
/* Virtual methods */
|
||||
virtual bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered) const override
|
||||
virtual bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered, int32_t thread_id) const override
|
||||
{
|
||||
attenuation = color(1,1,1);
|
||||
float refraction_ratio = rec.front_face ? (1.0/ri) : ri;
|
||||
|
|
@ -87,7 +87,7 @@ struct dielectric : material
|
|||
bool cannot_refract = refraction_ratio * sin_theta > 1.0;
|
||||
vec3 direction;
|
||||
|
||||
if (cannot_refract || reflectance(cos_theta, refraction_ratio) > random_float())
|
||||
if (cannot_refract || reflectance(cos_theta, refraction_ratio) > random_float(thread_id))
|
||||
direction = reflect(unit_direction, rec.normal);
|
||||
else
|
||||
direction = refract(unit_direction, rec.normal, refraction_ratio);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue