Project

General

Profile

Download (13.5 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / mesh / MeshSphere.java @ 4f81e0c8

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 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 Mesh which approximates a sphere.
25
 * <p>
26
 * Do so by starting off with a 16-faced solid which is basically a regular dodecahedron with each
27
 * of its 8 faces vertically split into 2 triangles, and which each step divide each of its triangular
28
 * faces into smaller and smaller subtriangles and inflate their vertices to lay on the surface or the
29
 * sphere.
30
 */
31
public class MeshSphere extends MeshBase
32
  {
33
  private static final int NUMFACES = 16;
34
  private static final double P = Math.PI;
35

    
36
  // An array of 16 entries, each describing a single face of a solid in an (admittedly) weird
37
  // fashion. Each face is a triangle, with 2 vertices on the same latitude.
38
  // Single row is (longitude of V1, longitude of V2, (common) latitude of V1 and V2, latitude of V3)
39
  // longitude of V3 is simply midpoint of V1 and V2 so we don't have to specify it here.
40

    
41
  private static final double[][] FACES =      {
42

    
43
      { 0.00*P, 0.25*P, 0.0, 0.5*P },
44
      { 0.25*P, 0.50*P, 0.0, 0.5*P },
45
      { 0.50*P, 0.75*P, 0.0, 0.5*P },
46
      { 0.75*P, 1.00*P, 0.0, 0.5*P },
47
      { 1.00*P, 1.25*P, 0.0, 0.5*P },
48
      { 1.25*P, 1.50*P, 0.0, 0.5*P },
49
      { 1.50*P, 1.75*P, 0.0, 0.5*P },
50
      { 1.75*P, 0.00*P, 0.0, 0.5*P },
51

    
52
      { 0.00*P, 0.25*P, 0.0,-0.5*P },
53
      { 0.25*P, 0.50*P, 0.0,-0.5*P },
54
      { 0.50*P, 0.75*P, 0.0,-0.5*P },
55
      { 0.75*P, 1.00*P, 0.0,-0.5*P },
56
      { 1.00*P, 1.25*P, 0.0,-0.5*P },
57
      { 1.25*P, 1.50*P, 0.0,-0.5*P },
58
      { 1.50*P, 1.75*P, 0.0,-0.5*P },
59
      { 1.75*P, 0.00*P, 0.0,-0.5*P },
60
                                               };
61
  private int currentVert;
62
  private int numVertices;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
// Each of the 16 faces of the solid requires (level*level + 4*level) vertices for the face
66
// itself and a join to the next face (which requires 2 vertices). We don't need the join in case
67
// of the last, 16th face, thus the -2.
68
// (level*level +4*level) because there are level*level little triangles, each requiring new vertex,
69
// plus 2 extra vertices to start off a row and 2 to move to the next row (or the next face in case
70
// of the last row) and there are 'level' rows.
71

    
72
  private void computeNumberOfVertices(int level)
73
    {
74
    numVertices = NUMFACES*level*(level+4) -2;
75
    currentVert = 0;
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  private void repeatVertex(float[] attribs1, float[] attribs2)
81
    {
82
    if( currentVert>0 )
83
      {
84
      attribs1[VERT1_ATTRIBS*currentVert + POS_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(currentVert-1) + POS_ATTRIB  ];
85
      attribs1[VERT1_ATTRIBS*currentVert + POS_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(currentVert-1) + POS_ATTRIB+1];
86
      attribs1[VERT1_ATTRIBS*currentVert + POS_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(currentVert-1) + POS_ATTRIB+2];
87

    
88
      attribs1[VERT1_ATTRIBS*currentVert + NOR_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(currentVert-1) + NOR_ATTRIB  ];
89
      attribs1[VERT1_ATTRIBS*currentVert + NOR_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(currentVert-1) + NOR_ATTRIB+1];
90
      attribs1[VERT1_ATTRIBS*currentVert + NOR_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(currentVert-1) + NOR_ATTRIB+2];
91

    
92
      attribs1[VERT1_ATTRIBS*currentVert + INF_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(currentVert-1) + INF_ATTRIB  ];
93
      attribs1[VERT1_ATTRIBS*currentVert + INF_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(currentVert-1) + INF_ATTRIB+1];
94
      attribs1[VERT1_ATTRIBS*currentVert + INF_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(currentVert-1) + INF_ATTRIB+2];
95

    
96
      attribs2[VERT2_ATTRIBS*currentVert + TEX_ATTRIB  ] = attribs2[VERT2_ATTRIBS*(currentVert-1) + TEX_ATTRIB  ];
97
      attribs2[VERT2_ATTRIBS*currentVert + TEX_ATTRIB+1] = attribs2[VERT2_ATTRIBS*(currentVert-1) + TEX_ATTRIB+1];
98
      attribs2[VERT2_ATTRIBS*currentVert + ASS_ATTRIB  ] = DEFAULT_ASSOCIATION;
99

    
100
      currentVert++;
101
      }
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
// Supposed to return the latitude of the point between two points on the sphere with latitudes
106
// lat1 and lat2, so if for example quot=0.2, then it will return the latitude of something 20%
107
// along the way from lat1 to lat2.
108
//
109
// This is approximation only - in general it is of course not true that the midpoint of two points
110
// on a unit sphere with spherical coords (A1,B1) and (A2,B2) is ( (A1+A2)/2, (B1+B2)/2 ) - take
111
// (0,0) and (PI, epsilon) as a counterexample.
112
//
113
// Here however, the latitudes we are interested at are the latitudes of the vertices of a regular
114
// icosahedron - i.e. +=A and +=PI/2, whose longitudes are close, and we don't really care if the
115
// split into smaller triangles is exact.
116
//
117
// quot better be between 0.0 and 1.0.
118
// this is 'directed' i.e. from lat1 to lat2.
119

    
120
  private double midLatitude(double lat1, double lat2, double quot)
121
    {
122
    return lat1*(1.0-quot)+lat2*quot;
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
// Same in case of longitude. This is for our needs exact, because we are ever only calling this with
127
// two longitudes of two vertices with the same latitude. Additional problem: things can wrap around
128
// the circle.
129
// this is 'undirected' i.e. we don't assume from lon1 to lon2 - just along the smaller arc joining
130
// lon1 to lon2.
131

    
132
  private double midLongitude(double lon1, double lon2, double quot)
133
    {
134
    double min, max;
135

    
136
    if( lon1<lon2 ) { min=lon1; max=lon2; }
137
    else            { min=lon2; max=lon1; }
138

    
139
    double diff = max-min;
140
    if( diff>P ) { diff=2*P-diff; min=max; }
141

    
142
    double ret = min+quot*diff;
143
    if( ret>=2*P ) ret-=2*P;
144

    
145
    return ret;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// linear map (column,row, level):
150
//
151
// (      0,       0, level) -> (lonV1,latV12)
152
// (      0, level-1, level) -> (lonV3,latV3 )
153
// (level-1,       0, level) -> (lonV2,latV12)
154

    
155
  private void addVertex(float[] attribs1, float[] attribs2, int column, int row, int level,
156
                         double lonV1, double lonV2, double latV12, double latV3)
157
    {
158
    double quotX = (double)column/level;
159
    double quotY = (double)row   /level;
160
    double quotZ;
161

    
162
    if( latV12*latV3 < 0.0 )  // equatorial triangle
163
      {
164
      quotZ = quotX + 0.5*quotY;
165
      }
166
    else                      // polar triangle
167
      {
168
      quotZ = (quotY==1.0 ? 0.5 : quotX / (1.0-quotY));
169
      }
170

    
171
    double longitude = midLongitude(lonV1, lonV2, quotZ );
172
    double latitude  = midLatitude(latV12, latV3, quotY );
173

    
174
    double sinLON = Math.sin(longitude);
175
    double cosLON = Math.cos(longitude);
176
    double sinLAT = Math.sin(latitude);
177
    double cosLAT = Math.cos(latitude);
178

    
179
    float x = (float)(cosLAT*sinLON / 2.0f);
180
    float y = (float)(sinLAT        / 2.0f);
181
    float z = (float)(cosLAT*cosLON / 2.0f);
182

    
183
    double texX = 0.5 + longitude/(2*P);
184
    if( texX>=1.0 ) texX-=1.0;
185

    
186
    double texY = 0.5 + latitude/P;
187

    
188
    attribs1[VERT1_ATTRIBS*currentVert + POS_ATTRIB  ] = x;  //
189
    attribs1[VERT1_ATTRIBS*currentVert + POS_ATTRIB+1] = y;  //
190
    attribs1[VERT1_ATTRIBS*currentVert + POS_ATTRIB+2] = z;  //
191
                                                             //  In case of this Mesh so it happens that
192
    attribs1[VERT1_ATTRIBS*currentVert + NOR_ATTRIB  ] = 2*x;//  the vertex coords, normal vector, and
193
    attribs1[VERT1_ATTRIBS*currentVert + NOR_ATTRIB+1] = 2*y;//  inflate vector have identical (x,y,z).
194
    attribs1[VERT1_ATTRIBS*currentVert + NOR_ATTRIB+2] = 2*z;//
195
                                                             //  TODO: think about some more efficient
196
    attribs1[VERT1_ATTRIBS*currentVert + INF_ATTRIB  ] = x;  //  representation.
197
    attribs1[VERT1_ATTRIBS*currentVert + INF_ATTRIB+1] = y;  //
198
    attribs1[VERT1_ATTRIBS*currentVert + INF_ATTRIB+2] = z;  //
199

    
200
    attribs2[VERT2_ATTRIBS*currentVert + TEX_ATTRIB  ] = (float)texX;
201
    attribs2[VERT2_ATTRIBS*currentVert + TEX_ATTRIB+1] = (float)texY;
202
    attribs2[VERT2_ATTRIBS*currentVert + ASS_ATTRIB  ] = DEFAULT_ASSOCIATION;
203

    
204
    currentVert++;
205

    
206
    ////////////////////////////////////////////////////////////////////////////////////////////////
207
    // Problem: on the 'change of date' line in the back of the sphere, some triangles see texX
208
    // coords suddenly jump from 1-epsilon to 0, which looks like a seam with a narrow copy of
209
    // the whole texture there. Solution: remap texX to 1.0.
210
    ////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
    if( currentVert>=3 && texX==0.0 )
213
      {
214
      double tex1 = attribs2[VERT2_ATTRIBS*(currentVert-2) + TEX_ATTRIB];
215
      double tex2 = attribs2[VERT2_ATTRIBS*(currentVert-3) + TEX_ATTRIB];
216

    
217
      // if the triangle is not degenerate and last vertex was on the western hemisphere
218
      if( tex1!=tex2 && tex1>0.5 )
219
        {
220
        attribs2[VERT2_ATTRIBS*(currentVert-1) + TEX_ATTRIB] = 1.0f;
221
        }
222
      }
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  private void buildFace(float[] attribs1, float[] attribs2, int level, int face, double lonV1, double lonV2, double latV12, double latV3)
228
    {
229
    for(int row=0; row<level; row++)
230
      {
231
      for (int column=0; column<level-row; column++)
232
        {
233
        addVertex(attribs1, attribs2, column, row  , level, lonV1, lonV2, latV12, latV3);
234
        if (column==0 && !(face==0 && row==0 ) ) repeatVertex(attribs1, attribs2);
235
        addVertex(attribs1, attribs2, column, row+1, level, lonV1, lonV2, latV12, latV3);
236
        }
237

    
238
      addVertex(attribs1, attribs2, level-row, row , level, lonV1, lonV2, latV12, latV3);
239
      if( row!=level-1 || face!=NUMFACES-1 ) repeatVertex(attribs1, attribs2);
240
      }
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
// PUBLIC API
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
  /**
247
   * Creates the underlying grid of vertices with the usual attribs which approximates a sphere.
248
   * <p>
249
   * When level=1, it outputs the 12 vertices of a regular icosahedron.
250
   * When level=N, it divides each of the 20 icosaherdon's triangular faces into N^2 smaller triangles
251
   * (by dividing each side into N equal segments) and 'inflates' the internal vertices so that they
252
   * touch the sphere.
253
   *
254
   * @param level Specifies the approximation level. Valid values: level &ge; 1
255
   */
256
  public MeshSphere(int level)
257
    {
258
    super();
259

    
260
    computeNumberOfVertices(level);
261
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
262
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
263

    
264
    for(int face=0; face<NUMFACES; face++ )
265
      {
266
      buildFace(attribs1, attribs2, level, face, FACES[face][0], FACES[face][1], FACES[face][2], FACES[face][3]);
267
      }
268

    
269
    if( currentVert!=numVertices )
270
      android.util.Log.d("MeshSphere", "currentVert= " +currentVert+" numVertices="+numVertices );
271

    
272
    setAttribs(attribs1, attribs2);
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
/**
277
 * Copy cconstructor.
278
 */
279
  public MeshSphere(MeshSphere mesh, boolean deep)
280
    {
281
    super(mesh,deep);
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285
/**
286
 * Copy the Mesh.
287
 *
288
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
289
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
290
 *             coordinates and effect assocciations, is always deep copied)
291
 */
292
  public MeshSphere copy(boolean deep)
293
    {
294
    return new MeshSphere(this,deep);
295
    }
296
  }
(6-6/7)