Renders the Scene and returns an OutputMatrix containing the pixels of the final image.
Source
OutputMatrix render() {
List<Sample> samples;
List<Ray> primaryRays;
OutputMatrix om = new OutputMatrix(yRes, xRes);
// for each pixel..
for (int x = 0; x < xRes; x++) {
if ((x+1) % 10 == 0) {print('Rendering column ${x+1}..');} //TODO
for (int y = 0; y < yRes; y++) {
// color information for this pixel
Vector4 color = new Vector4.zero();
// generate samples
samples = sampler.getSamples(x, y);
// for each sample
for (Sample s in samples) {
// temporary color vector for this sample
Vector4 tempColor = new Vector4.zero();
primaryRays = camera.getPrimaryRays(x, y);
// integrate rays..
for (Ray r in primaryRays) {
// integrate radiance
//TODO count bounces for indirect radiance
Intersection ret = scene.intersect(r, MAX_DIST);
if (ret.distance >= EPS && ret.distance < MAX_DIST) {
//print ('hit at ${ret.distance} with ${ret.prim} on ${ret.hitPoint}');
// get shader
Shader s = scene.getShader(ret);
// get ambience, reflectance and indirect radiance
tempColor += s.getAmbientCoeff().multiply(ambientLight);
//TODO for all light sources, integrate indirect radiance.
tempColor += s.getReflectance(-r.direction, r.origin - ret.hitPoint); //TODO dummy light source at camera
tempColor += s.getIndirectRadiance();
}
}
// weight ray
double rayWeight = 1.0 / primaryRays.length;
tempColor.scale(rayWeight);
// weight sample
tempColor.scale(s.weight);
// add to overall pixel color
color += tempColor;
// alpha always set manually - not supported in shaders yet
color.a = 1.0;
// set pixel in output matrix
om.setPixel(y, x, color.rgba);
}
}
}
return om;
}