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

The following examples show how to use com.jme3.scene.Mesh#clearBuffer() . 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: WrappedIndexBuffer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void convertToList(Mesh mesh){
    IndexBuffer inBuf = mesh.getIndicesAsList();
    IndexBuffer outBuf = IndexBuffer.createIndexBuffer(mesh.getVertexCount(),
                                                       inBuf.size());

    for (int i = 0; i < inBuf.size(); i++){
        outBuf.put(i, inBuf.get(i));
    }

    mesh.clearBuffer(Type.Index);
    switch (mesh.getMode()){
        case LineLoop:
        case LineStrip:
            mesh.setMode(Mode.Lines);
            break;
        case TriangleStrip:
        case TriangleFan:
            mesh.setMode(Mode.Triangles);
            break;
        default:
            break;
    }
    mesh.setBuffer(Type.Index, 3, outBuf.getFormat(), outBuf.getBuffer());
}
 
Example 2
Source File: MikkTSpaceImpl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public MikkTSpaceImpl(Mesh mesh) {
    this.mesh = mesh;
    //replacing any existing tangent buffer, if you came here you want them new.
    mesh.clearBuffer(VertexBuffer.Type.Tangent);
    FloatBuffer fb = BufferUtils.createFloatBuffer(mesh.getVertexCount() * 4);
    mesh.setBuffer(VertexBuffer.Type.Tangent, 4, fb);
}
 
Example 3
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 5 votes vote down vote up
private static void writeColorBuffer(List<VertexData> vertices, ColorRGBA[] cols, Mesh mesh) {
	FloatBuffer colors = BufferUtils.createFloatBuffer(vertices.size() * 4);
	colors.rewind();
	for (ColorRGBA color : cols) {
		colors.put(color.r);
		colors.put(color.g);
		colors.put(color.b);
		colors.put(color.a);
	}
	mesh.clearBuffer(Type.Color);
	mesh.setBuffer(Type.Color, 4, colors);
}
 
Example 4
Source File: ModelConverter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void optimize(Mesh mesh, boolean toFixed){
        // update any data that need updating
        mesh.updateBound();
        mesh.updateCounts();

        // set all buffers into STATIC_DRAW mode
        mesh.setStatic();

        if (mesh.getBuffer(Type.Index) != null){
            // compress index buffer from UShort to UByte (if possible)
            FloatToFixed.compressIndexBuffer(mesh);

            // generate triangle strips stitched with degenerate tris
            generateStrips(mesh, false, false, 16, 0);
        }

        IntMap<VertexBuffer> bufs = mesh.getBuffers();
        for (Entry<VertexBuffer> entry : bufs){
            VertexBuffer vb = entry.getValue();
            if (vb == null || vb.getBufferType() == Type.Index)
                continue;

             if (vb.getFormat() == Format.Float){
                if (vb.getBufferType() == Type.Color){
                    // convert the color buffer to UByte
                    vb = FloatToFixed.convertToUByte(vb);
                    vb.setNormalized(true);
                }else if (toFixed){
                    // convert normals, positions, and texcoords
                    // to fixed-point (16.16)
                    vb = FloatToFixed.convertToFixed(vb);
//                    vb = FloatToFixed.convertToFloat(vb);
                }
                mesh.clearBuffer(vb.getBufferType());
                mesh.setBuffer(vb);
            }
        }
        mesh.setInterleaved();
    }
 
Example 5
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 6
Source File: WrappedIndexBuffer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void convertToList(Mesh mesh){
    IndexBuffer inBuf = mesh.getIndicesAsList();
    IndexBuffer outBuf = IndexBuffer.createIndexBuffer(mesh.getVertexCount(),
                                                       inBuf.size());

    for (int i = 0; i < inBuf.size(); i++){
        outBuf.put(i, inBuf.get(i));
    }

    mesh.clearBuffer(Type.Index);
    switch (mesh.getMode()){
        case LineLoop:
        case LineStrip:
            mesh.setMode(Mode.Lines);
            break;
        case TriangleStrip:
        case TriangleFan:
            mesh.setMode(Mode.Triangles);
            break;
        default:
            break;
    }
    if (outBuf instanceof IndexIntBuffer){
        mesh.setBuffer(Type.Index, 3, (IntBuffer)outBuf.getBuffer());
    }else{
        mesh.setBuffer(Type.Index, 3, (ShortBuffer)outBuf.getBuffer());
    }
}
 
Example 7
Source File: GenerateTangentsTool.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void doUndoTool(AbstractSceneExplorerNode rootNode, Object undoObject) {
    Geometry geom = rootNode.getLookup().lookup(Geometry.class);
    Mesh mesh = geom.getMesh();
    if (mesh != null) {
        mesh.clearBuffer(Type.Tangent);
    }
}
 
