1 /*******************************************************************************
\r
2 * Copyright 2011 See AUTHORS file.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 ******************************************************************************/
\r
17 package com.badlogic.gdx.graphics.g2d;
\r
19 /** Defines a polygon shape on top of a texture region to avoid drawing transparent pixels.
\r
21 * THIS STUFF IS A WIP
\r
23 * @see PolygonRegionLoader
\r
24 * @author Stefan Bachmann
\r
25 * @author Nathan Sweet */
\r
26 public class PolygonRegion {
\r
27 final float[] textureCoords; // texture coordinates in atlas coordinates
\r
28 final float[] vertices; // pixel coordinates relative to source image.
\r
29 final short[] triangles;
\r
30 final TextureRegion region;
\r
32 /** Creates a PolygonRegin by triangulating the polygon coordinates in vertices and calculates uvs based on that. TextureRegion
\r
33 * can come from an atlas.
\r
34 * @param region the region used for drawing
\r
35 * @param vertices contains 2D polygon coordinates in pixels relative to source region */
\r
36 public PolygonRegion (TextureRegion region, float[] vertices, short[] triangles) {
\r
37 this.region = region;
\r
38 this.vertices = vertices;
\r
39 this.triangles = triangles;
\r
41 textureCoords = new float[vertices.length];
\r
42 float uvWidth = region.u2 - region.u;
\r
43 float uvHeight = region.v2 - region.v;
\r
44 for (int i = 0, n = vertices.length; i < n; i++) {
\r
45 textureCoords[i] = region.getU() + uvWidth * (vertices[i] / region.getRegionWidth());
\r
47 textureCoords[i] = region.getV() + uvHeight * (1 - (vertices[i] / region.getRegionHeight()));
\r
51 /** Returns the vertices in local space. */
\r
52 public float[] getVertices () {
\r
56 public short[] getTriangles () {
\r
60 public float[] getTextureCoords () {
\r
61 return textureCoords;
\r
64 public TextureRegion getRegion () {
\r