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