-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffuseMaterial.cpp
110 lines (92 loc) · 3.55 KB
/
DiffuseMaterial.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
105
106
107
108
109
110
#include "StdAfx.h"
#include "DiffuseMaterial.h"
Ray DiffuseMaterial::scatter(const SceneObject* object, const Ray& inRay, const bool russian) const
{
Ray outRay;
outRay.origin = 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;
printf("error in diffuse scatter\n");
return outRay;
}
//=================
LocalFrame lf = inRay.intersectObject->getAutoGenWorldLocalFrame(inRay.intersectObjectTriangleID, outRay.origin);
vec3f color = tex.getColor(inRay.intersectObject->getTexCoord(inRay.intersectObjectTriangleID, outRay.origin));
vec3f normal = lf.n;
outRay.insideObject = inRay.insideObject;
outRay.intersectObject = NULL;
// scatter--start
outRay.contactObject = (SceneObject*) object;
outRay.contactObjectTriangleID = inRay.intersectObjectTriangleID;
if(inRay.direction.dot(normal)>0) // leaving
{
outRay.direction = inRay.direction;
outRay.directionProb = 1;
outRay.color = vec3f(1, 1, 1);
outRay.photonProb = 1;
outRay.originSampleType = Ray::DEFINITE;
outRay.directionSampleType = Ray::DEFINITE;
return outRay;
}
outRay.direction = cosineSphericalSampler.genSample(lf);
float p = *std::max_element<float*>(&color.x, (&color.x)+3);
if(RandGenerator::genFloat() < p || (!russian))
{
outRay.color = bsdf.evaluate(lf, inRay.direction, outRay.direction, color);
if (russian)
{
outRay.directionProb = p*cosineSphericalSampler.getProbDensity(lf, outRay.direction);
outRay.photonProb = (1-p)*cosineSphericalSampler.getProbDensity(lf, outRay.direction);
}
else
{
outRay.directionProb = cosineSphericalSampler.getProbDensity(lf , outRay.direction);
outRay.photonProb = cosineSphericalSampler.getProbDensity(lf , outRay.direction);
}
}
else
{
outRay.direction = vec3f(0, 0, 0);
outRay.color = vec3f(0, 0, 0);
outRay.directionProb = 1 - p;
}
outRay.originSampleType = Ray::DEFINITE;
outRay.directionSampleType = Ray::RANDOM;
// scatter--end
return outRay;
}
float DiffuseMaterial::getDirectionSampleProbDensity(const Ray& inRay, const Ray& outRay) const
{
if(!outRay.contactObject)
return 0;
LocalFrame lf = outRay.contactObject->getAutoGenWorldLocalFrame(outRay.contactObjectTriangleID, outRay.origin);
vec3f color = tex.getColor(outRay.contactObject->getTexCoord(outRay.contactObjectTriangleID, outRay.origin));
float p = *std::max_element<float*>(&color.x, (&color.x)+3);
return p * cosineSphericalSampler.getProbDensity(lf, outRay.direction);
}
float DiffuseMaterial::getContinueProbability(const Ray &inRay, const Ray &outRay) const
{
vec3f color = tex.getColor(outRay.contactObject->getTexCoord(outRay.contactObjectTriangleID, outRay.origin));
float p = *std::max_element<float*>(&color.x, (&color.x)+3);
return p;
}
vec3f DiffuseMaterial::getBSDF(const Ray& inRay, const Ray& outRay) const
{
if(!outRay.contactObject)
return vec3f(0, 0, 0);
vec3f color = tex.getColor(outRay.contactObject->getTexCoord(outRay.contactObjectTriangleID, outRay.origin));
LocalFrame lf = outRay.contactObject->getAutoGenWorldLocalFrame(outRay.contactObjectTriangleID, outRay.origin);
return bsdf.evaluate(lf, inRay.direction, outRay.direction, color);
}
void DiffuseMaterial::loadMaterialFromXML(const ConfigManager* cm, xml_node<>* node)
{
if(node->first_node("color"))
tex.setColor(readVec(node->first_node("color")->value()));
if(node->first_node("Texture"))
tex.loadTextureFromXML(cm, node->first_node("Texture"));
}