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

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

Introduction

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

Prototype

public Mesh setIndices(short[] indices) 

Source Link

Document

Sets the indices of this Mesh

Usage

From source file:com.badlogic.gdx.tests.gles2.Shapes.java

License:Apache License

public static Mesh genCube() {
    Mesh mesh = new Mesh(true, 24, 36, new VertexAttribute(Usage.Position, 3, "a_position"),
            new VertexAttribute(Usage.Normal, 3, "a_normal"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "a_texcoords"));

    float[] cubeVerts = { -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f,
            0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
            0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f,
            0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f,
            0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, };

    float[] cubeNormals = { 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
            1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
            -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, };

    float[] cubeTex = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
            1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
            1.0f, 0.0f, };/*from w  w  w.j  a  va  2  s .  co m*/

    float[] vertices = new float[24 * 8];
    int pIdx = 0;
    int nIdx = 0;
    int tIdx = 0;
    for (int i = 0; i < vertices.length;) {
        vertices[i++] = cubeVerts[pIdx++];
        vertices[i++] = cubeVerts[pIdx++];
        vertices[i++] = cubeVerts[pIdx++];
        vertices[i++] = cubeNormals[nIdx++];
        vertices[i++] = cubeNormals[nIdx++];
        vertices[i++] = cubeNormals[nIdx++];
        vertices[i++] = cubeTex[tIdx++];
        vertices[i++] = cubeTex[tIdx++];
    }

    short[] indices = { 0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 15, 14, 12, 14, 13, 16, 17,
            18, 16, 18, 19, 20, 23, 22, 20, 22, 21 };

    mesh.setVertices(vertices);
    mesh.setIndices(indices);

    return mesh;
}

From source file:com.badlogic.invaders.simulation.Simulation.java

License:Apache License

private void populate() {
    ObjLoader objLoader = new ObjLoader();
    shipModel = objLoader.loadModel(Gdx.files.internal("data/ship.obj"));
    invaderModel = objLoader.loadModel(Gdx.files.internal("data/invader.obj"));
    blockModel = objLoader.loadModel(Gdx.files.internal("data/block.obj"));
    shotModel = objLoader.loadModel(Gdx.files.internal("data/shot.obj"));

    final Texture shipTexture = new Texture(Gdx.files.internal("data/ship.png"), Format.RGB565, true);
    shipTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
    final Texture invaderTexture = new Texture(Gdx.files.internal("data/invader.png"), Format.RGB565, true);
    invaderTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
    shipModel.materials.get(0).set(TextureAttribute.createDiffuse(shipTexture));
    invaderModel.materials.get(0).set(TextureAttribute.createDiffuse(invaderTexture));

    ((ColorAttribute) blockModel.materials.get(0).get(ColorAttribute.Diffuse)).color.set(0, 0, 1, 0.5f);
    blockModel.materials.get(0).set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA));

    shotModel.materials.get(0).set(ColorAttribute.createDiffuse(1, 1, 0, 1f));

    final Texture explosionTexture = new Texture(Gdx.files.internal("data/explode.png"), Format.RGBA4444, true);
    explosionTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);

    final Mesh explosionMesh = new Mesh(true, 4 * 16, 6 * 16,
            new VertexAttribute(Usage.Position, 3, "a_position"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0"));

    float[] vertices = new float[4 * 16 * (3 + 2)];
    short[] indices = new short[6 * 16];
    int idx = 0;/*from ww w.  j av a  2 s.  co  m*/
    int index = 0;
    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 4; column++) {
            vertices[idx++] = 1;
            vertices[idx++] = 1;
            vertices[idx++] = 0;
            vertices[idx++] = 0.25f + column * 0.25f;
            vertices[idx++] = 0 + row * 0.25f;

            vertices[idx++] = -1;
            vertices[idx++] = 1;
            vertices[idx++] = 0;
            vertices[idx++] = 0 + column * 0.25f;
            vertices[idx++] = 0 + row * 0.25f;

            vertices[idx++] = -1;
            vertices[idx++] = -1;
            vertices[idx++] = 0;
            vertices[idx++] = 0f + column * 0.25f;
            vertices[idx++] = 0.25f + row * 0.25f;

            vertices[idx++] = 1;
            vertices[idx++] = -1;
            vertices[idx++] = 0;
            vertices[idx++] = 0.25f + column * 0.25f;
            vertices[idx++] = 0.25f + row * 0.25f;

            final int t = (4 * row + column) * 4;
            indices[index++] = (short) (t);
            indices[index++] = (short) (t + 1);
            indices[index++] = (short) (t + 2);
            indices[index++] = (short) (t);
            indices[index++] = (short) (t + 2);
            indices[index++] = (short) (t + 3);
        }
    }

    explosionMesh.setVertices(vertices);
    explosionMesh.setIndices(indices);

    explosionModel = ModelBuilder.createFromMesh(explosionMesh, GL20.GL_TRIANGLES,
            new Material(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA),
                    TextureAttribute.createDiffuse(explosionTexture)));

    ship = new Ship(shipModel);
    ship.transform.rotate(0, 1, 0, 180);

    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 8; column++) {
            Invader invader = new Invader(invaderModel, -PLAYFIELD_MAX_X / 2 + column * 2f, 0,
                    PLAYFIELD_MIN_Z + row * 2f);
            invaders.add(invader);
        }
    }

    for (int shield = 0; shield < 3; shield++) {
        blocks.add(new Block(blockModel, -10 + shield * 10 - 1, 0, -2));
        blocks.add(new Block(blockModel, -10 + shield * 10 - 1, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 0, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 1, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 1, 0, -2));
    }
}

