/*
 * Sphere.java
 *
 * Alex S.
 */

/**
 * Sphere
 */
public class Sphere extends Shape {

    /**
     * make a globe sphere mesh
     *
     * @param m Horizontal splits.
     * @param n Vertical Splits.
     */
    public float[][][] globeMesh(int m,int n){
        float mesh[][][] = new float[m][n][3];
        for (int i = 0 ; i < m ; i++){
            for (int j = 0 ; j < n ; j++) {
                float u = i / (m - (float)1.0);
                float v = j / (n - (float)1.0);
                float theta = (float)2.0 * (float)Math.PI * u;
                float phi = (float)Math.PI * v - (float)Math.PI/(float)2.0;
                mesh[i][j][0] = (float)Math.cos(theta) * (float)Math.cos(phi);
                mesh[i][j][1] = (float)Math.sin(theta) * (float)Math.cos(phi);
                mesh[i][j][2] = (float)Math.sin(phi);
            }
        }
        return mesh;
    }

    /**
     * spheres
     */
    public Sphere (Material m){
        super(m);
        
        float[][][] mesh = globeMesh(16,16);

        faces = new int[15*15][4];
        for(int i=0;i<mesh.length-1;i++){
            for(int j=0;j<mesh[i].length-1;j++){
                faces[i*15+j][0] = i * 16 + j;
                faces[i*15+j][1] = (i+1) * 16 + j;
                faces[i*15+j][2] = (i+1) * 16 + (j+1);
                faces[i*15+j][3] = i * 16 + j+1;
            }
        }

        vertices = new float[16*16][8];
        for(int i=0;i<mesh.length;i++){
            for(int j=0;j<mesh[i].length;j++){
                vertices[i*16+j][0] = mesh[i][j][0];
                vertices[i*16+j][1] = mesh[i][j][1];
                vertices[i*16+j][2] = mesh[i][j][2];
                vertices[i*16+j][3] = mesh[i][j][0];
                vertices[i*16+j][4] = mesh[i][j][1];
                vertices[i*16+j][5] = mesh[i][j][2];
                vertices[i*16+j][7] = (float)i/(float)mesh.length;
                vertices[i*16+j][6] = (float)j/(float)mesh[i].length;

                //System.out.println("x: "+vertices[i*16+j][6]+"; y: "+vertices[i*16+j][7]);
                
            }
        }
        
        verticest = new float[vertices.length][8];        
    }

}



