コード例 #1
0
ファイル: main.cpp プロジェクト: zalcyon/raytracer
const Intersection findFirstIntersection(const Line& ray,
                                         double minDist, double maxDist) {
  Intersection intersection;

  for(int i=0; i<geometryCount; i++) {
    Intersection in = scene[i]->getIntersection(ray, minDist, maxDist);
    if(in.valid()) {
      if(!intersection.valid()) {
        intersection = in;
      }
      else if(in.t() < intersection.t()) {
        intersection = in;
      }
    }
  }

  return intersection;
}
コード例 #2
0
ファイル: main.cpp プロジェクト: candale/facultate
int main() {

  Vector viewPoint(0, 0, 0);
  Vector viewDirection(0, 0, 1);
  Vector viewUp(0, -1, 0);

  double frontPlaneDist = 2;
  double backPlaneDist = 1000;

  double viewPlaneDist = 100;
  double viewPlaneWidth = 200;
  double viewPlaneHeight = 100;

  int imageWidth = 1000;
  int imageHeight = 500;

  double lightIntesity = 0.8;

  Vector viewParallel = viewUp^viewDirection;

  viewDirection.normalize();
  viewUp.normalize();
  viewParallel.normalize();

  Image image(imageWidth, imageHeight);

  Color lightAmbient(1, 1, 1);
  Color lightDiffuse(1, 1, 1);
  Color lightSpecular(1, 1, 1);
  Material lightMat(lightAmbient, lightDiffuse, lightSpecular, 1, 1, 1);
  Vector light1(-1000, 1000, 0);

  Color ambient(0.1, 0.1, 0.1);
  Color difuse(0.3, 0.3, 0.3);
  Color specular(1, 1, 1);
  Material sphereMat(ambient, difuse, specular, 20, 0.5, 1);

  for (int i=0; i < imageWidth; i++)
    {
      double scaledi = imageToViewPlane (i, imageHeight, viewPlaneHeight);
      for (int j=0; j < imageHeight; j++)
        {
          double scaledj = imageToViewPlane (j, imageHeight, viewPlaneHeight);

          Vector x1 = viewPoint + 
            viewDirection * viewPlaneDist +
            viewUp * scaledj +
            viewParallel * scaledi;

          Line newLine =  Line(viewPoint, x1, false);
          Intersection in = findFirstIntersection(newLine, frontPlaneDist, backPlaneDist);

          if (in.valid())
            {
              // vector from the intersection point to the light
              Vector T = light - in.vec();
              T.normalize();

              // normal to the surface at the intersecction point
              Vector N = in.vec() - ((Sphere *)&in.geometry())->center();
              N.normalize();

              // vector from the intersection point to the camera
              Vector E = viewPoint - in.vec();
              E.normalize();

              // reflection vector
              Vector R = N * (N * T) * 2 - T;
              R.normalize();

              Color color = sphereMat.ambient() * lightMat.ambient() * in.geometry().color();

              // add diffused light
              if (N * T > 0) {
                color += sphereMat.diffuse() * lightMat.diffuse() * (N * T) * in.geometry().color();
              }

              // add specular light
              if (E * R > 0) {
                color += sphereMat.specular() * lightMat.specular() * pow(E * R,sphereMat.shininess()) * in.geometry().color();
              }

              color *= lightIntesity;
              image.setPixel(i,j,color );
            }
        }
    }

  image.store("scene.ppm");

  for(int i=0; i<geometryCount; i++) {
    delete scene[i];
  }

  return 0;
}
コード例 #3
0
ファイル: main.cpp プロジェクト: zalcyon/raytracer
int main(int argc, char* argv[]) {
  Vector viewPoint(0, 0, 0);
  Vector viewDirection(0, 0, 1);
  Vector viewUp(0, -1, 0);

  double frontPlaneDist = 2;
  double backPlaneDist = 1000;

  double viewPlaneDist = 90;
  double viewPlaneWidth = 160;
  double viewPlaneHeight = 90;

  int imageWidth = atoi(argv[1]);
  int imageHeight = atoi(argv[2]);

  Vector viewParallel = viewUp^viewDirection;

  viewDirection.normalize();
  viewUp.normalize();
  viewParallel.normalize();

  Image image(imageWidth, imageHeight);

  Color c;

  //Color only
  /*for (int ix = 0; ix < imageWidth; ix++)
  {
	  for (int iy = 0; iy < imageHeight; iy++)
	  {
		  Vector x0 = viewPoint;
		  Vector x1 = viewPoint + viewDirection*viewPlaneDist + viewUp*imageToViewPlane(iy, imageHeight, viewPlaneHeight)s + viewParallel*imageToViewPlane(ix, imageWidth, viewPlaneWidth);
		  Line ray = Line(x0, x1, false);
		  Intersection i = findFirstIntersection(ray, frontPlaneDist, backPlaneDist);
		  if (i.valid())
		  {
			  c = i.geometry().color();
			  image.setPixel(ix, iy, c);
		  }
	  }
  }*/

  Light l = Light(Vector(0,70,150), Color(1,1,1), Color(1, 1, 1), Color(1,1,1), 1);

  for (int ix = 0; ix < imageWidth; ix++)
  {
	  for (int iy = 0; iy < imageHeight; iy++)
	  {
		  Vector x0 = viewPoint;
		  Vector x1 = viewPoint + viewDirection*viewPlaneDist + viewUp*imageToViewPlane(iy, imageHeight, viewPlaneHeight) + viewParallel*imageToViewPlane(ix, imageWidth, viewPlaneWidth);
		  Line ray = Line(x0, x1, false);
		  Intersection i = findFirstIntersection(ray, frontPlaneDist, backPlaneDist);
		  if (i.valid())
		  {
			  Sphere* s = (Sphere*)i.sphere();
			  Material m = i.geometry().material();
			  Vector L = l.position();
			  Vector C = viewPoint;
			  Vector V = i.vec();
			  Vector S = s->center();
			  Vector N = V - S;
			  N.normalize();
			  Vector T = L - V;
			  T.normalize();
			  Vector E = C - V;
			  E.normalize();
			  Vector R = N*(N*T) * 2 - T;
			  R.normalize();
			  //printf("%f %f, %f\n", S.x(), S.y(), S.z());

			  c = m.ambient() * l.ambient();
			  if (N*T > 0)
			  {
				  c += m.diffuse() * l.diffuse() * (N*T);
			  }
			  if (E*R > 0)
			  {
				  c += m.specular() * l.specular() * pow(E*R, m.shininess());
			  }
			  c *= l.intensity();

			  image.setPixel(ix, iy, c);
		  }
	  }
  }

  image.store("scene.ppm");

  for(int i=0; i<geometryCount; i++) {
    delete scene[i];
  }

  return 0;
}