Skip to content

Commit

Permalink
Shadow fixed in sphere.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Abernethy committed Aug 10, 2015
1 parent 51dc6cf commit dce3c6c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 102 deletions.
10 changes: 5 additions & 5 deletions lib/geometry/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void shape::translate(vec3 t) {
0, 0, 1, -t[2],
0, 0, 0, 1
};
this->transforms *= mat4(tmp);
this->transforms = this->transforms*mat4(tmp);
this->inv_trans = mat4(itmp)*this->inv_trans;
}

Expand All @@ -53,7 +53,7 @@ void shape::scale(vec3 sf) {
0, 0, 1/sf[2], 0,
0, 0, 0, 1
};
this->transforms *= mat4(tmp);
this->transforms = this->transforms*mat4(tmp);
this->inv_trans = mat4(itmp)*this->inv_trans;
}

Expand All @@ -71,7 +71,7 @@ void shape::rotateX(double angle) {
0, sin(-angle), cos(-angle), 0,
0, 0, 0, 1
};
this->transforms *= mat4(tmp);
this->transforms = this->transforms*mat4(tmp);
this->inv_trans = mat4(itmp)*this->inv_trans;
}

Expand All @@ -89,7 +89,7 @@ void shape::rotateY(double angle) {
-sin(-angle), 0, cos(-angle), 0,
0, 0, 0, 1
};
this->transforms *= mat4(tmp);
this->transforms = this->transforms*mat4(tmp);
this->inv_trans = mat4(itmp)*this->inv_trans;
}

Expand All @@ -107,6 +107,6 @@ void shape::rotateZ(double angle) {
0, 0, 1, 0,
0, 0, 0, 1
};
this->transforms *= mat4(tmp);
this->transforms = this->transforms*mat4(tmp);
this->inv_trans = mat4(itmp)*this->inv_trans;
}
6 changes: 4 additions & 2 deletions lib/geometry/sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ ray sphere::intersect(ray r) {
}

