Example usage for com.badlogic.gdx.graphics.g2d PolygonRegion getVertices

List of usage examples for com.badlogic.gdx.graphics.g2d PolygonRegion getVertices

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g2d PolygonRegion getVertices.

Prototype

public float[] getVertices() 

Source Link

Document

Returns the vertices in local space.

Usage

From source file:cmnworks.com.angrybee.Actor.java

License:Open Source License

public void ready() {
    Texture.TextureFilter linear = Texture.TextureFilter.Linear;
    Texture actor1 = new Texture(this.setFolder + "actor1.png");
    Texture actor2 = new Texture(this.setFolder + "actor2.png");
    Texture actor3 = new Texture(this.setFolder + "actor3.png");

    actor1.setFilter(linear, linear);//from ww w  .  j  a va 2 s. co  m
    actor2.setFilter(linear, linear);
    actor3.setFilter(linear, linear);
    actor = new Animation(this.setFlappingSpeed, new TextureRegion(actor1), new TextureRegion(actor2),
            new TextureRegion(actor3), new TextureRegion(actor2));
    actor.setPlayMode(Animation.PlayMode.LOOP);

    this.dimension[0] = actor1.getWidth();
    this.dimension[1] = actor1.getHeight();
    this.startingYPoint = (Gdx.graphics.getHeight() / 2) - actor1.getHeight();

    PolygonRegionLoader loader = new PolygonRegionLoader();
    PolygonRegion region = loader.load(new TextureRegion(actor1), Gdx.files.internal(this.setPsh));
    this.polygon = new Polygon(region.getVertices());
}

From source file:cmnworks.com.angrybee.Obstacles.java

License:Open Source License

public void ready() {
    this.target = new Texture(this.setTextureObstacles);
    PolygonRegionLoader loader = new PolygonRegionLoader();
    PolygonRegion region = loader.load(new TextureRegion(target), Gdx.files.internal(this.setPsh));
    this.polygon = new Polygon(region.getVertices());
}

From source file:com.esotericsoftware.spine.utils.TwoColorPolygonBatch.java

License:Open Source License

/** Draws a polygon region with the bottom left corner at x,y having the width and height of the region. */
public void draw(PolygonRegion region, float x, float y) {
    if (!drawing)
        throw new IllegalStateException("begin must be called before draw.");

    final short[] triangles = this.triangles;
    final short[] regionTriangles = region.getTriangles();
    final int regionTrianglesLength = regionTriangles.length;
    final float[] regionVertices = region.getVertices();
    final int regionVerticesLength = regionVertices.length;

    final Texture texture = region.getRegion().getTexture();
    if (texture != lastTexture)
        switchTexture(texture);//from   w w  w. j  a  v  a2  s . co m
    else if (triangleIndex + regionTrianglesLength > triangles.length
            || vertexIndex + regionVerticesLength * VERTEX_SIZE / 2 > vertices.length)
        flush();

    int triangleIndex = this.triangleIndex;
    int vertexIndex = this.vertexIndex;
    final int startVertex = vertexIndex / VERTEX_SIZE;

    for (int i = 0; i < regionTrianglesLength; i++)
        triangles[triangleIndex++] = (short) (regionTriangles[i] + startVertex);
    this.triangleIndex = triangleIndex;

    final float[] vertices = this.vertices;
    final float light = this.lightPacked;
    final float dark = this.darkPacked;
    final float[] textureCoords = region.getTextureCoords();

    for (int i = 0; i < regionVerticesLength; i += 2) {
        vertices[vertexIndex++] = regionVertices[i] + x;
        vertices[vertexIndex++] = regionVertices[i + 1] + y;
        vertices[vertexIndex++] = light;
        vertices[vertexIndex++] = dark;
        vertices[vertexIndex++] = textureCoords[i];
        vertices[vertexIndex++] = textureCoords[i + 1];
    }
    this.vertexIndex = vertexIndex;
}

From source file:com.esotericsoftware.spine.utils.TwoColorPolygonBatch.java

License:Open Source License

