-
Notifications
You must be signed in to change notification settings - Fork 0
/
SceneGlossyObject.cpp
73 lines (60 loc) · 2.32 KB
/
SceneGlossyObject.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "StdAfx.h"
#include "SceneGlossyObject.h"
#include "CosineSphericalSampler.h"
Ray SceneGlossyObject::scatter(Ray& inRay) const
{
Ray outRay;
vec3f position = inRay.origin + inRay.direction*inRay.intersectDist;
LocalFrame lf = inRay.intersectObject->getAutoGenWorldLocalFrame(inRay.intersectObjectTriangleID, position);
vec3f normal = lf.n;
outRay.insideObject = inRay.insideObject;
outRay.intersectObject = NULL;
// scatter--start
outRay.origin = position;
outRay.contactObject = (SceneObject*) this;
outRay.contactObjectTriangleID = inRay.intersectObjectTriangleID;
CosineSphericalSampler cosineSphericalSampler;
outRay.direction = cosineSphericalSampler.genSample(lf);
vec3f color = bsdf.color;
float p = *std::max_element<float*>(&color.x, (&color.x)+3);
if(RandGenerator::genFloat() < p)
{
outRay.color = bsdf.evaluate(lf, -outRay.direction, -inRay.direction);
outRay.directionProb = p*cosineSphericalSampler.getProbDensity(lf, outRay.direction);
outRay.photonProb = (1-p)*cosineSphericalSampler.getProbDensity(lf, outRay.direction);
}
else
{
outRay.direction = vec3f(0, 0, 0);
outRay.color = vec3f(0, 0, 0);
outRay.directionProb = 1;
}
outRay.originSampleType = Ray::DEFINITE;
outRay.directionSampleType = Ray::RANDOM;
// scatter--end
return outRay;
}
float SceneGlossyObject::getDirectionSampleProbDensity(const Ray& inRay, const Ray& outRay) const
{
if(!outRay.contactObject)
return 0;
LocalFrame lf = outRay.contactObject->getAutoGenWorldLocalFrame(outRay.contactObjectTriangleID, outRay.origin);
vec3f color = bsdf.color;
float p = *std::max_element<float*>(&color.x, (&color.x)+3);
CosineSphericalSampler cosineSphericalSampler;
return p * cosineSphericalSampler.getProbDensity(lf, outRay.direction);
}
vec3f SceneGlossyObject::getBSDF(const Ray& inRay, const Ray& outRay) const
{
if(!outRay.contactObject)
return vec3f(0, 0, 0);
vec3f color = bsdf.color;
float p = *std::max_element<float*>(&color.x, (&color.x)+3);
LocalFrame lf = outRay.contactObject->getAutoGenWorldLocalFrame(outRay.contactObjectTriangleID, outRay.origin);
return p * bsdf.evaluate(lf, inRay.direction, outRay.direction);
}
float SceneGlossyObject::getContinueProbability(const Ray &inRay, const Ray &outRay) const{
vec3f color = bsdf.color;
float p = *std::max_element<float*>(&color.x, (&color.x)+3);
return p;
}