Java Code Examples for com.jme3.scene.VertexBuffer#getDataReadOnly()

The following examples show how to use com.jme3.scene.VertexBuffer#getDataReadOnly() . 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: RagdollUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test whether the indexed bone has at least one vertex in the specified
 * meshes with a weight greater than the specified threshold.
 *
 * @param boneIndex the index of the bone (≥0)
 * @param targets the meshes to search (not null, no null elements)
 * @param weightThreshold the threshold (≥0, ≤1)
 * @return true if at least 1 vertex found, otherwise false
 */
public static boolean hasVertices(int boneIndex, Mesh[] targets,
        float weightThreshold) {
    for (Mesh mesh : targets) {
        VertexBuffer biBuf = mesh.getBuffer(VertexBuffer.Type.BoneIndex);
        Buffer boneIndices = biBuf.getDataReadOnly();
        FloatBuffer boneWeight
                = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();

        boneIndices.rewind();
        boneWeight.rewind();

        int vertexComponents = mesh.getVertexCount() * 3;
        for (int i = 0; i < vertexComponents; i += 3) {
            int start = i / 3 * 4;
            for (int k = start; k < start + 4; k++) {
                if (readIndex(boneIndices, k) == boneIndex
                        && boneWeight.get(k) >= weightThreshold) {
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example 2
Source File: TestIssue1004.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleInitApp() {
    BulletAppState bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    String sinbadPath = "Models/Sinbad/SinbadOldAnim.j3o";
    Node sinbad = (Node) assetManager.loadModel(sinbadPath);

    Geometry geometry = (Geometry) sinbad.getChild(0);
    Mesh mesh = geometry.getMesh();
    VertexBuffer.Type bufferType = VertexBuffer.Type.BoneIndex;
    VertexBuffer vertexBuffer = mesh.getBuffer(bufferType);

    // Remove the existing bone-index buffer.
    mesh.getBufferList().remove(vertexBuffer);
    mesh.getBuffers().remove(bufferType.ordinal());

    // Copy the 8-bit bone indices to 16-bit indices.
    ByteBuffer oldBuffer = (ByteBuffer) vertexBuffer.getDataReadOnly();
    int numComponents = oldBuffer.limit();
    oldBuffer.rewind();
    short[] shortArray = new short[numComponents];
    for (int index = 0; oldBuffer.hasRemaining(); ++index) {
        shortArray[index] = oldBuffer.get();
    }

    // Add the 16-bit bone indices to the mesh.
    mesh.setBuffer(bufferType, 4, shortArray);

    KinematicRagdollControl ragdoll = new KinematicRagdollControl(0.5f);
    sinbad.addControl(ragdoll);

    stop();
}
 
Example 3
Source File: LodGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void gatherVertexData(Mesh mesh, List<Vertex> vertexLookup) {

        //in case the model is currently animating with software animation
        //attempting to retrieve the bind position instead of the position.
        VertexBuffer position = mesh.getBuffer(VertexBuffer.Type.BindPosePosition);
        if (position == null) {
            position = mesh.getBuffer(VertexBuffer.Type.Position);
        }
        FloatBuffer pos = (FloatBuffer) position.getDataReadOnly();
        pos.rewind();
        
        while (pos.remaining() != 0) {
            Vertex v = new Vertex();
            v.position.setX(pos.get());
            v.position.setY(pos.get());
            v.position.setZ(pos.get());
            v.isSeam = false;
            Vertex existingV = findSimilar(v);
            if (existingV != null) {
                //vertex position already exists
                existingV.isSeam = true;
                v.isSeam = true;
            } else {
                vertexList.add(v);
            }
            vertexLookup.add(v);
        }
        pos.rewind();
    }
 
Example 4
Source File: LodGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void gatherIndexData(Mesh mesh, List<Vertex> vertexLookup) {
    VertexBuffer indexBuffer = mesh.getBuffer(VertexBuffer.Type.Index);
    indexCount = indexBuffer.getNumElements() * 3;
    Buffer b = indexBuffer.getDataReadOnly();
    b.rewind();
    
    while (b.remaining() != 0) {
        Triangle tri = new Triangle();
        tri.isRemoved = false;
        triangleList.add(tri);            
        for (int i = 0; i < 3; i++) {
            if (b instanceof IntBuffer) {
                tri.vertexId[i] = ((IntBuffer) b).get();
            } else {
                //bit shift to avoid negative values due to conversion form short to int.
                //we need an unsigned int here.
                tri.vertexId[i] = ((ShortBuffer) b).get()& 0xffff;
            }
           // assert (tri.vertexId[i] < vertexLookup.size());
            tri.vertex[i] = vertexLookup.get(tri.vertexId[i]);
            //debug only;
            tri.vertex[i].index = tri.vertexId[i];
        }
        if (tri.isMalformed()) {
            if (!tri.isRemoved) {
                logger.log(Level.FINE, "malformed triangle found with ID:{0}\n{1} It will be excluded from Lod level calculations.", new Object[]{triangleList.indexOf(tri), tri.toString()});
                tri.isRemoved = true;
                indexCount -= 3;
            }
            
        } else {
            tri.computeNormal();
            addTriangleToEdges(tri);
        }
    }
    b.rewind();
}
 
Example 5
Source File: RagdollUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Enumerate vertices that meet the weight threshold for the indexed bone.
 *
 * @param mesh the mesh to analyze (not null)
 * @param boneIndex the index of the bone (&ge;0)
 * @param initialScale a scale applied to vertex positions (not null,
 * unaffected)
 * @param offset an offset subtracted from vertex positions (not null,
 * unaffected)
 * @param weightThreshold the minimum bone weight for inclusion in the
 * result (&ge;0, &le;1)
 * @return a new list of vertex coordinates (not null, length a multiple of
 * 3)
 */
private static List<Float> getPoints(Mesh mesh, int boneIndex, Vector3f initialScale, Vector3f offset, float weightThreshold) {

    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    VertexBuffer biBuf = mesh.getBuffer(VertexBuffer.Type.BoneIndex);
    Buffer boneIndices = biBuf.getDataReadOnly();
    FloatBuffer boneWeight = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();

    vertices.rewind();
    boneIndices.rewind();
    boneWeight.rewind();

    ArrayList<Float> results = new ArrayList<Float>();

    int vertexComponents = mesh.getVertexCount() * 3;

    for (int i = 0; i < vertexComponents; i += 3) {
        int k;
        boolean add = false;
        int start = i / 3 * 4;
        for (k = start; k < start + 4; k++) {
            if (readIndex(boneIndices, k) == boneIndex
                    && boneWeight.get(k) >= weightThreshold) {
                add = true;
                break;
            }
        }
        if (add) {

            Vector3f pos = new Vector3f();
            pos.x = vertices.get(i);
            pos.y = vertices.get(i + 1);
            pos.z = vertices.get(i + 2);
            pos.subtractLocal(offset).multLocal(initialScale);
            results.add(pos.x);
            results.add(pos.y);
            results.add(pos.z);

        }
    }

    return results;
}