Example usage for com.badlogic.gdx.graphics.glutils ShaderProgram POSITION_ATTRIBUTE

List of usage examples for com.badlogic.gdx.graphics.glutils ShaderProgram POSITION_ATTRIBUTE

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.glutils ShaderProgram POSITION_ATTRIBUTE.

Prototype

String POSITION_ATTRIBUTE

To view the source code for com.badlogic.gdx.graphics.glutils ShaderProgram POSITION_ATTRIBUTE.

Click Source Link

Document

default name for position attributes

Usage

From source file:color.guard.GameplayScreen.java

License:Open Source License

@Override
public void show() {
    guiRandom = new RNG(new Lathe32RNG(0x1337BEEFFEEDL));
    Gdx.gl.glDisable(GL20.GL_BLEND);//from   w  w w .  ja va 2s. co m
    viewport = new PixelPerfectViewport(Scaling.fill, visualWidth, visualHeight);
    //viewport = new ScreenViewport();
    //tempVector3 = new Vector3();
    viewport.getCamera().update();
    //prevCameraPosition = viewport.getCamera().position.cpy();
    nextCameraPosition = viewport.getCamera().position.cpy();
    atlas = new TextureAtlas("Iso_Mini.atlas");
    palettes = new Texture("palettes.png");
    tempSB = new StringBuilder(16);

    font = new BitmapFont(Gdx.files.internal("NanoOKExtended.fnt"), atlas.findRegion("NanoOKExtended"));
    //font.getData().setScale(2f);
    font.setColor(1f / 255f, 1f, 1f, 1f);
    //displayString = state.world.mapGen.atlas.getAt(0);
    String s, r;
    terrains = new TextureAtlas.AtlasRegion[WorldState.terrains.length << 2];
    for (int i = 0; i < terrains.length >> 2; i++) {
        terrains[i * 4] = atlas.findRegion(WorldState.terrains[i] + "_Huge_face0", 0);
        terrains[i * 4 + 1] = atlas.findRegion(WorldState.terrains[i] + "_Huge_face1", 0);
        terrains[i * 4 + 2] = atlas.findRegion(WorldState.terrains[i] + "_Huge_face2", 0);
        terrains[i * 4 + 3] = atlas.findRegion(WorldState.terrains[i] + "_Huge_face3", 0);
    }
    ;
    int pieceCount = PieceKind.kinds.size(), facilityCount = PieceKind.facilities.size(),
            totalCount = pieceCount + facilityCount << 2;
    PieceKind p;
    standing = new Animation[totalCount];
    acting0 = new Animation[totalCount];
    acting1 = new Animation[totalCount];
    dying = new Animation[totalCount];
    receiving0 = new Animation[totalCount];
    receiving1 = new Animation[totalCount];
    for (int i = 0; i < pieceCount; i++) {
        p = PieceKind.kinds.getAt(i);
        s = p.visual + "_Large_face";
        standing[i << 2] = new Animation<>(0.10f, atlas.createSprites(s + 0), Animation.PlayMode.LOOP);
        standing[i << 2 | 1] = new Animation<>(0.10f, atlas.createSprites(s + 1), Animation.PlayMode.LOOP);
        standing[i << 2 | 2] = new Animation<>(0.10f, atlas.createSprites(s + 2), Animation.PlayMode.LOOP);
        standing[i << 2 | 3] = new Animation<>(0.10f, atlas.createSprites(s + 3), Animation.PlayMode.LOOP);
        s = p.visual + "_Large_face";
        if ((p.weapons & 2) != 0) {
            acting0[i << 2] = new Animation<>(0.10f, atlas.createSprites(s + 0 + "_attack_0"));
            acting0[i << 2 | 1] = new Animation<>(0.10f, atlas.createSprites(s + 1 + "_attack_0"));
            acting0[i << 2 | 2] = new Animation<>(0.10f, atlas.createSprites(s + 2 + "_attack_0"));
            acting0[i << 2 | 3] = new Animation<>(0.10f, atlas.createSprites(s + 3 + "_attack_0"));
            r = p.show[0] + "_face";
            receiving0[i << 2] = new Animation<>(0.10f,
                    atlas.createSprites(r + 0 + "_strength_" + p.shownStrengths[0]));
            receiving0[i << 2 | 1] = new Animation<>(0.10f,
                    atlas.createSprites(r + 1 + "_strength_" + p.shownStrengths[0]));
            receiving0[i << 2 | 2] = new Animation<>(0.10f,
                    atlas.createSprites(r + 2 + "_strength_" + p.shownStrengths[0]));
            receiving0[i << 2 | 3] = new Animation<>(0.10f,
                    atlas.createSprites(r + 3 + "_strength_" + p.shownStrengths[0]));
        }
        if ((p.weapons & 1) != 0) {
            acting1[i << 2] = new Animation<>(0.10f, atlas.createSprites(s + 0 + "_attack_1"));
            acting1[i << 2 | 1] = new Animation<>(0.10f, atlas.createSprites(s + 1 + "_attack_1"));
            acting1[i << 2 | 2] = new Animation<>(0.10f, atlas.createSprites(s + 2 + "_attack_1"));
            acting1[i << 2 | 3] = new Animation<>(0.10f, atlas.createSprites(s + 3 + "_attack_1"));
            r = p.show[1] + "_face";
            receiving1[i << 2] = new Animation<>(0.10f,
                    atlas.createSprites(r + 0 + "_strength_" + p.shownStrengths[1]));
            receiving1[i << 2 | 1] = new Animation<>(0.10f,
                    atlas.createSprites(r + 1 + "_strength_" + p.shownStrengths[1]));
            receiving1[i << 2 | 2] = new Animation<>(0.10f,
                    atlas.createSprites(r + 2 + "_strength_" + p.shownStrengths[1]));
            receiving1[i << 2 | 3] = new Animation<>(0.10f,
                    atlas.createSprites(r + 3 + "_strength_" + p.shownStrengths[1]));
        }
        dying[i << 2] = new Animation<>(0.10f, atlas.createSprites(s + 0 + "_death"));
        dying[i << 2 | 1] = new Animation<>(0.10f, atlas.createSprites(s + 1 + "_death"));
        dying[i << 2 | 2] = new Animation<>(0.10f, atlas.createSprites(s + 2 + "_death"));
        dying[i << 2 | 3] = new Animation<>(0.10f, atlas.createSprites(s + 3 + "_death"));
    }
    for (int i = 0; i < facilityCount; i++) {
        p = PieceKind.facilities.getAt(i);
        s = p.visual + "_Large_face";
        standing[pieceCount + i << 2] = new Animation<>(0.10f, atlas.createSprites(s + 0),
                Animation.PlayMode.LOOP);
        standing[pieceCount + i << 2 | 1] = new Animation<>(0.10f, atlas.createSprites(s + 1),
                Animation.PlayMode.LOOP);
        standing[pieceCount + i << 2 | 2] = new Animation<>(0.10f, atlas.createSprites(s + 2),
                Animation.PlayMode.LOOP);
        standing[pieceCount + i << 2 | 3] = new Animation<>(0.10f, atlas.createSprites(s + 3),
                Animation.PlayMode.LOOP);
        s = p.visual + "_Large_face";
        dying[pieceCount + i << 2] = new Animation<>(0.10f, atlas.createSprites(s + 0 + "_death"));
        dying[pieceCount + i << 2 | 1] = new Animation<>(0.10f, atlas.createSprites(s + 1 + "_death"));
        dying[pieceCount + i << 2 | 2] = new Animation<>(0.10f, atlas.createSprites(s + 2 + "_death"));
        dying[pieceCount + i << 2 | 3] = new Animation<>(0.10f, atlas.createSprites(s + 3 + "_death"));
    }
    glp = new GLProfiler(Gdx.graphics);
    glp.enable();
    state.world.startBattle(state.world.factions);
    state.world.battle.resistanceMaps();
    final Coord playerPos = state.world.battle.pieces.firstKey();
    targetCell = playerPos;
    playerPiece = state.world.battle.pieces.getAt(0);
    dijkstra = new DijkstraMap(ArrayTools.fill('.', mapWidth, mapHeight), DijkstraMap.Measurement.MANHATTAN,
            new StatefulRNG(new Lathe32RNG(123456789, 987654321)));
    dijkstra.initializeCost(state.world.battle.resistances[playerPiece.pieceKind.mobility]);
    viewport.getCamera().position.set(32 * (playerPos.y - playerPos.x) + 9f,
            16 * (playerPos.y + playerPos.x) + 13f, 0f);
    viewport.getCamera().update();
    //prevCameraPosition = viewport.getCamera().position.cpy();
    nextCameraPosition = viewport.getCamera().position.cpy();

    //fog = new Noise.Layered3D(SeededNoise.instance, 2, 0.015);

    /*pieces = new int[mapWidth][mapHeight];
    int[] tempOrdering = new int[pieceCount];
    for (int x = mapWidth - 1; x >= 0; x--) {
    CELL_WISE:
    for (int y = mapHeight - 1; y >= 0; y--) {
        if(guiRandom.next(4) == 0) {
            guiRandom.randomOrdering(pieceCount, tempOrdering);
            for (int i = 0; i < pieceCount; i++) {
                if(PieceKind.kinds.getAt(tempOrdering[i]).mobilities[map[x][y]] < 6)
                {
                    pieces[x][y] = tempOrdering[i] << 2 | guiRandom.next(2);
                    continue CELL_WISE;
                }
            }
        }
        pieces[x][y] = -1;
    }
    }
    */
    String vertex = "attribute vec4 a_position;\n" + "attribute vec4 a_color;\n"
            + "attribute vec2 a_texCoord0;\n" + "uniform mat4 u_projTrans;\n" + "varying vec4 v_color;\n"
            + "varying vec2 v_texCoords;\n" + "void main()\n" + "{\n" + "v_color = "
            + ShaderProgram.COLOR_ATTRIBUTE + ";\n" + "v_color.a = v_color.a * (255.0/254.0);\n" + //* (256.0/255.0)
            "v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" + "gl_Position = u_projTrans * "
            + ShaderProgram.POSITION_ATTRIBUTE + ";\n" + "}\n";
    String fragment = "#ifdef GL_ES\n" + "#define LOWP lowp\n" + "precision mediump float;\n" + "#else\n"
            + "#define LOWP\n" + "#endif\n" + "varying LOWP vec4 v_color;\n" + "varying vec2 v_texCoords;\n"
            + "uniform sampler2D u_texture;\n" + "uniform sampler2D u_texPalette;\n" + "void main()\n" + "{\n"
            + "vec4 color = texture2D(u_texture, v_texCoords);\n"
            + "vec2 index = vec2(color.r * 255.0 / 255.5, v_color.r);\n"
            + "gl_FragColor = vec4(texture2D(u_texPalette, index).rgb, color.a);\n" + "}\n";
    //        String fragment =
    //                "#ifdef GL_ES\n" +
    //                "#define LOWP lowp\n" +
    //                "precision mediump float;\n" +
    //                "#else\n" +
    //                "#define LOWP\n" +
    //                "#endif\n" +
    //                "varying LOWP vec4 v_color;\n" +
    //                "varying vec2 v_texCoords;\n" +
    //                "uniform sampler2D u_texture;\n" +
    //                "uniform sampler2D u_texPalette;\n" +
    //                "// The MIT License\n" +
    //                "// Copyright  2013 Inigo Quilez\n" +
    //                "// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n" +
    //                "// Gradient Noise 3D             : https://www.shadertoy.com/view/Xsl3Dl\n" +
    //                "//===============================================================================================\n" +
    //                "vec3 hash( vec3 p ) // replace this by something better\n" +
    //                "{\n" +
    //                "    p = vec3( dot(p,vec3(127.1,311.7, 74.7)),\n" +
    //                "        dot(p,vec3(269.5,183.3,246.1)),\n" +
    //                "        dot(p,vec3(113.5,271.9,124.6)));\n" +
    //                "    return -1.0 + 2.0*fract(sin(p)*43758.5453123);\n" +
    //                "}\n" +
    //                "float noise(vec3 p)\n" +
    //                "{\n" +
    //                "    vec3 i = floor(p);\n" +
    //                "    vec3 f = fract(p);\n" +
    //                "    vec3 u = f*f*(3.0-2.0*f);\n" +
    //                "    return " +
    //                        "   mix( mix( mix( dot( hash( i + vec3(0.0,0.0,0.0) ), f - vec3(0.0,0.0,0.0) ), \n" +
    //                "                          dot( hash( i + vec3(1.0,0.0,0.0) ), f - vec3(1.0,0.0,0.0) ), u.x),\n" +
    //                "                     mix( dot( hash( i + vec3(0.0,1.0,0.0) ), f - vec3(0.0,1.0,0.0) ), \n" +
    //                "                          dot( hash( i + vec3(1.0,1.0,0.0) ), f - vec3(1.0,1.0,0.0) ), u.x), u.y),\n" +
    //                "                mix( mix( dot( hash( i + vec3(0.0,0.0,1.0) ), f - vec3(0.0,0.0,1.0) ), \n" +
    //                "                          dot( hash( i + vec3(1.0,0.0,1.0) ), f - vec3(1.0,0.0,1.0) ), u.x),\n" +
    //                "                     mix( dot( hash( i + vec3(0.0,1.0,1.0) ), f - vec3(0.0,1.0,1.0) ), \n" +
    //                "                          dot( hash( i + vec3(1.0,1.0,1.0) ), f - vec3(1.0,1.0,1.0) ), u.x), u.y), u.z)" +
    //                        ";\n" +
    //                "}\n" +
    //                "const mat3 m = mat3( 0.00,  0.80,  0.60,\n" +
    //                "                    -0.80,  0.36, -0.48,\n" +
    //                "                    -0.60, -0.48,  0.64 );\n"+
    //                "// End of MIT-licensed code\n"+
    //                //Jim Hejl and Richard Burgess-Dawson's tone mapping formula
    //                "vec3 tone(vec3 texColor, LOWP float change)\n" +
    //                "{\n" +
    //                "vec3 x = clamp((texColor * 0.666) - 0.004, 0.0, 100.0);\n" +
    //                "return mix(texColor, (x*(6.2*x+.5))/(x*(6.2*x+1.7)+0.06), change);\n" +
    //                "}\n" +
    //                "void main()\n" +
    //                "{\n" +
    //                "vec4 color = texture2D(u_texture, v_texCoords);\n" +
    //                "vec2 index = vec2(color.r * 255.0 / 255.5, v_color.r);\n" +
    //                "vec3 q = vec3(0.01125 * gl_FragCoord.xy, v_color.g * 2.75);\n" +
    ////                "float f = 0.5000*noise( q ); q = m*q*2.01;\n" +
    ////                "f      += 0.3125*noise( q ); q = m*q*2.02;\n" +
    ////                "f      += 0.1875*noise( q );\n" +
    ////                "f      += 0.0625*noise( q ); q = m*q*2.01;\n" +
    //                "gl_FragColor = vec4(tone(texture2D(u_texPalette, index).rgb, 0.65 - noise(q) * 0.75), color.a);\n" +
    //                "}\n";
    indexShader = new ShaderProgram(vertex, fragment);
    if (!indexShader.isCompiled())
        throw new GdxRuntimeException("Error compiling shader: " + indexShader.getLog());

    map = ArrayTools.copy(state.world.worldMap);
    for (int x = 0; x < mapWidth; x++) {
        for (int y = 0; y < mapHeight; y++) {
            map[x][y] = map[x][y] * 4 + guiRandom.next(2);
        }
    }
    batch = new SpriteBatch();
    proc = new InputAdapter() {
        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            if (inputMode == INPUT_LOCKED)
                return false;
            inputMode = INPUT_LOCKED;

            nextCameraPosition.set(screenX, screenY, 0);
            viewport.unproject(nextCameraPosition);
            //32 * y - 32 * x, 16 * y + 16 * x
            /*
            nx = 32 * y - 32 * x
            ny = 16 * y + 16 * x
            nx + 2ny = 64y
            nx - 2ny = -64x  
            */
            targetCell = Coord.get(
                    MathUtils.round((nextCameraPosition.x - nextCameraPosition.y * 2f + 32f) / -64f),
                    MathUtils.round((nextCameraPosition.x + nextCameraPosition.y * 2f - 96f) / 64f));
            dijkstra.clearGoals();
            dijkstra.findPath(playerPiece.pieceKind.stats[PieceKind.MOV], 20,
                    state.world.battle.pieces.keySet(), null, state.world.battle.pieces.firstKey(), targetCell);
            return true;
        }

        @Override
        public boolean keyDown(int keycode) {
            if (inputMode == INPUT_LOCKED)
                return false;
            switch (keycode) {
            case Input.Keys.RIGHT:
                lastArrow = Direction.LEFT;
                break;
            case Input.Keys.LEFT:
                lastArrow = Direction.RIGHT;
                break;
            case Input.Keys.UP:
                lastArrow = Direction.DOWN;
                break;
            case Input.Keys.DOWN:
                lastArrow = Direction.UP;
                break;
            //default: lastArrow = Direction.NONE;
            default:
                return false;
            }
            targetCell = playerPos.translate(lastArrow);
            dijkstra.clearGoals();
            dijkstra.findPath(playerPiece.pieceKind.stats[PieceKind.MOV], 20,
                    state.world.battle.pieces.keySet(), null, state.world.battle.pieces.firstKey(), targetCell);
            inputMode = INPUT_LOCKED;
            return true;
        }
    };
    Gdx.input.setInputProcessor(proc);
}

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

