use of org.sunflow.core.ShadingState in project joons-renderer by joonhyublee.
the class IrradianceCacheGIEngine method getIrradiance.
@Override
public Color getIrradiance(ShadingState state, Color diffuseReflectance) {
if (samples <= 0) {
return Color.BLACK;
}
if (state.getDiffuseDepth() > 0) {
// do simple path tracing for additional bounces (single ray)
float xi = (float) state.getRandom(0, 0, 1);
float xj = (float) state.getRandom(0, 1, 1);
float phi = (float) (xi * 2 * Math.PI);
float cosPhi = (float) Math.cos(phi);
float sinPhi = (float) Math.sin(phi);
float sinTheta = (float) Math.sqrt(xj);
float cosTheta = (float) Math.sqrt(1.0f - xj);
Vector3 w = new Vector3();
w.x = cosPhi * sinTheta;
w.y = sinPhi * sinTheta;
w.z = cosTheta;
OrthoNormalBasis onb = state.getBasis();
onb.transform(w);
Ray r = new Ray(state.getPoint(), w);
ShadingState temp = state.traceFinalGather(r, 0);
return temp != null ? getGlobalRadiance(temp).copy().mul((float) Math.PI) : Color.BLACK;
}
rwl.readLock().lock();
Color irr;
try {
irr = getIrradiance(state.getPoint(), state.getNormal());
} finally {
rwl.readLock().unlock();
}
if (irr == null) {
// compute new sample
irr = Color.black();
OrthoNormalBasis onb = state.getBasis();
float invR = 0;
float minR = Float.POSITIVE_INFINITY;
Vector3 w = new Vector3();
for (int i = 0; i < samples; i++) {
float xi = (float) state.getRandom(i, 0, samples);
float xj = (float) state.getRandom(i, 1, samples);
float phi = (float) (xi * 2 * Math.PI);
float cosPhi = (float) Math.cos(phi);
float sinPhi = (float) Math.sin(phi);
float sinTheta = (float) Math.sqrt(xj);
float cosTheta = (float) Math.sqrt(1.0f - xj);
w.x = cosPhi * sinTheta;
w.y = sinPhi * sinTheta;
w.z = cosTheta;
onb.transform(w);
Ray r = new Ray(state.getPoint(), w);
ShadingState temp = state.traceFinalGather(r, i);
if (temp != null) {
minR = Math.min(r.getMax(), minR);
invR += 1.0f / r.getMax();
temp.getInstance().prepareShadingState(temp);
irr.add(getGlobalRadiance(temp));
}
}
irr.mul((float) Math.PI / samples);
invR = samples / invR;
rwl.writeLock().lock();
try {
insert(state.getPoint(), state.getNormal(), invR, irr);
} finally {
rwl.writeLock().unlock();
}
}
return irr;
}
Aggregations