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

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

Introduction

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

Prototype

public int getNumVertices() 

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  a  v  a  2s  . c  o  m*/
    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.lyeeedar.Roguelike3D.Graphics.Models.Shapes.java

License:Open Source License

public static Mesh insertLight(Mesh mesh, LightManager lights, boolean bakeStatics, Matrix4 model_matrix) {
    VertexAttributes attributes = mesh.getVertexAttributes();
    final int vertCount = mesh.getNumVertices();
    final int vertexSize = attributes.vertexSize / 4;

    VertexAttribute[] newAttributes = new VertexAttribute[attributes.size() + 1];
    for (int i = 0; i < attributes.size(); i++) {
        newAttributes[i] = attributes.get(i);
    }// w  w  w .j  a va  2 s .c o  m
    newAttributes[attributes.size()] = new VertexAttribute(Usage.Generic, 3, "a_baked_light");

    final int newVertexSize = vertexSize + 3;

    float[] verts = new float[vertexSize * vertCount];
    mesh.getVertices(verts);
    short[] indices = new short[mesh.getNumIndices()];
    mesh.getIndices(indices);
    float[] newVerts = new float[newVertexSize * vertCount];

    int positionOffset = attributes.getOffset(Usage.Position);
    int normalOffset = attributes.getOffset(Usage.Normal);

    Matrix4 normal_matrix = new Matrix4();
    normal_matrix.set(model_matrix);

    Vector3 position = new Vector3();

    for (int i = 0; i < vertCount; i++) {
        int j = 0;
        for (; j < vertexSize; j++) {
            newVerts[(i * newVertexSize) + j] = verts[(i * vertexSize) + j];
        }

        position.set(verts[(i * vertexSize) + positionOffset], verts[(i * vertexSize) + positionOffset + 1],
                verts[(i * vertexSize) + positionOffset + 2]).mul(model_matrix);

        Vector3 normal = new Vector3(verts[(i * vertexSize) + normalOffset],
                verts[(i * vertexSize) + normalOffset + 1], verts[(i * vertexSize) + normalOffset + 2]);
        normal.rot(normal_matrix).nor();

        Color light_colour = lights.calculateLightAtPoint(position, normal, bakeStatics);

        newVerts[(i * newVertexSize) + j + 0] = light_colour.r;
        newVerts[(i * newVertexSize) + j + 1] = light_colour.g;
        newVerts[(i * newVertexSize) + j + 2] = light_colour.b;
    }

    Mesh newMesh = new Mesh(true, mesh.getNumVertices(), mesh.getNumIndices(), newAttributes);
    newMesh.setVertices(newVerts);
    newMesh.setIndices(indices);

    return newMesh;
}

From source file:com.lyeeedar.Roguelike3D.Graphics.Models.Shapes.java

License:Open Source License

