Example usage for com.badlogic.gdx.graphics VertexAttribute VertexAttribute

List of usage examples for com.badlogic.gdx.graphics VertexAttribute VertexAttribute

Introduction

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

Prototype

public VertexAttribute(int usage, int numComponents, String alias) 

Source Link

Document

Constructs a new VertexAttribute.

Usage

From source file:com.andgate.ikou.render.TileMesh.java

License:Open Source License

public void rebuild() {
    try {//  www .  ja  v a  2  s.  c  om
        synchronized (rebuilding) {
            rebuildingInProcess = true;
            mesh = new Mesh(true, VERTICES_PER_FACE * (vertices.size / NUM_COMPONENTS),
                    INDICES_PER_FACE * indicies.size,
                    new VertexAttribute(Usage.Position, POSITION_COMPONENTS, POSITION_ATTRIBUTE),
                    new VertexAttribute(Usage.ColorPacked, COLOR_COMPONENTS_EXPECTED, COLOR_ATTRIBUTE),
                    new VertexAttribute(Usage.Normal, NORMAL_COMPONENTS, NORMAL_ATTRIBUTE));
            mesh.setVertices(v);
            mesh.setIndices(i);

            // Clear everything so it can be garbage collected
            vertices.clear();
            indicies.clear();
            vertices = null;
            indicies = null;
            v = null;
            i = null;

            rebuildingInProcess = false;
            needsRebuild = false;
        }
    } catch (final Throwable e) {
        final String errorMessage = "Rebuilding mesh failed!";
        Gdx.app.error(TAG, errorMessage, e);
    }
}

From source file:com.anythingmachine.gdxwrapper.SpriteCache.java

License:Apache License

/**
 * Creates a cache with the specified size and OpenGL ES 2.0 shader.
 * // w  w w  .ja  v a 2 s  . co m
 * @param size
 *            The maximum number of images this cache can hold. The memory
 *            required to hold the images is allocated up front.
 * @param useIndices
 *            If true, indexed geometry will be used.
 */
public SpriteCache(int size, ShaderProgram shader, boolean useIndices) {
    this.shader = shader;

    mesh = new Mesh(true, size * (useIndices ? 4 : 6), useIndices ? size * 6 : 0,
            new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE),
            new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE),
            new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
    mesh.setAutoBind(false);

    if (useIndices) {
        int length = size * 6;
        short[] indices = new short[length];
        short j = 0;
        for (int i = 0; i < length; i += 6, j += 4) {
            indices[i + 0] = (short) j;
            indices[i + 1] = (short) (j + 1);
            indices[i + 2] = (short) (j + 2);
            indices[i + 3] = (short) (j + 2);
            indices[i + 4] = (short) (j + 3);
            indices[i + 5] = (short) j;
        }
        mesh.setIndices(indices);
    }

    projectionMatrix.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}

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

public StaticPlaneSimulationObject(Vector3 planeNormal, float planeConstant, float friction, int width,
        int height, Texture texture, boolean disposeTexture) {
    super();//from w  w w  .  j a  va  2s . co m

    this.texture = texture;
    this.disposeTexture = disposeTexture;

    this.mesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 3, "position"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "texture"));

    this.mesh.setVertices(new float[] {
            // Bottom left, tex
            -width / 2f, -height / 2f, 0, 0, 0,
            // Bottom right, tex
            width / 2f, -height / 2f, 0, width, 0,
            // Top right, tex
            width / 2f, height / 2f, 0, width, height,
            // Top left, tex
            -width / 2f, height / 2f, 0, 0, height });
    this.mesh.setIndices(new short[] { 0, 1, 2, 0, 3, 2 });

    this.staticPlaneShape = new btStaticPlaneShape(planeNormal, planeConstant);

    initialize(this.staticPlaneShape, 0, -1, DEFAULT_START_TRANSFORM);
}

From source file:com.badlogic.gdx.tests.box2d.PhysicalCell.java