License:Apache License

/**
 * Creates a cache with the specified size and OpenGL ES 2.0 shader.
 * /*from  ww w.j  av  a2s  .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.anythingmachine.gdxwrapper.SpriteCache.java

License:Apache License

static ShaderProgram createDefaultShader() {
    if (!true)/*from  ww w.ja v a2s.c  o m*/
        return null;
    String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
            + "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
            + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
            + "uniform mat4 u_projectionViewMatrix;\n" //
            + "varying vec4 v_color;\n" //
            + "varying vec2 v_texCoords;\n" //
            + "\n" //
            + "void main()\n" //
            + "{\n" //
            + "   v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
            + "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
            + "   gl_Position =  u_projectionViewMatrix * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
            + "}\n";
    String fragmentShader = "#ifdef GL_ES\n" //
            + "precision mediump float;\n" //
            + "#endif\n" //
            + "varying vec4 v_color;\n" //
            + "varying vec2 v_texCoords;\n" //
            + "uniform sampler2D u_texture;\n" //
            + "void main()\n"//
            + "{\n" //
            + "  gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" //
            + "}";
    ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
    if (shader.isCompiled() == false)
        throw new IllegalArgumentException("Error compiling shader: " + shader.getLog());
    return shader;
}

From source file:com.balloongame.handlers.CustomSpriteBatch.java

