com.jme3.scene.mesh.IndexBuffer Java Examples

The following examples show how to use com.jme3.scene.mesh.IndexBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Converter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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;
}
 
Example #2
Source File: TerrainPatch.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 = idxB.getBuffer();
            utp.setNewIndexBuffer(b);
        }

    }
 
Example #3
Source File: Converter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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;
}
 
Example #4
Source File: BIHTree.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void initTriList(FloatBuffer vb, IndexBuffer ib) {
    pointData = new float[numTris * 3 * 3];
    int p = 0;
    for (int i = 0; i < numTris * 3; i += 3) {
        int vert = ib.get(i) * 3;
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert);

        vert = ib.get(i + 1) * 3;
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert);

        vert = ib.get(i + 2) * 3;
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert);
    }

    triIndices = new int[numTris];
    for (int i = 0; i < numTris; i++) {
        triIndices[i] = i;
    }
}
 
Example #5
Source File: CurvesHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * The method generates normal buffer for the created mesh of the curve.
 * 
 * @param indexes
 *            the indexes of the mesh points
 * @param points
 *            the mesh's points
 * @param smooth
 *            the flag indicating if the result is to be smooth or solid
 * @return normals buffer for the mesh
 */
private FloatBuffer generateNormals(IndexBuffer indexes, FloatBuffer points, boolean smooth) {
    Map<Integer, Vector3f> normalMap = new TreeMap<Integer, Vector3f>();
    Vector3f[] allVerts = BufferUtils.getVector3Array(points);

    for (int i = 0; i < indexes.size(); i += 3) {
        int index1 = indexes.get(i);
        int index2 = indexes.get(i + 1);
        int index3 = indexes.get(i + 2);

        Vector3f n = FastMath.computeNormal(allVerts[index1], allVerts[index2], allVerts[index3]);
        this.addNormal(n, normalMap, smooth, index1, index2, index3);
    }

    FloatBuffer normals = BufferUtils.createFloatBuffer(normalMap.size() * 3);
    for (Entry<Integer, Vector3f> entry : normalMap.entrySet()) {
        normals.put(entry.getValue().x);
        normals.put(entry.getValue().y);
        normals.put(entry.getValue().z);
    }
    return normals;
}
 
Example #6
Source File: BIHTree.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void initTriList(FloatBuffer vb, IndexBuffer ib) {
    pointData = new float[numTris * 3 * 3];
    int p = 0;
    for (int i = 0; i < numTris * 3; i += 3) {
        int vert = ib.get(i) * 3;
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert);

        vert = ib.get(i + 1) * 3;
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert);

        vert = ib.get(i + 2) * 3;
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert++);
        pointData[p++] = vb.get(vert);
    }

    triIndices = new int[numTris];
    for (int i = 0; i < numTris; i++) {
        triIndices[i] = i;
    }
}
 
Example #7
Source File: BIHTree.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public BIHTree(Mesh mesh, int maxTrisPerNode) {
    this.mesh = mesh;
    this.maxTrisPerNode = maxTrisPerNode;

    if (maxTrisPerNode < 1 || mesh == null) {
        throw new IllegalArgumentException();
    }

    bihSwapTmp = new float[9];

    FloatBuffer vb = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    IndexBuffer ib = mesh.getIndexBuffer();
    if (ib == null) {
        ib = new VirtualIndexBuffer(mesh.getVertexCount(), mesh.getMode());
    } else if (mesh.getMode() != Mode.Triangles) {
        ib = new WrappedIndexBuffer(mesh);
    }

    numTris = ib.size() / 3;
    initTriList(vb, ib);
}
 
