Better disk surface sampling.

This commit is contained in:
2017-02-12 04:28:21 -04:00
parent 85ff1de130
commit 501e2afc14
6 changed files with 49 additions and 24 deletions

View File

@@ -1,12 +1,16 @@
#include <glm/gtc/constants.hpp> #include <iostream>
#include <cassert>
#include "disk.hpp" #include "disk.hpp"
#include "sampling.hpp" #include "sampling.hpp"
using glm::vec2;
using glm::cos; using glm::cos;
using glm::sin; using glm::sin;
using glm::dot; using glm::dot;
using glm::pi; using glm::distance;
using glm::cross;
using glm::abs;
bool Disk::intersect(Ray & r, float & t) const { bool Disk::intersect(Ray & r, float & t) const {
float _t; float _t;
@@ -23,14 +27,15 @@ bool Disk::intersect(Ray & r, float & t) const {
} }
vec3 Disk::sample_at_surface() const { vec3 Disk::sample_at_surface() const {
float theta = random01() * (2.0f * pi<float>()); float theta = random01() * pi2;
float r = glm::sqrt(random01() * m_radius); float r = random01() * m_radius;
float x = r * cos(theta); vec3 nt, nb;
float y = r * sin(theta); create_coords_system(m_normal, nt, nb);
float z = 0.0f; float x = m_point.x + (r * cos(theta) * nt.x) + (r * sin(theta) * nb.x);
vec3 sample = vec3(x, y, z); float y = m_point.y + (r * cos(theta) * nt.y) + (r * sin(theta) * nb.y);
rotate_sample(sample, m_normal); float z = m_point.z + (r * cos(theta) * nt.z) + (r * sin(theta) * nb.z);
return sample;
return vec3(x, y, z);
} }
void Disk::calculate_inv_area() { void Disk::calculate_inv_area() {

View File

@@ -3,25 +3,30 @@
#define DISK_HPP #define DISK_HPP
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#include "plane.hpp" #include "plane.hpp"
using glm::vec3; using glm::vec3;
using glm::normalize; using glm::normalize;
using glm::pi;
class Disk : public Plane { class Disk : public Plane {
public: public:
float m_radius; float m_radius;
Disk(Material * mat = NULL): Plane(mat), m_radius(1.0f) { Disk(Material * mat = NULL): Plane(mat), m_radius(1.0f) {
pi2 = 2.0f * pi<float>();
calculate_inv_area(); calculate_inv_area();
} }
Disk(float x, float y, float z, float nx, float ny, float nz, float _r, Material * mat = NULL): Plane(x, y, z, nx, ny, nz, mat), m_radius(_r) { Disk(float x, float y, float z, float nx, float ny, float nz, float _r, Material * mat = NULL): Plane(x, y, z, nx, ny, nz, mat), m_radius(_r) {
pi2 = 2.0f * pi<float>();
calculate_inv_area(); calculate_inv_area();
} }
Disk(vec3 _p, vec3 _n, float _r, Material * mat = NULL): Plane(_p, _n, mat), m_radius(_r) { Disk(vec3 _p, vec3 _n, float _r, Material * mat = NULL): Plane(_p, _n, mat), m_radius(_r) {
pi2 = 2.0f * pi<float>();
calculate_inv_area(); calculate_inv_area();
} }
@@ -32,6 +37,9 @@ public:
protected: protected:
virtual void calculate_inv_area(); virtual void calculate_inv_area();
private:
float pi2;
}; };

View File

@@ -5,29 +5,39 @@
#include "ray.hpp" #include "ray.hpp"
using glm::normalize; using glm::normalize;
using glm::dot;
const float BIAS = 0.000001f;
vec3 DiskAreaLight::diffuse(vec3 normal, Ray & r, vec3 i_pos, Material & m) const { vec3 DiskAreaLight::diffuse(vec3 normal, Ray & r, vec3 i_pos, Material & m) const {
float d, att; float d, att, ln_dot_d, d2, g;
vec3 l_dir, ref; vec3 l_dir, ref;
l_dir = normalize(direction(i_pos)); l_dir = normalize(direction(i_pos));
d = distance(i_pos); ln_dot_d = dot(-m_n_at_last_sample, l_dir);
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d))); 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();
return (att * m.m_brdf->diffuse(l_dir, normal, r, i_pos, m_diffuse)) / m_figure->pdf(); } else
return vec3(0.0f);
} }
vec3 DiskAreaLight::specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) const { vec3 DiskAreaLight::specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) const {
float d, att; float d, att, ln_dot_d;
vec3 l_dir, ref; vec3 l_dir, ref;
l_dir = normalize(direction(i_pos)); l_dir = normalize(direction(i_pos));
d = distance(i_pos); ln_dot_d = dot(-m_n_at_last_sample, l_dir);
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d))); 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();
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) {

View File

@@ -13,8 +13,9 @@
} }
}, },
"sphere_area_light": { "disk_area_light": {
"position": [0.0, 1.0, -2.0], "position": [0.0, 1.0, -2.0],
"normal": [0.0, -1.0, -2.0],
"radius": 0.15, "radius": 0.15,
"material": { "material": {
"emission": [1.0, 1.0, 1.0] "emission": [1.0, 1.0, 1.0]

View File

@@ -1,6 +1,7 @@
{ {
"sphere_area_light": { "disk_area_light": {
"position": [0.0, 0.75, -1.0], "position": [0.0, 0.75, -1.0],
"normal": [0.0, -1.0, 0.0],
"radius": 0.15, "radius": 0.15,
"material": { "material": {
"emission": [1.0, 1.0, 1.0] "emission": [1.0, 1.0, 1.0]

View File

@@ -10,7 +10,7 @@ vec3 SphereAreaLight::diffuse(vec3 normal, Ray & r, vec3 i_pos, Material & m) co
vec3 l_dir, ref; vec3 l_dir, ref;
l_dir = normalize(direction(i_pos)); l_dir = normalize(direction(i_pos));
ln_dot_d = dot(-m_n_at_last_sample, l_dir); ln_dot_d = dot(m_n_at_last_sample, l_dir);
if (ln_dot_d > 0.0f) { if (ln_dot_d > 0.0f) {
d2 = glm::distance(m_last_sample, i_pos); d2 = glm::distance(m_last_sample, i_pos);
d2 *= d2; d2 *= d2;
@@ -28,7 +28,7 @@ vec3 SphereAreaLight::specular(vec3 normal, Ray & r, vec3 i_pos, Material & m) c
vec3 l_dir, ref; vec3 l_dir, ref;
l_dir = normalize(direction(i_pos)); l_dir = normalize(direction(i_pos));
ln_dot_d = dot(-m_n_at_last_sample, l_dir); ln_dot_d = dot(m_n_at_last_sample, l_dir);
if (ln_dot_d > 0.0f) { if (ln_dot_d > 0.0f) {
d = distance(i_pos); d = distance(i_pos);
att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d))); att = 1.0f / (m_const_att + (m_lin_att * d) + (m_quad_att * (d * d)));