Skip to content

Commit 26baaed

Browse files
author
Victor Widell
committed
Implemented photon emission in SceneObjectUnitPlane.
1 parent 632c2f8 commit 26baaed

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

SceneObjectUnitPlane.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,49 @@ Intersection sceneObjectUnitPlaneIntersectRay(const SceneObject *superobject, co
4444

4545
bool sceneObjectUnitPlaneEmitPhotons(const SceneObject *superobject, const int numPhotons, PhotonContainer *photons) {
4646

47-
// TODO: Implement this. Use code from SceneObjectSphere.
47+
const SceneObjectUnitPlane *object = (SceneObjectUnitPlane *) superobject;
48+
49+
Color flux = sceneObjectUnitPlaneRadiantFlux(superobject);
50+
51+
// See comments in SceneObjectSphere.
4852

49-
return false;
53+
// The U and V axis in the plane.
54+
Vector n = object->plane.normal;
55+
Vector U = makeVector(n.y, n.z, n.x);
56+
Vector V = makeVector(n.z, n.x, n.y);
57+
58+
// Divide the photons onto a grid of n*m, most closely matching the wanted number.
59+
int numPhotonsU = ceil(sqrt(numPhotons/2.0))*2;
60+
int numPhotonsV = numPhotons/numPhotonsU;
61+
62+
// How many photons in the last row, and how tall it is.
63+
int lastRowNumPhotonsU = numPhotons - numPhotonsU*numPhotonsV;
64+
float lastRowFactor = lastRowNumPhotonsU / (float) numPhotons;
65+
66+
for (int iV = 0; iV < numPhotonsV; ++iV) {
67+
68+
for (int iU = 0; iU < numPhotonsU; ++iU) {
69+
70+
float u = (iU + randf()) / numPhotonsU;
71+
float v = (iV + randf()) / numPhotonsV * (1-lastRowFactor);
72+
73+
Vector position = vAdd(vsMul(U, u*2-1), vsMul(V, v*2-1));
74+
75+
photonContainerAddValue(photons, makePhoton(makeRay(position, vSampleHemisphere(object->plane.normal)), csMul(flux, 1.0 / numPhotons)));
76+
}
77+
}
78+
79+
for (int iU = 0; iU < lastRowNumPhotonsU; ++iU) {
80+
81+
float u = (iU + randf()) / lastRowNumPhotonsU;
82+
float v = (1 - lastRowFactor) + randf() * lastRowFactor;
83+
84+
Vector position = vAdd(vsMul(U, u*2-1), vsMul(V, v*2-1));
85+
86+
photonContainerAddValue(photons, makePhoton(makeRay(position, vSampleHemisphere(object->plane.normal)), csMul(flux, 1.0 / numPhotons)));
87+
}
88+
89+
return true;
5090
}
5191

5292
Color sceneObjectUnitPlaneRadiantFlux(const SceneObject *superobject) {

0 commit comments

Comments
 (0)