Example #8
Source File: TangentBinormalGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static  List<VertexData> processTriangles(Mesh mesh,
        int[] index, Vector3f[] v, Vector2f[] t, boolean splitMirrored) {
    IndexBuffer indexBuffer = mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    if (mesh.getBuffer(Type.TexCoord) == null) {
        throw new IllegalArgumentException("Can only generate tangents for "
                + "meshes with texture coordinates");
    }
    
    FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();
    
    List<VertexData> vertices = initVertexData(vertexBuffer.limit() / 3);
    
    for (int i = 0; i < indexBuffer.size() / 3; i++) {
        for (int j = 0; j < 3; j++) {
            index[j] = indexBuffer.get(i * 3 + j);
            populateFromBuffer(v[j], vertexBuffer, index[j]);
            populateFromBuffer(t[j], textureBuffer, index[j]);
        }
        
        TriangleData triData = processTriangle(index, v, t);
        if(splitMirrored){
            triData.setIndex(index);
            triData.triangleOffset = i * 3 ;
        }
        vertices.get(index[0]).triangles.add(triData);
        vertices.get(index[1]).triangles.add(triData);
        vertices.get(index[2]).triangles.add(triData);
    }
    
    return vertices;
}
 
Example #9
Source File: Converter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static 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();
    jBulletIndexedMesh.vertexStride = 12; //3 verts * 4 bytes per.
    for (int i = 0; i < verticesLength; i++) {
        float tempFloat = vertices.get();
        jBulletIndexedMesh.vertexBase.putFloat(tempFloat);
    }

    int indicesLength = mesh.getTriangleCount() * 3;
    jBulletIndexedMesh.numTriangles = mesh.getTriangleCount();
    jBulletIndexedMesh.triangleIndexStride = 12; //3 index entries * 4 bytes each.
    for (int i = 0; i < indicesLength; i++) {
        jBulletIndexedMesh.triangleIndexBase.putInt(indices.get(i));
    }
    vertices.rewind();
    vertices.clear();

    return jBulletIndexedMesh;
}
 
Example #10
Source File: NativeMeshUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static long getTriangleIndexVertexArray(Mesh mesh){
    ByteBuffer triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4);
    ByteBuffer vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4);
    int numVertices = mesh.getVertexCount();
    int vertexStride = 12; //3 verts * 4 bytes per.
    int numTriangles = mesh.getTriangleCount();
    int triangleIndexStride = 12; //3 index entries * 4 bytes each.

    IndexBuffer indices = mesh.getIndicesAsList();
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    vertices.rewind();

    int verticesLength = mesh.getVertexCount() * 3;
    for (int i = 0; i < verticesLength; i++) {
        float tempFloat = vertices.get();
        vertexBase.putFloat(tempFloat);
    }

    int indicesLength = mesh.getTriangleCount() * 3;
    for (int i = 0; i < indicesLength; i++) {
        triangleIndexBase.putInt(indices.get(i));
    }
    vertices.rewind();
    vertices.clear();

    return createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride);
}
 
Example #11
Source File: GImpactCollisionShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void createCollisionMesh(Mesh mesh) {
        triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4);
        vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4); 
//        triangleIndexBase = ByteBuffer.allocate(mesh.getTriangleCount() * 3 * 4);
//        vertexBase = ByteBuffer.allocate(mesh.getVertexCount() * 3 * 4);
        numVertices = mesh.getVertexCount();
        vertexStride = 12; //3 verts * 4 bytes per.
        numTriangles = mesh.getTriangleCount();
        triangleIndexStride = 12; //3 index entries * 4 bytes each.

        IndexBuffer indices = mesh.getIndexBuffer();
        FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
        vertices.rewind();

        int verticesLength = mesh.getVertexCount() * 3;
        for (int i = 0; i < verticesLength; i++) {
            float tempFloat = vertices.get();
            vertexBase.putFloat(tempFloat);
        }

        int indicesLength = mesh.getTriangleCount() * 3;
        for (int i = 0; i < indicesLength; i++) {
            triangleIndexBase.putInt(indices.get(i));
        }
        vertices.rewind();
        vertices.clear();

        createShape();
    }
 
Example #12
Source File: MeshCollisionShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void createCollisionMesh(Mesh mesh) {
    triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4);
    vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4);
    numVertices = mesh.getVertexCount();
    vertexStride = 12; //3 verts * 4 bytes per.
    numTriangles = mesh.getTriangleCount();
    triangleIndexStride = 12; //3 index entries * 4 bytes each.

    IndexBuffer indices = mesh.getIndexBuffer();
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    vertices.rewind();

    int verticesLength = mesh.getVertexCount() * 3;
    for (int i = 0; i < verticesLength; i++) {
        float tempFloat = vertices.get();
        vertexBase.putFloat(tempFloat);
    }

    int indicesLength = mesh.getTriangleCount() * 3;
    for (int i = 0; i < indicesLength; i++) {
        triangleIndexBase.putInt(indices.get(i));
    }
    vertices.rewind();
    vertices.clear();

    createShape();
}
 
