@@ -44,9 +44,49 @@ Intersection sceneObjectUnitPlaneIntersectRay(const SceneObject *superobject, co
44
44
45
45
bool sceneObjectUnitPlaneEmitPhotons (const SceneObject * superobject , const int numPhotons , PhotonContainer * photons ) {
46
46
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.
48
52
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;
50
90
}
51
91
52
92
Color sceneObjectUnitPlaneRadiantFlux (const SceneObject * superobject ) {
0 commit comments