public static Mesh insertTangents(Mesh mesh) {
    VertexAttributes attributes = mesh.getVertexAttributes();
    final int vertCount = mesh.getNumVertices();
    final int vertexSize = attributes.vertexSize / 4;

    VertexAttribute[] newAttributes = new VertexAttribute[attributes.size() + 1];
    for (int i = 0; i < attributes.size(); i++) {
        newAttributes[i] = attributes.get(i);
    }/*from   w w w  .j av a  2 s  .c o  m*/
    newAttributes[attributes.size()] = new VertexAttribute(Usage.Generic, 4, "a_tangent");

    final int newVertexSize = vertexSize + 4;

    float[] verts = new float[vertexSize * vertCount];
    mesh.getVertices(verts);
    short[] indices = new short[mesh.getNumIndices()];
    mesh.getIndices(indices);
    float[] newVerts = new float[newVertexSize * vertCount];

    int positionOffset = attributes.getOffset(Usage.Position);
    int normalOffset = attributes.getOffset(Usage.Normal);
    int textureOffset = attributes.getOffset(Usage.TextureCoordinates);

    int tangentOffset = 0;
    for (int i = 0; i < vertCount; i += 3) {
        int j = 0;
        for (; j < vertexSize; j++) {
            newVerts[(i * newVertexSize) + j] = verts[(i * vertexSize) + j];
            newVerts[((i + 1) * newVertexSize) + j] = verts[((i + 1) * vertexSize) + j];
            newVerts[((i + 2) * newVertexSize) + j] = verts[((i + 2) * vertexSize) + j];
        }
        tangentOffset = j;
    }
    for (int i = 0; i < mesh.getNumIndices(); i += 3) {
        int i1 = indices[i];
        int i2 = indices[i + 1];
        int i3 = indices[i + 2];

        Vector3 v1 = new Vector3(verts[(i1 * vertexSize) + positionOffset],
                verts[(i1 * vertexSize) + positionOffset] + 1, verts[(i1 * vertexSize) + positionOffset] + 2);
        Vector3 v2 = new Vector3(verts[(i2 * vertexSize) + positionOffset],
                verts[(i2 * vertexSize) + positionOffset] + 1, verts[(i2 * vertexSize) + positionOffset] + 2);
        Vector3 v3 = new Vector3(verts[(i3 * vertexSize) + positionOffset],
                verts[(i3 * vertexSize) + positionOffset] + 1, verts[(i3 * vertexSize) + positionOffset] + 2);

        float[] w1 = { verts[(i1 * vertexSize) + textureOffset], verts[(i1 * vertexSize) + textureOffset] + 1 };
        float[] w2 = { verts[(i2 * vertexSize) + textureOffset], verts[(i2 * vertexSize) + textureOffset] + 1 };
        float[] w3 = { verts[(i3 * vertexSize) + textureOffset], verts[(i3 * vertexSize) + textureOffset] + 1 };

        float x1 = v2.x - v1.x;
        float x2 = v3.x - v1.x;
        float y1 = v2.y - v1.y;
        float y2 = v3.y - v1.y;
        float z1 = v2.z - v1.z;
        float z2 = v3.z - v1.z;

        float s1 = w2[0] - w1[0];
        float s2 = w3[0] - w1[0];
        float t1 = w2[1] - w1[1];
        float t2 = w3[1] - w1[1];

        float div = s1 * t2 - s2 * t1;
        float r = div == 0.0f ? 0.0f : 1.0f / div;

        Vector3 t = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
        Vector3 n = new Vector3(verts[(i1 * vertexSize) + normalOffset],
                verts[(i1 * vertexSize) + normalOffset] + 1, verts[(i1 * vertexSize) + normalOffset] + 2);

        //Vector3 tangent = t.cpy().sub(n).mul(n.tmp().dot(t)).nor();
        Vector3 tangent = orthoNormalize(n, t);

        System.out.println(t2);

        Vector3 tan2 = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
        float handedness = (n.tmp().crs(t).dot(tan2) < 0.0f) ? -1.0f : 1.0f;

        Vector3 c1 = n.cpy().crs(0.0f, 0.0f, 1.0f);
        Vector3 c2 = n.cpy().crs(0.0f, 1.0f, 0.0f);

        if (c1.len2() > c2.len2()) {
            tangent = c1;
        } else {
            tangent = c2;
        }

        newVerts[(i1 * newVertexSize) + tangentOffset] = tangent.x;
        newVerts[(i1 * newVertexSize) + tangentOffset + 1] = tangent.y;
        newVerts[(i1 * newVertexSize) + tangentOffset + 2] = tangent.z;
        newVerts[(i1 * newVertexSize) + tangentOffset + 3] = handedness;

        newVerts[(i2 * newVertexSize) + tangentOffset] = tangent.x;
        newVerts[(i2 * newVertexSize) + tangentOffset + 1] = tangent.y;
        newVerts[(i2 * newVertexSize) + tangentOffset + 2] = tangent.z;
        newVerts[(i2 * newVertexSize) + tangentOffset + 3] = handedness;

        newVerts[(i3 * newVertexSize) + tangentOffset] = tangent.x;
        newVerts[(i3 * newVertexSize) + tangentOffset + 1] = tangent.y;
        newVerts[(i3 * newVertexSize) + tangentOffset + 2] = tangent.z;
        newVerts[(i3 * newVertexSize) + tangentOffset + 3] = handedness;
    }

    Mesh newMesh = new Mesh(true, mesh.getNumVertices(), mesh.getNumIndices(), newAttributes);
    newMesh.setVertices(newVerts);
    newMesh.setIndices(indices);

    return newMesh;
}

From source file:com.lyeeedar.Roguelike3D.Graphics.Models.Shapes.java

License:Open Source License

public static Mesh copyMesh(Mesh mesh) {
    VertexAttributes attributes = mesh.getVertexAttributes();
    final int vertCount = mesh.getNumVertices();
    final int vertexSize = attributes.vertexSize / 4;

    float[] verts = new float[vertexSize * vertCount];
    mesh.getVertices(verts);/*  w  ww.j ava  2s  . c om*/
    short[] indices = new short[mesh.getNumIndices()];
    mesh.getIndices(indices);

    Mesh newMesh = new Mesh(true, mesh.getNumVertices(), mesh.getNumIndices(), attributes);
    newMesh.setVertices(verts);
    newMesh.setIndices(indices);

    return newMesh;
}

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  w  w .ja v  a  2s.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));
    }/*from  w w  w  .  j a va 2s.  c o  m*/
    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());
}