From source file:com.hajnar.GravityShip.GameObjects.Terrain.java

License:Apache License

public void generateMeshes() {
    ArrayList<Fixture> terrainFixtures = objectBody.getFixtureList();
    Vector2 boxVertex = new Vector2();
    meshes = new ArrayList<Mesh>();
    boundingBoxes = new ArrayList<BoundingBox>();
    EarClippingTriangulator ear = new EarClippingTriangulator();

    for (Fixture terrainFixture : terrainFixtures) {
        PolygonShape shape = (PolygonShape) terrainFixture.getShape();
        boxVertex = new Vector2();
        int vc = shape.getVertexCount();
        ArrayList<Vector2> boxVertices = new ArrayList<Vector2>();
        ArrayList<Vector2> triaBoxVertices = new ArrayList<Vector2>();

        for (int i = 0; i < vc; i++) {
            shape.getVertex(i, boxVertex);
            boxVertex = objectBody.getWorldPoint(boxVertex).mul(Helper.BOX_TO_WORLD);
            boxVertices.add(boxVertex.cpy());
        }/*from  w  w  w .  java  2s .  co  m*/
        triaBoxVertices = (ArrayList<Vector2>) ear.computeTriangles(boxVertices);
        float[] meshVertices = new float[triaBoxVertices.size() * 4];
        short[] meshIndices = new short[triaBoxVertices.size()];
        for (int i = 0; i < triaBoxVertices.size(); i++) {
            meshVertices[i * 4] = triaBoxVertices.get(i).x;
            meshVertices[i * 4 + 1] = triaBoxVertices.get(i).y;
            meshVertices[i * 4 + 2] = triaBoxVertices.get(i).x * TEXTURE_SCALE;
            meshVertices[i * 4 + 3] = triaBoxVertices.get(i).y * TEXTURE_SCALE;
            meshIndices[i] = (short) i;
        }

        Mesh mesh = new Mesh(true, triaBoxVertices.size(), triaBoxVertices.size(),
                new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE),
                new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
        mesh.setVertices(meshVertices);
        mesh.setIndices(meshIndices);
        meshes.add(mesh);
        boundingBoxes.add(mesh.calculateBoundingBox());
    }
}

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

License:Open Source License

public static Mesh genIcosahedronMesh(float x, float z) {
    Mesh mesh = new Mesh(true, 12, 60, new VertexAttribute(Usage.Position, 3, "a_position"));
    mesh.setVertices(genIcosahedronVertices(x, z));
    mesh.setIndices(genIcosahedronIndicies());

    return mesh;/* ww  w .  j av  a  2 s .co m*/
}

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

License:Open Source License

public static Mesh genCuboid(float x, float y, float z) {

    Mesh mesh = new Mesh(true, 24, 36, new VertexAttribute(Usage.Position, 3, "a_position"),
            new VertexAttribute(Usage.Normal, 3, "a_normal"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0"));

    float[] vertices = genCubeVertices(x, y, z);

    short[] indices = genCubeIndices();

    mesh.setVertices(vertices);/*from  w  w  w . ja  va  2  s  . c  om*/
    mesh.setIndices(indices);

    return mesh;
}

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

License:Open Source License

public static Mesh genCuboid(float x, float y, float z, float offsetx, float offsety, float offsetz) {

    Mesh mesh = new Mesh(true, 24, 36, new VertexAttribute(Usage.Position, 3, "a_position"),
            new VertexAttribute(Usage.Normal, 3, "a_normal"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0"));

    float[] vertices = genCubeVertices(x, y, z, offsetx, offsety, offsetz);

    short[] indices = genCubeIndices();

    mesh.setVertices(vertices);/*from ww  w  . j a v  a 2s. com*/
    mesh.setIndices(indices);

    return mesh;
}

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

License:Open Source License

public static Mesh merge(final TempMesh[] tmpMeshes) {

    final int vertCount = tmpMeshes[0].vertexNum * tmpMeshes.length;
    final int idxCount = tmpMeshes[0].indiceNum * tmpMeshes.length;
    final int vertexSize = tmpMeshes[0].vertexSize;

    final float vertices[] = new float[vertCount * vertexSize];
    final short indices[] = new short[idxCount];

    int voffset = 0;
    int ioffset = 0;

    for (int i = 0; i < tmpMeshes.length; i++) {
        final TempMesh tmpMesh = tmpMeshes[i];

        final int vsize = tmpMesh.vertexNum * vertexSize;
        final int isize = tmpMesh.indiceNum;

        final float[] tempVerts = tmpMesh.vertices;
        final short[] tempIdxs = tmpMesh.indices;

        for (int j = 0; j < vsize; j++) {
            vertices[voffset + j] = tempVerts[j];
        }//from   w w w  .  ja  va  2  s  .co  m

        for (int j = 0; j < isize; j++) {
            indices[ioffset + j] = (short) (tempIdxs[j] + (i * tmpMesh.vertexNum));
        }

        voffset += vsize;
        ioffset += isize;
    }

    final Mesh result = new Mesh(true, vertCount, idxCount, tmpMeshes[0].attributes);
    result.setVertices(vertices);
    result.setIndices(indices);

    return result;
}

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);
    }/*from  w  w w  . j a v a2s . com*/
    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);
    }/* w  ww . j  av a2s  . com*/
    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);/*  ww  w.  j a  va2 s  .  com*/
    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;
}