summaryrefslogblamecommitdiff
path: root/hw6/bin/HW2.cpp
blob: 247d7d136685340e6ab9423a2ac9efe8bbf6189b (plain) (tree)















































































































                                                                                        
#include <iostream>

#include "image.h"
#include "camera.h"
#include "imageIO.h"
#include "triangle.h"

image generateImage(const camera& cam, const triangle& tri)
{
  float t;
  vec3d barycentricCoord;
  image result(cam.width(), cam.height());
  
  // for each pixel
  for(image::size_type y=0; y < result.height(); y++)
    for(image::size_type x=0; x < result.width(); x++)
    {
      // generate view ray
      ray r = cam(x,y);

      // intersect triangle
      bool hit = tri.intersect(r, barycentricCoord, t);

      // draw pixel
      if(hit)
	result(x,y) = color(barycentricCoord.x, barycentricCoord.y, barycentricCoord.z);
      else 
	result(x,y) = color(0.0f, 0.0f, 0.0f);
    }

  // Done.
  return result;
}

int main(int argc, char** argv)
{
  // setup camera
  camera cam( vec3d(0.0f, 0.0f, 0.0f),
	      vec3d(0.0f, 0.0f, -1.0f),
	      vec3d(0.0f, 1.0f, 0.0f),
	      60.0f,
	      512, 512 );
  
  // Image 1
  std::cout << "Generating image 1." << std::endl;
  
  triangle t1( vec3d(1.0f, -1.0f, -2.0f),
	       vec3d(0.0f, 1.0f, -2.0f),
	       vec3d(-1.0f, -1.0f, -2.0f) );

  image result1 = generateImage(cam, t1);
  exportImage("HW2-result1.ppm", result1);


  // Image 2
  std::cout << "Generating image 2." << std::endl;

  triangle t2( vec3d(1.0f, -1.0f, 2.0f),
	       vec3d(0.0f, 1.0f, 2.0f),
	       vec3d(-1.0f, -1.0f, 2.0f) );  

  image result2 = generateImage(cam, t2);
  exportImage("HW2-result2.ppm", result2);


  // Image 3
  std::cout << "Generating image 3." << std::endl;

  triangle t3( vec3d(-1.0f, -1.0f, -2.0f),
	       vec3d(1.0f, -1.0f, -2.0f),
	       vec3d(0.0f, 1.0f, -2.0f) );  

  image result3 = generateImage(cam, t3);
  exportImage("HW2-result3.ppm", result3);


  // Image 4
  std::cout << "Generating image 4." << std::endl;

  triangle t4( vec3d(-1.0f, -1.0f, 2.0f),
	       vec3d(0.0f, 1.0f, 2.0f),
	       vec3d(1.0f, -1.0f, 2.0f) );  

  image result4 = generateImage(cam, t4);
  exportImage("HW2-result4.ppm", result4);


  // Image 5
  std::cout << "Generating image 5." << std::endl;

  triangle t5( vec3d(-1.0f, -1.0f, -1.0f),
	       vec3d(1.0f, -1.0f, -1.0f),
	       vec3d(1.0f, 1.0f, -1.0f) );  

  image result5 = generateImage(cam, t5);
  exportImage("HW2-result5.ppm", result5);


  // Image 6
  std::cout << "Generating image 6." << std::endl;

  triangle t6( vec3d(-1.0f, 2.0f, -1.0f),
	       vec3d(0.0f, 2.0f, 1.0f),
	       vec3d(1.0f, 2.0f, -1.0f) );  

  image result6 = generateImage(cam, t6);
  exportImage("HW2-result6.ppm", result6);


  // Done.
  return 0;
}