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

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

Introduction

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

Prototype

public PolygonRegion(TextureRegion region, float[] vertices, short[] triangles) 

Source Link

Document

Creates a PolygonRegion by triangulating the polygon coordinates in vertices and calculates uvs based on that.

Usage

From source file:CB_UI_Base.graphics.PolygonDrawable.java

License:Open Source License

@Override
public boolean draw(Batch batch, float x, float y, float width, float height, float rotate) {
    synchronized (isDisposed) {

        if (isDisposed.get())
            return true;

        if (po == null) {

            if (this.PAINT.getBitmapShader() == null) {
                if (texReg == null)
                    createTexRegFromPixMap();
                po = new PolygonRegion(texReg, VERTICES, TRIANGLES);
            } else {
                Texture inputTex = this.PAINT.getBitmapShader().getTexture();
                if (inputTex != null) {
                    inputTex.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
                    po = new PolygonRegion(new TextureRegion(inputTex, (int) this.WIDTH, (int) this.HEIGHT),
                            VERTICES, TRIANGLES);
                }/*from  ww  w  .  j  av a 2  s  .  c o m*/
            }

        }

        Color c = batch.getColor();
        float a = c.a;
        float r = c.r;
        float g = c.g;
        float b = c.b;

        if (po == null)
            return true;

        if (this.PAINT.getBitmapShader() == null) {
            GL.setBatchColor(PAINT.getGlColor());
        } else {
            batch.setColor(new Color(Color.WHITE));
        }
        batch.flush();
        try {
            ((PolygonSpriteBatch) batch).draw(po, x, y, width, height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        batch.flush();
        // reset color
        batch.setColor(r, g, b, a);
    }
    return true;
}

From source file:dk.gruppeseks.bodtrd.engine.Game.java

private void drawFoV(float[] shape) {
    EarClippingTriangulator triangulator = new EarClippingTriangulator();
    ShortArray triangleIndices = triangulator.computeTriangles(shape);
    PolygonRegion polyReg = new PolygonRegion(_textureRegion, shape, triangleIndices.toArray());
    PolygonSprite polySprite = new PolygonSprite(polyReg);

    _polyBatch.begin();//from   ww w.j  a va2s. co  m
    polySprite.draw(_polyBatch);
    _polyBatch.end();

    _shapeRenderer.setProjectionMatrix(_gameCamera.combined);
    _shapeRenderer.begin(ShapeType.Line);
    _shapeRenderer.setColor(Color.BROWN);
    _shapeRenderer.polygon(shape);
    _shapeRenderer.end();

}

From source file:es.eucm.ead.editor.utils.TexturedShapeEditor.java

License:Open Source License

@Override
public void create() {
    super.create();

    executor = new AsyncExecutor(1);

    // create a string of generally-overlapping polygons, will draw in
    // blue//w  ww.  ja v a  2 s . c o  m
    GeoTester.randomPolys(3, 40, 80, new Vector2(100, 300), blue);
    float s = 10;
    Polygon p0 = new Polygon(new float[] {
            // north-west, low, north-east
            0, 3 * s, 0, 2 * s, 2 * s, 0, 3 * s, 0, 4.5f * s, 2 * s, 6 * s, 0, 7 * s, 0, 9 * s, 2 * s, 9 * s,
            3 * s,
            // north-east, high, north-west
            8 * s, 3 * s, 6.5f * s, 1 * s, 5 * s, 3 * s, 4 * s, 3 * s, 2.5f * s, s, 1 * s, 3 * s });
    blue.add(p0);
    // merge them into a single polygon, will draw in red
    for (Polygon bp : blue) {
        GeometryUtils.merge(geo, bp);
    }
    Geometry collapsed = GeometryUtils.collapse(geo);
    Polygon p = GeometryUtils.jtsCoordsToGdx(collapsed.getCoordinates());
    red.add(p);

    triangles = GeometryUtils.triangulate(collapsed);
    Gdx.app.error("GeoTester", "ready to display triangles worth " + triangles.length + " vertices");

    // use the polygon to clip a randomly-generated texture
    textureSolid = new Texture(GeoTester.randomPixmap(100, 100, null), false);

    PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid), p.getVertices(), triangles);
    poly = new PolygonSprite(polyReg);
    poly.setOrigin(p.getVertices()[0], p.getVertices()[1]);
    polyBatch = new PolygonSpriteBatch();

    // prepare rendering aids
    shapeRenderer = new ShapeRenderer();

    Gdx.input.setInputProcessor(this);
}