Example #13
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void compressIndexBuffer(Mesh mesh){
    int vertCount = mesh.getVertexCount();
    VertexBuffer vb = mesh.getBuffer(Type.Index);
    Format targetFmt;
    if (vb.getFormat() == Format.UnsignedInt && vertCount <= 0xffff){
        if (vertCount <= 256)
            targetFmt = Format.UnsignedByte;
        else
            targetFmt = Format.UnsignedShort;
    }else if (vb.getFormat() == Format.UnsignedShort && vertCount <= 0xff){
        targetFmt = Format.UnsignedByte;
    }else{
        return;
    }

    IndexBuffer src = mesh.getIndexBuffer();
    Buffer newBuf = VertexBuffer.createBuffer(targetFmt, vb.getNumComponents(), src.size());

    VertexBuffer newVb = new VertexBuffer(Type.Index);
    newVb.setupData(vb.getUsage(), vb.getNumComponents(), targetFmt, newBuf);
    mesh.clearBuffer(Type.Index);
    mesh.setBuffer(newVb);

    IndexBuffer dst = mesh.getIndexBuffer();
    for (int i = 0; i < src.size(); i++){
        dst.put(i, src.get(i));
    }
}
 
Example #14
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 5 votes vote down vote up
private static List<VertexData> processTriangles(Mesh mesh, int[] index, Vector3f[] v, Vector2f[] t, boolean splitMirrored) {
	IndexBuffer indexBuffer = mesh.getIndexBuffer();
	FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
	if (mesh.getBuffer(Type.TexCoord) == null) {
		throw new IllegalArgumentException("Can only generate tangents for " + "meshes with texture coordinates");
	}

	FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();

	List<VertexData> vertices = initVertexData(vertexBuffer.limit() / 3);

	for (int i = 0; i < indexBuffer.size() / 3; i++) {
		for (int j = 0; j < 3; j++) {
			index[j] = indexBuffer.get(i * 3 + j);
			populateFromBuffer(v[j], vertexBuffer, index[j]);
			populateFromBuffer(t[j], textureBuffer, index[j]);
		}

		TriangleData triData = processTriangle(index, v, t);
		if (splitMirrored) {
			triData.setIndex(index);
			triData.triangleOffset = i * 3;
		}
		if (triData != null) {
			vertices.get(index[0]).triangles.add(triData);
			vertices.get(index[1]).triangles.add(triData);
			vertices.get(index[2]).triangles.add(triData);
		}
	}

	return vertices;
}
 
Example #15
Source File: LODGeomap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Mesh createMesh(Vector3f scale, Vector2f tcScale, Vector2f tcOffset, float offsetAmount, int totalSize, boolean center, int lod, boolean rightLod, boolean topLod, boolean leftLod, boolean bottomLod) {
    FloatBuffer pb = writeVertexArray(null, scale, center);
    FloatBuffer texb = writeTexCoordArray(null, tcOffset, tcScale, offsetAmount, totalSize);
    FloatBuffer nb = writeNormalArray(null, scale);
    IndexBuffer ib = writeIndexArrayLodDiff(lod, rightLod, topLod, leftLod, bottomLod, totalSize);
    FloatBuffer bb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    FloatBuffer tanb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    writeTangentArray(nb, tanb, bb, texb, scale);
    Mesh m = new Mesh();
    m.setMode(Mode.TriangleStrip);
    m.setBuffer(Type.Position, 3, pb);
    m.setBuffer(Type.Normal, 3, nb);
    m.setBuffer(Type.Tangent, 3, tanb);
    m.setBuffer(Type.Binormal, 3, bb);
    m.setBuffer(Type.TexCoord, 2, texb);
    switch (ib.getFormat()) {
        case UnsignedInt:
            m.setBuffer(Type.Index, 3, (IntBuffer) ib.getBuffer());
            break;
        case UnsignedShort:
            m.setBuffer(Type.Index, 3, (ShortBuffer) ib.getBuffer());
            break;
        case UnsignedByte:
            m.setBuffer(Type.Index, 3, (ByteBuffer) ib.getBuffer());
            break;
    }
    m.setStatic();
    m.updateBound();
    return m;
}
 
