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:
Josh Holtrop 2009-03-04 02:03:21 +00:00
parent 7d0b4c5646
commit f9cf1381a6
4 changed files with 62 additions and 14 deletions

View File

@ -250,22 +250,31 @@ Color Scene::computePhong(const refptr<Material> material,
Vector reflectedLightDirection =
directionToLight.reflect(surfaceNormal);
/* calculate the diffuse term */
double diffuse_coef = directionToLight % surfaceNormal;
if (diffuse_coef > 0.0)
{
result += diffuseColor
* (*it)->getDiffuseColor()
* diffuse_coef;
}
Ray surfaceToLight(surfacePoint, directionToLight);
double light_contribution =
calculateLightContribution(surfaceToLight.shift(0.0001), *it);
/* calculate the specular term */
double specular_coef = reflectedLightDirection % viewDirection;
if (specular_coef > 0.0)
if (light_contribution > 0.0)
{
result += specularColor
* (*it)->getSpecularColor()
* pow(specular_coef, shininess);
/* calculate the diffuse term */
double diffuse_coef = directionToLight % surfaceNormal;
if (diffuse_coef > 0.0)
{
result += diffuseColor
* (*it)->getDiffuseColor()
* diffuse_coef
* light_contribution;
}
/* calculate the specular term */
double specular_coef = reflectedLightDirection % viewDirection;
if (specular_coef > 0.0)
{
result += specularColor
* (*it)->getSpecularColor()
* 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)
{

View File

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

View File

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

View File

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