Better orientation change support in live wallpapers.

Change-Id: I47070895bc9aed6681682315e1a97d4ed036fa69
diff --git a/res/raw/fall.rs b/res/raw/fall.rs
index c898645..4fc8b6e 100644
--- a/res/raw/fall.rs
+++ b/res/raw/fall.rs
@@ -47,8 +47,8 @@
 // The higher, the smaller the ripple
 #define RIPPLE_HEIGHT 10.0f
 
-float g_SkyOffsetX;
-float g_SkyOffsetY;
+float skyOffsetX;
+float skyOffsetY;
 
 struct vert_s {
     float nx;
@@ -235,35 +235,6 @@
     }
 }
 
-float averageZ(float x1, float x2, float y1, float y2, float* vertices,
-        int meshWidth, int meshHeight, float glWidth, float glHeight) {
-
-    x1 = ((x1 + glWidth * 0.5f) / glWidth) * meshWidth;
-    x2 = ((x2 + glWidth * 0.5f) / glWidth) * meshWidth;
-    y1 = ((y1 + glHeight * 0.5f) / glHeight) * meshHeight;
-    y2 = ((y2 + glHeight * 0.5f) / glHeight) * meshHeight;
-
-    int quadX1 = clamp(x1, 0, meshWidth);
-    int quadX2 = clamp(x2, 0, meshWidth);
-    int quadY1 = clamp(y1, 0, meshHeight);
-    int quadY2 = clamp(y2, 0, meshHeight);
-
-    float z = 0.0f;
-    int vertexCount = 0;
-
-    int y = quadY1;
-    for ( ; y < quadY2; y += 1) {
-        int x = quadX1;
-        int yOffset = y * meshWidth;
-        for ( ; x < quadX2; x += 1) {
-            z += vertices[(yOffset + x) << 3 + 7];
-            vertexCount += 1;
-        }
-    }
-
-    return 55.0f * z / vertexCount;
-}
-
 void drawLeaf(int index, float* vertices, int meshWidth, int meshHeight,
         float glWidth, float glHeight) {
 
@@ -292,11 +263,6 @@
     float tz = 0.0f;
     if (a > 0.0f) {
         tz = -a;
-    } else {
-//        z1 = averageZ(x1, x, y1, y, vertices, meshWidth, meshHeight, glWidth, glHeight);
-//        z2 = averageZ(x, x2, y1, y, vertices, meshWidth, meshHeight, glWidth, glHeight);
-//        z3 = averageZ(x, x2, y, y2, vertices, meshWidth, meshHeight, glWidth, glHeight);
-//        z4 = averageZ(x1, x, y, y2, vertices, meshWidth, meshHeight, glWidth, glHeight);
     }
 
     x1 -= x;
@@ -395,15 +361,15 @@
     bindProgramFragmentStore(NAMED_PFSLeaf);
     bindTexture(NAMED_PFSky, 0, NAMED_TSky);
 
-    float x = g_SkyOffsetX + State->skySpeedX;
-    float y = g_SkyOffsetY + State->skySpeedY;
+    float x = skyOffsetX + State->skySpeedX;
+    float y = skyOffsetY + State->skySpeedY;
 
     if (x > 1.0f) x = 0.0f;
     if (x < -1.0f) x = 0.0f;
     if (y > 1.0f) y = 0.0f;
 
-    g_SkyOffsetX = x;
-    g_SkyOffsetY = y;
+    skyOffsetX = x;
+    skyOffsetY = y;
 
     float matrix[16];
     matrixLoadTranslate(matrix, x, y, 0.0f);
diff --git a/src/com/android/wallpaper/fall/FallRS.java b/src/com/android/wallpaper/fall/FallRS.java
index 7aa0feb..10ceab4 100644
--- a/src/com/android/wallpaper/fall/FallRS.java
+++ b/src/com/android/wallpaper/fall/FallRS.java
@@ -16,14 +16,12 @@
 
 package com.android.wallpaper.fall;
 
-import android.renderscript.RenderScript;
 import android.renderscript.ScriptC;
 import android.renderscript.ProgramFragment;
 import android.renderscript.ProgramStore;
 import android.renderscript.ProgramVertex;
 import android.renderscript.Allocation;
 import android.renderscript.Sampler;
-import android.renderscript.Element;
 import android.renderscript.Light;
 import android.renderscript.Type;
 import android.renderscript.SimpleMesh;
@@ -142,11 +140,10 @@
 
         mWorldState.width = width;
         mWorldState.height = height;
+        mWorldState.rotate = 1;
         mState.data(mWorldState);
 
-        mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
-
-        // TODO: Resize everything else, including the mesh and glHeight
+        mPvOrthoAlloc.setupOrthoWindow(width, height);
     }
 
     @Override
@@ -290,6 +287,7 @@
         public float glHeight;
         public float skySpeedX;
         public float skySpeedY;
+        public int rotate;
     }
 
     static class DropState {
@@ -310,6 +308,7 @@
         mWorldState.glHeight = mGlHeight;
         mWorldState.skySpeedX = random(-0.001f, 0.001f);
         mWorldState.skySpeedY = random(0.00008f, 0.0002f);
+        mWorldState.rotate = mWidth > mHeight ? 1 : 0;
 
         mStateType = Type.createFromClass(mRS, WorldState.class, 1, "WorldState");
         mState = Allocation.createTyped(mRS, mStateType);
diff --git a/src/com/android/wallpaper/grass/GrassRS.java b/src/com/android/wallpaper/grass/GrassRS.java
index 8078e9e..8e01b6b 100644
--- a/src/com/android/wallpaper/grass/GrassRS.java
+++ b/src/com/android/wallpaper/grass/GrassRS.java
@@ -90,6 +90,7 @@
     private int mTriangles;
     private final float[] mFloatData5 = new float[5];
     private WorldState mWorldState;
+    private float[] mBladesData;
 
     GrassRS(int width, int height) {
         super(width, height);
@@ -103,9 +104,13 @@
         mWorldState.height = height;
         mState.data(mWorldState);
 
-        mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
+        final float[] blades = mBladesData;
+        for (int i = 0; i < blades.length; i+= BLADE_STRUCT_FIELDS_COUNT) {
+            updateBlade(blades, i);
+        }
+        mBlades.data(blades);
 
-        // TODO: REPOSITION BLADES
+        mPvOrthoAlloc.setupOrthoWindow(width, height);        
     }
 
     @Override
@@ -166,7 +171,9 @@
     private void createBlades() {
         int triangles = 0;
 
-        final float[] blades = new float[BLADES_COUNT * BLADE_STRUCT_FIELDS_COUNT];
+        mBladesData = new float[BLADES_COUNT * BLADE_STRUCT_FIELDS_COUNT];
+
+        final float[] blades = mBladesData;
         for (int i = 0; i < blades.length; i+= BLADE_STRUCT_FIELDS_COUNT) {
             triangles += createBlade(blades, i);
         }
@@ -234,6 +241,13 @@
         }
     }
 
+    private void updateBlade(float[] blades, int index) {
+        final int xpos = random(-mWidth, mWidth);
+        blades[index + BLADE_STRUCT_XPOS] = xpos;
+        blades[index + BLADE_STRUCT_TURBULENCEX] = xpos * 0.006f;
+        blades[index + BLADE_STRUCT_YPOS] = mHeight;
+    }
+
     private int createBlade(float[] blades, int index) {
         final float size = random(4.0f) + 4.0f;
         final int xpos = random(-mWidth, mWidth);