working on shadows but they are appearing in weird places...
git-svn-id: svn://anubis/fart/trunk@185 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
7d0b4c5646
commit
f9cf1381a6
@ -250,13 +250,20 @@ Color Scene::computePhong(const refptr<Material> material,
|
||||
Vector reflectedLightDirection =
|
||||
directionToLight.reflect(surfaceNormal);
|
||||
|
||||
Ray surfaceToLight(surfacePoint, directionToLight);
|
||||
double light_contribution =
|
||||
calculateLightContribution(surfaceToLight.shift(0.0001), *it);
|
||||
|
||||
if (light_contribution > 0.0)
|
||||
{
|
||||
/* calculate the diffuse term */
|
||||
double diffuse_coef = directionToLight % surfaceNormal;
|
||||
if (diffuse_coef > 0.0)
|
||||
{
|
||||
result += diffuseColor
|
||||
* (*it)->getDiffuseColor()
|
||||
* diffuse_coef;
|
||||
* diffuse_coef
|
||||
* light_contribution;
|
||||
}
|
||||
|
||||
/* calculate the specular term */
|
||||
@ -265,7 +272,9 @@ Color Scene::computePhong(const refptr<Material> material,
|
||||
{
|
||||
result += specularColor
|
||||
* (*it)->getSpecularColor()
|
||||
* pow(specular_coef, shininess);
|
||||
* pow(specular_coef, shininess)
|
||||
* light_contribution;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -279,6 +288,37 @@ Color Scene::computePhong(const refptr<Material> material,
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
double Scene::calculateLightContribution(const Ray & toLight,
|
||||
refptr<Light> light)
|
||||
{
|
||||
double contrib = 1.0;
|
||||
double dist_to_light = toLight.getOrigin().dist_to(light->getPosition());
|
||||
double dist_so_far = 0.0;
|
||||
|
||||
Ray currentRay = toLight;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
ShapeDistance hit = getRayClosestHit(currentRay);
|
||||
|
||||
if ( hit.shape.isNull() )
|
||||
break;
|
||||
|
||||
if ( dist_so_far + hit.dist > dist_to_light )
|
||||
break;
|
||||
|
||||
contrib *= hit.shape->getMaterial()->getTransparency();
|
||||
|
||||
if ( contrib < SCENE_FACTOR_THRESHOLD )
|
||||
break;
|
||||
|
||||
dist_so_far += hit.dist + 0.0001;
|
||||
}
|
||||
|
||||
return contrib;
|
||||
}
|
||||
|
||||
bool operator<(const Scene::ShapeDistance & sd1,
|
||||
const Scene::ShapeDistance & sd2)
|
||||
{
|
||||
|
@ -56,6 +56,8 @@ class Scene
|
||||
const Ray & viewRay,
|
||||
const Vector & surfacePoint,
|
||||
const Vector & surfaceNormal);
|
||||
double calculateLightContribution(const Ray & toLight,
|
||||
refptr<Light> light);
|
||||
|
||||
/* In Scene-load.cc */
|
||||
void load(const char * filename);
|
||||
|
@ -31,3 +31,8 @@ std::ostream & operator<<(std::ostream & out, const Ray & r)
|
||||
out << "(" << r.getOrigin() << " -> " << r.getDirection() << ")";
|
||||
return out;
|
||||
}
|
||||
|
||||
Ray Ray::shift(double amt)
|
||||
{
|
||||
return Ray(getPositionAt(amt), m_direction);
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ class Ray
|
||||
const Vector & getDirection() const { return m_direction; }
|
||||
Vector getPositionAt(double dist) const;
|
||||
Vector operator[](double dist) const { return getPositionAt(dist); }
|
||||
Ray shift(double amt);
|
||||
|
||||
protected:
|
||||
Vector m_origin;
|
||||
|
Loading…
x
Reference in New Issue
Block a user