void buildMesh() {

    mesh = new Mesh(true, 4 * 4 * 4, 3 * 2 * 2 * 2, new VertexAttribute(Usage.Position, 2, "a_position"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords"));

    float vertices[] = new float[4 * 4 * 4];
    short indices[] = new short[3 * 2 * 2 * 2];

    Vector2 pos, tex;/*from ww w.  j  a v a2  s.c  o m*/
    int v = 0;
    int ind = 0;

    // 4 x 1/4 cell

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            tex = buttomLeft[i][j];

            pos = bodies[i][j].getPosition();
            vertices[4 * (v + 0) + 0] = pos.x - (1 - i) * physicalSize.x / 6;
            vertices[4 * (v + 0) + 1] = pos.y - (1 - j) * physicalSize.y / 6;
            vertices[4 * (v + 0) + 2] = tex.x;
            vertices[4 * (v + 0) + 3] = tex.y;

            pos = bodies[i + 1][j].getPosition();
            vertices[4 * (v + 1) + 0] = pos.x - (0 - i) * physicalSize.x / 6;
            vertices[4 * (v + 1) + 1] = pos.y - (1 - j) * physicalSize.y / 6;
            vertices[4 * (v + 1) + 2] = tex.x + textureSize.x;
            vertices[4 * (v + 1) + 3] = tex.y;

            pos = bodies[i + 1][j + 1].getPosition();
            vertices[4 * (v + 2) + 0] = pos.x - (0 - i) * physicalSize.x / 6;
            vertices[4 * (v + 2) + 1] = pos.y - (0 - j) * physicalSize.y / 6;
            vertices[4 * (v + 2) + 2] = tex.x + textureSize.x;
            vertices[4 * (v + 2) + 3] = tex.y - textureSize.y; // rrrr!!               

            pos = bodies[i][j + 1].getPosition();
            vertices[4 * (v + 3) + 0] = pos.x - (1 - i) * physicalSize.x / 6;
            vertices[4 * (v + 3) + 1] = pos.y - (0 - j) * physicalSize.y / 6;
            vertices[4 * (v + 3) + 2] = tex.x;
            vertices[4 * (v + 3) + 3] = tex.y - textureSize.y; // rrrr!!               

            // two triangles
            indices[ind++] = (short) v;
            indices[ind++] = (short) (v + 1);
            indices[ind++] = (short) (v + 2);

            indices[ind++] = (short) v;
            indices[ind++] = (short) (v + 2);
            indices[ind++] = (short) (v + 3);

            v += 4;

        }
    }

    //Gdx.app.error("", "x = " + vertices[0] + " , y = " + vertices[1]);

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

}

From source file:com.badlogic.gdx.tests.dragome.examples.HelloTriangle.java

License:Apache License

@Override
public void create() {
    String vertexShader = "attribute vec4 vPosition;    \n" + "void main()                  \n"
            + "{                            \n" + "   gl_Position = vPosition;  \n"
            + "}                            \n";
    String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n"
            + "void main()                                  \n"
            + "{                                            \n"
            + "  gl_FragColor = vec4 ( 1.0, 1.0, 1.0, 1.0 );\n" + "}";

    shader = new ShaderProgram(vertexShader, fragmentShader);
    mesh = new Mesh(true, 3, 0, new VertexAttribute(Usage.Position, 3, "vPosition"));
    float[] vertices = { 0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f };
    mesh.setVertices(vertices);/*from   www  .ja  v  a  2 s .c o m*/
}

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

License:Apache License

@Override
public void create() {
    String vertexShader = "uniform float u_offset;      \n" + "attribute vec4 a_position;   \n"
            + "attribute vec2 a_texCoord;   \n" + "varying vec2 v_texCoord;     \n"
            + "void main()                  \n" + "{                            \n"
            + "   gl_Position = a_position; \n" + "   gl_Position.x += u_offset;\n"
            + "   v_texCoord = a_texCoord;  \n" + "}                            \n";
    String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n"
            + "varying vec2 v_texCoord;                            \n"
            + "uniform sampler2D s_texture;                        \n"
            + "void main()                                         \n"
            + "{                                                   \n"
            + "  gl_FragColor = texture2D( s_texture, v_texCoord );\n"
            + "}                                                   \n";
    shader = new ShaderProgram(vertexShader, fragmentShader);
    mesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 4, "a_position"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord"));
    float[] vertices = { -0.5f, 0.5f, 0.0f, 1.5f, // Position 0
            0.0f, 0.0f, // TexCoord 0
            -0.5f, -0.5f, 0.0f, 0.75f, // Position 1
            0.0f, 1.0f, // TexCoord 1
            0.5f, -0.5f, 0.0f, 0.75f, // Position 2
            1.0f, 1.0f, // TexCoord 2
            0.5f, 0.5f, 0.0f, 1.5f, // Position 3
            1.0f, 0.0f // TexCoord 3
    };//from w  w  w .  j ava 2 s. com
    short[] indices = { 0, 1, 2, 0, 2, 3 };
    mesh.setVertices(vertices);
    mesh.setIndices(indices);
    createTexture();
}

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 www . ja v  a  2  s .c o  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.gdx.tests.lw.PongLW.java

