From be3ae18a21edd71d120caca6b4b5c548f52c7998 Mon Sep 17 00:00:00 2001 From: Philip Abernethy Date: Wed, 12 Aug 2015 17:48:34 +0200 Subject: [PATCH] Fixed the transformations --- lib/geometry/sphere.cpp | 10 +++++----- main.cpp | 17 ++++++++++------- test.cpp | 6 ++++-- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/geometry/sphere.cpp b/lib/geometry/sphere.cpp index 1482eab..4d9e2a3 100644 --- a/lib/geometry/sphere.cpp +++ b/lib/geometry/sphere.cpp @@ -19,7 +19,7 @@ ray sphere::intersect(ray r) { mat4 m = this->getTransforms(); mat4 im = this->getInvTransforms(); ray tr = ray(im*r.getOrigin(), transform(im, r.getDirection())); - vec3 c = this->getPosition()-tr.getOrigin(); + vec3 c = -tr.getOrigin(); double a = std::max(0.0, dot(tr.getDirection(), c)); double b = std::sqrt(std::pow(length(c), 2)-std::pow(a, 2)); if (b < this->radius-1E-12) { @@ -29,11 +29,11 @@ ray sphere::intersect(ray r) { x = tr.getOrigin()+tr.getDirection()*(a-d); else x = tr.getOrigin()+tr.getDirection()*(a+d); - vec3 dir = x-this->getPosition(); + vec3 dir = x; + double local_len = length(x); + vec3 col = this->getColor(x[0]/local_len, x[1]/local_len); x = m*x; dir = transform(transpose(im), dir); - double local_len = length(x-this->getPosition()); - vec3 col = this->getColor(x[0]/local_len, x[1]/local_len); return ray(x, dir, col); } throw geometry_exception("No intersection"); @@ -42,7 +42,7 @@ ray sphere::intersect(ray r) { bool sphere::getShadow(vec3 origin, vec3 direction) { mat4 im = this->getInvTransforms(); ray tr = ray(im*origin, transform(im, direction)); - vec3 c = this->getPosition()-tr.getOrigin(); + vec3 c = -tr.getOrigin(); double a = dot(tr.getDirection(), c); double b = std::sqrt(std::pow(length(c), 2)-std::pow(a, 2)); return (a > 0 && b < this->radius); diff --git a/main.cpp b/main.cpp index 16e0bb5..4cf86d3 100644 --- a/main.cpp +++ b/main.cpp @@ -17,13 +17,16 @@ int main() { solid_material *red_lack = new solid_material(vec3{1, 0, 0}, lack, 0, 0, 0); std::vector scene; scene.push_back(new sphere(1.0, red_lack)); - scene[0]->translate(vec3{0, -2, -4}); - scene[0]->rotateZ(90); - //scene[0]->scale(sf); - /*scene.push_back(new sphere(1.0, red_lack)); - scene[1]->translate(vec3{0, -2, -1}); - double sf[] = {100, 0.1, 100}; - scene[1]->scale(sf);*/ + scene[0]->translate(vec3{0, -1, -7}); + scene[0]->rotateZ(30); + double sf[] = {2, 1, 1}; + scene[0]->scale(sf); + scene.push_back(new sphere(1.0, red_lack)); + scene[1]->translate(vec3{0, -4, -1}); + sf[0] = 100; + sf[1] = 0.1; + sf[2] = 100; + scene[1]->scale(sf); whitted_rt rt = whitted_rt(bgcolor, cam, lights, scene); rt.render(); png::image img(1024, 768); diff --git a/test.cpp b/test.cpp index 4daffd1..bb05c30 100644 --- a/test.cpp +++ b/test.cpp @@ -81,10 +81,12 @@ int main(int argc, char *argv[]) { sphere1.rotateZ(90); cout << "Transforms: " << sphere1.getTransforms() << endl; cout << "Inv. Transforms: " << sphere1.getInvTransforms() << endl; - cout << "Scale" << endl; + /*cout << "Scale" << endl; sf[0] = 2; sf[1] = 1; sphere1.scale(sf); cout << "Transforms: " << sphere1.getTransforms() << endl; - cout << "Inv. Transforms: " << sphere1.getInvTransforms() << endl; + cout << "Inv. Transforms: " << sphere1.getInvTransforms() << endl;*/ + cout << "M*[0 0 0]'=" << sphere1.getInvTransforms()*vec3{0, 0, 0} << endl; + cout << "M*[0 -0.2 -1]'=" << transform(sphere1.getInvTransforms(), normalise(vec3{0, -0.2, -1})) << endl; } \ No newline at end of file