Java Code Examples for com.jme3.scene.Mesh#getIndexBuffer()

The following examples show how to use com.jme3.scene.Mesh#getIndexBuffer() . 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 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 2
Source File: WrappedIndexBuffer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public WrappedIndexBuffer(Mesh mesh){
    super(mesh.getVertexCount(), mesh.getMode());
    this.ib = mesh.getIndexBuffer();
    switch (meshMode){
        case Points:
            numIndices = mesh.getTriangleCount();
            break;
        case Lines:
        case LineLoop:
        case LineStrip:
            numIndices = mesh.getTriangleCount() * 2;
            break;
        case Triangles:
        case TriangleStrip:
        case TriangleFan:
            numIndices = mesh.getTriangleCount() * 3;
            break;
        default:
            throw new UnsupportedOperationException();
    }
}
 
Example 3
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 4
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 5
Source File: WrappedIndexBuffer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public WrappedIndexBuffer(Mesh mesh){
    super(mesh.getVertexCount(), mesh.getMode());
    this.ib = mesh.getIndexBuffer();
    switch (meshMode){
        case Points:
            numIndices = mesh.getTriangleCount();
            break;
        case Lines:
        case LineLoop:
        case LineStrip:
            numIndices = mesh.getTriangleCount() * 2;
            break;
        case Triangles:
        case TriangleStrip:
        case TriangleFan:
            numIndices = mesh.getTriangleCount() * 3;
            break;
        default:
            throw new UnsupportedOperationException();
    }
}
 
Example 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 14
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 15
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;
}