-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSceneObjectUnitPlane.c
104 lines (69 loc) · 3.08 KB
/
SceneObjectUnitPlane.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
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 "SceneObjectUnitPlane.h"
#include "randf.h"
#include <math.h>
const SceneObjectVTable sceneObjectUnitPlaneVTable = (SceneObjectVTable) {
&sceneObjectUnitPlaneIntersectRay,
&sceneObjectUnitPlaneEmitPhotons,
&sceneObjectUnitPlaneRadiantFlux
};
SceneObjectUnitPlane makeSceneObjectUnitPlane (const Plane plane, const Material *material) {
return (SceneObjectUnitPlane) {makeSceneObject(&sceneObjectUnitPlaneVTable), plane, material};
}
defineAllocator(SceneObjectUnitPlane)
Intersection sceneObjectUnitPlaneIntersectRay(const SceneObject *superobject, const Ray ray) {
const SceneObjectUnitPlane *object = (SceneObjectUnitPlane *) superobject;
Intersection intersection = pIntersect(object->plane, ray);
if (intersection.hitType) {
// Intersection outside the unit cube should be ignored.
if (
intersection.position.x < -1 || intersection.position.x > 1 ||
intersection.position.y < -1 || intersection.position.y > 1 ||
intersection.position.z < -1 || intersection.position.z > 1
) {
intersection.hitType = missed;
// Return early.
return intersection;
}
intersection.material = object->material;
}
return intersection;
}
bool sceneObjectUnitPlaneEmitPhotons(const SceneObject *superobject, const int numPhotons, PhotonContainer *photons) {
if (!numPhotons) {
return false;
}
const SceneObjectUnitPlane *object = (SceneObjectUnitPlane *) superobject;
Color flux = sceneObjectUnitPlaneRadiantFlux(superobject);
// See comments in SceneObjectSphere.
// The U and V axis in the plane.
Vector n = object->plane.normal;
Vector U = makeVector(n.y, n.z, n.x);
Vector V = makeVector(n.z, n.x, n.y);
// Divide the photons onto a grid of n*m, most closely matching the wanted number.
int numPhotonsU = ceil(sqrt(numPhotons/2.0))*2;
int numPhotonsV = numPhotons/numPhotonsU;
// How many photons in the last row, and how tall it is.
int lastRowNumPhotonsU = numPhotons - numPhotonsU*numPhotonsV;
float lastRowFactor = lastRowNumPhotonsU / (float) numPhotons;
for (int iV = 0; iV < numPhotonsV; ++iV) {
for (int iU = 0; iU < numPhotonsU; ++iU) {
float u = (iU + randf()) / numPhotonsU;
float v = (iV + randf()) / numPhotonsV * (1-lastRowFactor);
Vector position = vAdd(vsMul(U, u*2-1), vsMul(V, v*2-1));
photonContainerAddValue(photons, makePhoton(makeRay(position, vSampleHemisphere(object->plane.normal)), csMul(flux, 1.0 / numPhotons)));
}
}
for (int iU = 0; iU < lastRowNumPhotonsU; ++iU) {
float u = (iU + randf()) / lastRowNumPhotonsU;
float v = (1 - lastRowFactor) + randf() * lastRowFactor;
Vector position = vAdd(vsMul(U, u*2-1), vsMul(V, v*2-1));
photonContainerAddValue(photons, makePhoton(makeRay(position, vSampleHemisphere(object->plane.normal)), csMul(flux, 1.0 / numPhotons)));
}
return true;
}
Color sceneObjectUnitPlaneRadiantFlux(const SceneObject *superobject) {
const SceneObjectUnitPlane *object = (SceneObjectUnitPlane *) superobject;
Color averageIrradiance = materialIrradience(object->material);
// The plane is [-1, 1] in 2 dimensions.
return csMul(averageIrradiance, 4);
}