1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
remainingVert = numVertices;
|
47
|
}
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
private int addVertex(int vertex, int polyBand, int polyVertex, int index, float[] attribs1, float[] attribs2)
|
52
|
{
|
53
|
remainingVert--;
|
54
|
|
55
|
if( polyBand<mNumPolygonBands-1 )
|
56
|
{
|
57
|
float band = mPolygonBands[2*polyBand ];
|
58
|
float elev = mPolygonBands[2*polyBand+1];
|
59
|
|
60
|
float Xfirst = mPolygonVertices[2*polyVertex ];
|
61
|
float Yfirst = mPolygonVertices[2*polyVertex+1];
|
62
|
|
63
|
int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
|
64
|
|
65
|
float Xlast = mPolygonVertices[2*polyEndVer ];
|
66
|
float Ylast = mPolygonVertices[2*polyEndVer+1];
|
67
|
|
68
|
float quot = (float)index / (mNumPolygonBands-1-polyBand);
|
69
|
|
70
|
float xEdge = Xfirst + quot*(Xlast-Xfirst);
|
71
|
float yEdge = Yfirst + quot*(Ylast-Yfirst);
|
72
|
|
73
|
float x = band*xEdge;
|
74
|
float y = band*yEdge;
|
75
|
|
76
|
int nextPolyBand = polyBand+1;
|
77
|
int prevPolyBand = (polyBand==0 ? polyBand : polyBand-1);
|
78
|
|
79
|
float concentricStep = mPolygonBands[2*nextPolyBand]-mPolygonBands[2*prevPolyBand];
|
80
|
float nx = concentricStep*xEdge;
|
81
|
float ny = concentricStep*yEdge;
|
82
|
|
83
|
float nz = mPolygonBands[2*prevPolyBand+1]-mPolygonBands[2*nextPolyBand+1];
|
84
|
float lenXYZ = (float)Math.sqrt(nx*nx + ny*ny + nz*nz);
|
85
|
float lenXY = (float)Math.sqrt(nx*nx + ny*ny);
|
86
|
|
87
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = x;
|
88
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
|
89
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = elev;
|
90
|
|
91
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB ] = (nz*nx) / (lenXY*lenXYZ);
|
92
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = (nz*ny) / (lenXY*lenXYZ);
|
93
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = lenXY / lenXYZ;
|
94
|
|
95
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = x+0.5f;
|
96
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
|
97
|
}
|
98
|
else
|
99
|
{
|
100
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = 0.0f;
|
101
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = 0.0f;
|
102
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = mPolygonBands[2*polyBand+1];
|
103
|
|
104
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB ] = 0.0f;
|
105
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
|
106
|
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
|
107
|
|
108
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = 0.5f;
|
109
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 0.5f;
|
110
|
}
|
111
|
|
112
|
return vertex+1;
|
113
|
}
|
114
|
|
115
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116
|
|
117
|
private int createBandStrip(int vertex, int polyBand, int polyVertex, float[] attribs1, float[] attribs2)
|
118
|
{
|
119
|
if( polyVertex==0 )
|
120
|
{
|
121
|
vertex = addVertex(vertex,polyBand,polyVertex,0,attribs1,attribs2);
|
122
|
|
123
|
if( polyBand>0 )
|
124
|
{
|
125
|
vertex = addVertex(vertex,polyBand,polyVertex,0,attribs1,attribs2);
|
126
|
}
|
127
|
}
|
128
|
|
129
|
int numPairs = mNumPolygonBands-1-polyBand;
|
130
|
|
131
|
for(int pair=0; pair<numPairs; pair++)
|
132
|
{
|
133
|
vertex = addVertex(vertex,polyBand+1,polyVertex,pair ,attribs1,attribs2);
|
134
|
vertex = addVertex(vertex,polyBand ,polyVertex,pair+1,attribs1,attribs2);
|
135
|
}
|
136
|
|
137
|
return vertex;
|
138
|
}
|
139
|
|
140
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141
|
|
142
|
private void buildGrid(float[] attribs1, float[] attribs2)
|
143
|
{
|
144
|
int vertex=0;
|
145
|
|
146
|
for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
|
147
|
for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
|
148
|
{
|
149
|
vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2);
|
150
|
}
|
151
|
}
|
152
|
|
153
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
154
|
// PUBLIC API
|
155
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156
|
/**
|
157
|
* Create a polygon of any shape and varying elevations from the edges towards the center.
|
158
|
*
|
159
|
* @param verticesXY 2N floats - packed description of polygon vertices. N pairs (x,y).
|
160
|
* @param bands 2K floats; K pairs of two floats each describing a single band.
|
161
|
* From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center,
|
162
|
* its elevation). The polygon is split into such concentric bands.
|
163
|
* Must be band[2*i] > band[2*(i+1)] !
|
164
|
*/
|
165
|
public MeshPolygon(float[] verticesXY, float[] bands)
|
166
|
{
|
167
|
super();
|
168
|
|
169
|
mPolygonVertices = verticesXY;
|
170
|
mPolygonBands = bands;
|
171
|
mNumPolygonVertices = mPolygonVertices.length /2;
|
172
|
mNumPolygonBands = mPolygonBands.length /2;
|
173
|
|
174
|
computeNumberOfVertices(mNumPolygonVertices,mNumPolygonBands);
|
175
|
|
176
|
float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
|
177
|
float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
|
178
|
|
179
|
buildGrid(attribs1,attribs2);
|
180
|
|
181
|
if( remainingVert!=0 )
|
182
|
android.util.Log.d("MeshPolygon", "remainingVert " +remainingVert );
|
183
|
|
184
|
setAttribs(attribs1,attribs2);
|
185
|
}
|
186
|
|
187
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188
|
/**
|
189
|
* Copy constructor.
|
190
|
*/
|
191
|
public MeshPolygon(MeshPolygon mesh, boolean deep)
|
192
|
{
|
193
|
super(mesh,deep);
|
194
|
}
|
195
|
|
196
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
197
|
/**
|
198
|
* Copy the Mesh.
|
199
|
*
|
200
|
* @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
|
201
|
* normals and inflates (the rest, in particular the mVertAttribs2 containing texture
|
202
|
* coordinates and effect associations, is always deep copied)
|
203
|
*/
|
204
|
public MeshPolygon copy(boolean deep)
|
205
|
{
|
206
|
return new MeshPolygon(this,deep);
|
207
|
}
|
208
|
}
|