Search in sources :

Example 46 with Vector3

use of org.sunflow.math.Vector3 in project joons-renderer by joonhyublee.

the class ShinyDiffuseShader method getRadiance.

public Color getRadiance(ShadingState state) {
    // make sure we are on the right side of the material
    state.faceforward();
    // direct lighting
    state.initLightSamples();
    state.initCausticSamples();
    Color d = getDiffuse(state);
    Color lr = state.diffuse(d);
    if (!state.includeSpecular()) {
        return lr;
    }
    float cos = state.getCosND();
    float dn = 2 * cos;
    Vector3 refDir = new Vector3();
    refDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
    refDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
    refDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
    Ray refRay = new Ray(state.getPoint(), refDir);
    // compute Fresnel term
    cos = 1 - cos;
    float cos2 = cos * cos;
    float cos5 = cos2 * cos2 * cos;
    Color ret = Color.white();
    Color r = d.copy().mul(refl);
    ret.sub(r);
    ret.mul(cos5);
    ret.add(r);
    return lr.add(ret.mul(state.traceReflection(refRay, 0)));
}
Also used : Color(org.sunflow.image.Color) Vector3(org.sunflow.math.Vector3) Ray(org.sunflow.core.Ray)

Example 47 with Vector3

use of org.sunflow.math.Vector3 in project joons-renderer by joonhyublee.

the class CornellBox method scatterPhoton.

@Override
public void scatterPhoton(ShadingState state, Color power) {
    int side = state.getPrimitiveID();
    Color kd = null;
    switch(side) {
        case 0:
            kd = left;
            break;
        case 1:
            kd = right;
            break;
        case 3:
            kd = back;
            break;
        case 4:
            kd = bottom;
            break;
        case 5:
            float lx = state.getPoint().x;
            float ly = state.getPoint().y;
            if (lx >= lxmin && lx < lxmax && ly >= lymin && ly < lymax && state.getRay().dz > 0) {
                return;
            }
            kd = top;
            break;
        default:
            assert false;
    }
    // make sure we are on the right side of the material
    if (Vector3.dot(state.getNormal(), state.getRay().getDirection()) > 0) {
        state.getNormal().negate();
        state.getGeoNormal().negate();
    }
    state.storePhoton(state.getRay().getDirection(), power, kd);
    double avg = kd.getAverage();
    double rnd = state.getRandom(0, 0, 1);
    if (rnd < avg) {
        // photon is scattered
        power.mul(kd).mul(1 / (float) avg);
        OrthoNormalBasis onb = OrthoNormalBasis.makeFromW(state.getNormal());
        double u = 2 * Math.PI * rnd / avg;
        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);
    }
}
Also used : Color(org.sunflow.image.Color) Vector3(org.sunflow.math.Vector3) Ray(org.sunflow.core.Ray) OrthoNormalBasis(org.sunflow.math.OrthoNormalBasis)

Example 48 with Vector3

use of org.sunflow.math.Vector3 in project joons-renderer by joonhyublee.

the class Cylinder method prepareShadingState.

@Override
public void prepareShadingState(ShadingState state) {
    state.init();
    state.getRay().getPoint(state.getPoint());
    Instance parent = state.getInstance();
    Point3 localPoint = state.transformWorldToObject(state.getPoint());
    state.getNormal().set(localPoint.x, localPoint.y, 0);
    state.getNormal().normalize();
    float phi = (float) Math.atan2(state.getNormal().y, state.getNormal().x);
    if (phi < 0) {
        phi += 2 * Math.PI;
    }
    state.getUV().x = phi / (float) (2 * Math.PI);
    state.getUV().y = (localPoint.z + 1) * 0.5f;
    state.setShader(parent.getShader(0));
    state.setModifier(parent.getModifier(0));
    // into world space
    Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal());
    Vector3 v = state.transformVectorObjectToWorld(new Vector3(0, 0, 1));
    state.getNormal().set(worldNormal);
    state.getNormal().normalize();
    state.getGeoNormal().set(state.getNormal());
    // compute basis in world space
    state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), v));
}
Also used : Point3(org.sunflow.math.Point3) Instance(org.sunflow.core.Instance) Vector3(org.sunflow.math.Vector3)