/** Draws a polygon region with the bottom left corner at x,y and stretching the region to cover the given width and height. */
public void draw(PolygonRegion region, float x, float y, float width, float height) {
    if (!drawing)
        throw new IllegalStateException("begin must be called before draw.");

    final short[] triangles = this.triangles;
    final short[] regionTriangles = region.getTriangles();
    final int regionTrianglesLength = regionTriangles.length;
    final float[] regionVertices = region.getVertices();
    final int regionVerticesLength = regionVertices.length;
    final TextureRegion textureRegion = region.getRegion();

    final Texture texture = textureRegion.getTexture();
    if (texture != lastTexture)
        switchTexture(texture);/*from   w w  w.ja v  a  2s  .  co m*/
    else if (triangleIndex + regionTrianglesLength > triangles.length
            || vertexIndex + regionVerticesLength * VERTEX_SIZE / 2 > vertices.length)
        flush();

    int triangleIndex = this.triangleIndex;
    int vertexIndex = this.vertexIndex;
    final int startVertex = vertexIndex / VERTEX_SIZE;

    for (int i = 0, n = regionTriangles.length; i < n; i++)
        triangles[triangleIndex++] = (short) (regionTriangles[i] + startVertex);
    this.triangleIndex = triangleIndex;

    final float[] vertices = this.vertices;
    final float light = this.lightPacked;
    final float dark = this.darkPacked;
    final float[] textureCoords = region.getTextureCoords();
    final float sX = width / textureRegion.getRegionWidth();
    final float sY = height / textureRegion.getRegionHeight();

    for (int i = 0; i < regionVerticesLength; i += 2) {
        vertices[vertexIndex++] = regionVertices[i] * sX + x;
        vertices[vertexIndex++] = regionVertices[i + 1] * sY + y;
        vertices[vertexIndex++] = light;
        vertices[vertexIndex++] = dark;
        vertices[vertexIndex++] = textureCoords[i];
        vertices[vertexIndex++] = textureCoords[i + 1];
    }
    this.vertexIndex = vertexIndex;
}

From source file:com.esotericsoftware.spine.utils.TwoColorPolygonBatch.java

License:Open Source License

/** Draws the polygon region with the bottom left corner at x,y and stretching the region to cover the given width and height.
 * The polygon region is offset by originX, originY relative to the origin. Scale specifies the scaling factor by which the
 * polygon region should be scaled around originX, originY. Rotation specifies the angle of counter clockwise rotation of the
 * rectangle around originX, originY. */
public void draw(PolygonRegion region, float x, float y, float originX, float originY, float width,
        float height, float scaleX, float scaleY, float rotation) {
    if (!drawing)
        throw new IllegalStateException("begin must be called before draw.");

    final short[] triangles = this.triangles;
    final short[] regionTriangles = region.getTriangles();
    final int regionTrianglesLength = regionTriangles.length;
    final float[] regionVertices = region.getVertices();
    final int regionVerticesLength = regionVertices.length;
    final TextureRegion textureRegion = region.getRegion();

    Texture texture = textureRegion.getTexture();
    if (texture != lastTexture)
        switchTexture(texture);/*from w ww  .  j  a va  2 s .c  o m*/
    else if (triangleIndex + regionTrianglesLength > triangles.length
            || vertexIndex + regionVerticesLength * VERTEX_SIZE / 2 > vertices.length)
        flush();

    int triangleIndex = this.triangleIndex;
    int vertexIndex = this.vertexIndex;
    final int startVertex = vertexIndex / VERTEX_SIZE;

    for (int i = 0; i < regionTrianglesLength; i++)
        triangles[triangleIndex++] = (short) (regionTriangles[i] + startVertex);
    this.triangleIndex = triangleIndex;

    final float[] vertices = this.vertices;
    final float light = this.lightPacked;
    final float dark = this.darkPacked;
    final float[] textureCoords = region.getTextureCoords();

    final float worldOriginX = x + originX;
    final float worldOriginY = y + originY;
    final float sX = width / textureRegion.getRegionWidth();
    final float sY = height / textureRegion.getRegionHeight();
    final float cos = MathUtils.cosDeg(rotation);
    final float sin = MathUtils.sinDeg(rotation);

    float fx, fy;
    for (int i = 0; i < regionVerticesLength; i += 2) {
        fx = (regionVertices[i] * sX - originX) * scaleX;
        fy = (regionVertices[i + 1] * sY - originY) * scaleY;
        vertices[vertexIndex++] = cos * fx - sin * fy + worldOriginX;
        vertices[vertexIndex++] = sin * fx + cos * fy + worldOriginY;
        vertices[vertexIndex++] = light;
        vertices[vertexIndex++] = dark;
        vertices[vertexIndex++] = textureCoords[i];
        vertices[vertexIndex++] = textureCoords[i + 1];
    }
    this.vertexIndex = vertexIndex;
}

