Example usage for com.badlogic.gdx.graphics Mesh getVertexSize

List of usage examples for com.badlogic.gdx.graphics Mesh getVertexSize

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics Mesh getVertexSize.

Prototype

public int getVertexSize() 

Source Link

Usage

From source file:com.badlogic.gdx.physics.bullet.demo.simulationobjects.MeshSimulationObject.java

public static btBvhTriangleMeshShape createTriangleMeshShape(Mesh mesh,
        AtomicReference<btTriangleMesh> triangleMesh) {
    final btTriangleMesh m = new btTriangleMesh();

    final ShortBuffer indices = mesh.getIndicesBuffer();
    indices.rewind();/*www.  j  ava2 s  . c  om*/
    final FloatBuffer vertices = mesh.getVerticesBuffer();
    vertices.rewind();

    // Some meshes have vertices but no indices declared
    final boolean hasIndices = mesh.getNumIndices() != 0;
    final int vertexStride = mesh.getVertexSize() / 4;

    final Vector3 v0 = Pools.VECTOR3.obtain();
    final Vector3 v1 = Pools.VECTOR3.obtain();
    final Vector3 v2 = Pools.VECTOR3.obtain();

    // Set up a small array to keep the loop code simpler
    final Vector3[] vectors = new Vector3[] { v0, v1, v2 };
    short vectorIndex = 0;

    int i = -1;
    int verticesRead = 0;
    while (verticesRead < mesh.getNumVertices()) {
        if (hasIndices) {
            i = indices.get();
        } else {
            i++;
        }

        vectors[vectorIndex++].set(vertices.get(i * vertexStride), vertices.get(i * vertexStride + 1),
                vertices.get(i * vertexStride + 2));

        if (vectorIndex == vectors.length) {
            m.addTriangle(v0, v1, v2, true);
            vectorIndex = 0;
        }

        verticesRead++;
    }

    Pools.VECTOR3.free(v0);
    Pools.VECTOR3.free(v1);
    Pools.VECTOR3.free(v2);

    triangleMesh.set(m);
    return new btBvhTriangleMeshShape(m, true);
}

From source file:com.mygdx.game.pathfinding.NavMeshGraph.java

License:Apache License

/**
 * Get an array of the vertex indices from the mesh. Any vertices which share the same position will be counted
 * as a single vertex and share the same index. That is, position duplicates will be filtered out.
 *
 * @param mesh/*from   w ww .  j a  v a2  s.  c  o  m*/
 * @return
 */
private static short[] getUniquePositionVertexIndices(Mesh mesh) {
    FloatBuffer verticesBuffer = mesh.getVerticesBuffer();
    int positionOffset = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position).offset / 4;
    // Number of array elements which make up a vertex
    int vertexSize = mesh.getVertexSize() / 4;
    // The indices tell us which vertices are part of a triangle.
    short[] indices = new short[mesh.getNumIndices()];
    mesh.getIndices(indices);
    // Marks true if an index has already been compared to avoid unnecessary comparisons
    Bits handledIndices = new Bits(mesh.getNumIndices());

    for (int i = 0; i < indices.length; i++) {
        short indexI = indices[i];
        if (handledIndices.get(indexI)) {
            // Index handled in an earlier iteration
            continue;
        }
        int vBufIndexI = indexI * vertexSize + positionOffset;
        float xi = verticesBuffer.get(vBufIndexI++);
        float yi = verticesBuffer.get(vBufIndexI++);
        float zi = verticesBuffer.get(vBufIndexI++);
        for (int j = i + 1; j < indices.length; j++) {
            short indexJ = indices[j];
            int vBufIndexJ = indexJ * vertexSize + positionOffset;
            float xj = verticesBuffer.get(vBufIndexJ++);
            float yj = verticesBuffer.get(vBufIndexJ++);
            float zj = verticesBuffer.get(vBufIndexJ++);
            if (xi == xj && yi == yj && zi == zj) {
                indices[j] = indexI;
            }
        }
        handledIndices.set(indexI);
    }
    return indices;
}

From source file:com.mygdx.game.pathfinding.NavMeshGraph.java

License:Apache License

/**
 * Creates Vector3 objects from the vertices of the mesh. The resulting array follows the ordering of the provided
 * index array./* w w  w.j  a  v  a 2s .c  o  m*/
 *
 * @param mesh
 * @param indices
 * @return
 */
