Search in sources :

Example 1 with PrimitiveList

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

the class SunflowAPI method geometry.

@Override
public final void geometry(String name, String typeName) {
    if (!isIncremental(typeName)) {
        // we are declaring a geometry for the first time
        if (renderObjects.has(name)) {
            UI.printError(Module.API, "Unable to declare geometry \"%s\", name is already in use", name);
            parameterList.clear(true);
            return;
        }
        // check tesselatable first
        if (PluginRegistry.tesselatablePlugins.hasType(typeName)) {
            Tesselatable tesselatable = PluginRegistry.tesselatablePlugins.createObject(typeName);
            if (tesselatable == null) {
                UI.printError(Module.API, "Unable to create tesselatable object of type \"%s\"", typeName);
                return;
            }
            renderObjects.put(name, tesselatable);
        } else {
            PrimitiveList primitives = PluginRegistry.primitivePlugins.createObject(typeName);
            if (primitives == null) {
                UI.printError(Module.API, "Unable to create primitive of type \"%s\"", typeName);
                return;
            }
            renderObjects.put(name, primitives);
        }
    }
    if (lookupGeometry(name) != null) {
        update(name);
    } else {
        UI.printError(Module.API, "Unable to update geometry \"%s\" - geometry object was not found", name);
        parameterList.clear(true);
    }
}
Also used : PrimitiveList(org.sunflow.core.PrimitiveList) Tesselatable(org.sunflow.core.Tesselatable)

Example 2 with PrimitiveList

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

the class BezierMesh method tesselate.

public PrimitiveList tesselate() {
    float[] vertices = new float[patches.length * (subdivs + 1) * (subdivs + 1) * 3];
    float[] normals = smooth ? new float[patches.length * (subdivs + 1) * (subdivs + 1) * 3] : null;
    float[] uvs = new float[patches.length * (subdivs + 1) * (subdivs + 1) * 2];
    int[] indices = new int[patches.length * subdivs * subdivs * (quads ? 4 : (2 * 3))];
    int vidx = 0, pidx = 0;
    float step = 1.0f / subdivs;
    int vstride = subdivs + 1;
    Point3 p = new Point3();
    Vector3 n = smooth ? new Vector3() : null;
    for (float[] patch : patches) {
        // create patch vertices
        for (int i = 0, voff = 0; i <= subdivs; i++) {
            float u = i * step;
            float[] bu = bernstein(u);
            float[] bdu = bernsteinDeriv(u);
            for (int j = 0; j <= subdivs; j++, voff += 3) {
                float v = j * step;
                float[] bv = bernstein(v);
                float[] bdv = bernsteinDeriv(v);
                getPatchPoint(u, v, patch, bu, bv, bdu, bdv, p, n);
                vertices[vidx + voff + 0] = p.x;
                vertices[vidx + voff + 1] = p.y;
                vertices[vidx + voff + 2] = p.z;
                if (smooth) {
                    normals[vidx + voff + 0] = n.x;
                    normals[vidx + voff + 1] = n.y;
                    normals[vidx + voff + 2] = n.z;
                }
                uvs[(vidx + voff) / 3 * 2 + 0] = u;
                uvs[(vidx + voff) / 3 * 2 + 1] = v;
            }
        }
        // generate patch triangles
        for (int i = 0, vbase = vidx / 3; i < subdivs; i++) {
            for (int j = 0; j < subdivs; j++) {
                int v00 = (i + 0) * vstride + (j + 0);
                int v10 = (i + 1) * vstride + (j + 0);
                int v01 = (i + 0) * vstride + (j + 1);
                int v11 = (i + 1) * vstride + (j + 1);
                if (quads) {
                    indices[pidx + 0] = vbase + v01;
                    indices[pidx + 1] = vbase + v00;
                    indices[pidx + 2] = vbase + v10;
                    indices[pidx + 3] = vbase + v11;
                    pidx += 4;
                } else {
                    // add 2 triangles
                    indices[pidx + 0] = vbase + v00;
                    indices[pidx + 1] = vbase + v10;
                    indices[pidx + 2] = vbase + v01;
                    indices[pidx + 3] = vbase + v10;
                    indices[pidx + 4] = vbase + v11;
                    indices[pidx + 5] = vbase + v01;
                    pidx += 6;
                }
            }
        }
        vidx += vstride * vstride * 3;
    }
    ParameterList pl = new ParameterList();
    pl.addPoints("points", InterpolationType.VERTEX, vertices);
    if (quads) {
        pl.addIntegerArray("quads", indices);
    } else {
        pl.addIntegerArray("triangles", indices);
    }
    pl.addTexCoords("uvs", InterpolationType.VERTEX, uvs);
    if (smooth) {
        pl.addVectors("normals", InterpolationType.VERTEX, normals);
    }
    PrimitiveList m = quads ? new QuadMesh() : new TriangleMesh();
    m.update(pl, null);
    pl.clear(true);
    return m;
}
Also used : QuadMesh(org.sunflow.core.primitive.QuadMesh) Point3(org.sunflow.math.Point3) PrimitiveList(org.sunflow.core.PrimitiveList) TriangleMesh(org.sunflow.core.primitive.TriangleMesh) ParameterList(org.sunflow.core.ParameterList) Vector3(org.sunflow.math.Vector3)

Aggregations

PrimitiveList (org.sunflow.core.PrimitiveList)2 ParameterList (org.sunflow.core.ParameterList)1 Tesselatable (org.sunflow.core.Tesselatable)1 QuadMesh (org.sunflow.core.primitive.QuadMesh)1 TriangleMesh (org.sunflow.core.primitive.TriangleMesh)1 Point3 (org.sunflow.math.Point3)1 Vector3 (org.sunflow.math.Vector3)1