Added mirror reflections.

This commit is contained in:
2016-12-27 19:22:34 -04:00
parent 1b8b382b0e
commit 414e907ee8
6 changed files with 3710 additions and 3568 deletions

View File

@@ -11,6 +11,7 @@ using glm::vec3;
class Figure {
public:
vec3 color;
float rho;
virtual ~Figure() { }

View File

@@ -18,8 +18,6 @@
using namespace std;
using namespace glm;
#define MAX_RECURSION 9
static const char * OUT_FILE = "output.ppm";
static char * input_file;
@@ -110,24 +108,28 @@ int main(int argc, char ** argv) {
s->set_color(1.0f, 1.0f, 0.0f);
figures.push_back(static_cast<Figure *>(s));
p = new Plane(vec3(0.0f, -1.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f));
p = new Plane(vec3(0.0f, -1.5f, 0.0f), vec3(0.0f, 1.0f, 0.0f));
p->set_color(1.0f, 0.5f, 0.4f);
figures.push_back(static_cast<Figure *>(p));
s = new Sphere(0.0f, 0.0f, -1.0f, 0.25f);
s->set_color(1.0f, 1.0f, 1.0f);
s->rho = 0.1f;
figures.push_back(static_cast<Figure *>(s));
s = new Sphere(-1.5f, 0.0f, -2.0f, 0.5f);
s->set_color(1.0f, 1.0f, 1.0f);
s->rho = 0.3f;
figures.push_back(static_cast<Figure *>(s));
s = new Sphere(1.5f, 0.0f, -2.0f, 0.5f);
s->set_color(1.0f, 1.0f, 1.0f);
s->rho = 0.08f;
figures.push_back(static_cast<Figure *>(s));
s = new Sphere(0.0f, 1.5f, -2.0f, 0.5f);
s->set_color(1.0f, 1.0f, 1.0f);
s->rho = 0.5f;
figures.push_back(static_cast<Figure *>(s));
l = new Light();
@@ -155,7 +157,7 @@ int main(int argc, char ** argv) {
for (int k = 0; k < g_samples; k++) {
sample = tracer.sample_pixel(i, j);
r = Ray(normalize(vec3(sample, -1.0f) - vec3(0.0f, 0.0f, 0.0f)), vec3(0.0f, 0.0f, 0.0f));
image[i][j] += tracer.trace_ray(r, figures, lights, MAX_RECURSION);
image[i][j] += tracer.trace_ray(r, figures, lights, 0);
#pragma omp critical
{
current++;

7235
output.ppm

File diff suppressed because one or more lines are too long

View File

@@ -14,11 +14,17 @@ public:
vec3 m_point;
vec3 m_normal;
Plane(): m_point(vec3(0.0f)), m_normal(0.0f, 0.0f, 1.0f) { }
Plane(): m_point(vec3(0.0f)), m_normal(0.0f, 0.0f, 1.0f) {
rho = 0.0f;
}
Plane(float x, float y, float z, float nx, float ny, float nz): m_point(vec3(x, y, z)), m_normal(normalize(vec3(nx, ny, nz))) { }
Plane(float x, float y, float z, float nx, float ny, float nz): m_point(vec3(x, y, z)), m_normal(normalize(vec3(nx, ny, nz))) {
rho = 0.0f;
}
Plane(vec3 _p, vec3 _n): m_point(_p), m_normal(normalize(_n)) { }
Plane(vec3 _p, vec3 _n): m_point(_p), m_normal(normalize(_n)) {
rho = 0.0f;
}
virtual ~Plane() { }

View File

@@ -13,9 +13,13 @@ public:
vec3 m_center;
float m_radius;
Sphere(): m_center(vec3(0.0f)), m_radius(0.5f) { }
Sphere(): m_center(vec3(0.0f)), m_radius(0.5f) {
rho = 0.0f;
}
Sphere(float x, float y, float z, float r): m_center(vec3(x, y, z)), m_radius(r) { }
Sphere(float x, float y, float z, float r): m_center(vec3(x, y, z)), m_radius(r) {
rho = 0.0f;
}
Sphere(vec3 _c, float r): m_center(_c), m_radius(r) { }

View File

@@ -9,6 +9,8 @@
#define PI 3.14159265358979f
#define SHININESS 89.0f
#define MAX_RECURSION 9
#define BIAS 0.000001f
using namespace std;
@@ -54,7 +56,7 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, uns
float t, _t, n_dot_l;
Figure * _f;
vec3 n, color, i_pos, ref;
Ray sr;
Ray sr, rr;
bool vis;
t = numeric_limits<float>::max();
@@ -72,6 +74,12 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, uns
i_pos = r.m_origin + (t * r.m_direction);
n = _f->normal_at_int(r, t);
if (vis && _f->rho > 0.0f && rec_level < MAX_RECURSION) {
rr = Ray(reflect(r.m_direction, n), i_pos + n * BIAS);
color = _f->rho * trace_ray(rr, vf, vl, rec_level + 1);
} else if (rec_level >= MAX_RECURSION)
return vec3(BCKG_COLOR);
for (size_t l = 0; l < vl.size(); l++) {
vis = true;
sr = Ray(vl[l]->m_position, i_pos);
@@ -83,6 +91,8 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, uns
}
}
color += 0.08f * BCKG_COLOR;
n_dot_l = max(dot(n, vl[l]->m_position), 0.0);
color += (vis ? 1.0f : 0.0f) * (_f->color / PI) * vl[l]->m_diffuse * n_dot_l;