package sed.sky;

import java.util.Random;

import com.jme3.math.FastMath;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer.Type;

public class StarField extends Mesh {
    
    private int numStars;
    private float radius;
    
    public StarField(int numStars, float radius) {
        this.numStars = numStars;
        this.radius = radius;
        initGeometry();
    }
    
    private void initGeometry() {
        float[] starFieldCoords = new float[numStars*3];
        float[] starFieldColors = new float[numStars*4];
        
        Random rand = new Random();
        for(int i = 0; i < numStars; i++) {
            float phi = (rand.nextFloat()*359f)/180f*FastMath.PI;
            float theta = (5+rand.nextFloat()*82f)/180f*FastMath.PI;
            starFieldCoords[i*3+0] = (float)( radius*Math.sin(theta)*Math.cos(phi));
            starFieldCoords[i*3+1] = (float)( radius*Math.cos(theta));
            starFieldCoords[i*3+2] = (float)(-radius*Math.sin(theta)*Math.sin(phi));
            float mag = 0.2f + 0.8f * rand.nextFloat();
            starFieldColors[i*4+0] = starFieldColors[i*4+1] = starFieldColors[i*4+2] = mag;
            starFieldColors[i*4+3] = 1f;
        }
        
        setBuffer(Type.Position, 3, starFieldCoords);
        setBuffer(Type.Color, 4, starFieldColors);
        setMode(Mode.Points);
        setStatic();
        updateBound();
    }
}

@@ -177,24 +177,311 @@
 Mesh %7B%0A    %0A
+    private static final float PhiRange = 359f; // in degree%0A    private static final float ThetaRange = 75f; // in degree%0A    private static final float ThetaOffset = 5f; // in degree%0A    private static final float MagOffset = 0.4f;%0A    private static final float MagRange = 0.6f;%0A    %0A
     private 
@@ -490,24 +490,24 @@
 t numStars;%0A
-
     private 
@@ -928,61 +928,323 @@
     
-float phi = (rand.nextFloat()*359f)/180f*FastMath.PI;
+// phi range should cover the whole 360%C2%B0%0A            float phi = (rand.nextFloat() * PhiRange) * FastMath.DEG_TO_RAD;%0A            // the theta offset prevents positions exactly in zenith%0A            // (theta = 0 in zenith), and theta range should be smaller than%0A            // 90%C2%B0-offset because horizon is at 90%C2%B0
 %0A   
@@ -1271,10 +1271,22 @@
  = (
-5+
+ThetaOffset + 
 rand
@@ -1301,31 +1301,220 @@
 at()
-*82f)/180f*FastMath.PI;
+ * ThetaRange) * FastMath.DEG_TO_RAD;%0A            // the magnitude provides a grey scale color value in %5B0.0, 1.0%5D%0A            float mag = MagOffset + MagRange * rand.nextFloat();%0A            // generate position
 %0A   
@@ -1767,51 +1767,25 @@
     
-float mag = 0.2f + 0.8f * rand.nextFloat();
+// generate color
 %0A   
@@ -1817,16 +1817,38 @@
 i*4+0%5D =
+ mag; // R%0A           
  starFie
@@ -1864,16 +1864,38 @@
 i*4+1%5D =
+ mag; // G%0A           
  starFie
@@ -1916,16 +1916,21 @@
 %5D = mag;
+ // B
 %0A       
@@ -1962,16 +1962,21 @@
 3%5D = 1f;
+ // A
 %0A       

Refactor out some star field constants