Example #1
0
SColor RayTracer::shadeIntersection(Intersection in, uint d) {
    if (d <= 0 || in.hasIntersected() == false) {
        // terminate recursion
        return SColor(0, 0, 0);
    }

    Vect shade(0, 0, 0);

    Material *mat = in.getMaterial();
    float kt = mat->getTransparency();
    SColor ks = mat->getSpecColor();
    SColor ka = mat->getAmbColor();
    SColor Cd = mat->getDiffColor();

    SColor ambLight = Whitted::AmbientLightning(kt, ka, Cd);

    std::vector<Light *> lts = scene->getLights();
    for (uint i = 0; i < lts.size(); i++) {
        Light *l = lts.at(i);

        SColor Sj = calculateShadowScalar(*l, in);
        shade = shade + Whitted::Illumination(l, in, Sj);
    }
    
    SColor reflection;
    if (ks.length() > 0) {
        Ray r = in.calculateReflection();
        Intersection rin = scene->calculateRayIntersection(r);
        reflection = shadeIntersection(rin, d-1).linearMult(ks);
    }

    SColor refraction;
    if (kt > 0) {
        Ray r = in.calculateRefraction();
        Intersection rin = scene->calculateRayIntersection(r);
        refraction = shadeIntersection(rin, d-1).linearMult(kt);
    }

    shade = ambLight + shade + reflection + refraction;

    return shade;
}
Example #2
0
SColor RayTracer::calculateShadowScalar(Light &lt, Intersection &in) {
    // cout << in.toString() << endl;
    Vect p = lt.getPos();
    Vect ori = in.calculateIntersectionPoint();// + in.calculateSurfaceNormal().linearMult(0.0001f);
    Vect dir;
    if (lt.getType() == DIRECTIONAL_LIGHT) {
        dir = lt.getDir().invert();
    } else {
        dir = p - ori;
        dir.normalize();
    }
    ori = ori + dir.linearMult(0.001f);
    Ray shdw(ori, dir);

    Intersection ins = scene->calculateRayIntersection(shdw);
    Material *mat = ins.getMaterial();
    
    if (!ins.hasIntersected()) {
        // The point is in direct light
        return SColor(1, 1, 1);
    } else if (mat->getTransparency() <= 0.00000001) {
        // The material is fully opaque
        Vect pos = ins.calculateIntersectionPoint();
        if (lt.getType() == DIRECTIONAL_LIGHT ||
            ori.euclideanDistance(pos) < ori.euclideanDistance(lt.getPos())) {
            // The ray intersects with an object before the light source
            return SColor(0, 0, 0);
        } else {
            // The ray intersects with an object behind the lightsource
            // or a direction light, thus fully in light
            return SColor(1, 1, 1);
        }
    } else { // The shape is transparent
        // Normalize the color for this material, and recursively trace for other
        // transparent objects
        SColor Cd = mat->getDiffColor();
        float m = max(Cd.R(), max(Cd.G(), Cd.B()));
        Cd.R(Cd.R()/m); Cd.G(Cd.G()/m); Cd.B(Cd.B()/m);
        SColor Si = Cd.linearMult(mat->getTransparency());
        return Si.linearMult(calculateShadowScalar(lt, ins));
    }
}
Example #3
0
Colour* rayTrace(const Colour& ambient, const Point3D& eye, Ray ray, SceneNode* root,
    const std::list<Light*>& lights, int level, double fogDist){
  if(level <= 0)
    return NULL;

  Intersection* i = root->intersect(ray);
  bool fogOn = true;
  if(fogDist <= 0)
    fogOn = false;

  if(i != NULL){
    Colour color(0,0,0);
    Colour fog(0.8,0.8,0.8);
    Colour diffuse(0,0,0), specular(0,0,0);

    Material* material = i->getMaterial();
    Vector3D n = i->getNormal();
    n.normalize();
    Point3D p = i->getPoint();
    Vector3D v = eye - p;
    v.normalize();

    for (std::list<Light*>::const_iterator I = lights.begin(); I != lights.end(); ++I) {
      Light light = **I;

      Vector3D l = light.position - p;
      l.normalize();
      Vector3D r = 2*l*n*n - l;
      r.normalize();

      // shadows
      Ray lightRay = Ray(p, l);
      Intersection* lightIsc = root->intersect(lightRay);
      if(lightIsc == NULL){
        // add light contribution
        //std::cerr << "light" << std::endl;
        if(n*l > 0)
          diffuse = diffuse + material->getDiffuse() * (l*n) * light.colour;
        if(r*v > 0)
          specular = specular + material->getSpecular() * pow((r*v), material->getShininess()) * light.colour;
      }

    }


    //secondaty rays
    Vector3D r = 2*v*n*n - v;
    r.normalize();
    Ray refRay(p, r);
    Colour* reflectedColor = rayTrace(ambient, eye, refRay, root, lights, level-1, fogDist);

    if(reflectedColor != NULL){
      if(n*r > 0)
        diffuse = diffuse + material->getDiffuse() * (r*n) * material->getReflectivity() * (*reflectedColor);
      if(r*v > 0)
        specular = specular + material->getSpecular() * pow((r*v), material->getShininess()) * material->getReflectivity() * (*reflectedColor);
    }

    color = ambient*material->getColor() + diffuse + specular;
    if(fogOn){
      double dist = i->getT()/fogDist;
      if(dist>1)
        dist=1;
      color = (1-dist)*color + dist*fog;
    }

    return new Colour(color);
  }  

  return NULL;
}