License:Apache License

/** <p>
 * Constructs a new SpriteBatch. Sets the projection matrix to an orthographic projection with y-axis point upwards, x-axis
 * point to the right and the origin being in the bottom left corner of the screen. The projection will be pixel perfect with
 * respect to the screen resolution.//  w  ww .  ja v  a  2s.c o m
 * </p>
 * 
 * <p>
 * The size parameter specifies the maximum size of a single batch in number of sprites
 * </p>
 * 
 * <p>
 * The defaultShader specifies the shader to use. Note that the names for uniforms for this default shader are different than
 * the ones expect for shaders set with {@link #setShader(ShaderProgram)}. See the {@link #createDefaultShader()} method.
 * </p>
 * 
 * @param size the batch size in number of sprites
 * @param buffers the number of buffers to use. only makes sense with VBOs. This is an expert function.
 * @param defaultShader the default shader to use. This is not owned by the SpriteBatch and must be disposed separately. */
public CustomSpriteBatch(int size, int buffers, ShaderProgram defaultShader) {
    super(0);
    super.dispose();
    //enable blending
    Gdx.gl.glEnable(GL20.GL_BLEND);
    if (blendSrcFunc != -1)
        Gdx.gl.glBlendFunc(blendSrcFunc, blendDstFunc);

    this.buffers = new Mesh[buffers];

    for (int i = 0; i < buffers; i++) {
        this.buffers[i] = new Mesh(VertexDataType.VertexArray, false, size * 4, size * 6,
                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"));
    }

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

    vertices = new float[size * SPRITE_SIZE];

    int len = size * 6;
    short[] indices = new short[len];
    short j = 0;
    for (int i = 0; i < len; i += 6, j += 4) {
        indices[i + 0] = (short) (j + 0);
        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 + 0);
    }
    for (int i = 0; i < buffers; i++) {
        this.buffers[i].setIndices(indices);
    }
    mesh = this.buffers[0];

    if (Gdx.graphics.isGL20Available() && defaultShader == null) {
        shader = createDefaultShader();
        ownsShader = true;
    } else
        shader = defaultShader;
}