Example 8
Source File: EntropyComputeUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices){
    // Bounding box for the terrain block
    BoundingBox bbox = (BoundingBox) terrainBlock.getBound();

    // Vertex positions for the block
    FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position);

    // Prepare to cast rays
    Vector3f pos = new Vector3f();
    Vector3f dir = new Vector3f(0, -1, 0);
    Ray ray = new Ray(pos, dir);

    // Prepare collision results
    CollisionResults results = new CollisionResults();

    // Set the LOD indices on the block
    VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index);

    terrainBlock.clearBuffer(Type.Index);
    if (lodIndices instanceof IntBuffer)
        terrainBlock.setBuffer(Type.Index, 3, (IntBuffer)lodIndices);
    else if (lodIndices instanceof ShortBuffer) {
        terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices);
    } else {
        terrainBlock.setBuffer(Type.Index, 3, (ByteBuffer) lodIndices);
    }

    // Recalculate collision mesh
    terrainBlock.createCollisionData();

    float entropy = 0;
    for (int i = 0; i < positions.limit() / 3; i++){
        BufferUtils.populateFromBuffer(pos, positions, i);

        float realHeight = pos.y;

        pos.addLocal(0, bbox.getYExtent(), 0);
        ray.setOrigin(pos);

        results.clear();
        terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results);

        if (results.size() > 0){
            Vector3f contactPoint = results.getClosestCollision().getContactPoint();
            float delta = Math.abs(realHeight - contactPoint.y);
            entropy = Math.max(delta, entropy);
        }
    }

    // Restore original indices
    terrainBlock.clearBuffer(Type.Index);
    terrainBlock.setBuffer(originalIndices);

    return entropy;
}
 
Example 9
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 4 votes vote down vote up
public static void generate(Mesh mesh, boolean approxTangents, boolean splitMirrored) {
	int[] index = new int[3];
	Vector3f[] v = new Vector3f[3];
	Vector2f[] t = new Vector2f[3];
	for (int i = 0; i < 3; i++) {
		v[i] = new Vector3f();
		t[i] = new Vector2f();
	}

	if (mesh.getBuffer(Type.Normal) == null) {
		throw new IllegalArgumentException("The given mesh has no normal data!");
	}

	List<VertexData> vertices;
	switch (mesh.getMode()) {
		case Triangles:
			vertices = processTriangles(mesh, index, v, t, splitMirrored);
			if (splitMirrored) {
				splitVertices(mesh, vertices, splitMirrored);
			}
			break;
		case TriangleStrip:
			vertices = processTriangleStrip(mesh, index, v, t);
			break;
		case TriangleFan:
			vertices = processTriangleFan(mesh, index, v, t);
			break;
		default:
			throw new UnsupportedOperationException(mesh.getMode() + " is not supported.");
	}

	processTriangleData(mesh, vertices, approxTangents, splitMirrored);

	// if the mesh has a bind pose, we need to generate the bind pose for the tangent buffer
	if (mesh.getBuffer(Type.BindPosePosition) != null) {

		VertexBuffer tangents = mesh.getBuffer(Type.Tangent);
		if (tangents != null) {
			VertexBuffer bindTangents = new VertexBuffer(Type.BindPoseTangent);
			bindTangents.setupData(Usage.CpuOnly, 4, Format.Float, BufferUtils.clone(tangents.getData()));

			if (mesh.getBuffer(Type.BindPoseTangent) != null) {
				mesh.clearBuffer(Type.BindPoseTangent);
			}
			mesh.setBuffer(bindTangents);
			tangents.setUsage(Usage.Stream);
		}
	}
}
 
Example 10
Source File: EntropyComputeUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices){
    // Bounding box for the terrain block
    BoundingBox bbox = (BoundingBox) terrainBlock.getBound();

    // Vertex positions for the block
    FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position);

    // Prepare to cast rays
    Vector3f pos = new Vector3f();
    Vector3f dir = new Vector3f(0, -1, 0);
    Ray ray = new Ray(pos, dir);

    // Prepare collision results
    CollisionResults results = new CollisionResults();

    // Set the LOD indices on the block
    VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index);

    terrainBlock.clearBuffer(Type.Index);
    if (lodIndices instanceof IntBuffer)
        terrainBlock.setBuffer(Type.Index, 3, (IntBuffer)lodIndices);
    else if (lodIndices instanceof ShortBuffer) {
        terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices);
    }

    // Recalculate collision mesh
    terrainBlock.createCollisionData();

    float entropy = 0;
    for (int i = 0; i < positions.limit() / 3; i++){
        BufferUtils.populateFromBuffer(pos, positions, i);

        float realHeight = pos.y;

        pos.addLocal(0, bbox.getYExtent(), 0);
        ray.setOrigin(pos);

        results.clear();
        terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results);

        if (results.size() > 0){
            Vector3f contactPoint = results.getClosestCollision().getContactPoint();
            float delta = Math.abs(realHeight - contactPoint.y);
            entropy = Math.max(delta, entropy);
        }
    }

    // Restore original indices
    terrainBlock.clearBuffer(Type.Index);
    terrainBlock.setBuffer(originalIndices);

    return entropy;
}