Example 49 with Vector3

use of org.sunflow.math.Vector3 in project joons-renderer by joonhyublee.

the class JuliaFractal method prepareShadingState.

@Override
public void prepareShadingState(ShadingState state) {
    state.init();
    state.getRay().getPoint(state.getPoint());
    Instance parent = state.getInstance();
    // compute local normal
    Point3 p = state.transformWorldToObject(state.getPoint());
    float gx1w = p.x - DELTA;
    float gx1x = p.y;
    float gx1y = p.z;
    float gx1z = 0;
    float gx2w = p.x + DELTA;
    float gx2x = p.y;
    float gx2y = p.z;
    float gx2z = 0;
    float gy1w = p.x;
    float gy1x = p.y - DELTA;
    float gy1y = p.z;
    float gy1z = 0;
    float gy2w = p.x;
    float gy2x = p.y + DELTA;
    float gy2y = p.z;
    float gy2z = 0;
    float gz1w = p.x;
    float gz1x = p.y;
    float gz1y = p.z - DELTA;
    float gz1z = 0;
    float gz2w = p.x;
    float gz2x = p.y;
    float gz2y = p.z + DELTA;
    float gz2z = 0;
    for (int i = 0; i < maxIterations; i++) {
        {
            // z = z*z + c
            float nw = gx1w * gx1w - gx1x * gx1x - gx1y * gx1y - gx1z * gx1z + cw;
            gx1x = 2 * gx1w * gx1x + cx;
            gx1y = 2 * gx1w * gx1y + cy;
            gx1z = 2 * gx1w * gx1z + cz;
            gx1w = nw;
        }
        {
            // z = z*z + c
            float nw = gx2w * gx2w - gx2x * gx2x - gx2y * gx2y - gx2z * gx2z + cw;
            gx2x = 2 * gx2w * gx2x + cx;
            gx2y = 2 * gx2w * gx2y + cy;
            gx2z = 2 * gx2w * gx2z + cz;
            gx2w = nw;
        }
        {
            // z = z*z + c
            float nw = gy1w * gy1w - gy1x * gy1x - gy1y * gy1y - gy1z * gy1z + cw;
            gy1x = 2 * gy1w * gy1x + cx;
            gy1y = 2 * gy1w * gy1y + cy;
            gy1z = 2 * gy1w * gy1z + cz;
            gy1w = nw;
        }
        {
            // z = z*z + c
            float nw = gy2w * gy2w - gy2x * gy2x - gy2y * gy2y - gy2z * gy2z + cw;
            gy2x = 2 * gy2w * gy2x + cx;
            gy2y = 2 * gy2w * gy2y + cy;
            gy2z = 2 * gy2w * gy2z + cz;
            gy2w = nw;
        }
        {
            // z = z*z + c
            float nw = gz1w * gz1w - gz1x * gz1x - gz1y * gz1y - gz1z * gz1z + cw;
            gz1x = 2 * gz1w * gz1x + cx;
            gz1y = 2 * gz1w * gz1y + cy;
            gz1z = 2 * gz1w * gz1z + cz;
            gz1w = nw;
        }
        {
            // z = z*z + c
            float nw = gz2w * gz2w - gz2x * gz2x - gz2y * gz2y - gz2z * gz2z + cw;
            gz2x = 2 * gz2w * gz2x + cx;
            gz2y = 2 * gz2w * gz2y + cy;
            gz2z = 2 * gz2w * gz2z + cz;
            gz2w = nw;
        }
    }
    float gradX = length(gx2w, gx2x, gx2y, gx2z) - length(gx1w, gx1x, gx1y, gx1z);
    float gradY = length(gy2w, gy2x, gy2y, gy2z) - length(gy1w, gy1x, gy1y, gy1z);
    float gradZ = length(gz2w, gz2x, gz2y, gz2z) - length(gz1w, gz1x, gz1y, gz1z);
    Vector3 n = new Vector3(gradX, gradY, gradZ);
    state.getNormal().set(state.transformNormalObjectToWorld(n));
    state.getNormal().normalize();
    state.getGeoNormal().set(state.getNormal());
    state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
    state.getPoint().x += state.getNormal().x * epsilon * 20;
    state.getPoint().y += state.getNormal().y * epsilon * 20;
    state.getPoint().z += state.getNormal().z * epsilon * 20;
    state.setShader(parent.getShader(0));
    state.setModifier(parent.getModifier(0));
}
Also used : Point3(org.sunflow.math.Point3) Instance(org.sunflow.core.Instance) Vector3(org.sunflow.math.Vector3)

