Search in sources :

Example 6 with Line

use of com.jme3.scene.shape.Line in project jmonkeyengine by jMonkeyEngine.

the class BatchNode method mergeGeometries.

/**
     * Merges all geometries in the collection into
     * the output mesh. Does not take into account materials.
     *
     * @param geometries
     * @param outMesh
     */
private void mergeGeometries(Mesh outMesh, List<Geometry> geometries) {
    int[] compsForBuf = new int[VertexBuffer.Type.values().length];
    VertexBuffer.Format[] formatForBuf = new VertexBuffer.Format[compsForBuf.length];
    boolean[] normForBuf = new boolean[VertexBuffer.Type.values().length];
    int totalVerts = 0;
    int totalTris = 0;
    int totalLodLevels = 0;
    int maxWeights = -1;
    Mesh.Mode mode = null;
    float lineWidth = 1f;
    for (Geometry geom : geometries) {
        totalVerts += geom.getVertexCount();
        totalTris += geom.getTriangleCount();
        totalLodLevels = Math.min(totalLodLevels, geom.getMesh().getNumLodLevels());
        if (maxVertCount < geom.getVertexCount()) {
            maxVertCount = geom.getVertexCount();
        }
        Mesh.Mode listMode;
        //float listLineWidth = 1f;
        int components;
        switch(geom.getMesh().getMode()) {
            case Points:
                listMode = Mesh.Mode.Points;
                components = 1;
                break;
            case LineLoop:
            case LineStrip:
            case Lines:
                listMode = Mesh.Mode.Lines;
                //listLineWidth = geom.getMesh().getLineWidth();
                components = 2;
                break;
            case TriangleFan:
            case TriangleStrip:
            case Triangles:
                listMode = Mesh.Mode.Triangles;
                components = 3;
                break;
            default:
                throw new UnsupportedOperationException();
        }
        for (VertexBuffer vb : geom.getMesh().getBufferList().getArray()) {
            int currentCompsForBuf = compsForBuf[vb.getBufferType().ordinal()];
            if (vb.getBufferType() != VertexBuffer.Type.Index && currentCompsForBuf != 0 && currentCompsForBuf != vb.getNumComponents()) {
                throw new UnsupportedOperationException("The geometry " + geom + " buffer " + vb.getBufferType() + " has different number of components than the rest of the meshes " + "(this: " + vb.getNumComponents() + ", expected: " + currentCompsForBuf + ")");
            }
            compsForBuf[vb.getBufferType().ordinal()] = vb.getNumComponents();
            formatForBuf[vb.getBufferType().ordinal()] = vb.getFormat();
            normForBuf[vb.getBufferType().ordinal()] = vb.isNormalized();
        }
        maxWeights = Math.max(maxWeights, geom.getMesh().getMaxNumWeights());
        if (mode != null && mode != listMode) {
            throw new UnsupportedOperationException("Cannot combine different" + " primitive types: " + mode + " != " + listMode);
        }
        mode = listMode;
        //Not needed anymore as lineWidth is now in RenderState and will be taken into account when merging according to the material
        //            if (mode == Mesh.Mode.Lines) {
        //                if (lineWidth != 1f && listLineWidth != lineWidth) {
        //                    throw new UnsupportedOperationException("When using Mesh Line mode, cannot combine meshes with different line width "
        //                            + lineWidth + " != " + listLineWidth);
        //                }
        //                lineWidth = listLineWidth;
        //            }
        compsForBuf[VertexBuffer.Type.Index.ordinal()] = components;
    }
    outMesh.setMaxNumWeights(maxWeights);
    outMesh.setMode(mode);
    //outMesh.setLineWidth(lineWidth);
    if (totalVerts >= 65536) {
        // make sure we create an UnsignedInt buffer so we can fit all of the meshes
        formatForBuf[VertexBuffer.Type.Index.ordinal()] = VertexBuffer.Format.UnsignedInt;
    } else {
        formatForBuf[VertexBuffer.Type.Index.ordinal()] = VertexBuffer.Format.UnsignedShort;
    }
    // generate output buffers based on retrieved info
    for (int i = 0; i < compsForBuf.length; i++) {
        if (compsForBuf[i] == 0) {
            continue;
        }
        Buffer data;
        if (i == VertexBuffer.Type.Index.ordinal()) {
            data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris);
        } else {
            data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts);
        }
        VertexBuffer vb = new VertexBuffer(VertexBuffer.Type.values()[i]);
        vb.setupData(VertexBuffer.Usage.Dynamic, compsForBuf[i], formatForBuf[i], data);
        vb.setNormalized(normForBuf[i]);
        outMesh.setBuffer(vb);
    }
    int globalVertIndex = 0;
    int globalTriIndex = 0;
    for (Geometry geom : geometries) {
        Mesh inMesh = geom.getMesh();
        if (!isBatch(geom)) {
            geom.associateWithGroupNode(this, globalVertIndex);
        }
        int geomVertCount = inMesh.getVertexCount();
        int geomTriCount = inMesh.getTriangleCount();
        for (int bufType = 0; bufType < compsForBuf.length; bufType++) {
            VertexBuffer inBuf = inMesh.getBuffer(VertexBuffer.Type.values()[bufType]);
            VertexBuffer outBuf = outMesh.getBuffer(VertexBuffer.Type.values()[bufType]);
            if (outBuf == null) {
                continue;
            }
            if (VertexBuffer.Type.Index.ordinal() == bufType) {
                int components = compsForBuf[bufType];
                IndexBuffer inIdx = inMesh.getIndicesAsList();
                IndexBuffer outIdx = outMesh.getIndexBuffer();
                for (int tri = 0; tri < geomTriCount; tri++) {
                    for (int comp = 0; comp < components; comp++) {
                        int idx = inIdx.get(tri * components + comp) + globalVertIndex;
                        outIdx.put((globalTriIndex + tri) * components + comp, idx);
                    }
                }
            } else if (VertexBuffer.Type.Position.ordinal() == bufType) {
                FloatBuffer inPos = (FloatBuffer) inBuf.getData();
                FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                doCopyBuffer(inPos, globalVertIndex, outPos, 3);
            } else if (VertexBuffer.Type.Normal.ordinal() == bufType || VertexBuffer.Type.Tangent.ordinal() == bufType) {
                FloatBuffer inPos = (FloatBuffer) inBuf.getData();
                FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                doCopyBuffer(inPos, globalVertIndex, outPos, compsForBuf[bufType]);
                if (VertexBuffer.Type.Tangent.ordinal() == bufType) {
                    useTangents = true;
                }
            } else {
                if (inBuf == null) {
                    throw new IllegalArgumentException("Geometry " + geom.getName() + " has no " + outBuf.getBufferType() + " buffer whereas other geoms have. all geometries should have the same types of buffers.\n Try to use GeometryBatchFactory.alignBuffer() on the BatchNode before batching");
                } else if (outBuf == null) {
                    throw new IllegalArgumentException("Geometry " + geom.getName() + " has a " + outBuf.getBufferType() + " buffer whereas other geoms don't. all geometries should have the same types of buffers.\n Try to use GeometryBatchFactory.alignBuffer() on the BatchNode before batching");
                } else {
                    inBuf.copyElements(0, outBuf, globalVertIndex, geomVertCount);
                }
            }
        }
        globalVertIndex += geomVertCount;
        globalTriIndex += geomTriCount;
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) Buffer(java.nio.Buffer) FloatBuffer(java.nio.FloatBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer)

