added refraction computations to Scene::traceRay()

git-svn-id: svn://anubis/fart/trunk@376 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2010-10-11 16:14:13 +00:00
parent 931cde802e
commit 22f4a79671
4 changed files with 21 additions and 8 deletions

View File

@ -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<Material> 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<Material> 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;
}
}

View File

@ -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<Material> last_material);
Shape::Intersection getRayClosestHit(const Ray & ray);
Color computePhong(const refptr<Material> material,
const Ray & viewRay,

View File

@ -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; }

View File

@ -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