Search in sources :

Example 21 with IndexBuffer

use of com.jme3.scene.mesh.IndexBuffer in project jmonkeyengine by jMonkeyEngine.

the class Converter method convert.

public static synchronized IndexedMesh convert(Mesh mesh) {
    IndexedMesh jBulletIndexedMesh = new IndexedMesh();
    jBulletIndexedMesh.triangleIndexBase = ByteBuffer.allocate(mesh.getTriangleCount() * 3 * 4);
    jBulletIndexedMesh.vertexBase = ByteBuffer.allocate(mesh.getVertexCount() * 3 * 4);
    IndexBuffer indices = mesh.getIndicesAsList();
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    vertices.rewind();
    int verticesLength = mesh.getVertexCount() * 3;
    jBulletIndexedMesh.numVertices = mesh.getVertexCount();
    //3 verts * 4 bytes per.
    jBulletIndexedMesh.vertexStride = 12;
    for (int i = 0; i < verticesLength; i++) {
        float tempFloat = vertices.get();
        jBulletIndexedMesh.vertexBase.putFloat(tempFloat);
    }
    int indicesLength = mesh.getTriangleCount() * 3;
    jBulletIndexedMesh.numTriangles = mesh.getTriangleCount();
    //3 index entries * 4 bytes each.
    jBulletIndexedMesh.triangleIndexStride = 12;
    for (int i = 0; i < indicesLength; i++) {
        jBulletIndexedMesh.triangleIndexBase.putInt(indices.get(i));
    }
    vertices.rewind();
    vertices.clear();
    return jBulletIndexedMesh;
}
Also used : IndexBuffer(com.jme3.scene.mesh.IndexBuffer) FloatBuffer(java.nio.FloatBuffer) IndexedMesh(com.bulletphysics.collision.shapes.IndexedMesh)

Example 22 with IndexBuffer

use of com.jme3.scene.mesh.IndexBuffer in project jmonkeyengine by jMonkeyEngine.

the class Converter method convert.

public static Mesh convert(IndexedMesh mesh) {
    Mesh jmeMesh = new Mesh();
    jmeMesh.setBuffer(Type.Index, 3, BufferUtils.createShortBuffer(mesh.numTriangles * 3));
    jmeMesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(mesh.numVertices * 3));
    IndexBuffer indicess = jmeMesh.getIndexBuffer();
    FloatBuffer vertices = jmeMesh.getFloatBuffer(Type.Position);
    for (int i = 0; i < mesh.numTriangles * 3; i++) {
        indicess.put(i, mesh.triangleIndexBase.getInt(i * 4));
    }
    for (int i = 0; i < mesh.numVertices * 3; i++) {
        vertices.put(i, mesh.vertexBase.getFloat(i * 4));
    }
    jmeMesh.updateCounts();
    jmeMesh.updateBound();
    jmeMesh.getFloatBuffer(Type.Position).clear();
    return jmeMesh;
}
Also used : IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IndexedMesh(com.bulletphysics.collision.shapes.IndexedMesh) Mesh(com.jme3.scene.Mesh) FloatBuffer(java.nio.FloatBuffer)

Example 23 with IndexBuffer

use of com.jme3.scene.mesh.IndexBuffer in project jmonkeyengine by jMonkeyEngine.

the class LODGeomap method writeIndexArrayLodDiff.

/**
     * Create the LOD index array that will seam its edges with its neighbour's LOD.
     * This is a scary method!!! It will break your mind.
     *
     * @param store to store the index buffer
     * @param lod level of detail of the mesh
     * @param rightLod LOD of the right neighbour
     * @param topLod LOD of the top neighbour
     * @param leftLod LOD of the left neighbour
     * @param bottomLod LOD of the bottom neighbour
     * @return the LOD-ified index buffer
     */
