added Color::operator*=(), added jitter computation to reflectance and transparency recursion, recursing now but there is still a jitter or recursion issue

git-svn-id: svn://anubis/fart/trunk@182 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-03-03 04:30:46 +00:00
parent 1f00625379
commit e233056880
9 changed files with 71 additions and 22 deletions

View File

@ -139,10 +139,13 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel)
Color Scene::traceRay(const Ray & ray)
{
return traceRayDepth(ray, m_max_depth);
return traceRayRecurse(ray, m_max_depth, 1.0);
}
Color Scene::traceRayDepth(const Ray & ray, int depth)
/**
* factor: the proportion of the final color that this computation is worth
*/
Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor)
{
Color color;
@ -154,13 +157,44 @@ Color Scene::traceRayDepth(const Ray & ray, int depth)
refptr<Material> material = hit.shape->getMaterial();
Vector surfacePoint = ray[hit.dist];
Vector surfaceNormal = hit.shape->getNormalAt(surfacePoint);
color += Lighting::computePhong(material,
color = Lighting::computePhong(material,
m_lights,
ray,
surfacePoint,
hit.shape->getNormalAt(surfacePoint),
surfaceNormal,
m_ambient_light);
if (depth > 0 && factor > SCENE_FACTOR_THRESHOLD)
{
double reflectance = material->getReflectance();
if (factor * reflectance > SCENE_FACTOR_THRESHOLD)
{
color *= (1.0 - reflectance);
Vector reflected_direction =
(-ray.getDirection()).reflect(surfaceNormal);
Ray newRay(surfacePoint, reflected_direction);
Vector jitter_surface_point = newRay[0.001];
Ray jitterNewRay(jitter_surface_point, reflected_direction);
Color c = traceRayRecurse(jitterNewRay,
depth - 1,
factor * reflectance);
color += c * reflectance;
}
double transparency = material->getTransparency();
if (factor * transparency > SCENE_FACTOR_THRESHOLD)
{
color *= (1.0 - transparency);
Vector jitter_surface_point = ray[hit.dist + 0.001];
Ray newRay(jitter_surface_point, ray.getDirection());
Color c = traceRayRecurse(newRay,
depth - 1,
factor * transparency);
color += c * transparency;
}
}
}
return color;

View File

@ -17,8 +17,7 @@
#include "parser/parser.h"
#include "parser/nodes.h"
#define SCENE_MAX_TRANSPARENT_HITS 8
#define SCENE_TRANSPARENCY_THRESHOLD 0.01
#define SCENE_FACTOR_THRESHOLD 0.02
class Scene
{
@ -51,7 +50,7 @@ class Scene
/* private methods */
void renderPixel(int x, int y, unsigned char * pixel);
Color traceRay(const Ray & ray);
Color traceRayDepth(const Ray & ray, int depth);
Color traceRayRecurse(const Ray & ray, int depth, double factor);
ShapeDistance getRayClosestHit(const Ray & ray);
/* In Scene-load.cc */

View File

@ -15,12 +15,19 @@ scene
up <0, 0, 1>
}
plane { position <1, 0, 0>, 2 }
plane { position <-1, 0, 0>, 2 }
plane { position <0, 1, 0>, 2 }
plane { position <0, -1, 0>, 2 }
plane { position <0, 0, 1>, 2 }
plane { position <0, 0, -1>, 2 }
define material mirror
{
color <1, 0, 0>
specular <0, 0, 0>
reflectance 1.0
}
plane { position <1, 0, 0>, 2 material mirror }
plane { position <-1, 0, 0>, 2 material mirror }
plane { position <0, 1, 0>, 2 material mirror }
plane { position <0, -1, 0>, 2 material mirror }
plane { position <0, 0, 1>, 2 material mirror }
plane { position <0, 0, -1>, 2 material mirror }
sphere
{

View File

@ -10,7 +10,6 @@ static bool default_material_initialized = false;
Shape::Shape()
{
m_transparency = 0.0;
if (default_material_initialized == false)
{
default_material = new Material();

View File

@ -106,16 +106,12 @@ class Shape
}
Transform & getTransform() { return m_transform; }
void setTransparency(double t) { m_transparency = t; }
double getTransparency() const { return m_transparency; }
void setMaterial(refptr<Material> material) { m_material = material; }
refptr<Material> getMaterial() const { return m_material; }
protected:
Transform m_transform;
Transform m_inverse;
double m_transparency;
refptr<Material> m_material;
};

View File

@ -71,6 +71,14 @@ Color & Color::operator-=(const Color & other)
return *this;
}
Color & Color::operator*=(double scale)
{
r *= scale;
g *= scale;
b *= scale;
return *this;
}
Color operator+(const Color & c1, const Color & c2)
{
return Color(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b);

View File

@ -21,6 +21,7 @@ class Color
Color operator/(double scale) const;
Color & operator+=(const Color & other);
Color & operator-=(const Color & other);
Color & operator*=(double scale);
static const Color black;
static const Color white;

View File

@ -8,4 +8,5 @@ Material::Material()
m_specular_color = Color::white;
m_shininess = 50.0;
m_reflectance = 0.0;
m_transparency = 0.0;
}

View File

@ -33,12 +33,16 @@ class Material
void setReflectance(double reflectance) { m_reflectance = reflectance; }
double getReflectance() const { return m_reflectance; }
void setTransparency(double t) { m_transparency = t; }
double getTransparency() const { return m_transparency; }
protected:
Color m_ambient_color;
Color m_diffuse_color;
Color m_specular_color;
double m_shininess;
double m_reflectance;
double m_transparency;
};
#endif