Example #16
Source File: TangentBinormalGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static List<VertexData> processTriangleFan(Mesh mesh,
        int[] index, Vector3f[] v, Vector2f[] t) {
    IndexBuffer indexBuffer = mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();
    
    List<VertexData> vertices = initVertexData(vertexBuffer.limit() / 3);
    
    index[0] = indexBuffer.get(0);
    index[1] = indexBuffer.get(1);
    
    populateFromBuffer(v[0], vertexBuffer, index[0]);
    populateFromBuffer(v[1], vertexBuffer, index[1]);
    
    populateFromBuffer(t[0], textureBuffer, index[0]);
    populateFromBuffer(t[1], textureBuffer, index[1]);
    
    for (int i = 2; i < vertexBuffer.limit() / 3; i++) {
        index[2] = indexBuffer.get(i);
        populateFromBuffer(v[2], vertexBuffer, index[2]);
        populateFromBuffer(t[2], textureBuffer, index[2]);
        
        TriangleData triData = processTriangle(index, v, t);
        vertices.get(index[0]).triangles.add(triData);
        vertices.get(index[1]).triangles.add(triData);
        vertices.get(index[2]).triangles.add(triData);
        
        Vector3f vTemp = v[1];
        v[1] = v[2];
        v[2] = vTemp;
        
        Vector2f tTemp = t[1];
        t[1] = t[2];
        t[2] = tTemp;
        
        index[1] = index[2];
    }
    
    return vertices;
}
 
Example #17
Source File: TangentBinormalGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static VertexData[] processTriangles(Mesh mesh,
        int[] index, Vector3f[] v, Vector2f[] t)
{
    IndexBuffer indexBuffer =  mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    if (mesh.getBuffer(Type.TexCoord) == null)
        throw new IllegalArgumentException("Can only generate tangents for "
                                         + "meshes with texture coordinates");
    
    FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();

    VertexData[] vertices = initVertexData(vertexBuffer.capacity() / 3);

    for (int i = 0; i < indexBuffer.size() / 3; i++) {
        for (int j = 0; j < 3; j++) {
            index[j] = indexBuffer.get(i*3 + j);
            populateFromBuffer(v[j], vertexBuffer, index[j]);
            populateFromBuffer(t[j], textureBuffer, index[j]);
        }

        TriangleData triData = processTriangle(index, v, t);
        if (triData != null) {
            vertices[index[0]].triangles.add(triData);
            vertices[index[1]].triangles.add(triData);
            vertices[index[2]].triangles.add(triData);
        }
    }
    
    return vertices;
}
 
Example #18
Source File: BIHTree.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public BIHTree(Mesh mesh, int maxTrisPerNode) {
    this.mesh = mesh;
    this.maxTrisPerNode = maxTrisPerNode;

    if (maxTrisPerNode < 1) {
        throw new IllegalArgumentException("maxTrisPerNode cannot be less than 1");
    }
    if (mesh == null) {
        throw new IllegalArgumentException("Mesh cannot be null");
    }

    bihSwapTmp = new float[9];

    VertexBuffer vBuffer = mesh.getBuffer(Type.Position);
    if(vBuffer == null){
        throw new IllegalArgumentException("A mesh should at least contain a Position buffer");
    }        
    IndexBuffer ib = mesh.getIndexBuffer();
    FloatBuffer vb = (FloatBuffer) vBuffer.getData();
    
    if (ib == null) {
        ib = new VirtualIndexBuffer(mesh.getVertexCount(), mesh.getMode());
    } else if (mesh.getMode() != Mode.Triangles) {
        ib = new WrappedIndexBuffer(mesh);
    }

    numTris = ib.size() / 3;
    initTriList(vb, ib);
}
 
