-
Notifications
You must be signed in to change notification settings - Fork 0
/
radiance.h
56 lines (45 loc) · 1.62 KB
/
radiance.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef _RADIANCE_H_
#define _RADIANCE_H_
#include <algorithm>
#include "ray.h"
#include "scene.h"
#include "sphere.h"
#include "hitpoint.h"
#include "random.h"
namespace gemspt {
// ray方向からの放射輝度を求める
Color radiance(const Ray &ray, Random &random, const int depth) {
const Color kBackgroundColor = Color(0.0f, 0.0f, 0.0f);
const int kDepthLimit = 10;
// 打ち切りチェック
if (depth >= kDepthLimit)
return Color();
// シーンと交差判定
Hitpoint hitpoint;
const SceneSphere *now_object = intersect_scene(ray, &hitpoint);
// 交差チェック
if (now_object == NULL)
return kBackgroundColor;
// マテリアル取得
const Material *now_material = now_object->get_material();
const Color emission = now_material->emission();
if (emission.x > 0.0 || emission.y > 0.0 || emission.z > 0.0) {
// 光源にヒットしたら放射項だけ返して終わる。
// (今回、光源は反射率0と仮定しているため)
return emission;
}
// 次の方向をサンプリング + その方向のBRDF項の値を得る。
double pdf = -1;
Color brdf_value;
const Vec dir_out = now_material->sample(random, ray.dir, hitpoint.normal, &pdf, &brdf_value);
// cos項。
const double cost = dot(hitpoint.normal, dir_out);
// レンダリング方程式をモンテカルロ積分によって再帰的に解く。
const Color L = multiply(
brdf_value,
radiance(Ray(hitpoint.position, dir_out),random, depth + 1))
* cost / pdf;
return L;
}
};
#endif