Example 7 with Line

use of com.jme3.scene.shape.Line in project jmonkeyengine by jMonkeyEngine.

the class CurvesHelper method transformToFirstLineOfBevelPoints.

/**
     * This method transforms the first line of the bevel points positioning it
     * on the first point of the curve.
     * 
     * @param startingLinePoints
     *            the vbevel shape points
     * @param firstCurvePoint
     *            the first curve's point
     * @param secondCurvePoint
     *            the second curve's point
     * @return points of transformed bevel
     */
protected Vector3f[] transformToFirstLineOfBevelPoints(Vector3f[] startingLinePoints, Vector3f firstCurvePoint, Vector3f secondCurvePoint) {
    Vector3f planeNormal = secondCurvePoint.subtract(firstCurvePoint).normalizeLocal();
    float angle = FastMath.acos(planeNormal.dot(Vector3f.UNIT_X));
    Vector3f rotationVector = Vector3f.UNIT_X.cross(planeNormal).normalizeLocal();
    Matrix4f m = new Matrix4f();
    m.setRotationQuaternion(new Quaternion().fromAngleAxis(angle, rotationVector));
    m.setTranslation(firstCurvePoint);
    Vector3f temp = new Vector3f();
    Vector3f[] verts = new Vector3f[startingLinePoints.length];
    for (int i = 0; i < verts.length; ++i) {
        verts[i] = m.mult(startingLinePoints[i], temp).clone();
    }
    return verts;
}
Also used : Matrix4f(com.jme3.math.Matrix4f) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f)

