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