diff --git a/main/Scene.cc b/main/Scene.cc index 43afa9b..542ccfa 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -119,13 +119,15 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel) Color Scene::traceRay(const Ray & ray) { - return traceRayRecurse(ray, m_max_depth, 1.0); + static refptr air = new Material(); + return traceRayRecurse(ray, m_max_depth, 1.0, air); } /** * factor: the proportion of the final color that this computation is worth */ -Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor) +Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor, + refptr last_material) { Color color; @@ -161,7 +163,8 @@ Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor) Ray jitterNewRay(jitter_surface_point, reflected_direction); Color c = traceRayRecurse(jitterNewRay, depth - 1, - factor * reflectance); + factor * reflectance, + material); color += c * reflectance; } @@ -171,10 +174,15 @@ Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor) color *= (1.0 - transparency); Vector jitter_surface_point = hit.position + ray.getDirection() * 0.0001; - Ray newRay(jitter_surface_point, ray.getDirection()); + Vector refracted_direction = + ray.getDirection().refract(hit.normal, + last_material->getRefraction(), + material->getRefraction()); + Ray newRay(jitter_surface_point, refracted_direction); Color c = traceRayRecurse(newRay, depth - 1, - factor * transparency); + factor * transparency, + material); color += c * transparency; } } diff --git a/main/Scene.h b/main/Scene.h index eb789d2..0d810fd 100644 --- a/main/Scene.h +++ b/main/Scene.h @@ -46,7 +46,8 @@ class Scene protected: /* private methods */ Color traceRay(const Ray & ray); - Color traceRayRecurse(const Ray & ray, int depth, double factor); + Color traceRayRecurse(const Ray & ray, int depth, double factor, + refptr last_material); Shape::Intersection getRayClosestHit(const Ray & ray); Color computePhong(const refptr material, const Ray & viewRay, diff --git a/util/Material.h b/util/Material.h index 42c3940..a0633ca 100644 --- a/util/Material.h +++ b/util/Material.h @@ -42,7 +42,11 @@ class Material void setReflectance(double reflectance) { m_reflectance = reflectance; } double getReflectance() const { return m_reflectance; } - void setRefraction(double refraction) { m_refraction = refraction; } + void setRefraction(double refraction) + { + if (refraction > 0.0) + m_refraction = refraction; + } double getRefraction() const { return m_refraction; } void setTransparency(double t) { m_transparency = t; } diff --git a/util/Vector.h b/util/Vector.h index b723ac0..ebc944f 100644 --- a/util/Vector.h +++ b/util/Vector.h @@ -77,7 +77,7 @@ class Vector const double sinT2 = n * n * (1.0 - cosI * cosI); if (sinT2 > 1.0) return Vector(0.0, 0.0, 0.0); - return (*this) * n - target * (n + sqrt(1.0 - sinT2)); + return (*this) * n - target * (n * cosI + sqrt(1.0 - sinT2)); } Vector getPerpendicular() const