Search in sources :

Example 26 with Ray

use of org.sunflow.core.Ray in project joons-renderer by joonhyublee.

the class PointLight method getSamples.

public void getSamples(ShadingState state) {
    Vector3 d = Point3.sub(lightPoint, state.getPoint(), new Vector3());
    if (Vector3.dot(d, state.getNormal()) > 0 && Vector3.dot(d, state.getGeoNormal()) > 0) {
        LightSample dest = new LightSample();
        // prepare shadow ray
        dest.setShadowRay(new Ray(state.getPoint(), lightPoint));
        float scale = 1.0f / (float) (4 * Math.PI * lightPoint.distanceToSquared(state.getPoint()));
        dest.setRadiance(power, power);
        dest.getDiffuseRadiance().mul(scale);
        dest.getSpecularRadiance().mul(scale);
        dest.traceShadow(state);
        state.addSample(dest);
    }
}
Also used : LightSample(org.sunflow.core.LightSample) Vector3(org.sunflow.math.Vector3) Ray(org.sunflow.core.Ray)

Example 27 with Ray

use of org.sunflow.core.Ray 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;
}
Also used : ShadingState(org.sunflow.core.ShadingState) Color(org.sunflow.image.Color) Vector3(org.sunflow.math.Vector3) Ray(org.sunflow.core.Ray) OrthoNormalBasis(org.sunflow.math.OrthoNormalBasis)

Aggregations

Ray (org.sunflow.core.Ray)27 Vector3 (org.sunflow.math.Vector3)25 Color (org.sunflow.image.Color)22 OrthoNormalBasis (org.sunflow.math.OrthoNormalBasis)13 LightSample (org.sunflow.core.LightSample)9 Point3 (org.sunflow.math.Point3)5 ShadingState (org.sunflow.core.ShadingState)3 Instance (org.sunflow.core.Instance)1 Shader (org.sunflow.core.Shader)1 XYZColor (org.sunflow.image.XYZColor)1