public IndexBuffer writeIndexArrayLodDiff(int lod, boolean rightLod, boolean topLod, boolean leftLod, boolean bottomLod, int totalSize) {
    int numIndexes = calculateNumIndexesLodDiff(lod);
    IndexBuffer ib = IndexBuffer.createIndexBuffer(numIndexes, numIndexes);
    VerboseBuffer buffer = new VerboseBuffer(ib);
    //System.out.println("	for (z="+lod+"; z<"+(getWidth()-(1*lod))+"; z+="+lod+")");
    for (int r = lod; r < getWidth() - (2 * lod); r += lod) {
        // row
        int rowIdx = r * getWidth();
        int nextRowIdx = (r + 1 * lod) * getWidth();
        for (int c = lod; c < getWidth() - (1 * lod); c += lod) {
            // column
            int idx = rowIdx + c;
            buffer.put(idx);
            idx = nextRowIdx + c;
            buffer.put(idx);
        }
        // add degenerate triangles
        if (r < getWidth() - (3 * lod)) {
            int idx = nextRowIdx + getWidth() - (1 * lod) - 1;
            buffer.put(idx);
            // inset by 1
            idx = nextRowIdx + (1 * lod);
            buffer.put(idx);
        //System.out.println("");
        }
    }
    //System.out.println("\nright:");
    //int runningBufferCount = buffer.getCount();
    //System.out.println("buffer start: "+runningBufferCount);
    // right
    int br = getWidth() * (getWidth() - lod) - 1 - lod;
    // bottom right -1
    buffer.put(br);
    int corner = getWidth() * getWidth() - 1;
    // bottom right corner
    buffer.put(corner);
    if (rightLod) {
        // if lower LOD
        for (int row = getWidth() - lod; row >= 1 + lod; row -= 2 * lod) {
            int idx = (row) * getWidth() - 1 - lod;
            buffer.put(idx);
            idx = (row - lod) * getWidth() - 1;
            buffer.put(idx);
            if (row > lod + 1) {
                //if not the last one
                idx = (row - lod) * getWidth() - 1 - lod;
                buffer.put(idx);
                idx = (row - lod) * getWidth() - 1;
                buffer.put(idx);
            } else {
            }
        }
    } else {
        //br+1);//degenerate to flip winding order
        buffer.put(corner);
        for (int row = getWidth() - lod; row > lod; row -= lod) {
            // mult to get row
            int idx = row * getWidth() - 1;
            buffer.put(idx);
            buffer.put(idx - lod);
        }
    }
    buffer.put(getWidth() - 1);
    // top 			(the order gets reversed here so the diagonals line up)
    if (topLod) {
        // if lower LOD
        if (rightLod) {
            buffer.put(getWidth() - 1);
        }
        for (int col = getWidth() - 1; col >= lod; col -= 2 * lod) {
            // next row
            int idx = (lod * getWidth()) + col - lod;
            buffer.put(idx);
            idx = col - 2 * lod;
            buffer.put(idx);
            if (col > lod * 2) {
                //if not the last one
                idx = (lod * getWidth()) + col - 2 * lod;
                buffer.put(idx);
                idx = col - 2 * lod;
                buffer.put(idx);
            } else {
            }
        }
    } else {
        if (rightLod) {
            buffer.put(getWidth() - 1);
        }
        for (int col = getWidth() - 1 - lod; col > 0; col -= lod) {
            int idx = col + (lod * getWidth());
            buffer.put(idx);
            idx = col;
            buffer.put(idx);
        }
        buffer.put(0);
    }
    buffer.put(0);
    // left
    if (leftLod) {
        // if lower LOD
        if (topLod) {
            buffer.put(0);
        }
        for (int row = 0; row < getWidth() - lod; row += 2 * lod) {
            int idx = (row + lod) * getWidth() + lod;
            buffer.put(idx);
            idx = (row + 2 * lod) * getWidth();
            buffer.put(idx);
            if (row < getWidth() - 1 - 2 * lod) {
                //if not the last one
                idx = (row + 2 * lod) * getWidth() + lod;
                buffer.put(idx);
                idx = (row + 2 * lod) * getWidth();
                buffer.put(idx);
            } else {
            }
        }
    } else {
        if (!topLod) {
            buffer.put(0);
        }
        //buffer.put(0); // degenerate winding-flip
        for (int row = lod; row < getWidth() - lod; row += lod) {
            int idx = row * getWidth();
            buffer.put(idx);
            idx = row * getWidth() + lod;
            buffer.put(idx);
        }
    }
    buffer.put(getWidth() * (getWidth() - 1));
    // bottom
    if (bottomLod) {
        // if lower LOD
        if (leftLod) {
            buffer.put(getWidth() * (getWidth() - 1));
        }
        // seemed to be fixed by making "getWidth()-1-2-lod" this: "getWidth()-1-2*lod", which seems more correct
        for (int col = 0; col < getWidth() - lod; col += 2 * lod) {
            int idx = getWidth() * (getWidth() - 1 - lod) + col + lod;
            buffer.put(idx);
            idx = getWidth() * (getWidth() - 1) + col + 2 * lod;
            buffer.put(idx);
            if (col < getWidth() - 1 - 2 * lod) {
                //if not the last one
                idx = getWidth() * (getWidth() - 1 - lod) + col + 2 * lod;
                buffer.put(idx);
                idx = getWidth() * (getWidth() - 1) + col + 2 * lod;
                buffer.put(idx);
            } else {
            }
        }
    } else {
        if (leftLod) {
            buffer.put(getWidth() * (getWidth() - 1));
        }
        for (int col = lod; col < getWidth() - lod; col += lod) {
            // up
            int idx = getWidth() * (getWidth() - 1 - lod) + col;
            buffer.put(idx);
            // down
            idx = getWidth() * (getWidth() - 1) + col;
            buffer.put(idx);
        }
    //buffer.put(getWidth()*getWidth()-1-lod); // <-- THIS caused holes at the end!
    }
    buffer.put(getWidth() * getWidth() - 1);
    // fill in the rest of the buffer with degenerates, there should only be a couple
    for (int i = buffer.getCount(); i < numIndexes; i++) {
        buffer.put(getWidth() * getWidth() - 1);
    }
    return buffer.delegate;
}
Also used : IndexBuffer(com.jme3.scene.mesh.IndexBuffer)

