Added direct diffuse lighting.
This commit is contained in:
@@ -1,2 +0,0 @@
|
|||||||
# PhotonMF
|
|
||||||
An implementation of path tracing with photon mapping.
|
|
@@ -14,7 +14,9 @@ public:
|
|||||||
|
|
||||||
virtual ~Figure() { }
|
virtual ~Figure() { }
|
||||||
|
|
||||||
virtual bool intersect(Ray & r, float & t, vec3 & n) const = 0;
|
virtual bool intersect(Ray & r, float & t) const = 0;
|
||||||
|
|
||||||
|
virtual vec3 normal_at_int(Ray & r, float & t) const = 0;
|
||||||
|
|
||||||
virtual void set_color(float r, float g, float b) {
|
virtual void set_color(float r, float g, float b) {
|
||||||
color = vec3(r, g, b);
|
color = vec3(r, g, b);
|
||||||
|
14
light.hpp
14
light.hpp
@@ -4,15 +4,17 @@
|
|||||||
|
|
||||||
#include <glm/vec3.hpp>
|
#include <glm/vec3.hpp>
|
||||||
|
|
||||||
|
using glm::vec3;
|
||||||
|
|
||||||
class Light {
|
class Light {
|
||||||
public:
|
public:
|
||||||
vec3 position;
|
vec3 m_position;
|
||||||
vec3 diffuse;
|
vec3 m_diffuse;
|
||||||
vec3 specular;
|
vec3 m_specular;
|
||||||
vec3 ambient;
|
vec3 m_ambient;
|
||||||
|
|
||||||
Light(): position(vec3(0.0f)), diffuse(vec3(1.0f)), specular(vec3(1.0f)), ambient(vec3(0.1f)) { }
|
Light(): m_position(vec3(0.0f)), m_diffuse(vec3(1.0f)), m_specular(vec3(1.0f)), m_ambient(vec3(0.1f)) { }
|
||||||
Light(vec3 _p, vec3 _d, vec3 _s, vec3 _a): position(_p), diffuse(_d), specular(_s), ambient(_a) { }
|
Light(vec3 _p, vec3 _d, vec3 _s, vec3 _a): m_position(_p), m_diffuse(_d), m_specular(_s), m_ambient(_a) { }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
6
main.cpp
6
main.cpp
@@ -112,6 +112,12 @@ int main(int argc, char ** argv) {
|
|||||||
figures.push_back(static_cast<Figure *>(p));
|
figures.push_back(static_cast<Figure *>(p));
|
||||||
|
|
||||||
l = new Light();
|
l = new Light();
|
||||||
|
l->m_position = normalize(vec3(1.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_diffuse = vec3(1.0f, 1.0f, 0.0f);
|
||||||
lights.push_back(l);
|
lights.push_back(l);
|
||||||
|
|
||||||
tracer = Tracer(g_h, g_w, g_fov);
|
tracer = Tracer(g_h, g_w, g_fov);
|
||||||
|
631
output.ppm
631
output.ppm
File diff suppressed because one or more lines are too long
11
plane.cpp
11
plane.cpp
@@ -4,17 +4,20 @@
|
|||||||
|
|
||||||
#define TOL 1e-6
|
#define TOL 1e-6
|
||||||
|
|
||||||
using std::abs;
|
using std::fabs;
|
||||||
using glm::dot;
|
using glm::dot;
|
||||||
|
|
||||||
bool Plane::intersect(Ray & r, float & t, vec3 & n) const {
|
bool Plane::intersect(Ray & r, float & t) const {
|
||||||
float d = dot(r.m_direction, m_normal);
|
float d = dot(r.m_direction, m_normal);
|
||||||
|
|
||||||
if (abs(d) > TOL) {
|
if (fabs(d) > TOL) {
|
||||||
t = dot(m_normal, (m_point - r.m_origin)) / d;
|
t = dot(m_normal, (m_point - r.m_origin)) / d;
|
||||||
n = vec3(m_normal);
|
|
||||||
return t >= 0.0f;
|
return t >= 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vec3 Plane::normal_at_int(Ray & r, float & t) const {
|
||||||
|
return vec3(m_normal);
|
||||||
|
}
|
||||||
|
@@ -22,7 +22,9 @@ public:
|
|||||||
|
|
||||||
virtual ~Plane() { }
|
virtual ~Plane() { }
|
||||||
|
|
||||||
virtual bool intersect(Ray & r, float & t, vec3 & n) const;
|
virtual bool intersect(Ray & r, float & t) const;
|
||||||
|
|
||||||
|
virtual vec3 normal_at_int(Ray & r, float & t) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
10
sphere.cpp
10
sphere.cpp
@@ -4,8 +4,7 @@
|
|||||||
|
|
||||||
using glm::normalize;
|
using glm::normalize;
|
||||||
|
|
||||||
bool Sphere::intersect(Ray & r, float & t, vec3 & n) const {
|
bool Sphere::intersect(Ray & r, float & t) const {
|
||||||
vec3 i;
|
|
||||||
float d;
|
float d;
|
||||||
|
|
||||||
float a = (r.m_direction.x * r.m_direction.x) +
|
float a = (r.m_direction.x * r.m_direction.x) +
|
||||||
@@ -28,10 +27,13 @@ bool Sphere::intersect(Ray & r, float & t, vec3 & n) const {
|
|||||||
|
|
||||||
if (d >= 0.0f) {
|
if (d >= 0.0f) {
|
||||||
t = (-b - sqrt(d)) / (2 * a);
|
t = (-b - sqrt(d)) / (2 * a);
|
||||||
i = vec3(r.m_origin + (t * r.m_direction));
|
|
||||||
n = normalize(vec3((i - m_center) / m_radius));
|
|
||||||
return t >= 0.0f;
|
return t >= 0.0f;
|
||||||
|
|
||||||
} else
|
} else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vec3 Sphere::normal_at_int(Ray & r, float & t) const {
|
||||||
|
vec3 i = vec3(r.m_origin + (t * r.m_direction));
|
||||||
|
return normalize(vec3((i - m_center) / m_radius));
|
||||||
|
}
|
||||||
|
@@ -21,7 +21,9 @@ public:
|
|||||||
|
|
||||||
virtual ~Sphere() { }
|
virtual ~Sphere() { }
|
||||||
|
|
||||||
virtual bool intersect(Ray & r, float & t, vec3 & n) const;
|
virtual bool intersect(Ray & r, float & t) const;
|
||||||
|
|
||||||
|
virtual vec3 normal_at_int(Ray & r, float & t) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
33
tracer.cpp
33
tracer.cpp
@@ -1,3 +1,4 @@
|
|||||||
|
#include <iostream>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
@@ -8,8 +9,17 @@
|
|||||||
|
|
||||||
using namespace std;
|
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 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() {
|
static inline float random01() {
|
||||||
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
|
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 {
|
vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, unsigned int rec_level) const {
|
||||||
float t;
|
float t, _t, n_dot_l;
|
||||||
float _t;
|
|
||||||
Figure * _f;
|
Figure * _f;
|
||||||
vec3 n;
|
vec3 n, color, i_pos;
|
||||||
|
vec3 light_dir;
|
||||||
|
|
||||||
t = numeric_limits<float>::max();
|
t = numeric_limits<float>::max();
|
||||||
_f = NULL;
|
_f = NULL;
|
||||||
|
|
||||||
for (size_t f = 0; f < vf.size(); f++) {
|
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;
|
t = _t;
|
||||||
_f = vf[f];
|
_f = vf[f];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_f != NULL)
|
i_pos = r.m_origin + (t * r.m_direction);
|
||||||
return _f->color;
|
|
||||||
else
|
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);
|
return vec3(BCKG_COLOR);
|
||||||
}
|
}
|
||||||
|
@@ -11,7 +11,6 @@
|
|||||||
#include "ray.hpp"
|
#include "ray.hpp"
|
||||||
|
|
||||||
using std::vector;
|
using std::vector;
|
||||||
using glm::radians;
|
|
||||||
using glm::vec3;
|
using glm::vec3;
|
||||||
using glm::vec2;
|
using glm::vec2;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user