bool sphere::getShadow(vec3 origin, vec3 direction) {
vec3 c = this->getPosition()-origin;
double a = dot(direction, c);
mat4 im = this->getInvTransforms();
ray tr = ray(im*origin, transform(im, direction));
vec3 c = this->getPosition()-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
178 changes: 89 additions & 89 deletions lib/math/mat4.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
#include "mat4.h"

mat4::mat4() {
for (int i = 0; i < 15; i += 5) {
this->a[i] = 1;
std::fill(a+i+1, a+i+4, 0);
memset(this->a, 0, 16 * sizeof(double));
for (int i = 0; i < 4; i++) {
this->a[i * 4 + i] = 1;
}
this->a[15] = 1;
}

mat4::mat4(const double *in) {
for (int i = 0; i < 16; i += 4) {
this->a[i] = in[i];
this->a[i+1] = in[i+1];
this->a[i+2] = in[i+2];
this->a[i+3] = in[i+3];
this->a[i + 1] = in[i + 1];
this->a[i + 2] = in[i + 2];
this->a[i + 3] = in[i + 3];
}
}

mat4::mat4(const mat4 &in) {
for (int i = 0; i < 16; i += 4) {
this->a[i] = in[i];
this->a[i+1] = in[i+1];
this->a[i+2] = in[i+2];
this->a[i+3] = in[i+3];
this->a[i + 1] = in[i + 1];
this->a[i + 2] = in[i + 2];
this->a[i + 3] = in[i + 3];
}
}

mat4 &mat4::operator=(const mat4 &in) {
for (int i = 0; i < 16; i += 4) {
this->a[i] = in[i];
this->a[i+1] = in[i+1];
this->a[i+2] = in[i+2];
this->a[i+3] = in[i+3];
this->a[i + 1] = in[i + 1];
this->a[i + 2] = in[i + 2];
this->a[i + 3] = in[i + 3];
}
return *this;
}
Expand All @@ -52,7 +51,7 @@ std::ostream &operator<<(std::ostream &out, const mat4 &a) {
out << "[";
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
out << a.a[i*4+j];
out << a.a[i * 4 + j];
if (j != 3)
out << ", ";
}
Expand All @@ -66,41 +65,41 @@ std::ostream &operator<<(std::ostream &out, const mat4 &a) {
mat4 operator+(const mat4 &a, const mat4 &b) {
double *tmp = new double[16];
for (int i = 0; i < 16; i += 4) {
tmp[i] = a[i]+b[i];
tmp[i+1] = a[i+1]+b[i+1];
tmp[i+2] = a[i+2]+b[i+2];
tmp[i+3] = a[i+3]+b[i+3];
tmp[i] = a[i] + b[i];
tmp[i + 1] = a[i + 1] + b[i + 1];
tmp[i + 2] = a[i + 2] + b[i + 2];
tmp[i + 3] = a[i + 3] + b[i + 3];
}
return mat4(tmp);
}

mat4 &operator+=(mat4 &a, const mat4 &b) {
for (int i = 0; i < 16; i += 4) {
a[i] += b[i];
a[i+1] += b[i+1];
a[i+2] += b[i+2];
a[i+3] += b[i+3];
a[i + 1] += b[i + 1];
a[i + 2] += b[i + 2];
a[i + 3] += b[i + 3];
}
return a;
}

mat4 operator-(const mat4 &a, const mat4 &b) {
double *tmp = new double[16];
for (int i = 0; i < 16; i += 4) {
tmp[i] = a[i]-b[i];
tmp[i+1] = a[i+1]-b[i+1];
tmp[i+2] = a[i+2]-b[i+2];
tmp[i+3] = a[i+3]-b[i+3];
tmp[i] = a[i] - b[i];
tmp[i + 1] = a[i + 1] - b[i + 1];
tmp[i + 2] = a[i + 2] - b[i + 2];
tmp[i + 3] = a[i + 3] - b[i + 3];
}
return mat4(tmp);
}

mat4 &operator-=(mat4 &a, const mat4 &b) {
for (int i = 0; i < 16; i += 4) {
a[i] -= b[i];
a[i+1] -= b[i+1];
a[i+2] -= b[i+2];
a[i+3] -= b[i+3];
a[i + 1] -= b[i + 1];
a[i + 2] -= b[i + 2];
a[i + 3] -= b[i + 3];
}
return a;
}
Expand All @@ -109,137 +108,138 @@ mat4 operator-(const mat4 &a) {
double *tmp = new double[16];
for (int i = 0; i < 16; i += 4) {
tmp[i] = -a[i];
tmp[i+1] = -a[i+1];
tmp[i+2] = -a[i+2];
tmp[i+3] = -a[i+3];
tmp[i + 1] = -a[i + 1];
tmp[i + 2] = -a[i + 2];
tmp[i + 3] = -a[i + 3];
}
return mat4(tmp);
}

mat4 operator*(const mat4 &a, const double n) {
double *tmp = new double[16];
for (int i = 0; i < 16; i += 4) {
tmp[i] = a[i]*n;
tmp[i+1] = a[i+1]*n;
tmp[i+2] = a[i+2]*n;
tmp[i+3] = a[i+3]*n;
tmp[i] = a[i] * n;
tmp[i + 1] = a[i + 1] * n;
tmp[i + 2] = a[i + 2] * n;
tmp[i + 3] = a[i + 3] * n;
}
return mat4(tmp);
}

vec4 operator*(const mat4 &a, const vec4 &b) {
double tmp[4];
for (int i = 0; i < 4; i++)
tmp[i] = a[i*4]*b[0]+a[i*4+1]*b[1]+a[i*4+2]*b[2]+a[i*4+3]*b[3];
tmp[i] = a[i * 4] * b[0] + a[i * 4 + 1] * b[1] + a[i * 4 + 2] * b[2] + a[i * 4 + 3] * b[3];
return vec4(tmp);
}

vec3 operator*(const mat4 &a, const vec3 &b) {
double tmp[3];
for (int i = 0; i < 3; i++)
tmp[i] = a[i*4]*b[0]+a[i*4+1]*b[1]+a[i*4+2]*b[2]+a[i*4+3];
tmp[i] = a[i * 4] * b[0] + a[i * 4 + 1] * b[1] + a[i * 4 + 2] * b[2] + a[i * 4 + 3];
return vec3(tmp);
}

vec4 operator*(const vec4 &a, const mat4 &b) {
double tmp[4];
for (int i = 0; i < 4; i++)
tmp[i] = a[0]*b[i]+a[1]*b[i+4]+a[2]*b[i+8]+a[3]*b[i+12];
tmp[i] = a[0] * b[i] + a[1] * b[i + 4] + a[2] * b[i + 8] + a[3] * b[i + 12];
return vec4(tmp);
}

mat4 operator*(const mat4 &a, const mat4 &b) {
double tmp[16];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
tmp[i*4+j] = a[i*4]*b[j]+a[i*4+1]*b[4+j]+a[i*4+2]*b[8+j]+a[i*4+3]*b[12+j];
tmp[i * 4 + j] =
a[i * 4] * b[j] + a[i * 4 + 1] * b[4 + j] + a[i * 4 + 2] * b[8 + j] + a[i * 4 + 3] * b[12 + j];
return mat4(tmp);
}

mat4 &operator*=(mat4 &a, const mat4 &b) {
a = a*b;
a = a * b;
return a;
}

mat4 &operator*=(mat4 &a, const double n) {
for (int i = 0; i < 16; i += 4) {
a[i] *= n;
a[i+1] *= n;
a[i+2] *= n;
a[i+3] *= n;
a[i + 1] *= n;
a[i + 2] *= n;
a[i + 3] *= n;
}
return a;
}

mat4 invert(const mat4 &a) {
double det = a[0]*a[5]*a[10]*a[15]+a[0]*a[6]*a[11]*a[13]+
a[0]*a[7]*a[9]*a[14]+a[1]*a[4]*a[11]*a[14]+
a[1]*a[6]*a[8]*a[15]+a[1]*a[7]*a[10]*a[12]+
a[2]*a[4]*a[9]*a[15]+a[2]*a[5]*a[11]*a[12]+
a[2]*a[7]*a[8]*a[13]+a[3]*a[4]*a[10]*a[13]+
a[3]*a[5]*a[8]*a[14]+a[3]*a[6]*a[9]*a[12]-
a[0]*a[5]*a[11]*a[14]-a[0]*a[6]*a[9]*a[15]-
a[0]*a[7]*a[10]*a[13]-a[1]*a[4]*a[10]*a[15]-
a[1]*a[6]*a[11]*a[12]-a[1]*a[7]*a[8]*a[14]-
a[2]*a[4]*a[11]*a[13]-a[2]*a[5]*a[8]*a[15]-
a[2]*a[7]*a[9]*a[12]-a[3]*a[4]*a[9]*a[14]-
a[3]*a[5]*a[10]*a[12]-a[3]*a[6]*a[8]*a[13];
double det = a[0] * a[5] * a[10] * a[15] + a[0] * a[6] * a[11] * a[13] +
a[0] * a[7] * a[9] * a[14] + a[1] * a[4] * a[11] * a[14] +
a[1] * a[6] * a[8] * a[15] + a[1] * a[7] * a[10] * a[12] +
a[2] * a[4] * a[9] * a[15] + a[2] * a[5] * a[11] * a[12] +
a[2] * a[7] * a[8] * a[13] + a[3] * a[4] * a[10] * a[13] +
a[3] * a[5] * a[8] * a[14] + a[3] * a[6] * a[9] * a[12] -
a[0] * a[5] * a[11] * a[14] - a[0] * a[6] * a[9] * a[15] -
a[0] * a[7] * a[10] * a[13] - a[1] * a[4] * a[10] * a[15] -
a[1] * a[6] * a[11] * a[12] - a[1] * a[7] * a[8] * a[14] -
a[2] * a[4] * a[11] * a[13] - a[2] * a[5] * a[8] * a[15] -
a[2] * a[7] * a[9] * a[12] - a[3] * a[4] * a[9] * a[14] -
a[3] * a[5] * a[10] * a[12] - a[3] * a[6] * a[8] * a[13];
double tmp[] = {
(a[5]*a[10]*a[15]+a[6]*a[11]*a[13]+a[7]*a[9]*a[14]-
a[5]*a[11]*a[14]-a[6]*a[9]*a[15]-a[7]*a[10]*a[13])/
(a[5] * a[10] * a[15] + a[6] * a[11] * a[13] + a[7] * a[9] * a[14] -
a[5] * a[11] * a[14] - a[6] * a[9] * a[15] - a[7] * a[10] * a[13]) /
det,
(a[1]*a[11]*a[14]+a[2]*a[9]*a[15]+a[3]*a[10]*a[13]-
a[1]*a[10]*a[15]-a[2]*a[11]*a[13]-a[3]*a[9]*a[14])/
(a[1] * a[11] * a[14] + a[2] * a[9] * a[15] + a[3] * a[10] * a[13] -
a[1] * a[10] * a[15] - a[2] * a[11] * a[13] - a[3] * a[9] * a[14]) /
det,
(a[1]*a[6]*a[15]+a[2]*a[7]*a[13]+a[3]*a[5]*a[14]-
a[1]*a[7]*a[14]-a[2]*a[5]*a[15]-a[3]*a[6]*a[13])/det,
(a[1]*a[7]*a[10]+a[2]*a[5]*a[11]+a[3]*a[6]*a[9]-
a[1]*a[6]*a[11]-a[2]*a[7]*a[9]-a[3]*a[5]*a[10])/det,
(a[4]*a[11]*a[14]+a[6]*a[8]*a[15]+a[7]*a[10]*a[12]-
a[4]*a[10]*a[15]-a[6]*a[11]*a[12]-a[7]*a[8]*a[14])/
(a[1] * a[6] * a[15] + a[2] * a[7] * a[13] + a[3] * a[5] * a[14] -
a[1] * a[7] * a[14] - a[2] * a[5] * a[15] - a[3] * a[6] * a[13]) / det,
(a[1] * a[7] * a[10] + a[2] * a[5] * a[11] + a[3] * a[6] * a[9] -
a[1] * a[6] * a[11] - a[2] * a[7] * a[9] - a[3] * a[5] * a[10]) / det,
(a[4] * a[11] * a[14] + a[6] * a[8] * a[15] + a[7] * a[10] * a[12] -
a[4] * a[10] * a[15] - a[6] * a[11] * a[12] - a[7] * a[8] * a[14]) /
det,
(a[0]*a[10]*a[15]+a[2]*a[11]*a[12]+a[3]*a[8]*a[14]-
a[0]*a[11]*a[14]-a[2]*a[8]*a[15]-a[3]*a[10]*a[12])/
(a[0] * a[10] * a[15] + a[2] * a[11] * a[12] + a[3] * a[8] * a[14] -
a[0] * a[11] * a[14] - a[2] * a[8] * a[15] - a[3] * a[10] * a[12]) /
det,
(a[0]*a[7]*a[14]+a[2]*a[4]*a[15]+a[3]*a[6]*a[12]-
a[0]*a[6]*a[15]-a[2]*a[7]*a[12]-a[3]*a[4]*a[14])/det,
(a[0]*a[6]*a[11]+a[2]*a[7]*a[8]+a[3]*a[4]*a[10]-
a[0]*a[7]*a[10]-a[2]*a[4]*a[11]-a[3]*a[6]*a[8])/det,
(a[4]*a[9]*a[15]+a[5]*a[11]*a[12]+a[7]*a[8]*a[13]-
a[4]*a[11]*a[13]-a[5]*a[8]*a[15]-a[7]*a[9]*a[12])/
(a[0] * a[7] * a[14] + a[2] * a[4] * a[15] + a[3] * a[6] * a[12] -
a[0] * a[6] * a[15] - a[2] * a[7] * a[12] - a[3] * a[4] * a[14]) / det,
(a[0] * a[6] * a[11] + a[2] * a[7] * a[8] + a[3] * a[4] * a[10] -
a[0] * a[7] * a[10] - a[2] * a[4] * a[11] - a[3] * a[6] * a[8]) / det,
(a[4] * a[9] * a[15] + a[5] * a[11] * a[12] + a[7] * a[8] * a[13] -
a[4] * a[11] * a[13] - a[5] * a[8] * a[15] - a[7] * a[9] * a[12]) /
det,
(a[0]*a[11]*a[13]+a[1]*a[8]*a[15]+a[3]*a[9]*a[12]-
a[0]*a[9]*a[15]-a[1]*a[11]*a[12]-a[3]*a[8]*a[13])/
(a[0] * a[11] * a[13] + a[1] * a[8] * a[15] + a[3] * a[9] * a[12] -
a[0] * a[9] * a[15] - a[1] * a[11] * a[12] - a[3] * a[8] * a[13]) /
det,
(a[0]*a[5]*a[15]+a[1]*a[7]*a[12]+a[3]*a[4]*a[13]-
a[0]*a[7]*a[13]-a[1]*a[4]*a[15]-a[3]*a[5]*a[12])/det,
(a[0]*a[7]*a[9]+a[1]*a[4]*a[11]+a[3]*a[5]*a[8]-
a[0]*a[5]*a[11]-a[1]*a[7]*a[8]-a[3]*a[4]*a[9])/det,
(a[4]*a[10]*a[13]+a[5]*a[8]*a[14]+a[6]*a[9]*a[12]-
a[4]*a[9]*a[14]-a[5]*a[10]*a[12]-a[6]*a[8]*a[13])/
(a[0] * a[5] * a[15] + a[1] * a[7] * a[12] + a[3] * a[4] * a[13] -
a[0] * a[7] * a[13] - a[1] * a[4] * a[15] - a[3] * a[5] * a[12]) / det,
(a[0] * a[7] * a[9] + a[1] * a[4] * a[11] + a[3] * a[5] * a[8] -
a[0] * a[5] * a[11] - a[1] * a[7] * a[8] - a[3] * a[4] * a[9]) / det,
(a[4] * a[10] * a[13] + a[5] * a[8] * a[14] + a[6] * a[9] * a[12] -
a[4] * a[9] * a[14] - a[5] * a[10] * a[12] - a[6] * a[8] * a[13]) /
det,
(a[0]*a[9]*a[14]+a[1]*a[10]*a[12]+a[2]*a[8]*a[13]-
a[0]*a[10]*a[13]-a[1]*a[8]*a[14]-a[2]*a[9]*a[12])/
(a[0] * a[9] * a[14] + a[1] * a[10] * a[12] + a[2] * a[8] * a[13] -
a[0] * a[10] * a[13] - a[1] * a[8] * a[14] - a[2] * a[9] * a[12]) /
det,
(a[0]*a[6]*a[13]+a[1]*a[4]*a[14]+a[2]*a[5]*a[12]-
a[0]*a[5]*a[14]-a[1]*a[6]*a[12]-a[2]*a[4]*a[13])/det,
(a[0]*a[5]*a[10]+a[1]*a[6]*a[8]+a[2]*a[4]*a[9]-
a[0]*a[6]*a[9]-a[1]*a[4]*a[10]-a[2]*a[5]*a[8])/det
(a[0] * a[6] * a[13] + a[1] * a[4] * a[14] + a[2] * a[5] * a[12] -
a[0] * a[5] * a[14] - a[1] * a[6] * a[12] - a[2] * a[4] * a[13]) / det,
(a[0] * a[5] * a[10] + a[1] * a[6] * a[8] + a[2] * a[4] * a[9] -
a[0] * a[6] * a[9] - a[1] * a[4] * a[10] - a[2] * a[5] * a[8]) / det
};
return mat4(tmp);
}

vec3 transform(const mat4 &a, const vec3 &b) {
double tmp[3];
for (int i = 0; i < 3; i++)
tmp[i] = a[i*4]*b[0]+a[i*4+1]*b[1]+a[i*4+2]*b[2];
tmp[i] = a[i * 4] * b[0] + a[i * 4 + 1] * b[1] + a[i * 4 + 2] * b[2];
return vec3(tmp);
}

mat4 transpose(const mat4 &a) {
double tmp[16];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
tmp[j*4+i] = a[i*4+j];
tmp[j * 4 + i] = a[i * 4 + j];
return mat4(tmp);
}
12 changes: 6 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ int main() {
vec3 bgcolor = vec3();
std::vector<ambient_light*> lights;
lights.push_back(new ambient_light(vec3{1, 1, 1}));
lights.push_back(new parallel_light(vec3{1, 1, 1}, normalise(vec3{0, -1, 0})));
lights.push_back(new parallel_light(vec3{1, 1, 1}, vec3{0, -1, 0}));
phong lack = phong(0.1, 0.6, 0.7, 200);
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{2, 0, -3});
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, -1, -1.5});
double sf[] = {10, 0.1, 10};
scene[1]->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);*/
whitted_rt rt = whitted_rt(bgcolor, cam, lights, scene);
rt.render();
png::image<png::rgb_pixel> img(1024, 768);
Expand Down

0 comments on commit dce3c6c

Please sign in to comment.