Example 8 with Line

use of com.jme3.scene.shape.Line in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeDefinitionLoader method load.

@Override
public Object load(AssetInfo assetInfo) throws IOException {
    AssetKey k = assetInfo.getKey();
    if (!(k instanceof ShaderNodeDefinitionKey)) {
        throw new IOException("ShaderNodeDefinition file must be loaded via ShaderNodeDefinitionKey");
    }
    ShaderNodeDefinitionKey key = (ShaderNodeDefinitionKey) k;
    loaderDelegate = new ShaderNodeLoaderDelegate();
    InputStream in = assetInfo.openStream();
    List<Statement> roots = BlockLanguageParser.parse(in);
    if (roots.size() == 2) {
        Statement exception = roots.get(0);
        String line = exception.getLine();
        if (line.startsWith("Exception")) {
            throw new AssetLoadException(line.substring("Exception ".length()));
        } else {
            throw new MatParseException("In multiroot shader node definition, expected first statement to be 'Exception'", exception);
        }
    } else if (roots.size() != 1) {
        throw new MatParseException("Too many roots in J3SN file", roots.get(0));
    }
    return loaderDelegate.readNodesDefinitions(roots.get(0).getContents(), key);
}
Also used : AssetKey(com.jme3.asset.AssetKey) ShaderNodeDefinitionKey(com.jme3.asset.ShaderNodeDefinitionKey) InputStream(java.io.InputStream) Statement(com.jme3.util.blockparser.Statement) IOException(java.io.IOException) AssetLoadException(com.jme3.asset.AssetLoadException)

Example 9 with Line

use of com.jme3.scene.shape.Line in project jmonkeyengine by jMonkeyEngine.

the class TangentBinormalGenerator method processTriangle.

