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:
parent
1f00625379
commit
e233056880
@ -139,10 +139,13 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel)
|
|||||||
|
|
||||||
Color Scene::traceRay(const Ray & ray)
|
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;
|
Color color;
|
||||||
|
|
||||||
@ -154,13 +157,44 @@ Color Scene::traceRayDepth(const Ray & ray, int depth)
|
|||||||
refptr<Material> material = hit.shape->getMaterial();
|
refptr<Material> material = hit.shape->getMaterial();
|
||||||
|
|
||||||
Vector surfacePoint = ray[hit.dist];
|
Vector surfacePoint = ray[hit.dist];
|
||||||
|
Vector surfaceNormal = hit.shape->getNormalAt(surfacePoint);
|
||||||
|
|
||||||
color += Lighting::computePhong(material,
|
color = Lighting::computePhong(material,
|
||||||
m_lights,
|
m_lights,
|
||||||
ray,
|
ray,
|
||||||
surfacePoint,
|
surfacePoint,
|
||||||
hit.shape->getNormalAt(surfacePoint),
|
surfaceNormal,
|
||||||
m_ambient_light);
|
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;
|
return color;
|
||||||
|
@ -17,8 +17,7 @@
|
|||||||
#include "parser/parser.h"
|
#include "parser/parser.h"
|
||||||
#include "parser/nodes.h"
|
#include "parser/nodes.h"
|
||||||
|
|
||||||
#define SCENE_MAX_TRANSPARENT_HITS 8
|
#define SCENE_FACTOR_THRESHOLD 0.02
|
||||||
#define SCENE_TRANSPARENCY_THRESHOLD 0.01
|
|
||||||
|
|
||||||
class Scene
|
class Scene
|
||||||
{
|
{
|
||||||
@ -51,7 +50,7 @@ class Scene
|
|||||||
/* private methods */
|
/* private methods */
|
||||||
void renderPixel(int x, int y, unsigned char * pixel);
|
void renderPixel(int x, int y, unsigned char * pixel);
|
||||||
Color traceRay(const Ray & ray);
|
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);
|
ShapeDistance getRayClosestHit(const Ray & ray);
|
||||||
|
|
||||||
/* In Scene-load.cc */
|
/* In Scene-load.cc */
|
||||||
|
@ -15,12 +15,19 @@ scene
|
|||||||
up <0, 0, 1>
|
up <0, 0, 1>
|
||||||
}
|
}
|
||||||
|
|
||||||
plane { position <1, 0, 0>, 2 }
|
define material mirror
|
||||||
plane { position <-1, 0, 0>, 2 }
|
{
|
||||||
plane { position <0, 1, 0>, 2 }
|
color <1, 0, 0>
|
||||||
plane { position <0, -1, 0>, 2 }
|
specular <0, 0, 0>
|
||||||
plane { position <0, 0, 1>, 2 }
|
reflectance 1.0
|
||||||
plane { position <0, 0, -1>, 2 }
|
}
|
||||||
|
|
||||||
|
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
|
sphere
|
||||||
{
|
{
|
||||||
|
@ -10,7 +10,6 @@ static bool default_material_initialized = false;
|
|||||||
|
|
||||||
Shape::Shape()
|
Shape::Shape()
|
||||||
{
|
{
|
||||||
m_transparency = 0.0;
|
|
||||||
if (default_material_initialized == false)
|
if (default_material_initialized == false)
|
||||||
{
|
{
|
||||||
default_material = new Material();
|
default_material = new Material();
|
||||||
|
@ -106,16 +106,12 @@ class Shape
|
|||||||
}
|
}
|
||||||
Transform & getTransform() { return m_transform; }
|
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; }
|
void setMaterial(refptr<Material> material) { m_material = material; }
|
||||||
refptr<Material> getMaterial() const { return m_material; }
|
refptr<Material> getMaterial() const { return m_material; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Transform m_transform;
|
Transform m_transform;
|
||||||
Transform m_inverse;
|
Transform m_inverse;
|
||||||
double m_transparency;
|
|
||||||
refptr<Material> m_material;
|
refptr<Material> m_material;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -71,6 +71,14 @@ Color & Color::operator-=(const Color & other)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Color & Color::operator*=(double scale)
|
||||||
|
{
|
||||||
|
r *= scale;
|
||||||
|
g *= scale;
|
||||||
|
b *= scale;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
Color operator+(const Color & c1, const Color & c2)
|
Color operator+(const Color & c1, const Color & c2)
|
||||||
{
|
{
|
||||||
return Color(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b);
|
return Color(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b);
|
||||||
|
@ -21,6 +21,7 @@ class Color
|
|||||||
Color operator/(double scale) const;
|
Color operator/(double scale) const;
|
||||||
Color & operator+=(const Color & other);
|
Color & operator+=(const Color & other);
|
||||||
Color & operator-=(const Color & other);
|
Color & operator-=(const Color & other);
|
||||||
|
Color & operator*=(double scale);
|
||||||
|
|
||||||
static const Color black;
|
static const Color black;
|
||||||
static const Color white;
|
static const Color white;
|
||||||
|
@ -8,4 +8,5 @@ Material::Material()
|
|||||||
m_specular_color = Color::white;
|
m_specular_color = Color::white;
|
||||||
m_shininess = 50.0;
|
m_shininess = 50.0;
|
||||||
m_reflectance = 0.0;
|
m_reflectance = 0.0;
|
||||||
|
m_transparency = 0.0;
|
||||||
}
|
}
|
||||||
|
@ -33,12 +33,16 @@ class Material
|
|||||||
void setReflectance(double reflectance) { m_reflectance = reflectance; }
|
void setReflectance(double reflectance) { m_reflectance = reflectance; }
|
||||||
double getReflectance() const { return m_reflectance; }
|
double getReflectance() const { return m_reflectance; }
|
||||||
|
|
||||||
|
void setTransparency(double t) { m_transparency = t; }
|
||||||
|
double getTransparency() const { return m_transparency; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Color m_ambient_color;
|
Color m_ambient_color;
|
||||||
Color m_diffuse_color;
|
Color m_diffuse_color;
|
||||||
Color m_specular_color;
|
Color m_specular_color;
|
||||||
double m_shininess;
|
double m_shininess;
|
||||||
double m_reflectance;
|
double m_reflectance;
|
||||||
|
double m_transparency;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user