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

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

Introduction

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

Prototype

public short[] getTriangles() 

Source Link

Usage

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  .ja  v  a  2s .  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();

    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.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, 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);//  ww  w .  j ava  2 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();

    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.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   w  ww .  ja v  a  2s  .co 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;//  ww  w.j a  v a2  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./*from  w  ww. j av  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;
}