From source file:es.eucm.ead.editor.utils.TexturedShapeEditor.java

License:Open Source License

@Override
public void render() {
    super.render();
    renderPolygonSprite();//from  ww  w .  ja  v  a2 s.  c o  m

    renderTriangles(red.get(0), triangles);
    renderPolygonShapes(blue, Color.BLUE, 0, 0);
    renderPolygonShapes(red, Color.RED, 0, 0);

    Array<Polygon> a = new Array<Polygon>();
    a.add(GeometryUtils.createPoly(6, polySize, new Vector2(lastX, lastY)));
    renderPolygonShapes(a, Color.DARK_GRAY, 0, 0);

    if (Gdx.input.isTouched()) {
        int mouseX = Gdx.input.getX();
        int mouseY = height - Gdx.input.getY();
        if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
            pendingMerges.offer(GeometryUtils.createPoly(6, polySize, new Vector2(mouseX, mouseY)));
        } else if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)) {
            pendingRemoves.offer(GeometryUtils.createPoly(6, polySize, new Vector2(mouseX, mouseY)));
        }
    }

    if (updatedRegion != null && updatedRegion.isDone()) {
        Gdx.app.error("GeoTester", "merging in...");
        poly.setRegion(updatedRegion.get());
        triangles = updatedRegion.get().getTriangles();
        red.clear();
        red.add(new Polygon(updatedRegion.get().getVertices()));
        updatedRegion = null;
    } else if (!pendingMerges.isEmpty() && updatedRegion == null) {
        updatedRegion = executor.submit(new AsyncTask<PolygonRegion>() {
            @Override
            public PolygonRegion call() throws Exception {
                long t0 = System.nanoTime();
                while (!pendingMerges.isEmpty()) {
                    GeometryUtils.merge(geo, pendingMerges.poll());
                }
                Geometry collapsed = GeometryUtils.collapse(geo);
                GeometryUtils.simplify(geo, 3);
                Polygon p = GeometryUtils.jtsCoordsToGdx(collapsed.getCoordinates());
                short[] ts = GeometryUtils.triangulate(collapsed);
                PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid), p.getVertices(), ts);
                long t1 = System.nanoTime() - t0;
                Gdx.app.error("GeoTester", "ready to display triangles worth " + ts.length
                        + " vertices after merge in " + (t1 / 1000000) + " ms");
                return polyReg;
            }
        });
    } else if (!pendingRemoves.isEmpty() && updatedRegion == null) {
        updatedRegion = executor.submit(new AsyncTask<PolygonRegion>() {
            @Override
            public PolygonRegion call() throws Exception {
                long t0 = System.nanoTime();
                while (!pendingRemoves.isEmpty()) {
                    GeometryUtils.subtract(geo, pendingRemoves.poll());
                }
                Geometry collapsed = GeometryUtils.collapse(geo);
                if (GeoTester.r(0, 10) < 4) {
                    GeometryUtils.simplify(geo, 3);
                }
                Polygon p = GeometryUtils.jtsCoordsToGdx(collapsed.getCoordinates());
                short[] ts = GeometryUtils.triangulate(collapsed);
                PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid), p.getVertices(), ts);
                long t1 = System.nanoTime() - t0;
                Gdx.app.error("GeoTester", "ready to display triangles worth " + ts.length
                        + " vertices after removal in " + (t1 / 1000000) + " ms");
                return polyReg;
            }
        });
    }
}

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   www .j  av a 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;/*from  w w w .j  av a 2s. 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  ww  . ja  v a  2 s .  c o m*/
 * 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

/**
 * Make new polygon regions.// ww  w  .j  a v  a 2  s .  co m
 *
 * @param triangles one triangle for each polygon region.
 */
private void setTriangles(float[] triangles) {
    if (textureRegion == null)
        throw new RuntimeException("You must set the texture region before using this method. ");

    this.triangles = triangles;
    polygonRegions.clear();

    for (int i = 0; i < triangles.length;) {

        float[] triangle = new float[6];
        for (int j = 0; j < triangle.length; j++) {
            triangle[j] = (triangles[i++] / textureScale);
        }

        PolygonRegion polygonRegion = new PolygonRegion(textureRegion, triangle, fillingTriangle);

        polygonRegions.add(polygonRegion);
    }
}