Search in sources :

Example 1 with Instance

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

the class TriangleMesh method prepareShadingState.

@Override
public void prepareShadingState(ShadingState state) {
    state.init();
    Instance parent = state.getInstance();
    int primID = state.getPrimitiveID();
    float u = state.getU();
    float v = state.getV();
    float w = 1 - u - v;
    state.getRay().getPoint(state.getPoint());
    int tri = 3 * primID;
    int index0 = triangles[tri + 0];
    int index1 = triangles[tri + 1];
    int index2 = triangles[tri + 2];
    Point3 v0p = getPoint(index0);
    Point3 v1p = getPoint(index1);
    Point3 v2p = getPoint(index2);
    Vector3 ng = Point3.normal(v0p, v1p, v2p);
    ng = state.transformNormalObjectToWorld(ng);
    ng.normalize();
    state.getGeoNormal().set(ng);
    switch(normals.interp) {
        case NONE:
        case FACE:
            {
                state.getNormal().set(ng);
                break;
            }
        case VERTEX:
            {
                int i30 = 3 * index0;
                int i31 = 3 * index1;
                int i32 = 3 * index2;
                float[] normalsu = this.normals.data;
                state.getNormal().x = w * normalsu[i30 + 0] + u * normalsu[i31 + 0] + v * normalsu[i32 + 0];
                state.getNormal().y = w * normalsu[i30 + 1] + u * normalsu[i31 + 1] + v * normalsu[i32 + 1];
                state.getNormal().z = w * normalsu[i30 + 2] + u * normalsu[i31 + 2] + v * normalsu[i32 + 2];
                state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal()));
                state.getNormal().normalize();
                break;
            }
        case FACEVARYING:
            {
                int idx = 3 * tri;
                float[] normalsu = this.normals.data;
                state.getNormal().x = w * normalsu[idx + 0] + u * normalsu[idx + 3] + v * normalsu[idx + 6];
                state.getNormal().y = w * normalsu[idx + 1] + u * normalsu[idx + 4] + v * normalsu[idx + 7];
                state.getNormal().z = w * normalsu[idx + 2] + u * normalsu[idx + 5] + v * normalsu[idx + 8];
                state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal()));
                state.getNormal().normalize();
                break;
            }
    }
    float uv00 = 0, uv01 = 0, uv10 = 0, uv11 = 0, uv20 = 0, uv21 = 0;
    switch(uvs.interp) {
        case NONE:
        case FACE:
            {
                state.getUV().x = 0;
                state.getUV().y = 0;
                break;
            }
        case VERTEX:
            {
                int i20 = 2 * index0;
                int i21 = 2 * index1;
                int i22 = 2 * index2;
                float[] uvsu = this.uvs.data;
                uv00 = uvsu[i20 + 0];
                uv01 = uvsu[i20 + 1];
                uv10 = uvsu[i21 + 0];
                uv11 = uvsu[i21 + 1];
                uv20 = uvsu[i22 + 0];
                uv21 = uvsu[i22 + 1];
                break;
            }
        case FACEVARYING:
            {
                int idx = tri << 1;
                float[] uvsu = this.uvs.data;
                uv00 = uvsu[idx + 0];
                uv01 = uvsu[idx + 1];
                uv10 = uvsu[idx + 2];
                uv11 = uvsu[idx + 3];
                uv20 = uvsu[idx + 4];
                uv21 = uvsu[idx + 5];
                break;
            }
    }
    if (uvs.interp != InterpolationType.NONE) {
        // get exact uv coords and compute tangent vectors
        state.getUV().x = w * uv00 + u * uv10 + v * uv20;
        state.getUV().y = w * uv01 + u * uv11 + v * uv21;
        float du1 = uv00 - uv20;
        float du2 = uv10 - uv20;
        float dv1 = uv01 - uv21;
        float dv2 = uv11 - uv21;
        Vector3 dp1 = Point3.sub(v0p, v2p, new Vector3()), dp2 = Point3.sub(v1p, v2p, new Vector3());
        float determinant = du1 * dv2 - dv1 * du2;
        if (determinant == 0.0f) {
            // create basis in world space
            state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
        } else {
            float invdet = 1.f / determinant;
            // Vector3 dpdu = new Vector3();
            // dpdu.x = (dv2 * dp1.x - dv1 * dp2.x) * invdet;
            // dpdu.y = (dv2 * dp1.y - dv1 * dp2.y) * invdet;
            // dpdu.z = (dv2 * dp1.z - dv1 * dp2.z) * invdet;
            Vector3 dpdv = new Vector3();
            dpdv.x = (-du2 * dp1.x + du1 * dp2.x) * invdet;
            dpdv.y = (-du2 * dp1.y + du1 * dp2.y) * invdet;
            dpdv.z = (-du2 * dp1.z + du1 * dp2.z) * invdet;
            dpdv = state.transformVectorObjectToWorld(dpdv);
            // create basis in world space
            state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), dpdv));
        }
    } else {
        state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
    }
    int shaderIndex = faceShaders == null ? 0 : (faceShaders[primID] & 0xFF);
    state.setShader(parent.getShader(shaderIndex));
    state.setModifier(parent.getModifier(shaderIndex));
}
Also used : Point3(org.sunflow.math.Point3) Instance(org.sunflow.core.Instance) Vector3(org.sunflow.math.Vector3)

