Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshSphere.java @ d23592bb

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.mesh;
22

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

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

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

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

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

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

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

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

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

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

    
93
      attribs2[VERT2_ATTRIBS*currentVert + TEX_ATTRIB  ] = attribs2[VERT2_ATTRIBS*(currentVert-1) + TEX_ATTRIB  ];
94
      attribs2[VERT2_ATTRIBS*currentVert + TEX_ATTRIB+1] = attribs2[VERT2_ATTRIBS*(currentVert-1) + TEX_ATTRIB+1];
95

    
96
      currentVert++;
97
      }
98
    }
99

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

    
116
  private double midLatitude(double lat1, double lat2, double quot)
117
    {
118
    return lat1*(1.0-quot)+lat2*quot;
119
    }
120

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

    
128
  private double midLongitude(double lon1, double lon2, double quot)
129
    {
130
    double min, max;
131

    
132
    if( lon1<lon2 ) { min=lon1; max=lon2; }
133
    else            { min=lon2; max=lon1; }
134

    
135
    double diff = max-min;
136
    if( diff>P ) { diff=2*P-diff; min=max; }
137

    
138
    double ret = min+quot*diff;
139
    if( ret>=2*P ) ret-=2*P;
140

    
141
    return ret;
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
// linear map (column,row, level):
146
//
147
// (      0,       0, level) -> (lonV1,latV12)
148
// (      0, level-1, level) -> (lonV3,latV3 )
149
// (level-1,       0, level) -> (lonV2,latV12)
150

    
151
  private void addVertex(float[] attribs1, float[] attribs2, int column, int row, int level,
152
                         double lonV1, double lonV2, double latV12, double latV3)
153
    {
154
    double quotX = (double)column/level;
155
    double quotY = (double)row   /level;
156
    double quotZ;
157

    
158
    if( latV12*latV3 < 0.0 )  // equatorial triangle
159
      {
160
      quotZ = quotX + 0.5*quotY;
161
      }
162
    else                      // polar triangle
163
      {
164
      quotZ = (quotY==1.0 ? 0.5 : quotX / (1.0-quotY));
165
      }
166

    
167
    double longitude = midLongitude(lonV1, lonV2, quotZ );
168
    double latitude  = midLatitude(latV12, latV3, quotY );
169

    
170
    double sinLON = Math.sin(longitude);
171
    double cosLON = Math.cos(longitude);
172
    double sinLAT = Math.sin(latitude);
173
    double cosLAT = Math.cos(latitude);
174

    
175
    float x = (float)(cosLAT*sinLON / 2.0f);
176
    float y = (float)(sinLAT        / 2.0f);
177
    float z = (float)(cosLAT*cosLON / 2.0f);
178

    
179
    double texX = 0.5 + longitude/(2*P);
180
    if( texX>=1.0 ) texX-=1.0;
181

    
182
    double texY = 0.5 + latitude/P;
183

    
184
    attribs1[VERT1_ATTRIBS*currentVert + POS_ATTRIB  ] = x;  //
185
    attribs1[VERT1_ATTRIBS*currentVert + POS_ATTRIB+1] = y;  //
186
    attribs1[VERT1_ATTRIBS*currentVert + POS_ATTRIB+2] = z;  //
187
                                                             //  In case of this Mesh so it happens that
188
    attribs1[VERT1_ATTRIBS*currentVert + NOR_ATTRIB  ] = 2*x;//  the vertex coords, normal vector, and
189
    attribs1[VERT1_ATTRIBS*currentVert + NOR_ATTRIB+1] = 2*y;//  inflate vector have identical (x,y,z).
190
    attribs1[VERT1_ATTRIBS*currentVert + NOR_ATTRIB+2] = 2*z;//
191

    
192
    attribs2[VERT2_ATTRIBS*currentVert + TEX_ATTRIB  ] = (float)texX;
193
    attribs2[VERT2_ATTRIBS*currentVert + TEX_ATTRIB+1] = (float)texY;
194

    
195
    currentVert++;
196

    
197
    ////////////////////////////////////////////////////////////////////////////////////////////////
198
    // Problem: on the 'change of date' line in the back of the sphere, some triangles see texX
199
    // coords suddenly jump from 1-epsilon to 0, which looks like a seam with a narrow copy of
200
    // the whole texture there. Solution: remap texX to 1.0.
201
    ////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    if( currentVert>=3 && texX==0.0 )
204
      {
205
      double tex1 = attribs2[VERT2_ATTRIBS*(currentVert-2) + TEX_ATTRIB];
206
      double tex2 = attribs2[VERT2_ATTRIBS*(currentVert-3) + TEX_ATTRIB];
207

    
208
      // if the triangle is not degenerate and last vertex was on the western hemisphere
209
      if( tex1!=tex2 && tex1>0.5 )
210
        {
211
        attribs2[VERT2_ATTRIBS*(currentVert-1) + TEX_ATTRIB] = 1.0f;
212
        }
213
      }
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  private void buildFace(float[] attribs1, float[] attribs2, int level, int face, double lonV1, double lonV2, double latV12, double latV3)
219
    {
220
    for(int row=0; row<level; row++)
221
      {
222
      for (int column=0; column<level-row; column++)
223
        {
224
        addVertex(attribs1, attribs2, column, row  , level, lonV1, lonV2, latV12, latV3);
225
        if (column==0 && !(face==0 && row==0 ) ) repeatVertex(attribs1, attribs2);
226
        addVertex(attribs1, attribs2, column, row+1, level, lonV1, lonV2, latV12, latV3);
227
        }
228

    
229
      addVertex(attribs1, attribs2, level-row, row , level, lonV1, lonV2, latV12, latV3);
230
      if( row!=level-1 || face!=NUMFACES-1 ) repeatVertex(attribs1, attribs2);
231
      }
232
    }
233

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

    
251
    computeNumberOfVertices(level);
252
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
253
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
254

    
255
    for(int face=0; face<NUMFACES; face++ )
256
      {
257
      buildFace(attribs1, attribs2, level, face, FACES[face][0], FACES[face][1], FACES[face][2], FACES[face][3]);
258
      }
259

    
260
    if( currentVert!=numVertices )
261
      android.util.Log.d("MeshSphere", "currentVert= " +currentVert+" numVertices="+numVertices );
262

    
263
    setAttribs(attribs1, attribs2);
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
/**
268
 * Copy cconstructor.
269
 */
270
  public MeshSphere(MeshSphere mesh, boolean deep)
271
    {
272
    super(mesh,deep);
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
/**
277
 * Copy the Mesh.
278
 *
279
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
280
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
281
 *             coordinates and effect associations, is always deep copied)
282
 */
283
  public MeshSphere copy(boolean deep)
284
    {
285
    return new MeshSphere(this,deep);
286
    }
287
  }
(8-8/10)