Search in sources :

Example 31 with Color

use of org.sunflow.image.Color in project joons-renderer by joonhyublee.

the class UberShader method scatterPhoton.

public void scatterPhoton(ShadingState state, Color power) {
    Color diffuse, specular;
    // make sure we are on the right side of the material
    state.faceforward();
    diffuse = getDiffuse(state);
    specular = getSpecular(state);
    state.storePhoton(state.getRay().getDirection(), power, diffuse);
    float d = diffuse.getAverage();
    float r = specular.getAverage();
    double rnd = state.getRandom(0, 0, 1);
    if (rnd < d) {
        // photon is scattered
        power.mul(diffuse).mul(1.0f / d);
        OrthoNormalBasis onb = state.getBasis();
        double u = 2 * Math.PI * rnd / d;
        double v = state.getRandom(0, 1, 1);
        float s = (float) Math.sqrt(v);
        float s1 = (float) Math.sqrt(1.0 - v);
        Vector3 w = new Vector3((float) Math.cos(u) * s, (float) Math.sin(u) * s, s1);
        w = onb.transform(w, new Vector3());
        state.traceDiffusePhoton(new Ray(state.getPoint(), w), power);
    } else if (rnd < d + r) {
        if (glossyness == 0) {
            float cos = -Vector3.dot(state.getNormal(), state.getRay().getDirection());
            power.mul(diffuse).mul(1.0f / d);
            // photon is reflected
            float dn = 2 * cos;
            Vector3 dir = new Vector3();
            dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
            dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
            dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
            state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power);
        } else {
            float dn = 2.0f * state.getCosND();
            // reflected direction
            Vector3 refDir = new Vector3();
            refDir.x = (dn * state.getNormal().x) + state.getRay().dx;
            refDir.y = (dn * state.getNormal().y) + state.getRay().dy;
            refDir.z = (dn * state.getNormal().z) + state.getRay().dz;
            power.mul(spec).mul(1.0f / r);
            OrthoNormalBasis onb = state.getBasis();
            double u = 2 * Math.PI * (rnd - r) / r;
            double v = state.getRandom(0, 1, 1);
            float s = (float) Math.pow(v, 1 / ((1.0f / glossyness) + 1));
            float s1 = (float) Math.sqrt(1 - s * s);
            Vector3 w = new Vector3((float) Math.cos(u) * s1, (float) Math.sin(u) * s1, s);
            w = onb.transform(w, new Vector3());
            state.traceReflectionPhoton(new Ray(state.getPoint(), w), power);
        }
    }
}
Also used : Color(org.sunflow.image.Color) Vector3(org.sunflow.math.Vector3) Ray(org.sunflow.core.Ray) OrthoNormalBasis(org.sunflow.math.OrthoNormalBasis)

Example 32 with Color

use of org.sunflow.image.Color in project joons-renderer by joonhyublee.

the class GlassShader method scatterPhoton.

public void scatterPhoton(ShadingState state, Color power) {
    Color refr = Color.mul(1 - f0, color);
    Color refl = Color.mul(f0, color);
    float avgR = refl.getAverage();
    float avgT = refr.getAverage();
    double rnd = state.getRandom(0, 0, 1);
    if (rnd < avgR) {
        state.faceforward();
        // don't reflect internally
        if (state.isBehind()) {
            return;
        }
        // photon is reflected
        float cos = state.getCosND();
        power.mul(refl).mul(1.0f / avgR);
        float dn = 2 * cos;
        Vector3 dir = new Vector3();
        dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
        dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
        dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
        state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power);
    } else if (rnd < avgR + avgT) {
        state.faceforward();
        // photon is refracted
        float cos = state.getCosND();
        float neta = state.isBehind() ? eta : 1.0f / eta;
        power.mul(refr).mul(1.0f / avgT);
        float wK = -neta;
        float arg = 1 - (neta * neta * (1 - (cos * cos)));
        Vector3 dir = new Vector3();
        if (state.isBehind() && absorptionDistance > 0) {
            // this ray is inside the object and leaving it
            // compute attenuation that occured along the ray
            power.mul(Color.mul(-state.getRay().getMax() / absorptionDistance, absorptionColor.copy().opposite()).exp());
        }
        if (arg < 0) {
            // TIR
            float dn = 2 * cos;
            dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
            dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
            dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
            state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power);
        } else {
            float nK = (neta * cos) - (float) Math.sqrt(arg);
            dir.x = (-wK * state.getRay().dx) + (nK * state.getNormal().x);
            dir.y = (-wK * state.getRay().dy) + (nK * state.getNormal().y);
            dir.z = (-wK * state.getRay().dz) + (nK * state.getNormal().z);
            state.traceRefractionPhoton(new Ray(state.getPoint(), dir), power);
        }
    }
}
Also used : Color(org.sunflow.image.Color) Vector3(org.sunflow.math.Vector3) Ray(org.sunflow.core.Ray)

Example 33 with Color

use of org.sunflow.image.Color in project joons-renderer by joonhyublee.

the class GlassShader method getRadiance.