From source file:com.balloongame.handlers.CustomSpriteBatch.java

License:Apache License

/** Returns a new instance of the default shader used by SpriteBatch for GL2 when no shader is specified. */
static public ShaderProgram createDefaultShader() {
    String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
            + "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
            + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
            + "uniform mat4 u_projTrans;\n" //
            + "varying vec4 v_color;\n" //
            + "varying vec2 v_texCoords;\n" //
            + "\n" //
            + "void main()\n" //
            + "{\n" //
            + "   v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
            + "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
            + "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
            + "}\n";
    String fragmentShader = "#ifdef GL_ES\n" //
            + "#define LOWP lowp\n" //
            + "precision mediump float;\n" //
            + "#else\n" //
            + "#define LOWP \n" //
            + "#endif\n" //
            + "varying LOWP vec4 v_color;\n" //
            + "varying vec2 v_texCoords;\n" //
            + "uniform sampler2D u_texture;\n" //
            + "void main()\n"//
            + "{\n" //
            + "  gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" //
            + "}";

    ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
    if (shader.isCompiled() == false)
        throw new IllegalArgumentException("couldn't compile shader: " + shader.getLog());
    return shader;
}

From source file:com.cyphercove.doublehelix.points.BillboardDecalBatch.java

License:Apache License

public void initialize(int size) {
    vertices = new float[size * BillboardDecal.VERTEX_SIZE];

    Mesh.VertexDataType vertexDataType = Mesh.VertexDataType.VertexArray;
    if (Gdx.gl30 != null) {
        vertexDataType = Mesh.VertexDataType.VertexBufferObjectWithVAO;
    }//  www. j a v a 2  s.  c o m
    mesh = new Mesh(vertexDataType, false, size, 0,
            new VertexAttribute(VertexAttributes.Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
            new VertexAttribute(VertexAttributes.Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE),
            new VertexAttribute(VertexAttributes.Usage.Generic, 1, SIZE_ATTRIBUTE),
            new VertexAttribute(VertexAttributes.Usage.Generic, 1, ROTATION_ATTRIBUTE));
}

From source file:com.deepwallgames.quantumhue.ImmediateModeShader30.java

License:Apache License

static private String createVertexShader(boolean hasNormals, boolean hasColors, int numTexCoords) {
    String prefix = "#version 100\n";
    String shader = prefix + "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n"
            + (hasNormals ? "attribute vec3 " + ShaderProgram.NORMAL_ATTRIBUTE + ";\n" : "")
            + (hasColors ? "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" : "");

    for (int i = 0; i < numTexCoords; i++) {
        shader += "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + i + ";\n";
    }//from  www  . j  av a  2  s . c om

    shader += "uniform mat4 u_projModelView;\n";
    shader += (hasColors ? "varying vec4 v_col;\n" : "");

    for (int i = 0; i < numTexCoords; i++) {
        shader += "varying vec2 v_tex" + i + ";\n";
    }

    shader += "void main() {\n" + "   gl_Position = u_projModelView * " + ShaderProgram.POSITION_ATTRIBUTE
            + ";\n" + (hasColors ? "   v_col = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" : "");

    for (int i = 0; i < numTexCoords; i++) {
        shader += "   v_tex" + i + " = " + ShaderProgram.TEXCOORD_ATTRIBUTE + i + ";\n";
    }
    shader += "   gl_PointSize = 1.0;\n";
    shader += "}\n";
    return shader;
}

From source file:com.doom.render.QuadBatch.java

License:Apache License

/** Constructs a new SpriteBatch. Sets the projection matrix to an orthographic projection with y-axis point upwards, x-axis
 * point to the right and the origin being in the bottom left corner of the screen. The projection will be pixel perfect with
 * respect to the current screen resolution.
 * <p>/*from  w  w  w  .  j a  v  a  2s. c  o  m*/
 * The defaultShader specifies the shader to use. Note that the names for uniforms for this default shader are different than
 * the ones expect for shaders set with {@link #setShader(ShaderProgram)}. See {@link #createDefaultShader()}.
 * @param size The max number of sprites in a single batch. Max of 5460.
 * @param defaultShader The default shader to use. This is not owned by the SpriteBatch and must be disposed separately. */
public QuadBatch(int size, ShaderProgram defaultShader) {
    // 32767 is max index, so 32767 / 6 - (32767 / 6 % 3) = 5460.
    if (size > 5460)
        throw new IllegalArgumentException("Can't have more than 5460 sprites per batch: " + size);

    mesh = new Mesh(VertexDataType.VertexArray, false, size * 4, size * 6,
            new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
            new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"),
            new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE));

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

    vertices = new float[size * (3 + 1 + 2) * 4];

    int len = size * 6;
    short[] indices = new short[len];
    short j = 0;
    for (int i = 0; i < len; i += 6, j += 4) {
        indices[i] = 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] = j;
    }
    mesh.setIndices(indices);

    if (defaultShader == null) {
        shader = createDefaultShader();
        ownsShader = true;
    } else
        shader = defaultShader;
}

