/* (non-Javadoc)
* @see ca.eandb.jmist.framework.SceneElement#createLight()
*/
public Light createLight() {
final List<Light> lights = new ArrayList<Light>();
Light light = null;
int numLights = 0;
for (SceneElement child : children) {
light = child.createLight();
lights.add(light);
if (light != null) {
numLights++;
}
}
final int lightCount = numLights;
switch (numLights) {
case 0:
return null;
case 1:
return light;
default:
return new AbstractLight() {
/** Serialization version ID. */
private static final long serialVersionUID = 6299798465595032610L;
@Override
public double getSamplePDF(final SurfacePoint x, final PathInfo pathInfo) {
return lights.get(x.getPrimitiveIndex()).getSamplePDF(x, pathInfo) / (double) lightCount;
}
@Override
public void illuminate(SurfacePoint x, WavelengthPacket lambda,
Random rnd, Illuminable target) {
throw new UnimplementedException();
}
@Override
public LightNode sample(PathInfo pathInfo, double ru,
double rv, double rj) {
SeedReference ref = new SeedReference(rj);
int index = RandomUtil.discrete(0, lightCount - 1, ref);
for (int i = 0, n = lights.size(); i < n; i++) {
Light light = lights.get(i);
if (light != null) {
if (index-- == 0) {
return ScaledLightNode.create(1.0 / (double) lightCount,
light.sample(pathInfo, ru, rv, ref.seed), rj);
}
}
}
return null;