-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRefractiveMaterial.cpp
105 lines (88 loc) · 2.94 KB
/
RefractiveMaterial.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "StdAfx.h"
#include "RefractiveMaterial.h"
Ray RefractiveMaterial::scatter(const SceneObject* object, const Ray& inRay, const bool russian) const
{
Ray outRay;
vec3f position = inRay.origin + inRay.direction*inRay.intersectDist;
//==== FIX ME =====
if (inRay.intersectObject == NULL)
{
outRay.direction = vec3f(0.f);
outRay.contactObject = NULL;
outRay.contactObjectTriangleID = -1;
outRay.directionSampleType = Ray::DEFINITE;
return outRay;
}
//=================
LocalFrame lf = inRay.intersectObject->getAutoGenWorldLocalFrame(inRay.intersectObjectTriangleID, position);
vec3f normal = lf.n;
// scatter--start
outRay.color = surfColor;
outRay.origin = position;
outRay.direction = inRay.direction;
vec3f reflDir = -normal.dot(inRay.direction)*normal*2 + inRay.direction;
outRay.contactObject = (SceneObject*)object;
outRay.contactObjectTriangleID = inRay.intersectObjectTriangleID;
outRay.directionSampleType = Ray::DEFINITE;
if(refrCoeff < 0)
{
outRay.direction = reflDir;
outRay.insideObject = inRay.insideObject;
outRay.directionProb = 1;
outRay.color /= outRay.getCosineTerm();
return outRay;
}
if(inRay.intersectObject != object && inRay.insideObject == object)
{
outRay = inRay.intersectObject->scatter(inRay);
return outRay;
}
float theta = acos(clampf(inRay.direction.dot(normal), -1, 1));
SceneObject* currentInsideObject = inRay.insideObject;
SceneObject* outSideObject = (SceneObject*) object;
if(inRay.insideObject == object)
outSideObject = object->findInsideObject(outRay);
float current_n = currentInsideObject ? currentInsideObject->getRefrCoeff() : 1;
float next_n = outSideObject ? outSideObject->getRefrCoeff() : 1;
outRay.intersectObject = NULL;
outRay.directionProb = 1;
float sin_phi = current_n / next_n * sin(theta);
if(sin_phi >= 1)
{
outRay.direction = reflDir;
outRay.insideObject = inRay.insideObject;
outRay.directionProb = 1;
outRay.color /= outRay.getCosineTerm();
}
else
{
float phi = asin(sin_phi);
if(theta > M_PI/2)
phi = M_PI - phi;
vec3f axis = normal.cross(inRay.direction);
axis.normalize();
outRay.direction = vec3f(rotMat(axis, phi) * vec4<float>(normal, 0));
outRay.direction.normalize();
float cos_theta = abs(cos(theta));
float cos_phi = abs(cos(phi));
float esr = powf(abs(current_n*cos_theta-next_n*cos_phi)/(current_n*cos_theta+next_n*cos_phi),2);
float epr = powf(abs(next_n*cos_theta-current_n*cos_phi)/(next_n*cos_theta+current_n*cos_phi),2);
float er = (esr+epr)/2;
float p = clampf(er , 0.f , 1.f);
if(RandGenerator::genFloat() < p)
{
outRay.direction = reflDir;
outRay.color *= er / outRay.getCosineTerm();
outRay.photonProb = outRay.directionProb = p;
outRay.insideObject = inRay.insideObject;
}
else
{
outRay.color *= (1-er) / outRay.getCosineTerm();
outRay.photonProb = outRay.directionProb = 1-p;
outRay.insideObject = outSideObject;
}
}
outRay.direction.normalize();
return outRay;
}