summaryrefslogtreecommitdiff
path: root/hw6/bin/HW2.cpp
blob: 247d7d136685340e6ab9423a2ac9efe8bbf6189b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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;
}