summaryrefslogtreecommitdiff
path: root/hw2/bin
diff options
context:
space:
mode:
Diffstat (limited to 'hw2/bin')
-rw-r--r--hw2/bin/HW1.cpp38
-rw-r--r--hw2/bin/HW2.cpp112
-rw-r--r--hw2/bin/helloworld.cpp9
-rw-r--r--hw2/bin/image_compare.cpp90
4 files changed, 249 insertions, 0 deletions
diff --git a/hw2/bin/HW1.cpp b/hw2/bin/HW1.cpp
new file mode 100644
index 0000000..8e66297
--- /dev/null
+++ b/hw2/bin/HW1.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+#include "vec3d.h"
+
+int main(int argc, char** argv)
+{
+ std::cout << "CSCI-427/527 Homework 1: Vectors" << std::endl;
+
+ vec3d a(1.0f, 0.0f, -1.0f);
+ vec3d b(-1.0f, 1.0f, 1.0f);
+
+ std::cout << "a = " << a << std::endl;
+ std::cout << "b = " << b << std::endl;
+
+ std::cout << "a+b = " << a+b << std::endl;
+ std::cout << "a-b = " << a-b << std::endl;
+ std::cout << "a*b = " << a*b << std::endl;
+ std::cout << "a/b = " << a/b << std::endl;
+ std::cout << "a*2 = " << a*2.0f << std::endl;
+ std::cout << "b/2 = " << b/2.0f << std::endl;
+
+ std::cout << "a.dot(b) = " << a.dot(b) << std::endl;
+ std::cout << "b.dot(a) = " << b.dot(a) << std::endl;
+ std::cout << "a.squared_length() = " << a.squared_length() << std::endl;
+ std::cout << "b.length() = " << b.length() << std::endl;
+ std::cout << "a.distance(b) = " << a.distance(b) << std::endl;
+ std::cout << "a.cross(b) = " << a.cross(b) << std::endl;
+ std::cout << "b.cross(a) = " << b.cross(a) << std::endl;
+ std::cout << "normalize(a) = " << normalize(a) << std::endl;
+ std::cout << "abs(a) = " << abs(a) << std::endl;
+ std::cout << "b.normalize() = " << b.normalize() << std::endl;
+ std::cout << "b.abs() = " << b.abs() << std::endl;
+
+ std::cout << "a = " << a << std::endl;
+ std::cout << "b = " << b << std::endl;
+
+ // Done.
+ return 0;
+}
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;
+}
diff --git a/hw2/bin/helloworld.cpp b/hw2/bin/helloworld.cpp
new file mode 100644
index 0000000..4911d87
--- /dev/null
+++ b/hw2/bin/helloworld.cpp
@@ -0,0 +1,9 @@
+#include <iostream>
+#include <cassert>
+
+int main(int argc, char** argv)
+{
+ ///////////////////////////
+ std::cout << "Hello World." << std::endl;
+ return(0);
+}
diff --git a/hw2/bin/image_compare.cpp b/hw2/bin/image_compare.cpp
new file mode 100644
index 0000000..4f1a798
--- /dev/null
+++ b/hw2/bin/image_compare.cpp
@@ -0,0 +1,90 @@
+#include <string>
+#include <iostream>
+
+#include "util.h"
+#include "image.h"
+#include "imageIO.h"
+#include "constants.h"
+#include "errorMessage.h"
+
+int main(int argc, char** argv)
+{
+ // parse command line
+ if(argc != 3)
+ {
+ std::cout << "Usage: " << argv[0] << " <image A> <image B>" << std::endl;
+ return -1;
+ }
+
+ std::string nameA(argv[1]);
+ std::string nameB(argv[2]);
+
+ // get image extensions
+ std::string extA = getExtension(nameA);
+ std::string extB = getExtension(nameB);
+
+ // check if both images have the same extension
+ if(extA != extB) errorMessage(" => different image types (PPM vs PFM).");
+
+ // load images
+ image imgA, imgB;
+ importImage(nameA, imgA);
+ importImage(nameB, imgB);
+
+ // check if both have the same dimension
+ if(imgA.width() != imgB.width() || imgA.height() != imgB.height())
+ errorMessage(" => different images sizes (%dx%d) vs (%dx%d).", imgA.width(), imgA.height(), imgB.width(), imgB.height());
+
+ // compute the difference image
+ image diff(imgA.width(), imgA.height());
+ color avgDiff, maxDiff;
+ unsigned long diffCount = 0;
+
+ for(image::size_type y=0; y < diff.height(); y++)
+ for(image::size_type x=0; x < diff.width(); x++)
+ {
+ // per pixel difference
+ color diffColor = imgA(x,y) - imgB(x,y);
+ diff(x,y) = (extA == "ppm") ? abs(diffColor) : diffColor;
+
+ // if different => update average and max (abs value)
+ diffColor.abs();
+ if(diffColor.r > EPSILON || diffColor.g > EPSILON || diffColor.b > EPSILON)
+ {
+ diffCount++;
+ avgDiff += diffColor;
+ maxDiff = color(std::max(maxDiff.r, diffColor.r), std::max(maxDiff.g, diffColor.g), std::max(maxDiff.b, diffColor.b));
+ }
+ }
+
+ // output results if different
+ if(extA == "ppm" && diffCount != 0)
+ {
+ avgDiff /= diffCount;
+ std::cout << " => There are " << diffCount << " pixels that differ." << std::endl;
+ std::cout << " Average difference: " << avgDiff << " [base 256: " << (avgDiff * 256.0f) << "]" << std::endl;
+ std::cout << " Maximum difference: " << maxDiff << " [base 256: " << (maxDiff * 256.0f) << "]" << std::endl;
+
+ // write out difference image
+ exportImage("diff.ppm", diff);
+ }
+
+ else if(extA == "pfm" && diffCount != 0)
+ {
+ avgDiff /= diffCount;
+ std::cout << " => There are " << diffCount << " pixels that differ." << std::endl;
+ std::cout << " Average difference: " << avgDiff << std::endl;
+ std::cout << " Maximum difference: " << maxDiff << std::endl;
+
+ // write out difference image
+ exportImage("diff.pfm", diff);
+ }
+
+ // if equal, let the user know
+ else {
+ std::cout << " => Both images are equal.\r\n";
+ }
+
+ // Done.
+ return 0;
+}