Example 24 with IndexBuffer

use of com.jme3.scene.mesh.IndexBuffer in project jmonkeyengine by jMonkeyEngine.

the class TerrainPatch method reIndexGeometry.

protected void reIndexGeometry(HashMap<String, UpdatedTerrainPatch> updated, boolean useVariableLod) {
    UpdatedTerrainPatch utp = updated.get(getName());
    if (utp != null && utp.isReIndexNeeded()) {
        int pow = (int) Math.pow(2, utp.getNewLod());
        boolean left = utp.getLeftLod() > utp.getNewLod();
        boolean top = utp.getTopLod() > utp.getNewLod();
        boolean right = utp.getRightLod() > utp.getNewLod();
        boolean bottom = utp.getBottomLod() > utp.getNewLod();
        IndexBuffer idxB;
        if (useVariableLod)
            idxB = geomap.writeIndexArrayLodVariable(pow, (int) Math.pow(2, utp.getRightLod()), (int) Math.pow(2, utp.getTopLod()), (int) Math.pow(2, utp.getLeftLod()), (int) Math.pow(2, utp.getBottomLod()), totalSize);
        else
            idxB = geomap.writeIndexArrayLodDiff(pow, right, top, left, bottom, totalSize);
        Buffer b;
        if (idxB.getBuffer() instanceof IntBuffer)
            b = (IntBuffer) idxB.getBuffer();
        else
            b = (ShortBuffer) idxB.getBuffer();
        utp.setNewIndexBuffer(b);
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) VertexBuffer(com.jme3.scene.VertexBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IntBuffer(java.nio.IntBuffer) ShortBuffer(java.nio.ShortBuffer)

Aggregations

IndexBuffer (com.jme3.scene.mesh.IndexBuffer)21 FloatBuffer (java.nio.FloatBuffer)21 IntBuffer (java.nio.IntBuffer)10 ShortBuffer (java.nio.ShortBuffer)10 Buffer (java.nio.Buffer)9 VertexBuffer (com.jme3.scene.VertexBuffer)5 Vector3f (com.jme3.math.Vector3f)4 Mesh (com.jme3.scene.Mesh)3 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 IndexedMesh (com.bulletphysics.collision.shapes.IndexedMesh)2 Vector2f (com.jme3.math.Vector2f)2 IndexIntBuffer (com.jme3.scene.mesh.IndexIntBuffer)2 IndexShortBuffer (com.jme3.scene.mesh.IndexShortBuffer)2 HashMap (java.util.HashMap)2 Matrix4f (com.jme3.math.Matrix4f)1 Spline (com.jme3.math.Spline)1 Vector4f (com.jme3.math.Vector4f)1 Mode (com.jme3.scene.Mesh.Mode)1 CullHint (com.jme3.scene.Spatial.CullHint)1