public Color getRadiance(ShadingState state) {
    if (!state.includeSpecular()) {
        return Color.BLACK;
    }
    Vector3 reflDir = new Vector3();
    Vector3 refrDir = new Vector3();
    state.faceforward();
    float cos = state.getCosND();
    boolean inside = state.isBehind();
    float neta = inside ? eta : 1.0f / eta;
    float dn = 2 * cos;
    reflDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
    reflDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
    reflDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
    // refracted ray
    float arg = 1 - (neta * neta * (1 - (cos * cos)));
    boolean tir = arg < 0;
    if (tir) {
        refrDir.x = refrDir.y = refrDir.z = 0;
    } else {
        float nK = (neta * cos) - (float) Math.sqrt(arg);
        refrDir.x = (neta * state.getRay().dx) + (nK * state.getNormal().x);
        refrDir.y = (neta * state.getRay().dy) + (nK * state.getNormal().y);
        refrDir.z = (neta * state.getRay().dz) + (nK * state.getNormal().z);
    }
    // compute Fresnel terms
    float cosTheta1 = Vector3.dot(state.getNormal(), reflDir);
    float cosTheta2 = -Vector3.dot(state.getNormal(), refrDir);
    float pPara = (cosTheta1 - eta * cosTheta2) / (cosTheta1 + eta * cosTheta2);
    float pPerp = (eta * cosTheta1 - cosTheta2) / (eta * cosTheta1 + cosTheta2);
    float kr = 0.5f * (pPara * pPara + pPerp * pPerp);
    float kt = 1 - kr;
    Color absorbtion = null;
    if (inside && absorptionDistance > 0) {
        // this ray is inside the object and leaving it
        // compute attenuation that occured along the ray
        absorbtion = Color.mul(-state.getRay().getMax() / absorptionDistance, absorptionColor.copy().opposite()).exp();
        if (absorbtion.isBlack()) {
            // nothing goes through
            return Color.BLACK;
        }
    }
    // refracted ray
    Color ret = Color.black();
    if (!tir) {
        ret.madd(kt, state.traceRefraction(new Ray(state.getPoint(), refrDir), 0)).mul(color);
    }
    if (!inside || tir) {
        ret.add(Color.mul(kr, state.traceReflection(new Ray(state.getPoint(), reflDir), 0)).mul(color));
    }
    return absorbtion != null ? ret.mul(absorbtion) : ret;
}
Also used : Color(org.sunflow.image.Color) Vector3(org.sunflow.math.Vector3) Ray(org.sunflow.core.Ray)

Example 34 with Color

use of org.sunflow.image.Color in project joons-renderer by joonhyublee.

the class ViewCausticsShader method getRadiance.

public Color getRadiance(ShadingState state) {
    state.faceforward();
    state.initCausticSamples();
    // integrate a diffuse function
    Color lr = Color.black();
    for (LightSample sample : state) {
        lr.madd(sample.dot(state.getNormal()), sample.getDiffuseRadiance());
    }
    return lr.mul(1.0f / (float) Math.PI);
}
Also used : LightSample(org.sunflow.core.LightSample) Color(org.sunflow.image.Color)

Example 35 with Color

use of org.sunflow.image.Color in project joons-renderer by joonhyublee.

the class InstantGI method getGlobalRadiance.

@Override
public Color getGlobalRadiance(ShadingState state) {
    Point3 p = state.getPoint();
    Vector3 n = state.getNormal();
    int set = (int) (state.getRandom(0, 1, 1) * numSets);
    float maxAvgPow = 0;
    float minDist = 1;
    Color pow = null;
    for (PointLight vpl : virtualLights[set]) {
        maxAvgPow = Math.max(maxAvgPow, vpl.power.getAverage());
        if (Vector3.dot(n, vpl.n) > 0.9f) {
            float d = vpl.p.distanceToSquared(p);
            if (d < minDist) {
                pow = vpl.power;
                minDist = d;
            }
        }
    }
    return pow == null ? Color.BLACK : pow.copy().mul(1.0f / maxAvgPow);
}
Also used : Point3(org.sunflow.math.Point3) Color(org.sunflow.image.Color) Vector3(org.sunflow.math.Vector3)

Aggregations

Color (org.sunflow.image.Color)48 Vector3 (org.sunflow.math.Vector3)34 Ray (org.sunflow.core.Ray)22 OrthoNormalBasis (org.sunflow.math.OrthoNormalBasis)15 LightSample (org.sunflow.core.LightSample)8 Point3 (org.sunflow.math.Point3)7 ShadingState (org.sunflow.core.ShadingState)6 Bitmap (org.sunflow.image.Bitmap)2 XYZColor (org.sunflow.image.XYZColor)2 Timer (org.sunflow.system.Timer)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 ConstantSpectralCurve (org.sunflow.image.ConstantSpectralCurve)1 GenericBitmap (org.sunflow.image.formats.GenericBitmap)1 BoundingBox (org.sunflow.math.BoundingBox)1 IntArray (org.sunflow.util.IntArray)1