Exemple #1
0
Ray RayTracer::CreateRefractRay(glm::dvec3 rDir, glm::dvec3 surfNormal,
				glm::dvec3 startP, int depth){
  double n1Overn2 = 1.0;
  
  if (depth % 2 == 1){
    n1Overn2 =1.33;
    surfNormal = -surfNormal; 
  }
  else{
    n1Overn2 = .7518797;
  }
  glm::dvec3 refDir = glm::normalize(glm::refract(rDir,surfNormal,n1Overn2));
  Ray refRay(startP,refDir,startP);
  return refRay;
}
Exemple #2
0
Colour World::spawnrec(const Ray& r,const int& depth) {
  Point p;
  Vector normal;
  Vector incoming;
  Vector reflective;
  std::list<Light*> lights(getLightList());
  IntersectData data;
  double lightW;
//  bool useBG = true;

  Colour illumination = getBgColour();
  double max_w = std::numeric_limits<double>::max();
  double least_w = max_w;
  Object* close_o = intersectWithObjects(r,least_w);

  // If intersection 
  if (close_o){
//    useBG = false;
    // Initialize things
    illumination = Colour();
    Vector origin = (r.getOrigin()).getVec();
    Vector dir = r.getDirection();
    p = Point(origin+(dir*least_w));
    normal = close_o->getNormal();
    data.setPoint(p);
    data.setNormal(normal.normalize());
    data.setViewing((p-Point()).normalize());
 
    Ray lightRay;
    Ray shadowRay;
    Ray outShadowRay;
    Object* blockingObject;
    least_w = std::numeric_limits<double>::max();
    std::list<Light*>::iterator it;
    Vector lightDir;
    for(it = lights.begin(); it != lights.end(); it++){
      lightDir = (p-((*it)->getPosition())).normalize();
      lightRay = Ray(p,lightDir);
      blockingObject = intersectWithObjects(lightRay,least_w,close_o);
      incoming = p-((*it)->getPosition());
      reflective = ((*it)->getPosition()-p).reflect(normal).normalize();
      data.setLight(*(*it));
      data.setIncoming(incoming.normalize());
      data.setReflective(reflective.normalize());
        
      if (blockingObject){
        // In shadow
        if (blockingObject->getkt()>0){
          //shadowRay.setOrigin(Point(origin+(lightDir*least_w)));
          //shadowRay.setDirection(lightRay.getDirection());
          //outShadowRay = calcTrans(shadowRay,(blockingObject->getNormal()).normalize(),blockingObject->getn());
          illumination = illumination +
                          model_->illuminate(close_o,data) * blockingObject->getkt();
        }
 
      } else {
        // Not in shadow
        //std::cout<<(blockingObject->toString())<<"\n";
        lightW = close_o->intersect(lightRay);
        if (std::isfinite(lightW) || (lightW>0)){
         illumination = illumination + model_->illuminate(close_o,data);
        }
      }
      
    }
    if (depth < close_o->getDepth()){
      // Reflected Ray
      if (close_o->getkr()>0){
        Ray refRay(p,(Point()-p).reflect(normal).normalize());
        illumination = illumination +
                        (spawnrec(refRay,depth+1)
                        * close_o->getkr());
      }
      // Transmission ray
      if (close_o->getkt()>0){
        Ray inRay(p,r.getDirection());
//        std::cout<<"In: "<<inRay.toString()<<"\n";
        Ray transRay = calcTrans(inRay,normal,close_o->getn());
//        std::cout<<"Out: "<<transRay.toString()<<"\n";
        illumination = illumination +
                        (spawnrec(transRay,depth+1)
                        * close_o->getkt());
      }
    }

/*    if (useBG){
      illumination = getBgColour();
    }*/
  }
  return illumination;
}
Exemple #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;
}
Exemple #4
0
Ray RayTracer::CreateReflectRay(glm::dvec3 rDir, glm::dvec3 surfNormal, glm::dvec3 startP){
  glm::dvec3 refDir = glm::reflect(rDir,surfNormal);
  Ray refRay(startP+(surfNormal*.01),refDir,startP);
  return refRay; 
}