Added direct diffuse lighting.

This commit is contained in:
2016-12-26 19:14:21 -04:00
parent 9ad63d489a
commit d48e8e0ba3
11 changed files with 566 additions and 154 deletions

View File

@@ -1,3 +1,4 @@
#include <iostream>
#include <limits>
#include <cstdlib>
#include <cmath>
@@ -8,8 +9,17 @@
using namespace std;
using std::numeric_limits;
using glm::normalize;
using glm::radians;
using glm::dot;
static const vec3 BCKG_COLOR = vec3(0.16f, 0.66f, 0.72f);
static inline float max(float a, float b) {
return a >= b ? a : b;
}
static inline float random01() {
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}
@@ -35,23 +45,32 @@ 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 {
float t;
float _t;
float t, _t, n_dot_l;
Figure * _f;
vec3 n;
vec3 n, color, i_pos;
vec3 light_dir;
t = numeric_limits<float>::max();
_f = NULL;
for (size_t f = 0; f < vf.size(); f++) {
if (vf[f]->intersect(r, _t, n) && _t < t) {
if (vf[f]->intersect(r, _t) && _t < t) {
t = _t;
_f = vf[f];
}
}
if (_f != NULL)
return _f->color;
else
i_pos = r.m_origin + (t * r.m_direction);
if (_f != NULL) {
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;
}
return color;
} else
return vec3(BCKG_COLOR);
}