Exemplo n.º 1
0
void render(Scene s, PpmCanvas *canvas) {
  printf("\nComputing field of view\n");
  float fovRadians, halfWidth, halfHeight;
  float previousfraction, currentfraction,pixelWidth, pixelHeight, width, height;
  int  ix, iy;
  Ray eye = initRay();
  Vector vpRight, vpUp, UP;

  fovRadians = PI * (s.fieldOfView / 2.0) / 180.0;
  halfWidth = tan(fovRadians);
  halfHeight = 0.75 * halfWidth;



  width = halfWidth * 2;
  height = halfHeight * 2;
  pixelWidth = width / ((*canvas).width -1);
  pixelHeight = height / ((*canvas).height -1);
  printf("\n\nfieldOfView: %f \nfovRadians: %f \nhalfwidth: %f \n height: %f ",s.fieldOfView,fovRadians,halfWidth, halfHeight);
  printf ("\n\npixelHeight:%f, pixelWidth:%f", pixelHeight, pixelWidth);
  UP.x = 0;
  UP.y = 1;
  UP.z = 0;

  eye.point = s.position;
  eye.vector = normalized(sub(s.lookingAt, s.position));
  vpRight = normalized(cross(eye.vector,UP));
  vpUp = normalized(cross(vpRight, eye.vector));

  printf("\nLooping over pixels\n");
  previousfraction = 0;
  for (iy=0; iy<(*canvas).height; iy++){
    currentfraction = iy / (*canvas).height;
    if ((currentfraction - previousfraction)> 0.05){
      save(canvas);
      printf("\n%f complete", (currentfraction * 100));
      previousfraction = currentfraction;
    }
    for (ix=0; ix<(*canvas).width; ix++  ){
      Vector xcomp, ycomp, bothcomp, colour;
      Ray ray = initRay();
      xcomp = scale(vpRight, ix * pixelWidth - halfWidth);
      ycomp = scale(vpUp, iy * pixelHeight - halfHeight);
      bothcomp = add(xcomp, ycomp);
      ray.point = eye.point;
      ray.vector = add(add(eye.vector, xcomp), ycomp);
      colour = rayColour(&s, &ray);
      (*canvas).colours[ix][iy].red = colour.x * 255;
      (*canvas).colours[ix][iy].green = colour.y * 255;
      (*canvas).colours[ix][iy].blue = colour.z * 255;
    }
  }
}
Exemplo n.º 2
0
void Grid::castRay(const Vec3r& orig,
		   const Vec3r& end,
		   OccludersSet& occluders,
		   unsigned timestamp) {
  //  printf("inGrid = %s, orig = %f %f %f\n", inGrid(orig) ? "TRUE" : "FALSE", orig[0], orig[1], orig[2]);
  

  initRay(orig, end, timestamp);
  allOccludersGridVisitor visitor(occluders);
  castRayInternal(visitor);
}
Exemplo n.º 3
0
Vector colorAt(SimpleSurface *s, Scene *scene, Ray ray, Vector *p, Vector normal){
  Vector b, c;
  double lambCoef = (*s).lambertCoefficient;
  b = baseColorAt(s, p);
  c.x = 0.0;
  c.y = 0.0;
  c.z = 0.0;
  //printf("lambert: %f\n", lambCoef);
  if ((*s).specularCoefficient > 0) {
    Ray reflectedRay = initRay();
    Vector reflectedColour;
    reflectedRay.point = (*p);
    reflectedRay.vector = reflectThru(ray.vector, normal);
    reflectedRay.recursionDepth = ray.recursionDepth;
    reflectedColour = rayColour(scene, &reflectedRay);
    c = addColours(c, (*s).specularCoefficient, reflectedColour);
    //reflectedColour.x = 0.123;
    //reflectedColour.y = 0.78123;
    //reflectedColour.z = 0.9887123;
    //c = addColours(c, 0.345, reflectedColour);

  }
  if ((*s).lambertCoefficient > 0){
    double lambertAmount = 0;
    int il;
    for (il=0; il<(*scene).lightPointsSize; il++){
      double contribution = dot(normalized(sub((*scene).lightPoints[il],(*p))),normal);
      if (contribution > 0) {
	lambertAmount = lambertAmount + contribution;
      }
    }
    lambertAmount = ( 1 > lambertAmount ) ? lambertAmount : 1;
    //printf("lambert: %f %f\n",lambertAmount, (*s).lambertCoefficient);
    c = addColours(c, (*s).lambertCoefficient * lambertAmount , (*s).baseColour);
  }
  return c;
};