From source file:com.doom.render.QuadBatch.java

License:Apache License

/** Returns a new instance of the default shader used by SpriteBatch for GL2 when no shader is specified. */
static public ShaderProgram createDefaultShader() {
    String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
            + "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
            + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
            + "uniform mat4 u_projTrans;\n" //
            + "varying vec4 v_color;\n" //
            + "varying vec2 v_texCoords;\n" //
            + "\n" //
            + "void main()\n" //
            + "{\n" //
            + "   v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
            + "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
            + "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
            + "}\n";
    String fragmentShader = "#ifdef GL_ES\n" //
            + "#define LOWP lowp\n" //
            + "precision mediump float;\n" //
            + "#else\n" //
            + "#define LOWP \n" //
            + "#endif\n" //
            + "varying LOWP vec4 v_color;\n" //
            + "varying vec2 v_texCoords;\n" //
            + "uniform sampler2D u_texture;\n" //
            + "void main()\n"//
            + "{\n" //
            + "  gl_FragColor = texture2D(u_texture, v_texCoords) * v_color;\n" //
            + "}";

    ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
    if (shader.isCompiled() == false)
        throw new IllegalArgumentException("Error compiling shader: " + shader.getLog());
    return shader;
}

From source file:com.gemserk.commons.gdx.graphics.Gdx2dMeshBuilder.java

License:Apache License

private VertexAttribute[] buildVertexAttributes(boolean hasNormals, boolean hasColor, int numTexCoords) {
    Array<VertexAttribute> attribs = new Array<VertexAttribute>();
    attribs.add(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE));
    if (hasNormals)
        attribs.add(new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE));
    if (hasColor)
        attribs.add(new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE));
    for (int i = 0; i < numTexCoords; i++) {
        attribs.add(new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + i));
    }/*from w w  w  .  j a va  2  s.c  o m*/
    VertexAttribute[] array = new VertexAttribute[attribs.size];
    for (int i = 0; i < attribs.size; i++)
        array[i] = attribs.get(i);
    return array;
}