Example #19
Source File: Converter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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();
    jBulletIndexedMesh.vertexStride = 12; //3 verts * 4 bytes per.
    for (int i = 0; i < verticesLength; i++) {
        float tempFloat = vertices.get();
        jBulletIndexedMesh.vertexBase.putFloat(tempFloat);
    }

    int indicesLength = mesh.getTriangleCount() * 3;
    jBulletIndexedMesh.numTriangles = mesh.getTriangleCount();
    jBulletIndexedMesh.triangleIndexStride = 12; //3 index entries * 4 bytes each.
    for (int i = 0; i < indicesLength; i++) {
        jBulletIndexedMesh.triangleIndexBase.putInt(indices.get(i));
    }
    vertices.rewind();
    vertices.clear();

    return jBulletIndexedMesh;
}
 
Example #20
Source File: NativeMeshUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Pass a mesh to Native Bullet.
 *
 * @param mesh the JME mesh to pass (not null)
 * @return the unique identifier of the resulting btTriangleIndexVertexArray
 * (not 0)
 */
public static long getTriangleIndexVertexArray(Mesh mesh){
    ByteBuffer triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4);
    ByteBuffer vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4);
    int numVertices = mesh.getVertexCount();
    int vertexStride = 12; //3 verts * 4 bytes each
    int numTriangles = mesh.getTriangleCount();
    int triangleIndexStride = 12; //3 index entries * 4 bytes each

    IndexBuffer indices = mesh.getIndicesAsList();
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    vertices.rewind();

    int verticesLength = mesh.getVertexCount() * 3;
    for (int i = 0; i < verticesLength; i++) {
        float tempFloat = vertices.get();
        vertexBase.putFloat(tempFloat);
    }

    int indicesLength = mesh.getTriangleCount() * 3;
    for (int i = 0; i < indicesLength; i++) {
        triangleIndexBase.putInt(indices.get(i));
    }
    vertices.rewind();
    vertices.clear();

    return createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride);
}
 
Example #21
Source File: GImpactCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void createCollisionMesh(Mesh mesh) {
        triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4);
        vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4); 
//        triangleIndexBase = ByteBuffer.allocate(mesh.getTriangleCount() * 3 * 4);
//        vertexBase = ByteBuffer.allocate(mesh.getVertexCount() * 3 * 4);
        numVertices = mesh.getVertexCount();
        vertexStride = 12; //3 verts * 4 bytes per.
        numTriangles = mesh.getTriangleCount();
        triangleIndexStride = 12; //3 index entries * 4 bytes each.

        IndexBuffer indices = mesh.getIndicesAsList();
        FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
        vertices.rewind();

        int verticesLength = mesh.getVertexCount() * 3;
        for (int i = 0; i < verticesLength; i++) {
            float tempFloat = vertices.get();
            vertexBase.putFloat(tempFloat);
        }

        int indicesLength = mesh.getTriangleCount() * 3;
        for (int i = 0; i < indicesLength; i++) {
            triangleIndexBase.putInt(indices.get(i));
        }
        vertices.rewind();
        vertices.clear();

        createShape();
    }
 
Example #22
Source File: MeshCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void createCollisionMesh(Mesh mesh) {
    this.triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4);
    this.vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4);
    this.numVertices = mesh.getVertexCount();
    this.vertexStride = 12; // 3 verts * 4 bytes per.
    this.numTriangles = mesh.getTriangleCount();
    this.triangleIndexStride = 12; // 3 index entries * 4 bytes each.

    IndexBuffer indices = mesh.getIndicesAsList();
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    vertices.rewind();

    int verticesLength = mesh.getVertexCount() * 3;
    for (int i = 0; i < verticesLength; i++) {
        float tempFloat = vertices.get();
        vertexBase.putFloat(tempFloat);
    }

    int indicesLength = mesh.getTriangleCount() * 3;
    for (int i = 0; i < indicesLength; i++) {
        triangleIndexBase.putInt(indices.get(i));
    }
    vertices.rewind();
    vertices.clear();

    this.createShape(null);
}
 
Example #23
Source File: TerrainPatch.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * This calculation is slow, so don't use it often.
 */
