Better galaxy.

Change-Id: I8f04409db1976e474e336a393bf3d41cbfcb7a42
diff --git a/res/raw/galaxy.rs b/res/raw/galaxy.rs
index 09c387a..cf7c7ec 100644
--- a/res/raw/galaxy.rs
+++ b/res/raw/galaxy.rs
@@ -25,14 +25,12 @@
 #define ELLIPSE_TWIST 0.023333333f
 
 float angle;
-float distance;
 
 /**
  * Script initialization. Called automatically.
  */
 void init() {
-    angle = 37.0f;
-    distance = 0.55f;
+    angle = 50.0f;
 }
 
 /**
@@ -96,6 +94,10 @@
  * Initialize all the stars. Called from Java.
  */
 void initParticles() {
+    if (State->isPreview == 1) {
+        angle = 0.0f;
+    }
+
     struct Stars_s *star = Stars;
     struct Particles_s *part = Particles;
     int particlesCount = State->particlesCount;
@@ -135,20 +137,28 @@
              x, y + scale, 0.0f);
 }
 
-void drawParticles(float xOffset, int width, int height) {
+void drawParticles(float xOffset, float offset, int width, int height) {
     bindProgramVertex(NAMED_PVStars);
     bindProgramFragment(NAMED_PFStars);
     bindProgramFragmentStore(NAMED_PFSLights);
     bindTexture(NAMED_PFStars, 0, NAMED_TFlares);
 
+    float a = offset * angle;
+    float absoluteAngle = fabsf(a);
+
     float matrix[16];
-    matrixLoadTranslate(matrix, 0.0f, 0.0f, 10.0f - 6.0f * distance);
-    matrixScale(matrix, 6.6f, 6.0f, 1.0f);
-    matrixRotate(matrix, angle, 1.0f, 0.5f, 0.0f);
+    matrixLoadTranslate(matrix, 0.0f, 0.0f, 10.0f - 6.0f * absoluteAngle / 50.0f);
+    if (State->scale == 0) {
+        matrixScale(matrix, 6.6f, 6.0f, 1.0f);
+    } else {
+        matrixScale(matrix, 7.6f, 16.0f, 1.0f);
+    }
+    matrixRotate(matrix, absoluteAngle, 1.0f, 0.0f, 0.0f);
+    matrixRotate(matrix, a, 0.0f, 0.4f, 0.1f);
     vpLoadModelMatrix(matrix);
 
     // quadratic attenuation
-    pointAttenuation(0.1f, 0.0f, 0.06f);
+    pointAttenuation(0.1f + 0.3f * fabsf(offset), 0.0f, 0.06f  + 0.1f *  fabsf(offset));
 
     int radius = State->galaxyRadius;
     int particlesCount = State->particlesCount;
@@ -179,24 +189,13 @@
     int width = State->width;
     int height = State->height;
 
-    float x = lerpf(1.0f, -1.0f, State->xOffset);
+    float x = 0.0f;
+    float offset = lerpf(-1.0f, 1.0f, State->xOffset);
 
     drawSpace(x, width, height);
-    drawParticles(x, width, height);
-    drawLights(x, width, height);
 
-    if (State->isPreview == 0) {
-        if (angle > 0.0f) {
-            angle -= 0.4f;
-            distance = angle / 68.0f;
-        }
-    } else {
-        // Unfortunately this cannot happen in init()
-        // since the State structure instance does not
-        // exist at this point
-        angle = 0.0f;
-        distance = 0.0f;
-    }
+    drawParticles(x, offset, width, height);
+    drawLights(x, width, height);
 
     return 1;
 }
diff --git a/src/com/android/wallpaper/galaxy/GalaxyRS.java b/src/com/android/wallpaper/galaxy/GalaxyRS.java
index e428943..b01dcad 100644
--- a/src/com/android/wallpaper/galaxy/GalaxyRS.java
+++ b/src/com/android/wallpaper/galaxy/GalaxyRS.java
@@ -89,6 +89,7 @@
     private Allocation mParticlesBuffer;
     @SuppressWarnings({"FieldCanBeLocal"})
     private SimpleMesh mParticlesMesh;
+    private ScriptC.Invokable mInitParticles;
 
     GalaxyRS(int width, int height) {
         super(width, height);
@@ -110,18 +111,18 @@
         sb.setType(mStateType, "State", RSID_STATE);
         sb.setType(mParticlesMesh.getVertexType(0), "Particles", RSID_PARTICLES_BUFFER);
         sb.setType(mParticlesType, "Stars", RSID_PARTICLES);
-        ScriptC.Invokable initParticles = sb.addInvokable("initParticles");
+        mInitParticles = sb.addInvokable("initParticles");
         sb.setScript(mResources, R.raw.galaxy);
         sb.setRoot(true);
 
         ScriptC script = sb.create();
-        script.setClearColor(1.0f, 0.0f, 0.0f, 1.0f);
+        script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
         script.setTimeZone(TimeZone.getDefault().getID());
 
         script.bindAllocation(mState, RSID_STATE);
         script.bindAllocation(mParticles, RSID_PARTICLES);
         script.bindAllocation(mParticlesBuffer, RSID_PARTICLES_BUFFER);
-        initParticles.execute();
+        mInitParticles.execute();
 
         return script;
     }
@@ -162,9 +163,11 @@
 
         mGalaxyState.width = width;
         mGalaxyState.height = height;
+        mGalaxyState.scale = width > height ? 1 : 0;
         mState.data(mGalaxyState);
 
         mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
+        mInitParticles.execute();
     }
 
     static class GalaxyState {
@@ -174,6 +177,7 @@
         public int galaxyRadius;
         public float xOffset;
         public int isPreview;
+        public int scale;
     }
 
     static class GalaxyParticle {
@@ -190,6 +194,7 @@
         mGalaxyState = new GalaxyState();
         mGalaxyState.width = mWidth;
         mGalaxyState.height = mHeight;
+        mGalaxyState.scale = mWidth > mHeight ? 1 : 0;
         mGalaxyState.particlesCount = PARTICLES_COUNT;
         mGalaxyState.galaxyRadius = GALAXY_RADIUS;
         mGalaxyState.isPreview = isPreview ? 1 : 0;