Example 50 with Vector3

use of org.sunflow.math.Vector3 in project joons-renderer by joonhyublee.

the class Plane method update.

@Override
public boolean update(ParameterList pl, SunflowAPI api) {
    center = pl.getPoint("center", center);
    Point3 b = pl.getPoint("point1", null);
    Point3 c = pl.getPoint("point2", null);
    if (b != null && c != null) {
        Point3 v0 = center;
        Point3 v1 = b;
        Point3 v2 = c;
        Vector3 ng = normal = Vector3.cross(Point3.sub(v1, v0, new Vector3()), Point3.sub(v2, v0, new Vector3()), new Vector3()).normalize();
        if (Math.abs(ng.x) > Math.abs(ng.y) && Math.abs(ng.x) > Math.abs(ng.z)) {
            k = 0;
        } else if (Math.abs(ng.y) > Math.abs(ng.z)) {
            k = 1;
        } else {
            k = 2;
        }
        float ax, ay, bx, by, cx, cy;
        switch(k) {
            case 0:
                {
                    ax = v0.y;
                    ay = v0.z;
                    bx = v2.y - ax;
                    by = v2.z - ay;
                    cx = v1.y - ax;
                    cy = v1.z - ay;
                    break;
                }
            case 1:
                {
                    ax = v0.z;
                    ay = v0.x;
                    bx = v2.z - ax;
                    by = v2.x - ay;
                    cx = v1.z - ax;
                    cy = v1.x - ay;
                    break;
                }
            case 2:
            default:
                {
                    ax = v0.x;
                    ay = v0.y;
                    bx = v2.x - ax;
                    by = v2.y - ay;
                    cx = v1.x - ax;
                    cy = v1.y - ay;
                }
        }
        float det = bx * cy - by * cx;
        bnu = -by / det;
        bnv = bx / det;
        bnd = (by * ax - bx * ay) / det;
        cnu = cy / det;
        cnv = -cx / det;
        cnd = (cx * ay - cy * ax) / det;
    } else {
        normal = pl.getVector("normal", normal);
        k = 3;
        bnu = bnv = bnd = 0;
        cnu = cnv = cnd = 0;
    }
    return true;
}
Also used : Point3(org.sunflow.math.Point3) Vector3(org.sunflow.math.Vector3)

Aggregations

Vector3 (org.sunflow.math.Vector3)75 Color (org.sunflow.image.Color)34 Point3 (org.sunflow.math.Point3)27 Ray (org.sunflow.core.Ray)25 OrthoNormalBasis (org.sunflow.math.OrthoNormalBasis)17 Instance (org.sunflow.core.Instance)11 LightSample (org.sunflow.core.LightSample)8 ShadingState (org.sunflow.core.ShadingState)3 ParameterList (org.sunflow.core.ParameterList)2 TriangleMesh (org.sunflow.core.primitive.TriangleMesh)2 XYZColor (org.sunflow.image.XYZColor)2 Timer (org.sunflow.system.Timer)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 FloatBuffer (java.nio.FloatBuffer)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 PrimitiveList (org.sunflow.core.PrimitiveList)1