License:Apache License

/**
 * This method sets up all the graphics related stuff like the Meshes, the camera and the Font
 *///from w  w w .j a v a 2s  .  com
private void setupGraphics() {
    //
    // We first construct the paddle mesh which consists of
    // four 2D vertices forming a vertically elongated rectangle
    // constructed around the origin. We don't use colors, normals
    // texture coordinates or indices. Note that we use a fixed
    // point Mesh here. The paddle has dimensions (10, 60).
    //
    paddleMesh = new Mesh(true, 4, 0, new VertexAttribute(Usage.Position, 2, "a_position"));
    paddleMesh.setVertices(new float[] { -5, -30, 5, -30, 5, 30, -5, 30 });

    //
    // We do the same for the ball which has dimensions (10,10)
    //
    ballMesh = new Mesh(true, 4, 0, new VertexAttribute(Usage.Position, 2, "a_position"));
    ballMesh.setVertices(new float[] { -5, -5, 5, -5, 5, 5, -5, 5 });

    //
    // We construct a new font from a system font. We assume
    // Arial is installed on both the desktop and Android.
    //
    // font = Gdx.graphics.newFont("Arial", 30, FontStyle.Plain);
    score = "0 : 0";
    spriteBatch = new SpriteBatch();

    //
    // Finally we construct an {@link OrthographicCamera} which
    // will scale our scene to 480x320 pixels no matter what the
    // real screen dimensions. This will of course squish the scene
    // on devices like the Droid. The screen center will be at (0,0)
    // so that's the reference frame for our scene.
    //
    camera = new OrthographicCamera(480, 320);
}

From source file:com.badlogic.gdx.tests.lw.WaterRipplesLW.java

License:Apache License

@Override
public void create() {

    camera = new PerspectiveCamera(90, Gdx.graphics.getWidth(), Gdx.graphics.getWidth());
    camera.position.set((WIDTH) / 2.0f, (HEIGHT) / 2.0f, WIDTH / 2.0f);
    camera.near = 0.1f;//  w w w.  j  ava2 s. c om
    camera.far = 1000;
    last = new float[WIDTH + 1][HEIGHT + 1];
    curr = new float[WIDTH + 1][HEIGHT + 1];
    intp = new float[WIDTH + 1][HEIGHT + 1];
    vertices = new float[(WIDTH + 1) * (HEIGHT + 1) * 5];
    mesh = new Mesh(false, (WIDTH + 1) * (HEIGHT + 1), WIDTH * HEIGHT * 6,
            new VertexAttribute(VertexAttributes.Usage.Position, 3, "a_Position"),
            new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_texCoords"));
    texture = new Texture(Gdx.files.internal("data/stones.jpg"));
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    createIndices();
    updateVertices(curr);
    initialized = true;

    batch = new SpriteBatch();
    Gdx.input.setInputProcessor(this);
    // font = Gdx.graphics.newFont("Arial", 12, FontStyle.Plain);
}

From source file:com.badlogic.gdx.tests.MyFirstTriangle.java

License:Apache License

@Override
public void create() {
    if (mesh == null) {
        mesh = new Mesh(true, 3, 3, new VertexAttribute(Usage.Position, 3, "a_position"));

        mesh.setVertices(new float[] { -0.5f, -0.5f, 0, 0.5f, -0.5f, 0, 0, 0.5f, 0 });
        mesh.setIndices(new short[] { 0, 1, 2 });
    }/*from w w w .j a v  a2s  .c  o m*/
}