public void generateLodEntropies() {
    float[] entropies = new float[getMaxLod()+1];
    for (int i = 0; i <= getMaxLod(); i++){
        int curLod = (int) Math.pow(2, i);
        IndexBuffer idxB = geomap.writeIndexArrayLodDiff(curLod, false, false, false, false, totalSize);
        Buffer ib = idxB.getBuffer();
        entropies[i] = EntropyComputeUtil.computeLodEntropy(mesh, ib);
    }

    lodEntropy = entropies;
}
 
Example #24
Source File: SkinningControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Method to apply skinning transforms to a mesh's buffers
 *
 * @param mesh           the mesh
 * @param offsetMatrices the offset matices to apply
 */
private void applySkinning(Mesh mesh, Matrix4f[] offsetMatrices) {
    int maxWeightsPerVert = mesh.getMaxNumWeights();
    if (maxWeightsPerVert <= 0) {
        throw new IllegalStateException("Max weights per vert is incorrectly set!");
    }
    int fourMinusMaxWeights = 4 - maxWeightsPerVert;

    // NOTE: This code assumes the vertex buffer is in bind pose
    // resetToBind() has been called this frame
    VertexBuffer vb = mesh.getBuffer(Type.Position);
    FloatBuffer fvb = (FloatBuffer) vb.getData();
    fvb.rewind();

    VertexBuffer nb = mesh.getBuffer(Type.Normal);
    FloatBuffer fnb = (FloatBuffer) nb.getData();
    fnb.rewind();

    // get boneIndexes and weights for mesh
    IndexBuffer ib = IndexBuffer.wrapIndexBuffer(mesh.getBuffer(Type.BoneIndex).getData());
    FloatBuffer wb = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();

    wb.rewind();

    float[] weights = wb.array();
    int idxWeights = 0;

    TempVars vars = TempVars.get();

    float[] posBuf = vars.skinPositions;
    float[] normBuf = vars.skinNormals;

    int iterations = (int) FastMath.ceil(fvb.limit() / ((float) posBuf.length));
    int bufLength = posBuf.length;
    for (int i = iterations - 1; i >= 0; i--) {
        // read next set of positions and normals from native buffer
        bufLength = Math.min(posBuf.length, fvb.remaining());
        fvb.get(posBuf, 0, bufLength);
        fnb.get(normBuf, 0, bufLength);
        int verts = bufLength / 3;
        int idxPositions = 0;

        // iterate vertices and apply skinning transform for each effecting bone
        for (int vert = verts - 1; vert >= 0; vert--) {
            // Skip this vertex if the first weight is zero.
            if (weights[idxWeights] == 0) {
                idxPositions += 3;
                idxWeights += 4;
                continue;
            }

            float nmx = normBuf[idxPositions];
            float vtx = posBuf[idxPositions++];
            float nmy = normBuf[idxPositions];
            float vty = posBuf[idxPositions++];
            float nmz = normBuf[idxPositions];
            float vtz = posBuf[idxPositions++];

            float rx = 0, ry = 0, rz = 0, rnx = 0, rny = 0, rnz = 0;

            for (int w = maxWeightsPerVert - 1; w >= 0; w--) {
                float weight = weights[idxWeights];
                Matrix4f mat = offsetMatrices[ib.get(idxWeights++)];

                rx += (mat.m00 * vtx + mat.m01 * vty + mat.m02 * vtz + mat.m03) * weight;
                ry += (mat.m10 * vtx + mat.m11 * vty + mat.m12 * vtz + mat.m13) * weight;
                rz += (mat.m20 * vtx + mat.m21 * vty + mat.m22 * vtz + mat.m23) * weight;

                rnx += (nmx * mat.m00 + nmy * mat.m01 + nmz * mat.m02) * weight;
                rny += (nmx * mat.m10 + nmy * mat.m11 + nmz * mat.m12) * weight;
                rnz += (nmx * mat.m20 + nmy * mat.m21 + nmz * mat.m22) * weight;
            }

            idxWeights += fourMinusMaxWeights;

            idxPositions -= 3;
            normBuf[idxPositions] = rnx;
            posBuf[idxPositions++] = rx;
            normBuf[idxPositions] = rny;
            posBuf[idxPositions++] = ry;
            normBuf[idxPositions] = rnz;
            posBuf[idxPositions++] = rz;
        }

        fvb.position(fvb.position() - bufLength);
        fvb.put(posBuf, 0, bufLength);
        fnb.position(fnb.position() - bufLength);
        fnb.put(normBuf, 0, bufLength);
    }

    vars.release();

    vb.updateData(fvb);
    nb.updateData(fnb);

}
 
