reworked Scene::traceRayRecurse() to handle backfaces properly for refraction
git-svn-id: svn://anubis/fart/trunk@378 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
b24aea4dba
commit
1d5d043928
@ -119,8 +119,7 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel)
|
||||
|
||||
Color Scene::traceRay(const Ray & ray)
|
||||
{
|
||||
static refptr<Material> air = new Material();
|
||||
return traceRayRecurse(ray, m_max_depth, 1.0, air);
|
||||
return traceRayRecurse(ray, m_max_depth, 1.0, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -129,7 +128,11 @@ Color Scene::traceRay(const Ray & ray)
|
||||
Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor,
|
||||
refptr<Material> last_material)
|
||||
{
|
||||
Color color;
|
||||
static refptr<Material> air = new Material();
|
||||
Color color(0, 0, 0);
|
||||
|
||||
if (last_material.isNull())
|
||||
last_material = air;
|
||||
|
||||
Shape::Intersection hit = getRayClosestHit(ray);
|
||||
|
||||
@ -139,12 +142,10 @@ Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor,
|
||||
refptr<Material> material = hit.shape->getMaterial();
|
||||
|
||||
/* check for backfaces */
|
||||
if (ray.getDirection() % hit.normal > 0.0)
|
||||
{
|
||||
/* if dot product is positive, this is a back-face */
|
||||
hit.normal = -hit.normal;
|
||||
}
|
||||
bool frontface = ray.getDirection() % hit.normal < 0.0;
|
||||
|
||||
if (frontface)
|
||||
{
|
||||
color = computePhong(material,
|
||||
ray,
|
||||
hit.position,
|
||||
@ -187,6 +188,19 @@ Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor,
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
material = air;
|
||||
Vector jitter_surface_point = hit.position
|
||||
+ ray.getDirection() * 0.0001;
|
||||
Vector refracted_direction =
|
||||
ray.getDirection().refract(-hit.normal,
|
||||
last_material->getRefraction(),
|
||||
material->getRefraction());
|
||||
Ray newRay(jitter_surface_point, refracted_direction);
|
||||
color = traceRayRecurse(newRay, depth, factor, material);
|
||||
}
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ scene
|
||||
{
|
||||
camera
|
||||
{
|
||||
position <0, 0, 40>
|
||||
position <0, -40, 40>
|
||||
look_at <0, 100, 5>
|
||||
}
|
||||
|
||||
@ -17,6 +17,12 @@ scene
|
||||
color <0, 0.8, 1>
|
||||
}
|
||||
|
||||
define material glass
|
||||
{
|
||||
transparency 1
|
||||
refraction 1.5
|
||||
}
|
||||
|
||||
plane
|
||||
{
|
||||
position <0, 0, 1>, 0
|
||||
@ -34,8 +40,16 @@ scene
|
||||
{
|
||||
radius 10
|
||||
translate <$x, $y, 10>
|
||||
if ($x == 0 && $y == 0)
|
||||
{
|
||||
material glass
|
||||
translate <0, 0, 20>
|
||||
}
|
||||
else
|
||||
{
|
||||
material mat
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user