private static Vector3[] createVertexVectors(Mesh mesh, short[] indices) {
    FloatBuffer verticesBuffer = mesh.getVerticesBuffer();
    int positionOffset = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position).offset / 4;
    int vertexSize = mesh.getVertexSize() / 4;
    Vector3[] vertexVectors = new Vector3[mesh.getNumIndices()];
    for (int i = 0; i < indices.length; i++) {
        short index = indices[i];
        int a = index * vertexSize + positionOffset;
        float x = verticesBuffer.get(a++);
        float y = verticesBuffer.get(a++);
        float z = verticesBuffer.get(a);
        vertexVectors[index] = new Vector3(x, y, z);
    }
    return vertexVectors;
}

From source file:com.mygdx.game.scene.GameObjectBlueprint.java

License:Apache License

private void setRigidBody(BlenderEmpty blenderEmpty, Model model, Vector3 scale) {
    this.shapeType = blenderEmpty.custom_properties.get(blenderCollisionShapeField);
    this.mass = Float.parseFloat(blenderEmpty.custom_properties.get(blenderMassField));

    tmpScale.set(blenderEmpty.scale.x * scale.x, blenderEmpty.scale.y * scale.y,
            blenderEmpty.scale.z * scale.z);

    if (shapeType.equals("capsule")) {
        float radius = Math.max(tmpScale.x, tmpScale.z);
        shape = new btCapsuleShape(radius, scale.y);

    } else if (shapeType.equals("sphere")) {
        float radius = Math.max(Math.max(tmpScale.x, tmpScale.y), tmpScale.z);
        shape = new btSphereShape(radius);

    } else if (shapeType.equals("box")) {
        shape = new btBoxShape(tmpScale);

    } else if (shapeType.equals("convex_hull")) {
        // We need a model instance with the correct scale
        ModelInstance modelInstance = new ModelInstance(model);
        GameModel.applyTransform(position, rotation, scale, modelInstance);
        // Copy the vertices to a work buffer, where we apply the model global transform to them
        Matrix4 transform = new Matrix4(modelInstance.nodes.get(0).globalTransform);
        Mesh mesh = modelInstance.model.meshes.get(0);
        FloatBuffer workBuffer = BufferUtils.newFloatBuffer(mesh.getVerticesBuffer().capacity());
        BufferUtils.copy(mesh.getVerticesBuffer(), workBuffer,
                mesh.getNumVertices() * mesh.getVertexSize() / 4);
        BufferUtils.transform(workBuffer, 3, mesh.getVertexSize(), mesh.getNumVertices(), transform);

        // First create a shape using all the vertices, then use the built in tool to reduce
        // the number of vertices to a manageable amount.
        btConvexShape convexShape = new btConvexHullShape(workBuffer, mesh.getNumVertices(),
                mesh.getVertexSize());// w  ww. j  a v  a2 s  .c  o m
        btShapeHull hull = new btShapeHull(convexShape);
        hull.buildHull(convexShape.getMargin());
        shape = new btConvexHullShape(hull);
        convexShape.dispose();
        hull.dispose();

    } else if (shapeType.equals("none")) {
        shape = null;

    } else {
        throw new GdxRuntimeException("Cannot load collision shape data for " + blenderEmpty.name + " from '"
                + blenderEmpty.name + "'");
    }
    setCollisionFlags(this.mass);
}

From source file:mobi.shad.s3lib.gfx.g3d.simpleobject.DebugObject.java

License:Apache License

public static void debugMesh(Mesh mesh) {

    int numVertices = mesh.getNumVertices();
    int numIndices = mesh.getNumIndices();
    ShortBuffer indicesBuffer = mesh.getIndicesBuffer();
    FloatBuffer verticesBuffer = mesh.getVerticesBuffer();

    S3Log.log("debugMesh", "NumVertices: " + numVertices);
    S3Log.log("debugMesh", "NumIndices: " + numIndices);
    S3Log.log("debugMesh", "vertexSize (bytes): " + mesh.getVertexSize());

    S3Log.log("debugMesh", "Indices Buffer:", 1);
    StringBuilder buffer = new StringBuilder(32);
    buffer.append('[');
    buffer.append(indicesBuffer.get(0));
    for (int i = 1; i < numIndices; i++) {
        buffer.append(", ");
        buffer.append(indicesBuffer.get(i));
    }//w  ww. ja  v  a 2s .  c om
    buffer.append(']');
    S3Log.log("debugMesh", buffer.toString());

    S3Log.log("debugMesh", "Vertices Buffer:", 1);
    StringBuilder buffer2 = new StringBuilder(32);
    buffer2.append('[');
    buffer2.append(verticesBuffer.get(0));
    for (int i = 1; i < numIndices; i++) {
        buffer2.append(", ");
        buffer2.append(verticesBuffer.get(i));
    }
    buffer.append(']');
    S3Log.log("debugMesh", buffer2.toString());
}