Example #25
Source File: TangentBinormalGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static VertexData[] processTriangleStrip(Mesh mesh,
        int[] index, Vector3f[] v, Vector2f[] t)
{
    IndexBuffer indexBuffer =  mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();

    VertexData[] vertices = initVertexData(vertexBuffer.capacity() / 3);

    index[0] = indexBuffer.get(0);
    index[1] = indexBuffer.get(1);

    populateFromBuffer(v[0], vertexBuffer, index[0]);
    populateFromBuffer(v[1], vertexBuffer, index[1]);

    populateFromBuffer(t[0], textureBuffer, index[0]);
    populateFromBuffer(t[1], textureBuffer, index[1]);
    
    for (int i = 2; i < indexBuffer.size(); i++) {
        index[2] = indexBuffer.get(i);
        BufferUtils.populateFromBuffer(v[2], vertexBuffer, index[2]);
        BufferUtils.populateFromBuffer(t[2], textureBuffer, index[2]);
        
        boolean isDegenerate = isDegenerateTriangle(v[0], v[1], v[2]);
        TriangleData triData = processTriangle(index, v, t);
        
        if (triData != null && !isDegenerate) {
            vertices[index[0]].triangles.add(triData);
            vertices[index[1]].triangles.add(triData);
            vertices[index[2]].triangles.add(triData);
        }

        Vector3f vTemp = v[0];
        v[0] = v[1];
        v[1] = v[2];
        v[2] = vTemp;

        Vector2f tTemp = t[0];
        t[0] = t[1];
        t[1] = t[2];
        t[2] = tTemp;

        index[0] = index[1];
        index[1] = index[2];
    }

    return vertices;
}
 
Example #26
Source File: TangentBinormalGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static VertexData[] processTriangleFan(Mesh mesh,
        int[] index, Vector3f[] v, Vector2f[] t)
{
    IndexBuffer indexBuffer =  mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();

    VertexData[] vertices = initVertexData(vertexBuffer.capacity() / 3);

    index[0] = indexBuffer.get(0);
    index[1] = indexBuffer.get(1);

    populateFromBuffer(v[0], vertexBuffer, index[0]);
    populateFromBuffer(v[1], vertexBuffer, index[1]);

    populateFromBuffer(t[0], textureBuffer, index[0]);
    populateFromBuffer(t[1], textureBuffer, index[1]);

    for (int i = 2; i < vertexBuffer.capacity() / 3; i++) {
        index[2] = indexBuffer.get(i);
        populateFromBuffer(v[2], vertexBuffer, index[2]);
        populateFromBuffer(t[2], textureBuffer, index[2]);

        TriangleData triData = processTriangle(index, v, t);
        if (triData != null) {
            vertices[index[0]].triangles.add(triData);
            vertices[index[1]].triangles.add(triData);
            vertices[index[2]].triangles.add(triData);
        }

        Vector3f vTemp = v[1];
        v[1] = v[2];
        v[2] = vTemp;

        Vector2f tTemp = t[1];
        t[1] = t[2];
        t[2] = tTemp;

        index[1] = index[2];
    }

    return vertices;
}
 
Example #27
Source File: LODGeomap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public VerboseBuffer(IndexBuffer d) {
    this.delegate = d;
}
 
