-
Notifications
You must be signed in to change notification settings - Fork 3
/
MaterialDiffuse.c
54 lines (40 loc) · 1.38 KB
/
MaterialDiffuse.c
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
#include "MaterialDiffuse.h"
#include "randf.h"
#include "pi.h"
#include <math.h>
const MaterialVTable materialDiffuseVTable = (MaterialVTable) {
&materialDiffuseSampleBRDF,
&materialDiffuseBRDF,
&materialDiffuseIrradience
};
MaterialDiffuse makeMaterialDiffuse(const Color reflectivity) {
return (MaterialDiffuse) {makeMaterial(&materialDiffuseVTable), reflectivity};
}
defineAllocator(MaterialDiffuse)
Photon materialDiffuseSampleBRDF(const Material *superObject, const Intersection intersection, const Photon incoming) {
MaterialDiffuse *material = (MaterialDiffuse *) superObject;
Vector reflectedDirection = vRotated(
vRotated(intersection.normal, vTangent(intersection.normal), acosf(randf())),
intersection.normal,
randf() * 2*PI
);
return makePhoton(
makeRay(
vAdd(
intersection.position,
vsMul(intersection.normal, vEpsilon)
),
reflectedDirection
),
cMul(incoming.energy, material->reflectivity)
);
}
Color materialDiffuseBRDF(const Material *superObject, const Intersection intersection, const Vector incoming, const Vector outgoing) {
MaterialDiffuse *material = (MaterialDiffuse *) superObject;
float surfaceIllumination = fmax(0, vDot(intersection.normal, incoming));
return csMul(material->reflectivity, surfaceIllumination);
}
Color materialDiffuseIrradience(const Material *material) {
// No light source.
return makeColorBlack();
}