summaryrefslogtreecommitdiff
path: root/hw2/bin/HW2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'hw2/bin/HW2.cpp')
-rw-r--r--hw2/bin/HW2.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/hw2/bin/HW2.cpp b/hw2/bin/HW2.cpp
new file mode 100644
index 0000000..c116f01
--- /dev/null
+++ b/hw2/bin/HW2.cpp
@@ -0,0 +1,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;
+}