Example #28
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 4 votes vote down vote up
private static List<VertexData> processTriangleFan(Mesh mesh, int[] index, Vector3f[] v, Vector2f[] t) {
	IndexBuffer indexBuffer = mesh.getIndexBuffer();
	FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
	FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();

	List<VertexData> vertices = initVertexData(vertexBuffer.limit() / 3);

	index[0] = indexBuffer.get(0);
	index[1] = indexBuffer.get(1);

	populateFromBuffer(v[0], vertexBuffer, index[0]);
	populateFromBuffer(v[1], vertexBuffer, index[1]);

	populateFromBuffer(t[0], textureBuffer, index[0]);
	populateFromBuffer(t[1], textureBuffer, index[1]);

	for (int i = 2; i < vertexBuffer.limit() / 3; i++) {
		index[2] = indexBuffer.get(i);
		populateFromBuffer(v[2], vertexBuffer, index[2]);
		populateFromBuffer(t[2], textureBuffer, index[2]);

		TriangleData triData = processTriangle(index, v, t);
		if (triData != null) {
			vertices.get(index[0]).triangles.add(triData);
			vertices.get(index[1]).triangles.add(triData);
			vertices.get(index[2]).triangles.add(triData);
		}

		Vector3f vTemp = v[1];
		v[1] = v[2];
		v[2] = vTemp;

		Vector2f tTemp = t[1];
		t[1] = t[2];
		t[2] = tTemp;

		index[1] = index[2];
	}

	return vertices;
}
 
Example #29
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 4 votes vote down vote up
private static List<VertexData> processTriangleStrip(Mesh mesh, int[] index, Vector3f[] v, Vector2f[] t) {
	IndexBuffer indexBuffer = mesh.getIndexBuffer();
	FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
	FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();

	List<VertexData> vertices = initVertexData(vertexBuffer.limit() / 3);

	index[0] = indexBuffer.get(0);
	index[1] = indexBuffer.get(1);

	populateFromBuffer(v[0], vertexBuffer, index[0]);
	populateFromBuffer(v[1], vertexBuffer, index[1]);

	populateFromBuffer(t[0], textureBuffer, index[0]);
	populateFromBuffer(t[1], textureBuffer, index[1]);

	for (int i = 2; i < indexBuffer.size(); i++) {
		index[2] = indexBuffer.get(i);
		BufferUtils.populateFromBuffer(v[2], vertexBuffer, index[2]);
		BufferUtils.populateFromBuffer(t[2], textureBuffer, index[2]);

		boolean isDegenerate = isDegenerateTriangle(v[0], v[1], v[2]);
		TriangleData triData = processTriangle(index, v, t);

		if (triData != null && !isDegenerate) {
			vertices.get(index[0]).triangles.add(triData);
			vertices.get(index[1]).triangles.add(triData);
			vertices.get(index[2]).triangles.add(triData);
		}

		Vector3f vTemp = v[0];
		v[0] = v[1];
		v[1] = v[2];
		v[2] = vTemp;

		Vector2f tTemp = t[0];
		t[0] = t[1];
		t[1] = t[2];
		t[2] = tTemp;

		index[0] = index[1];
		index[1] = index[2];
	}

	return vertices;
}
 
Example #30
Source File: TangentBinormalGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static List<VertexData> processTriangleStrip(Mesh mesh,
        int[] index, Vector3f[] v, Vector2f[] t) {
    IndexBuffer indexBuffer = mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();
    
    List<VertexData> vertices = initVertexData(vertexBuffer.limit() / 3);
    
    index[0] = indexBuffer.get(0);
    index[1] = indexBuffer.get(1);
    
    populateFromBuffer(v[0], vertexBuffer, index[0]);
    populateFromBuffer(v[1], vertexBuffer, index[1]);
    
    populateFromBuffer(t[0], textureBuffer, index[0]);
    populateFromBuffer(t[1], textureBuffer, index[1]);
    
    for (int i = 2; i < indexBuffer.size(); i++) {
        index[2] = indexBuffer.get(i);
        BufferUtils.populateFromBuffer(v[2], vertexBuffer, index[2]);
        BufferUtils.populateFromBuffer(t[2], textureBuffer, index[2]);
        
        boolean isDegenerate = isDegenerateTriangle(v[0], v[1], v[2]);
        TriangleData triData = processTriangle(index, v, t);
        
        if (!isDegenerate) {
            vertices.get(index[0]).triangles.add(triData);
            vertices.get(index[1]).triangles.add(triData);
            vertices.get(index[2]).triangles.add(triData);
        }
        
        Vector3f vTemp = v[0];
        v[0] = v[1];
        v[1] = v[2];
        v[2] = vTemp;
        
        Vector2f tTemp = t[0];
        t[0] = t[1];
        t[1] = t[2];
        t[2] = tTemp;
        
        index[0] = index[1];
        index[1] = index[2];
    }
    
    return vertices;
}