Specular component added to lighting.

This commit is contained in:
2016-12-27 15:26:49 -04:00
parent ab427597fe
commit 1b8b382b0e
3 changed files with 3337 additions and 1099 deletions

4419
output.ppm

File diff suppressed because one or more lines are too long

View File

@@ -8,6 +8,7 @@
#include "tracer.hpp" #include "tracer.hpp"
#define PI 3.14159265358979f #define PI 3.14159265358979f
#define SHININESS 89.0f
using namespace std; using namespace std;
@@ -15,6 +16,8 @@ using std::numeric_limits;
using glm::normalize; using glm::normalize;
using glm::radians; using glm::radians;
using glm::dot; using glm::dot;
using glm::reflect;
using glm::clamp;
static const vec3 BCKG_COLOR = vec3(0.16f, 0.66f, 0.72f); static const vec3 BCKG_COLOR = vec3(0.16f, 0.66f, 0.72f);
@@ -26,10 +29,10 @@ static inline float random01() {
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
} }
Tracer::Tracer(): m_h(640), m_w(480), m_fov(90.0f), m_a_ratio(640.0f / 480.0f) {} Tracer::Tracer(): m_h(480), m_w(640), m_fov(90.0f), m_a_ratio(640.0f / 480.0f) {}
Tracer::Tracer(int h, int w, float fov): m_h(h), m_w(w), m_fov(fov) { Tracer::Tracer(int h, int w, float fov): m_h(h), m_w(w), m_fov(fov) {
m_a_ratio = static_cast<float>(w) / h; m_a_ratio = static_cast<float>(w) / static_cast<float>(h);
} }
vec2 Tracer::sample_pixel(int i, int j) const { vec2 Tracer::sample_pixel(int i, int j) const {
@@ -50,7 +53,7 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, uns
size_t f_index; size_t f_index;
float t, _t, n_dot_l; float t, _t, n_dot_l;
Figure * _f; Figure * _f;
vec3 n, color, i_pos; vec3 n, color, i_pos, ref;
Ray sr; Ray sr;
bool vis; bool vis;
@@ -82,9 +85,12 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, uns
n_dot_l = max(dot(n, vl[l]->m_position), 0.0); 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; color += (vis ? 1.0f : 0.0f) * (_f->color / PI) * vl[l]->m_diffuse * n_dot_l;
ref = reflect(vl[l]->m_position, n);
color += (vis ? 1.0f : 0.0f) * vl[l]->m_specular * pow(max(dot(ref, r.m_direction), 0.0f), SHININESS);
} }
return color; return clamp(color, 0.0f, 1.0f);
} else } else
return vec3(BCKG_COLOR); return vec3(BCKG_COLOR);
} }

View File

@@ -22,7 +22,8 @@ public:
float m_a_ratio; float m_a_ratio;
Tracer(); Tracer();
Tracer(int w, int h, float fov);
Tracer(int h, int w, float fov);
vec2 sample_pixel(int i, int j) const; vec2 sample_pixel(int i, int j) const;
vec3 trace_ray(Ray & r, vector<Figure *> & f, vector<Light *> & l, unsigned int rec_level) const; vec3 trace_ray(Ray & r, vector<Figure *> & f, vector<Light *> & l, unsigned int rec_level) const;