Example 2 with Instance

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

the class Hair method prepareShadingState.

@Override
public void prepareShadingState(ShadingState state) {
    state.init();
    Instance i = state.getInstance();
    state.getRay().getPoint(state.getPoint());
    Ray r = state.getRay();
    Shader s = i.getShader(0);
    state.setShader(s != null ? s : this);
    int primID = state.getPrimitiveID();
    int hair = primID / numSegments;
    int line = primID % numSegments;
    int vRoot = hair * 3 * (numSegments + 1);
    int v0 = vRoot + line * 3;
    // tangent vector
    Vector3 v = getTangent(line, v0, state.getV());
    v = state.transformVectorObjectToWorld(v);
    state.setBasis(OrthoNormalBasis.makeFromWV(v, new Vector3(-r.dx, -r.dy, -r.dz)));
    state.getBasis().swapVW();
    // normal
    state.getNormal().set(0, 0, 1);
    state.getBasis().transform(state.getNormal());
    state.getGeoNormal().set(state.getNormal());
    state.getUV().set(0, (line + state.getV()) / numSegments);
}
Also used : Instance(org.sunflow.core.Instance) Vector3(org.sunflow.math.Vector3) Ray(org.sunflow.core.Ray) Shader(org.sunflow.core.Shader)

Example 3 with Instance

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

the class SunflowAPI method render.

@Override
public final void render(String optionsName, Display display) {
    renderObjects.updateScene(scene);
    Options opt = lookupOptions(optionsName);
    if (opt == null) {
        opt = new Options();
    }
    scene.setCamera(lookupCamera(opt.getString("camera", null)));
    // shader override
    String shaderOverrideName = opt.getString("override.shader", "none");
    boolean overridePhotons = opt.getBoolean("override.photons", false);
    if (shaderOverrideName.equals("none")) {
        scene.setShaderOverride(null, false);
    } else {
        Shader shader = lookupShader(shaderOverrideName);
        if (shader == null) {
            UI.printWarning(Module.API, "Unable to find shader \"%s\" for override, disabling", shaderOverrideName);
        }
        scene.setShaderOverride(shader, overridePhotons);
    }
    // baking
    String bakingInstanceName = opt.getString("baking.instance", null);
    if (bakingInstanceName != null) {
        Instance bakingInstance = lookupInstance(bakingInstanceName);
        if (bakingInstance == null) {
            UI.printError(Module.API, "Unable to bake instance \"%s\" - not found", bakingInstanceName);
            return;
        }
        scene.setBakingInstance(bakingInstance);
    } else {
        scene.setBakingInstance(null);
    }
    ImageSampler sampler = PluginRegistry.imageSamplerPlugins.createObject(opt.getString("sampler", "bucket"));
    scene.render(opt, sampler, display);
}
Also used : Options(org.sunflow.core.Options) Instance(org.sunflow.core.Instance) ImageSampler(org.sunflow.core.ImageSampler) Shader(org.sunflow.core.Shader)

Example 4 with Instance

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

the class RenderObjectMap method remove.