From source file:org.ams.prettypaint.PrettyPolygonBatch.java

License:Open Source License

protected void drawTexture(PolygonRegion region, float pos_x, float pos_y, float width, float height,
        float scaleX, float scaleY, float rotation, float texture_pos_x, float texture_pos_y, float tex_trans_x,
        float tex_trans_y, float region_width, float region_height, float packedColor) {
    if (!isStarted)
        throw new RuntimeException("You must call begin() before calling this method.");

    final float[] regionVertices = region.getVertices();
    final int regionVerticesLength = regionVertices.length;
    final TextureRegion textureRegion = region.getRegion();

    Texture texture = textureRegion.getTexture();

    float vertexCount = 3f;

    float degenerateVertexCount = 2f;

    float totalData = dataPerVertex * (vertexCount + degenerateVertexCount);

    if (texture != lastTexture) {
        flush();/*from   w  w  w .  ja v  a  2s  .co m*/
        lastTexture = texture;
    } else if (dataCount + totalData > maxData) {
        flush();
    }

    final float[] textureCoordinates = region.getTextureCoords();

    final float worldOriginX = pos_x;
    final float worldOriginY = pos_y;
    final float sX = width / textureRegion.getRegionWidth();
    final float sY = height / textureRegion.getRegionHeight();
    final float cos = MathUtils.cos(rotation);
    final float sin = MathUtils.sin(rotation);

    float fx, fy;

    int i = 0;
    {
        fx = (regionVertices[i] * sX) * scaleX;
        fy = (regionVertices[i + 1] * sY) * scaleY;

        data[dataCount++] = cos * fx - sin * fy + worldOriginX;
        data[dataCount++] = sin * fx + cos * fy + worldOriginY;
        data[dataCount++] = packedColor;

        data[dataCount++] = textureCoordinates[i] + tex_trans_x;
        data[dataCount++] = textureCoordinates[i + 1] + tex_trans_y;

        data[dataCount++] = texture_pos_x;
        data[dataCount++] = texture_pos_y;

        data[dataCount++] = region_width;
        data[dataCount++] = region_height;

    }

    for (; i < regionVerticesLength; i += 2) {
        fx = (regionVertices[i] * sX) * scaleX;
        fy = (regionVertices[i + 1] * sY) * scaleY;

        data[dataCount++] = cos * fx - sin * fy + worldOriginX;
        data[dataCount++] = sin * fx + cos * fy + worldOriginY;
        data[dataCount++] = packedColor;

        data[dataCount++] = textureCoordinates[i] + tex_trans_x;
        data[dataCount++] = textureCoordinates[i + 1] + tex_trans_y;

        data[dataCount++] = texture_pos_x;
        data[dataCount++] = texture_pos_y;

        data[dataCount++] = region_width;
        data[dataCount++] = region_height;

    }

    {
        i -= 2;
        fx = (regionVertices[i] * sX) * scaleX;
        fy = (regionVertices[i + 1] * sY) * scaleY;

        data[dataCount++] = cos * fx - sin * fy + worldOriginX;
        data[dataCount++] = sin * fx + cos * fy + worldOriginY;
        data[dataCount++] = packedColor;

        data[dataCount++] = textureCoordinates[i] + tex_trans_x;
        data[dataCount++] = textureCoordinates[i + 1] + tex_trans_y;

        data[dataCount++] = texture_pos_x;
        data[dataCount++] = texture_pos_y;

        data[dataCount++] = region_width;
        data[dataCount++] = region_height;
    }
}

From source file:org.ams.prettypaint.TexturePolygon.java

License:Open Source License

/**
 * With texture angle you can rotate the texture without rotating the edges of the polygon.
 *
 * @param textureAngle the angle of the texture in radians.
 *///from ww w.ja va  2  s .c o  m
