Skip to content

Commit

Permalink
Fixed the transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Abernethy committed Aug 12, 2015
1 parent dce3c6c commit be3ae18
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
10 changes: 5 additions & 5 deletions lib/geometry/sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand All @@ -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);
Expand Down
17 changes: 10 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ int main() {
solid_material *red_lack = new solid_material(vec3{1, 0, 0}, lack, 0, 0, 0);
std::vector<shape*> 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<png::rgb_pixel> img(1024, 768);
Expand Down
6 changes: 4 additions & 2 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit be3ae18

Please sign in to comment.