Added shadow rays.
This commit is contained in:
@@ -13,7 +13,7 @@ public:
|
||||
vec3 m_specular;
|
||||
vec3 m_ambient;
|
||||
|
||||
Light(): m_position(vec3(0.0f)), m_diffuse(vec3(1.0f)), m_specular(vec3(1.0f)), m_ambient(vec3(0.1f)) { }
|
||||
Light(): m_position(vec3(0.0f)), m_diffuse(vec3(1.0f)), m_specular(vec3(1.0f)), m_ambient(vec3(0.05f)) { }
|
||||
Light(vec3 _p, vec3 _d, vec3 _s, vec3 _a): m_position(_p), m_diffuse(_d), m_specular(_s), m_ambient(_a) { }
|
||||
};
|
||||
|
||||
|
45
main.cpp
45
main.cpp
@@ -1,4 +1,5 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -38,8 +39,10 @@ int main(int argc, char ** argv) {
|
||||
vector<Figure *> figures;
|
||||
vector<Light *> lights;
|
||||
Tracer tracer;
|
||||
int total;
|
||||
int current = 0;
|
||||
|
||||
if(argc < 2 || argc == 3 || argc > 6) {
|
||||
if(argc < 2 || argc == 3 || argc > 7) {
|
||||
cerr << "USAGE: " << argv[0] << " IN FILE [OUT FILE [HEIGHT WIDTH [SAMPLES [FIELD OF VIEW]]]]" << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
@@ -108,32 +111,64 @@ int main(int argc, char ** argv) {
|
||||
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->set_color(0.0f, 1.0f, 1.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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
figures.push_back(static_cast<Figure *>(s));
|
||||
|
||||
l = new Light();
|
||||
l->m_position = normalize(vec3(1.0f, 1.0f, 1.0f));
|
||||
l->m_diffuse = vec3(0.0f, 1.0f, 1.0f);
|
||||
lights.push_back(l);
|
||||
|
||||
l = new Light();
|
||||
l->m_position = normalize(vec3(-1.0f, 0.0f, 0.0f));
|
||||
l->m_position = normalize(vec3(-1.0f, 1.0f, 1.0f));
|
||||
l->m_diffuse = vec3(1.0f, 1.0f, 0.0f);
|
||||
lights.push_back(l);
|
||||
|
||||
l = new Light();
|
||||
l->m_position = normalize(vec3(0.0f, 1.0f, -1.0f));
|
||||
l->m_diffuse = vec3(1.0f, 0.0f, 1.0f);
|
||||
lights.push_back(l);
|
||||
|
||||
tracer = Tracer(g_h, g_w, g_fov);
|
||||
|
||||
#pragma omp parallel for schedule(dynamic, 1) private(r, sample)
|
||||
total = g_h * g_w * g_samples;
|
||||
|
||||
#pragma omp parallel for schedule(dynamic, 1) private(r, sample) shared(current)
|
||||
for (int i = 0; i < g_h; i++) {
|
||||
for (int j = 0; j < g_w; j++) {
|
||||
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);
|
||||
#pragma omp critical
|
||||
{
|
||||
current++;
|
||||
}
|
||||
}
|
||||
|
||||
image[i][j] /= g_samples;
|
||||
}
|
||||
#pragma omp critical
|
||||
{
|
||||
cout << "\r" << setw(3) << static_cast<int>((static_cast<float>(current) / static_cast<float>(total)) * 100.0f) << "% done";
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
for (size_t i = 0; i < figures.size(); i++) {
|
||||
delete figures[i];
|
||||
|
1807
output.ppm
1807
output.ppm
File diff suppressed because one or more lines are too long
@@ -5,6 +5,7 @@
|
||||
using glm::normalize;
|
||||
|
||||
bool Sphere::intersect(Ray & r, float & t) const {
|
||||
float t1, t2;
|
||||
float d;
|
||||
|
||||
float a = (r.m_direction.x * r.m_direction.x) +
|
||||
@@ -26,7 +27,9 @@ bool Sphere::intersect(Ray & r, float & t) const {
|
||||
d = (b * b) - (4 * a * c);
|
||||
|
||||
if (d >= 0.0f) {
|
||||
t = (-b - sqrt(d)) / (2 * a);
|
||||
t1 = (-b - sqrt(d)) / (2 * a);
|
||||
t2 = (-b + sqrt(d)) / (2 * a);
|
||||
t = t1 < t2 ? t1 : t2;
|
||||
return t >= 0.0f;
|
||||
|
||||
} else
|
||||
|
28
tracer.cpp
28
tracer.cpp
@@ -7,6 +7,8 @@
|
||||
|
||||
#include "tracer.hpp"
|
||||
|
||||
#define PI 3.14159265358979f
|
||||
|
||||
using namespace std;
|
||||
|
||||
using std::numeric_limits;
|
||||
@@ -45,10 +47,12 @@ vec2 Tracer::sample_pixel(int i, int j) const {
|
||||
}
|
||||
|
||||
vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, unsigned int rec_level) const {
|
||||
size_t f_index;
|
||||
float t, _t, n_dot_l;
|
||||
Figure * _f;
|
||||
vec3 n, color, i_pos;
|
||||
vec3 light_dir;
|
||||
Ray sr;
|
||||
bool vis;
|
||||
|
||||
t = numeric_limits<float>::max();
|
||||
_f = NULL;
|
||||
@@ -57,17 +61,27 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, uns
|
||||
if (vf[f]->intersect(r, _t) && _t < t) {
|
||||
t = _t;
|
||||
_f = vf[f];
|
||||
f_index = f;
|
||||
}
|
||||
}
|
||||
|
||||
i_pos = r.m_origin + (t * r.m_direction);
|
||||
|
||||
if (_f != NULL) {
|
||||
i_pos = r.m_origin + (t * r.m_direction);
|
||||
n = _f->normal_at_int(r, t);
|
||||
|
||||
for (size_t l = 0; l < vl.size(); l++) {
|
||||
n = _f->normal_at_int(r, t);
|
||||
light_dir = vl[l]->m_position;
|
||||
n_dot_l = max(dot(n, light_dir), 0.0);
|
||||
color += (_f->color / 3.1416f) * vl[l]->m_diffuse * n_dot_l;
|
||||
vis = true;
|
||||
sr = Ray(vl[l]->m_position, i_pos);
|
||||
|
||||
for (size_t f = 0; f < vf.size(); f++) {
|
||||
if (f != f_index && vf[f]->intersect(sr, _t)) {
|
||||
vis = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return color;
|
||||
|
Reference in New Issue
Block a user