public static TriangleData processTriangle(int[] index, Vector3f[] v, Vector2f[] t) {
    TempVars tmp = TempVars.get();
    try {
        Vector3f edge1 = tmp.vect1;
        Vector3f edge2 = tmp.vect2;
        Vector2f edge1uv = tmp.vect2d;
        Vector2f edge2uv = tmp.vect2d2;
        Vector3f tangent = tmp.vect3;
        Vector3f binormal = tmp.vect4;
        Vector3f normal = tmp.vect5;
        t[1].subtract(t[0], edge1uv);
        t[2].subtract(t[0], edge2uv);
        float det = edge1uv.x * edge2uv.y - edge1uv.y * edge2uv.x;
        boolean normalize = false;
        if (Math.abs(det) < ZERO_TOLERANCE) {
            log.log(Level.WARNING, "Colinear uv coordinates for triangle " + "[{0}, {1}, {2}]; tex0 = [{3}, {4}], " + "tex1 = [{5}, {6}], tex2 = [{7}, {8}]", new Object[] { index[0], index[1], index[2], t[0].x, t[0].y, t[1].x, t[1].y, t[2].x, t[2].y });
            det = 1;
            normalize = true;
        }
        v[1].subtract(v[0], edge1);
        v[2].subtract(v[0], edge2);
        tangent.set(edge1);
        tangent.normalizeLocal();
        binormal.set(edge2);
        binormal.normalizeLocal();
        if (Math.abs(Math.abs(tangent.dot(binormal)) - 1) < ZERO_TOLERANCE) {
            log.log(Level.WARNING, "Vertices are on the same line " + "for triangle [{0}, {1}, {2}].", new Object[] { index[0], index[1], index[2] });
        }
        float factor = 1 / det;
        tangent.x = (edge2uv.y * edge1.x - edge1uv.y * edge2.x) * factor;
        tangent.y = (edge2uv.y * edge1.y - edge1uv.y * edge2.y) * factor;
        tangent.z = (edge2uv.y * edge1.z - edge1uv.y * edge2.z) * factor;
        if (normalize) {
            tangent.normalizeLocal();
        }
        binormal.x = (edge1uv.x * edge2.x - edge2uv.x * edge1.x) * factor;
        binormal.y = (edge1uv.x * edge2.y - edge2uv.x * edge1.y) * factor;
        binormal.z = (edge1uv.x * edge2.z - edge2uv.x * edge1.z) * factor;
        if (normalize) {
            binormal.normalizeLocal();
        }
        tangent.cross(binormal, normal);
        normal.normalizeLocal();
        return new TriangleData(tangent.clone(), binormal.clone(), normal.clone());
    } finally {
        tmp.release();
    }
}
Also used : Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f)

Example 10 with Line

use of com.jme3.scene.shape.Line in project Lina by MisterCavespider.

the class LineString method update.

/**
     * Don't call it regularly.
     * 
     * It will be internally called.
     * 
     * @throws nl.mistercavespider.lina.UnavailiblePointException
     */
protected void update() throws UnavailiblePointException {
    int total = waypoints.size();
    int counter = 0;
    while (counter < total) {
        Vector3f p1 = waypoints.get(counter);
        Vector3f p2 = waypoints.get(counter + 1);
        //Is it the last one? Should we connect?
        if (counter == total - 1 && connection && p2 == null) {
            p2 = waypoints.get(0);
        } else if (p2 == null) {
            //Throw an exception
            throw new UnavailiblePointException();
        }
        //Creates the lines
        Line l = new Line(p1, p2);
        Geometry g = new Geometry("Geometry in a LineString: " + counter, l);
        g.setMaterial(mat);
        //In case 
        //Attaches the lines to a node
        lineNode.attachChild(g);
        //Add the counter
        counter++;
    }
}
Also used : Line(com.jme3.scene.shape.Line) Geometry(com.jme3.scene.Geometry) Vector3f(com.jme3.math.Vector3f)

Aggregations

Vector3f (com.jme3.math.Vector3f)8 Material (com.jme3.material.Material)5 Geometry (com.jme3.scene.Geometry)5 Statement (com.jme3.util.blockparser.Statement)4 IOException (java.io.IOException)4 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)3 FloatBuffer (java.nio.FloatBuffer)3 AssetLoadException (com.jme3.asset.AssetLoadException)2 ShaderNodeDefinitionKey (com.jme3.asset.ShaderNodeDefinitionKey)2 Vector2f (com.jme3.math.Vector2f)2 Mesh (com.jme3.scene.Mesh)2 Box (com.jme3.scene.shape.Box)2 Curve (com.jme3.scene.shape.Curve)2 Line (com.jme3.scene.shape.Line)2 Texture (com.jme3.texture.Texture)2 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 AssetInfo (com.jme3.asset.AssetInfo)1