public void setTextureAngle(float textureAngle) {
    Vector2 v = new Vector2();
    for (int i = 0; i < polygonRegions.size; i++) {
        PolygonRegion toReplace = polygonRegions.get(i);

        float[] vertices = toReplace.getVertices();

        for (int j = 0; j < vertices.length;) {
            v.set(vertices[j], vertices[j + 1]);
            v.rotateRad(this.textureAngle - textureAngle);
            vertices[j] = v.x;
            vertices[j + 1] = v.y;
            j += 2;
        }

        PolygonRegion replacement = new PolygonRegion(toReplace.getRegion(), vertices,
                toReplace.getTriangles());

        polygonRegions.set(i, replacement);

    }
    this.textureAngle = textureAngle;
}

From source file:org.ams.prettypaint.TexturePolygon.java

License:Open Source License

/** Set the texture to be upright at the polygons current angle. */
public void setTextureUprightForCurrentAngle() {
    Vector2 v = new Vector2();
    for (int i = 0; i < polygonRegions.size; i++) {
        PolygonRegion toReplace = polygonRegions.get(i);

        float[] vertices = toReplace.getVertices();

        for (int j = 0; j < vertices.length;) {
            v.set(vertices[j], vertices[j + 1]);
            v.rotateRad(textureAngle + angleRad);
            vertices[j] = v.x;/* w  w w . j  av a 2  s  . c  om*/
            vertices[j + 1] = v.y;
            j += 2;
        }

        PolygonRegion replacement = new PolygonRegion(toReplace.getRegion(), vertices,
                toReplace.getTriangles());

        polygonRegions.set(i, replacement);

    }
    textureAngle = -angleRad;

}

From source file:org.ams.prettypaint.TexturePolygon.java

License:Open Source License

/**
 * The given region should be square, otherwise it will not look seamless. I hope to fix
 * this soon.//  w  w  w.j a v a 2s .c  om
 * The texture region should contain a seamless texture.
 * <p>
 * Use a {@link TextureAtlas} to manage your texture regions.
 *
 * @param textureRegion the region you wish to draw on the polygon defined by {@link #setVertices(Array)}.
 * @return this for chaining.
 */
public TexturePolygon setTextureRegion(TextureRegion textureRegion) {
    if (textureRegion == null)
        throw new RuntimeException("TextureRegion can not be null. ");

    boolean change = this.textureRegion == null || !this.textureRegion.equals(textureRegion);

    if (change) {

        this.textureRegion = textureRegion;
        regionBounds = new Rectangle(textureRegion.getRegionX(), textureRegion.getRegionY(),
                textureRegion.getRegionWidth(), textureRegion.getRegionHeight());

        Array<PolygonRegion> newRegions = new Array<PolygonRegion>(true, 4, PolygonRegion.class);
        for (int i = 0; i < polygonRegions.size; i++) {
            PolygonRegion pr = polygonRegions.get(i);
            // reuse the triangles and vertices
            newRegions.add(new PolygonRegion(textureRegion, pr.getVertices(), pr.getTriangles()));
        }
        polygonRegions.clear();
        polygonRegions.addAll(newRegions);

        setTextureTranslation(getTextureTranslation());

        if (setTrianglesLater) {
            setTrianglesLater = false;
            setTriangles(triangles);
        }
    }
    return this;
}

From source file:org.ams.prettypaint.TexturePolygon.java

License:Open Source License

/** Computes the culling area of the polygon after scaling, rotating and translating. */
private Rectangle getCullingArea(Rectangle cullingArea, PolygonRegion pr, float rotation, Vector2 translation,
        float scale) {

    float[] vertices = pr.getVertices();

    tmpVector.set(vertices[0], vertices[1]);
    tmpVector.scl(scale);//www .j a v a2 s  .  co m
    tmpVector.rotateRad(rotation);
    tmpVector.add(translation);
    cullingArea.set(tmpVector.x, tmpVector.y, 0, 0);

    tmpVector.set(vertices[2], vertices[3]);
    tmpVector.scl(scale);
    tmpVector.rotateRad(rotation);
    tmpVector.add(translation);
    cullingArea.merge(tmpVector);

    tmpVector.set(vertices[4], vertices[5]);
    tmpVector.scl(scale);
    tmpVector.rotateRad(rotation);
    tmpVector.add(translation);
    cullingArea.merge(tmpVector);

    return cullingArea;

}