final void remove(String name) {
    RenderObjectHandle obj = renderObjects.get(name);
    if (obj == null) {
        UI.printWarning(Module.API, "Unable to remove \"%s\" - object was not defined yet");
        return;
    }
    UI.printDetailed(Module.API, "Removing object \"%s\"", name);
    renderObjects.remove(name);
    // references to the old object still around
    switch(obj.type) {
        case SHADER:
            Shader s = obj.getShader();
            for (FastHashMap.Entry<String, RenderObjectHandle> e : renderObjects) {
                Instance i = e.getValue().getInstance();
                if (i != null) {
                    UI.printWarning(Module.API, "Removing shader \"%s\" from instance \"%s\"", name, e.getKey());
                    i.removeShader(s);
                }
            }
            break;
        case MODIFIER:
            Modifier m = obj.getModifier();
            for (FastHashMap.Entry<String, RenderObjectHandle> e : renderObjects) {
                Instance i = e.getValue().getInstance();
                if (i != null) {
                    UI.printWarning(Module.API, "Removing modifier \"%s\" from instance \"%s\"", name, e.getKey());
                    i.removeModifier(m);
                }
            }
            break;
        case GEOMETRY:
            {
                Geometry g = obj.getGeometry();
                for (FastHashMap.Entry<String, RenderObjectHandle> e : renderObjects) {
                    Instance i = e.getValue().getInstance();
                    if (i != null && i.hasGeometry(g)) {
                        UI.printWarning(Module.API, "Removing instance \"%s\" because it referenced geometry \"%s\"", e.getKey(), name);
                        remove(e.getKey());
                    }
                }
                break;
            }
        case INSTANCE:
            rebuildInstanceList = true;
            break;
        case LIGHT:
            rebuildLightList = true;
            break;
        default:
            // no dependencies
            break;
    }
}
Also used : Geometry(org.sunflow.core.Geometry) Instance(org.sunflow.core.Instance) Shader(org.sunflow.core.Shader) FastHashMap(org.sunflow.util.FastHashMap) Modifier(org.sunflow.core.Modifier)

Example 5 with Instance

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

the class RenderObjectMap method updateScene.

final void updateScene(Scene scene) {
    if (rebuildInstanceList) {
        UI.printInfo(Module.API, "Building scene instance list for rendering ...");
        int numInfinite = 0, numInstance = 0;
        for (FastHashMap.Entry<String, RenderObjectHandle> e : renderObjects) {
            Instance i = e.getValue().getInstance();
            if (i != null) {
                i.updateBounds();
                if (i.getBounds() == null) {
                    numInfinite++;
                } else if (!i.getBounds().isEmpty()) {
                    numInstance++;
                } else {
                    UI.printWarning(Module.API, "Ignoring empty instance: \"%s\"", e.getKey());
                }
            }
        }
        Instance[] infinite = new Instance[numInfinite];
        Instance[] instance = new Instance[numInstance];
        numInfinite = numInstance = 0;
        for (FastHashMap.Entry<String, RenderObjectHandle> e : renderObjects) {
            Instance i = e.getValue().getInstance();
            if (i != null) {
                if (i.getBounds() == null) {
                    infinite[numInfinite] = i;
                    numInfinite++;
                } else if (!i.getBounds().isEmpty()) {
                    instance[numInstance] = i;
                    numInstance++;
                }
            }
        }
        scene.setInstanceLists(instance, infinite);
        rebuildInstanceList = false;
    }
    if (rebuildLightList) {
        UI.printInfo(Module.API, "Building scene light list for rendering ...");
        ArrayList<LightSource> lightList = new ArrayList<LightSource>();
        for (FastHashMap.Entry<String, RenderObjectHandle> e : renderObjects) {
            LightSource light = e.getValue().getLight();
            if (light != null) {
                lightList.add(light);
            }
        }
        scene.setLightList(lightList.toArray(new LightSource[lightList.size()]));
        rebuildLightList = false;
    }
}
Also used : Instance(org.sunflow.core.Instance) ArrayList(java.util.ArrayList) FastHashMap(org.sunflow.util.FastHashMap) LightSource(org.sunflow.core.LightSource)

Aggregations

Instance (org.sunflow.core.Instance)15 Vector3 (org.sunflow.math.Vector3)11 Point3 (org.sunflow.math.Point3)9 Shader (org.sunflow.core.Shader)3 FastHashMap (org.sunflow.util.FastHashMap)2 ArrayList (java.util.ArrayList)1 Geometry (org.sunflow.core.Geometry)1 ImageSampler (org.sunflow.core.ImageSampler)1 LightSource (org.sunflow.core.LightSource)1 Modifier (org.sunflow.core.Modifier)1 Options (org.sunflow.core.Options)1 Ray (org.sunflow.core.Ray)1