OSDN Git Service

Javadocs.
[mikumikustudio/libgdx-mikumikustudio.git] / gdx / src / com / badlogic / gdx / graphics / g2d / PolygonRegion.java
1 /*******************************************************************************\r
2  * Copyright 2011 See AUTHORS file.\r
3  * \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
7  * \r
8  *   http://www.apache.org/licenses/LICENSE-2.0\r
9  * \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
16 \r
17 package com.badlogic.gdx.graphics.g2d;\r
18 \r
19 /** Defines a polygon shape on top of a texture region to avoid drawing transparent pixels.\r
20  * <p>\r
21  * THIS STUFF IS A WIP\r
22  * <p>\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
31 \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
40 \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
46                         i++;\r
47                         textureCoords[i] = region.getV() + uvHeight * (1 - (vertices[i] / region.getRegionHeight()));\r
48                 }\r
49         }\r
50 \r
51         /** Returns the vertices in local space. */\r
52         public float[] getVertices () {\r
53                 return vertices;\r
54         }\r
55 \r
56         public short[] getTriangles () {\r
57                 return triangles;\r
58         }\r
59 \r
60         public float[] getTextureCoords () {\r
61                 return textureCoords;\r
62         }\r
63 \r
64         public TextureRegion getRegion () {\r
65                 return region;\r
66         }\r
67 }\r