commit 89b935767c6b5674726dc80800c71156d0b9277e
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Mon Dec 17 15:45:22 2018 +0000

    Add support for MeshSphere (add ability to display it in the 'Effects3D' and 'Inflate' apps).
    Still a bit buggy!

diff --git a/src/main/java/org/distorted/library/mesh/MeshSphere.java b/src/main/java/org/distorted/library/mesh/MeshSphere.java
new file mode 100644
index 0000000..262a5d9
--- /dev/null
+++ b/src/main/java/org/distorted/library/mesh/MeshSphere.java
@@ -0,0 +1,255 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Copyright 2018 Leszek Koltunski                                                               //
+//                                                                                               //
+// This file is part of Distorted.                                                               //
+//                                                                                               //
+// Distorted is free software: you can redistribute it and/or modify                             //
+// it under the terms of the GNU General Public License as published by                          //
+// the Free Software Foundation, either version 2 of the License, or                             //
+// (at your option) any later version.                                                           //
+//                                                                                               //
+// Distorted is distributed in the hope that it will be useful,                                  //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
+// GNU General Public License for more details.                                                  //
+//                                                                                               //
+// You should have received a copy of the GNU General Public License                             //
+// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+package org.distorted.library.mesh;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Create a Mesh which approximates a sphere.
+ * <p>
+ * Do so by dividing each of the 20 faces of the regular icosahedron into smaller triangles and inflating
+ * those to lay on the surface of the sphere.
+ */
+public class MeshSphere extends MeshBase
+  {
+  private static final int NUMFACES = 20;
+  private static final double sqrt2 = Math.sqrt(2.0);
+  private static final double P = Math.PI;
+  private static final double A = 0.463647609; // arctan(0.5), +-latitude of the 10 'middle' vertices
+                                               // https://en.wikipedia.org/wiki/Regular_icosahedron
+
+  // An array of 20 entries, each describing a single face of the regular icosahedron in an (admittedly)
+  // weird fashion.
+  // Each face of a regular icosahedron is a equilateral triangle, with 2 vertices on the same latitude.
+  // Single row is (longitude of V1, longitude of V2, (common) latitude of V1 and V2, latitude of V3)
+  // longitude of V3 is simply midpoint of V1 and V2 so we don't have to specify it here.
+
+  private static final double FACES[][] =      {
+                                                   { 0.0  , 0.4*P,  A, 0.5*P },
+                                                   { 0.4*P, 0.8*P,  A, 0.5*P },
+                                                   { 0.8*P, 1.2*P,  A, 0.5*P },  // 5 'top' faces with
+                                                   { 1.2*P, 1.6*P,  A, 0.5*P },  // the North Pole
+                                                   { 1.6*P, 2.0*P,  A, 0.5*P },
+
+                                                   { 0.0  , 0.4*P,  A,    -A },
+                                                   { 0.4*P, 0.8*P,  A,    -A },
+                                                   { 0.8*P, 1.2*P,  A,    -A },  // 5 faces mostly above
+                                                   { 1.2*P, 1.6*P,  A,    -A },  // the equator
+                                                   { 1.6*P, 2.0*P,  A,    -A },
+
+                                                   { 0.2  , 0.6*P, -A,     A },
+                                                   { 0.6*P, 1.0*P, -A,     A },
+                                                   { 1.0*P, 1.4*P, -A,     A },  // 5 faces mostly below
+                                                   { 1.4*P, 1.8*P, -A,     A },  // the equator
+                                                   { 1.8*P, 0.2*P, -A,     A },
+
+                                                   { 0.2  , 0.6*P, -A,-0.5*P },
+                                                   { 0.6*P, 1.0*P, -A,-0.5*P },
+                                                   { 1.0*P, 1.4*P, -A,-0.5*P },  // 5 'bottom' faces with
+                                                   { 1.4*P, 1.8*P, -A,-0.5*P },  // the South Pole
+                                                   { 1.8*P, 0.2*P, -A,-0.5*P }
+                                               };
+  private int currentVert;
+  private int numVertices;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Each of the 20 faces of the icosahedron requires (level*level + 4*level) vertices for the face
+// itself and a join to the next face (which requires 2 vertices). We don't need the join in case
+// of the last, 20th face, thus the -2.
+// (level*level +4*level) because there are level*level little triangles, each requiring new vertex,
+// plus 2 extra vertices to start off a row and 2 to move to the next row (or the next face in case
+// of the last row) and there are 'level' rows.
+
+  private void computeNumberOfVertices(int level)
+    {
+    numVertices = 20*level*(level+4) -2;
+    currentVert = 0;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// (longitude,latitude) - spherical coordinates of a point on a unit sphere.
+// Cartesian (0,0,1) - i.e. the point of the sphere closest to the camera - is spherical (0,0).
+
+  private void addVertex( double longitude, double latitude, float[] attribs)
+    {
+    double sinLON = Math.sin(longitude);
+    double cosLON = Math.cos(longitude);
+    double sinLAT = Math.sin(latitude);
+    double cosLAT = Math.cos(latitude);
+
+    float x = (float)(cosLAT*sinLON / sqrt2);
+    float y = (float)(sinLAT        / sqrt2);
+    float z = (float)(cosLAT*cosLON / sqrt2);
+
+    attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB  ] = x;  //
+    attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB+1] = y;  //
+    attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB+2] = z;  //
+                                                           //  In case of this Mesh so it happens that
+    attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB  ] = x;  //  the vertex coords, normal vector, and
+    attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB+1] = y;  //  inflate vector have identical (x,y,z).
+    attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB+2] = z;  //
+                                                           //  TODO: think about some more efficient
+    attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB  ] = x;  //  representation.
+    attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB+1] = y;  //
+    attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB+2] = z;  //
+
+    attribs[VERT_ATTRIBS*currentVert + TEX_ATTRIB  ] = (float)longitude;
+    attribs[VERT_ATTRIBS*currentVert + TEX_ATTRIB+1] = (float)latitude;
+
+    currentVert++;
+    }
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private void repeatLast(float[] attribs)
+    {
+    if( currentVert>0 )
+      {
+      attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB  ] = attribs[VERT_ATTRIBS*(currentVert-1) + POS_ATTRIB  ];
+      attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + POS_ATTRIB+1];
+      attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(currentVert-1) + POS_ATTRIB+2];
+
+      attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB  ] = attribs[VERT_ATTRIBS*(currentVert-1) + NOR_ATTRIB  ];
+      attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + NOR_ATTRIB+1];
+      attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(currentVert-1) + NOR_ATTRIB+2];
+
+      attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB  ] = attribs[VERT_ATTRIBS*(currentVert-1) + INF_ATTRIB  ];
+      attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + INF_ATTRIB+1];
+      attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(currentVert-1) + INF_ATTRIB+2];
+
+      attribs[VERT_ATTRIBS*currentVert + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(currentVert-1) + TEX_ATTRIB  ];
+      attribs[VERT_ATTRIBS*currentVert + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + TEX_ATTRIB+1];
+
+      currentVert++;
+      }
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Supposed to return the latitude of the point between two points on the sphere with latitudes
+// lat1 and lat2, so if for example quot=0.2, then it will return the latitude of something 20%
+// along the way from lat1 to lat2.
+//
+// This is approximation only - in general it is of course not true that the midpoint of two points
+// on a unit sphere with spherical coords (A1,B1) and (A2,B2) is ( (A1+A2)/2, (B1+B2)/2 ) - take
+// (0,0) and (PI, epsilon) as a counterexample.
+//
+// Here however, the latitudes we are interested at are the latitudes of the vertices of a regular
+// icosahedron - i.e. +=A and +=PI/2, whose longitudes are close, and we don't really care if the
+// split into smaller triangles is exact.
+//
+// quot better be between 0.0 and 1.0.
+// this is 'directed' i.e. from lat1 to lat2.
+
+  private double midLatitude(double lat1, double lat2, double quot)
+    {
+    return lat1*(1.0-quot)+lat2*quot;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Same in case of longitude. This is for our needs exact, because we are ever only calling this with
+// two longitudes of two vertices with the same latitude. Additional problem: things can wrap around
+// the circle.
+// this is 'undirected' i.e. we don't assume from lon1 to lon2 - just along the smaller arc joining
+// lon1 to lon2.
+
+  private double midLongitude(double lon1, double lon2, double quot)
+    {
+    double min, max;
+
+    if( lon1<lon2 ) { min=lon1; max=lon2; }
+    else            { min=lon2; max=lon1; }
+
+    double diff = max-min;
+    if( diff>P ) { diff=2*P-diff; min=max; }
+
+    double ret = min+quot*diff;
+    if( ret>=2*P ) ret-=2*P;
+
+    return ret;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// linear map (column,row, level):
+//
+// (      0,       0, level) -> (lonV1,latV12)
+// (      0, level-1, level) -> (lonV3,latV3 )
+// (level-1,       0, level) -> (lonV2,latV12)
+
+  private void newVertex(float[] attribs, int column, int row, int level,
+                         double lonV1, double lonV2, double latV12, double latV3)
+    {
+    double quotX = (double)column/(level-1);
+    double quotY = (double)row   /(level-1);
+
+    double lonPoint = midLongitude(lonV1,lonV2, (quotX+0.5*quotY) );
+    double latPoint = midLatitude(latV12,latV3, quotY);
+
+    addVertex(lonPoint,latPoint,attribs);
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private void buildFace(float[] attribs, int level, int face, double lonV1, double lonV2, double latV12, double latV3)
+    {
+    for(int row=0; row<level; row++)
+      {
+      for (int column=0; column<level-row; column++)
+        {
+        newVertex(attribs, column, row  , level, lonV1, lonV2, latV12, latV3);
+        if (column==0 && !(face==0 && row==0 ) ) repeatLast(attribs);
+        newVertex(attribs, column, row+1, level, lonV1, lonV2, latV12, latV3);
+        }
+
+      newVertex(attribs, level-row, row , level, lonV1, lonV2, latV12, latV3);
+      if( row!=level-1 || face!=NUMFACES-1 ) repeatLast(attribs);
+      }
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// PUBLIC API
+///////////////////////////////////////////////////////////////////////////////////////////////////
+  /**
+   * Creates the underlying grid of vertices with the usual attribs which approximates a sphere.
+   * <p>
+   * When level=1, it outputs the 12 vertices of a regular icosahedron.
+   * When level=N, it divides each of the 20 icosaherdon's triangular faces into N^2 smaller triangles
+   * (by dividing each side into N equal segments) and 'inflates' the internal vertices so that they
+   * touch the sphere.
+   *
+   * @param level Specifies the approximation level. Valid values: level &ge; 1
+   */
+  public MeshSphere(int level)
+    {
+    super(1.0f);
+
+    computeNumberOfVertices(level);
+    float[] attribs= new float[VERT_ATTRIBS*numVertices];
+
+    for(int face=0; face<NUMFACES; face++ )
+      {
+      buildFace(attribs, level, face, FACES[face][0], FACES[face][1], FACES[face][2], FACES[face][3]);
+      }
+
+    if( currentVert!=numVertices )
+      android.util.Log.d("MeshSphere", "currentVert= " +currentVert+" numVertices="+numVertices );
+
+    setAttribs(attribs);
+    }
+  }
\ No newline at end of file
