1 |
808ef3aa
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2020 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Distorted. //
|
5 |
|
|
// //
|
6 |
|
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
|
|
// it under the terms of the GNU General Public License as published by //
|
8 |
|
|
// the Free Software Foundation, either version 2 of the License, or //
|
9 |
|
|
// (at your option) any later version. //
|
10 |
|
|
// //
|
11 |
|
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14 |
|
|
// GNU General Public License for more details. //
|
15 |
|
|
// //
|
16 |
|
|
// You should have received a copy of the GNU General Public License //
|
17 |
|
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.library.mesh;
|
21 |
|
|
|
22 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23 |
|
|
/**
|
24 |
|
|
* Create a polygon of any shape and varying elevations from the edges towards the center.
|
25 |
|
|
* <p>
|
26 |
|
|
* Specify a list of vertices. Any two adjacent vertices + the center (0,0,0) form a triangle. The
|
27 |
|
|
* polygon is going to be split into such triangles, and each triange is split into adjustable number
|
28 |
|
|
* of 'bands' form the outer edge towards the center. Edges of each band can can at any elevation.
|
29 |
|
|
*/
|
30 |
|
|
public class MeshPolygon extends MeshBase
|
31 |
|
|
{
|
32 |
|
|
private float[] mPolygonVertices;
|
33 |
|
|
private int mNumPolygonVertices;
|
34 |
|
|
private float[] mPolygonBands;
|
35 |
|
|
private int mNumPolygonBands;
|
36 |
|
|
|
37 |
|
|
private int remainingVert;
|
38 |
|
|
private int numVertices;
|
39 |
|
|
|
40 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41 |
|
|
// polygonVertices>=3 , polygonBands>=2
|
42 |
|
|
|
43 |
|
|
private void computeNumberOfVertices(int numPolygonVertices, int numPolygonBands)
|
44 |
|
|
{
|
45 |
|
|
numVertices = (numPolygonVertices*numPolygonBands+2)*(numPolygonBands-1) - 1;
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49 |
|
|
|
50 |
|
|
private int addVertex(int vertex, int polyBand, int polyVertex, int index, float[] attribs1, float[] attribs2)
|
51 |
|
|
{
|
52 |
|
|
remainingVert--;
|
53 |
|
|
|
54 |
|
|
float band = mPolygonBands[2*polyBand ];
|
55 |
|
|
float elev = mPolygonBands[2*polyBand+1];
|
56 |
|
|
|
57 |
|
|
float Xfirst = mPolygonVertices[2*polyVertex ]*band;
|
58 |
|
|
float Yfirst = mPolygonVertices[2*polyVertex+1]*band;
|
59 |
|
|
|
60 |
|
|
int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
|
61 |
|
|
|
62 |
|
|
float Xlast = mPolygonVertices[2*polyEndVer ]*band;
|
63 |
|
|
float Ylast = mPolygonVertices[2*polyEndVer+1]*band;
|
64 |
|
|
|
65 |
|
|
float quot = (float)index / (polyBand-1);
|
66 |
|
|
|
67 |
|
|
float x = Xfirst + quot*(Xlast-Xfirst);
|
68 |
|
|
float y = Yfirst + quot*(Ylast-Yfirst);
|
69 |
|
|
|
70 |
|
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = x;
|
71 |
|
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
|
72 |
|
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = elev;
|
73 |
|
|
|
74 |
|
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB ] = 0.0f; //
|
75 |
|
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f; // TODO
|
76 |
|
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f; //
|
77 |
|
|
|
78 |
|
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = x+0.5f;
|
79 |
|
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
|
80 |
|
|
|
81 |
|
|
return vertex+1;
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85 |
|
|
|
86 |
|
|
private int createBandStrip(int vertex, int polyBand, int polyVertex, float[] attribs1, float[] attribs2)
|
87 |
|
|
{
|
88 |
|
|
if( polyVertex==0 )
|
89 |
|
|
{
|
90 |
|
|
vertex = addVertex(vertex,polyBand,polyVertex,0,attribs1,attribs2);
|
91 |
|
|
|
92 |
|
|
if( polyBand>0 )
|
93 |
|
|
{
|
94 |
|
|
vertex = addVertex(vertex,polyBand,polyVertex,0,attribs1,attribs2);
|
95 |
|
|
}
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
int numPairs = mNumPolygonBands-1-polyBand;
|
99 |
|
|
|
100 |
|
|
for(int pair=0; pair<numPairs; pair++)
|
101 |
|
|
{
|
102 |
|
|
vertex = addVertex(vertex,polyBand+1,polyVertex,pair ,attribs1,attribs2);
|
103 |
|
|
vertex = addVertex(vertex,polyBand ,polyVertex,pair+1,attribs1,attribs2);
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
return vertex;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110 |
|
|
|
111 |
|
|
private void buildGrid(float[] attribs1, float[] attribs2)
|
112 |
|
|
{
|
113 |
|
|
int vertex=0;
|
114 |
|
|
|
115 |
|
|
for(int band=0; band<mNumPolygonBands-1; band++)
|
116 |
|
|
for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
|
117 |
|
|
{
|
118 |
|
|
vertex = createBandStrip(vertex,band,polyVertex,attribs1,attribs2);
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
123 |
|
|
// PUBLIC API
|
124 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125 |
|
|
/**
|
126 |
|
|
* Create a polygon of any shape and varying elevations from the edges towards the center.
|
127 |
|
|
*
|
128 |
|
|
* @param verticesXY 2N floats - packed description of polygon vertices. N pairs (x,y).
|
129 |
|
|
* @param bands 2K floats; K pairs of two floats each describing a single band.
|
130 |
|
|
* From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center,
|
131 |
|
|
* its elevation). The polygon is split into such concentric bands.
|
132 |
|
|
*/
|
133 |
|
|
public MeshPolygon(float[] verticesXY, float[] bands)
|
134 |
|
|
{
|
135 |
|
|
super();
|
136 |
|
|
|
137 |
|
|
mPolygonVertices = verticesXY;
|
138 |
|
|
mPolygonBands = bands;
|
139 |
|
|
mNumPolygonVertices = mPolygonVertices.length /2;
|
140 |
|
|
mNumPolygonBands = mPolygonBands.length;
|
141 |
|
|
|
142 |
|
|
computeNumberOfVertices(mNumPolygonVertices,mNumPolygonBands);
|
143 |
|
|
|
144 |
|
|
float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
|
145 |
|
|
float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
|
146 |
|
|
|
147 |
|
|
buildGrid(attribs1,attribs2);
|
148 |
|
|
|
149 |
|
|
if( remainingVert!=0 )
|
150 |
|
|
android.util.Log.d("MeshPolygon", "remainingVert " +remainingVert );
|
151 |
|
|
|
152 |
|
|
setAttribs(attribs1,attribs2);
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156 |
|
|
/**
|
157 |
|
|
* Copy constructor.
|
158 |
|
|
*/
|
159 |
|
|
public MeshPolygon(MeshPolygon mesh, boolean deep)
|
160 |
|
|
{
|
161 |
|
|
super(mesh,deep);
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165 |
|
|
/**
|
166 |
|
|
* Copy the Mesh.
|
167 |
|
|
*
|
168 |
|
|
* @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
|
169 |
|
|
* normals and inflates (the rest, in particular the mVertAttribs2 containing texture
|
170 |
|
|
* coordinates and effect associations, is always deep copied)
|
171 |
|
|
*/
|
172 |
|
|
public MeshPolygon copy(boolean deep)
|
173 |
|
|
{
|
174 |
|
|
return new MeshPolygon(this,deep);
|
175 |
|
|
}
|
176 |
|
|
}
|