Moved area light base class to it's own file.
This commit is contained in:
86
area_light.hpp
Normal file
86
area_light.hpp
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef AREA_LIGHT_HPP
|
||||||
|
#define AREA_LIGHT_HPP
|
||||||
|
|
||||||
|
#include "light.hpp"
|
||||||
|
|
||||||
|
using glm::length;
|
||||||
|
using glm::normalize;
|
||||||
|
using glm::dot;
|
||||||
|
|
||||||
|
class AreaLight: public Light {
|
||||||
|
public:
|
||||||
|
float m_const_att;
|
||||||
|
float m_lin_att;
|
||||||
|
float m_quad_att;
|
||||||
|
Figure * m_figure;
|
||||||
|
|
||||||
|
AreaLight():
|
||||||
|
Light(AREA),
|
||||||
|
m_const_att(1.0),
|
||||||
|
m_lin_att(0.0),
|
||||||
|
m_quad_att(0.0),
|
||||||
|
m_figure(NULL),
|
||||||
|
m_last_sample(vec3()),
|
||||||
|
m_n_at_last_sample(vec3())
|
||||||
|
{ }
|
||||||
|
|
||||||
|
AreaLight(Figure * _f, float _c = 1.0, float _l = 0.0, float _q = 0.0):
|
||||||
|
Light(AREA),
|
||||||
|
m_const_att(_c),
|
||||||
|
m_lin_att(_l),
|
||||||
|
m_quad_att(_q),
|
||||||
|
m_figure(_f),
|
||||||
|
m_last_sample(vec3()),
|
||||||
|
m_n_at_last_sample(vec3())
|
||||||
|
{ }
|
||||||
|
|
||||||
|
virtual vec3 direction(vec3 point) const {
|
||||||
|
return normalize(m_last_sample - point);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual float distance(vec3 point) const {
|
||||||
|
return length(m_last_sample - point);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 diffuse(vec3 normal, Ray & r, vec3 i_pos, Material & m) const {
|
||||||
|
float d, att, ln_dot_d, d2, g;
|
||||||
|
vec3 l_dir, ref;
|
||||||
|
|
||||||
|
l_dir = normalize(direction(i_pos));
|
||||||
|
ln_dot_d = dot(m_n_at_last_sample, l_dir);
|
||||||
|
if (ln_dot_d > 0.0f) {
|
||||||
|
d2 = distance(i_pos);
|
||||||
|
d2 *= d2;
|
||||||
|
g = ln_dot_d / d2;
|
||||||
|
d = distance(i_pos);
|
||||||
|
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d)));
|
||||||
|
return (att * m.m_brdf->diffuse(l_dir, normal, r, i_pos, m_figure->m_mat->m_emission) * g) / m_figure->pdf();
|
||||||
|
|
||||||
|
} else
|
||||||
|
return vec3(0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) const {
|
||||||
|
float d, att, ln_dot_d;
|
||||||
|
vec3 l_dir, ref;
|
||||||
|
|
||||||
|
l_dir = normalize(direction(i_pos));
|
||||||
|
ln_dot_d = dot(m_n_at_last_sample, l_dir);
|
||||||
|
if (ln_dot_d > 0.0f) {
|
||||||
|
d = distance(i_pos);
|
||||||
|
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d)));
|
||||||
|
return (att * m.m_brdf->specular(l_dir, normal, r, i_pos, m_figure->m_mat->m_emission, m.m_shininess)) / m_figure->pdf();
|
||||||
|
|
||||||
|
} else
|
||||||
|
return vec3(0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void sample_at_surface(vec3 point) = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
vec3 m_last_sample;
|
||||||
|
vec3 m_n_at_last_sample;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@@ -1,44 +1,4 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <cassert>
|
|
||||||
|
|
||||||
#include "disk_area_light.hpp"
|
#include "disk_area_light.hpp"
|
||||||
#include "ray.hpp"
|
|
||||||
|
|
||||||
using glm::normalize;
|
|
||||||
using glm::dot;
|
|
||||||
|
|
||||||
vec3 DiskAreaLight::diffuse(vec3 normal, Ray & r, vec3 i_pos, Material & m) const {
|
|
||||||
float d, att, ln_dot_d, d2, g;
|
|
||||||
vec3 l_dir, ref;
|
|
||||||
|
|
||||||
l_dir = normalize(direction(i_pos));
|
|
||||||
ln_dot_d = dot(-m_n_at_last_sample, l_dir);
|
|
||||||
if (ln_dot_d > 0.0f) {
|
|
||||||
d2 = distance(i_pos);
|
|
||||||
d2 *= d2;
|
|
||||||
g = ln_dot_d / d2;
|
|
||||||
d = distance(i_pos);
|
|
||||||
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d)));
|
|
||||||
return (att * m.m_brdf->diffuse(l_dir, normal, r, i_pos, m_diffuse) * g) / m_figure->pdf();
|
|
||||||
|
|
||||||
} else
|
|
||||||
return vec3(0.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 DiskAreaLight::specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) const {
|
|
||||||
float d, att, ln_dot_d;
|
|
||||||
vec3 l_dir, ref;
|
|
||||||
|
|
||||||
l_dir = normalize(direction(i_pos));
|
|
||||||
ln_dot_d = dot(-m_n_at_last_sample, l_dir);
|
|
||||||
if (ln_dot_d > 0.0f) {
|
|
||||||
d = distance(i_pos);
|
|
||||||
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d)));
|
|
||||||
return (att * m.m_brdf->specular(l_dir, normal, r, i_pos, m_specular, m.m_shininess)) / m_figure->pdf();
|
|
||||||
|
|
||||||
} else
|
|
||||||
return vec3(0.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DiskAreaLight::sample_at_surface(vec3 point) {
|
void DiskAreaLight::sample_at_surface(vec3 point) {
|
||||||
Disk * d = static_cast<Disk *>(m_figure);
|
Disk * d = static_cast<Disk *>(m_figure);
|
||||||
|
@@ -2,24 +2,13 @@
|
|||||||
#ifndef DISK_AREA_LIGHT_HPP
|
#ifndef DISK_AREA_LIGHT_HPP
|
||||||
#define DISK_AREA_LIGHT_HPP
|
#define DISK_AREA_LIGHT_HPP
|
||||||
|
|
||||||
#include "light.hpp"
|
#include "area_light.hpp"
|
||||||
#include "disk.hpp"
|
#include "disk.hpp"
|
||||||
|
|
||||||
class DiskAreaLight: public AreaLight {
|
class DiskAreaLight: public AreaLight {
|
||||||
public:
|
public:
|
||||||
float m_const_att;
|
DiskAreaLight(Disk * _s, float _c = 1.0, float _l = 0.0, float _q = 0.0): AreaLight(static_cast<Figure *>(_s), _c, _l, _q) { }
|
||||||
float m_lin_att;
|
|
||||||
float m_quad_att;
|
|
||||||
|
|
||||||
DiskAreaLight(Disk * _s, float _c = 1.0, float _l = 0.0, float _q = 0.0):
|
|
||||||
AreaLight(static_cast<Figure *>(_s)),
|
|
||||||
m_const_att(_c),
|
|
||||||
m_lin_att(_l),
|
|
||||||
m_quad_att(_q)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
virtual vec3 diffuse(vec3 normal, Ray & r, vec3 i_pos, Material & m) const;
|
|
||||||
virtual vec3 specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) const;
|
|
||||||
virtual void sample_at_surface(vec3 point);
|
virtual void sample_at_surface(vec3 point);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
27
light.hpp
27
light.hpp
@@ -9,8 +9,6 @@
|
|||||||
#include "ray.hpp"
|
#include "ray.hpp"
|
||||||
|
|
||||||
using glm::vec3;
|
using glm::vec3;
|
||||||
using glm::length;
|
|
||||||
using glm::normalize;
|
|
||||||
|
|
||||||
class Light {
|
class Light {
|
||||||
public:
|
public:
|
||||||
@@ -53,29 +51,4 @@ public:
|
|||||||
virtual vec3 specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) const = 0;
|
virtual vec3 specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class AreaLight: public Light {
|
|
||||||
public:
|
|
||||||
Figure * m_figure;
|
|
||||||
|
|
||||||
AreaLight(): Light(AREA), m_figure(NULL), m_last_sample(vec3()), m_n_at_last_sample(vec3()) { }
|
|
||||||
|
|
||||||
AreaLight(Figure * _f): Light(AREA), m_figure(_f), m_last_sample(vec3()), m_n_at_last_sample(vec3()) { }
|
|
||||||
|
|
||||||
virtual vec3 direction(vec3 point) const {
|
|
||||||
return normalize(m_last_sample - point);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual float distance(vec3 point) const {
|
|
||||||
return length(m_last_sample - point);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual vec3 diffuse(vec3 normal, Ray & r, vec3 i_pos, Material & m) const = 0;
|
|
||||||
virtual vec3 specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) const = 0;
|
|
||||||
virtual void sample_at_surface(vec3 point) = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
vec3 m_last_sample;
|
|
||||||
vec3 m_n_at_last_sample;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "path_tracer.hpp"
|
#include "path_tracer.hpp"
|
||||||
#include "sampling.hpp"
|
#include "sampling.hpp"
|
||||||
|
#include "area_light.hpp"
|
||||||
|
|
||||||
using std::numeric_limits;
|
using std::numeric_limits;
|
||||||
using namespace glm;
|
using namespace glm;
|
||||||
|
@@ -12,7 +12,7 @@
|
|||||||
"radius": 0.25,
|
"radius": 0.25,
|
||||||
"material": {
|
"material": {
|
||||||
"diffuse": [1.0, 1.0, 1.0],
|
"diffuse": [1.0, 1.0, 1.0],
|
||||||
"rho": 0.2
|
"rho": 0.4
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@@ -1,42 +1,4 @@
|
|||||||
#include "sphere_area_light.hpp"
|
#include "sphere_area_light.hpp"
|
||||||
#include "ray.hpp"
|
|
||||||
|
|
||||||
using glm::max;
|
|
||||||
using glm::dot;
|
|
||||||
using glm::normalize;
|
|
||||||
|
|
||||||
vec3 SphereAreaLight::diffuse(vec3 normal, Ray & r, vec3 i_pos, Material & m) const {
|
|
||||||
float d, att, ln_dot_d, d2, g;
|
|
||||||
vec3 l_dir, ref;
|
|
||||||
|
|
||||||
l_dir = normalize(direction(i_pos));
|
|
||||||
ln_dot_d = dot(m_n_at_last_sample, l_dir);
|
|
||||||
if (ln_dot_d > 0.0f) {
|
|
||||||
d2 = glm::distance(m_last_sample, i_pos);
|
|
||||||
d2 *= d2;
|
|
||||||
g = ln_dot_d / d2;
|
|
||||||
d = distance(i_pos);
|
|
||||||
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d)));
|
|
||||||
return (att * m.m_brdf->diffuse(l_dir, normal, r, i_pos, m_diffuse) * g) / m_figure->pdf();
|
|
||||||
|
|
||||||
} else
|
|
||||||
return vec3(0.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 SphereAreaLight::specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) const {
|
|
||||||
float d, att, ln_dot_d;
|
|
||||||
vec3 l_dir, ref;
|
|
||||||
|
|
||||||
l_dir = normalize(direction(i_pos));
|
|
||||||
ln_dot_d = dot(m_n_at_last_sample, l_dir);
|
|
||||||
if (ln_dot_d > 0.0f) {
|
|
||||||
d = distance(i_pos);
|
|
||||||
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d)));
|
|
||||||
return (att * m.m_brdf->specular(l_dir, normal, r, i_pos, m_specular, m.m_shininess)) / m_figure->pdf();
|
|
||||||
|
|
||||||
} else
|
|
||||||
return vec3(0.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SphereAreaLight::sample_at_surface(vec3 point) {
|
void SphereAreaLight::sample_at_surface(vec3 point) {
|
||||||
Sphere * s = static_cast<Sphere *>(m_figure);
|
Sphere * s = static_cast<Sphere *>(m_figure);
|
||||||
|
@@ -2,24 +2,13 @@
|
|||||||
#ifndef SPHERE_AREA_LIGHT_HPP
|
#ifndef SPHERE_AREA_LIGHT_HPP
|
||||||
#define SPHERE_AREA_LIGHT_HPP
|
#define SPHERE_AREA_LIGHT_HPP
|
||||||
|
|
||||||
#include "light.hpp"
|
#include "area_light.hpp"
|
||||||
#include "sphere.hpp"
|
#include "sphere.hpp"
|
||||||
|
|
||||||
class SphereAreaLight: public AreaLight {
|
class SphereAreaLight: public AreaLight {
|
||||||
public:
|
public:
|
||||||
float m_const_att;
|
SphereAreaLight(Sphere * _s, float _c = 1.0, float _l = 0.0, float _q = 0.0): AreaLight(static_cast<Figure *>(_s), _c, _l, _q) { }
|
||||||
float m_lin_att;
|
|
||||||
float m_quad_att;
|
|
||||||
|
|
||||||
SphereAreaLight(Sphere * _s, float _c = 1.0, float _l = 0.0, float _q = 0.0):
|
|
||||||
AreaLight(static_cast<Figure *>(_s)),
|
|
||||||
m_const_att(_c),
|
|
||||||
m_lin_att(_l),
|
|
||||||
m_quad_att(_q)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
virtual vec3 diffuse(vec3 normal, Ray & r, vec3 i_pos, Material & m) const;
|
|
||||||
virtual vec3 specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) const;
|
|
||||||
virtual void sample_at_surface(vec3 point);
|
virtual void sample_at_surface(vec3 point);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -4,6 +4,7 @@
|
|||||||
#include <glm/gtc/constants.hpp>
|
#include <glm/gtc/constants.hpp>
|
||||||
|
|
||||||
#include "whitted_tracer.hpp"
|
#include "whitted_tracer.hpp"
|
||||||
|
#include "area_light.hpp"
|
||||||
|
|
||||||
using std::numeric_limits;
|
using std::numeric_limits;
|
||||||
using namespace glm;
|
using namespace glm;
|
||||||
|
Reference in New Issue
Block a user