diff options
author | Romain Guy <romainguy@android.com> | 2009-07-31 15:33:59 -0700 |
---|---|---|
committer | Romain Guy <romainguy@android.com> | 2009-07-31 21:36:18 -0700 |
commit | a32d100b34d048cf0c765d8f31d87b81ab88d1eb (patch) | |
tree | d0393450dfb3abcbe2aa44aca5b3e289360b19b2 /libs | |
parent | 4aa38681c6daa932fc1642dbf27dc2f938771147 (diff) | |
download | frameworks_base-a32d100b34d048cf0c765d8f31d87b81ab88d1eb.zip frameworks_base-a32d100b34d048cf0c765d8f31d87b81ab88d1eb.tar.gz frameworks_base-a32d100b34d048cf0c765d8f31d87b81ab88d1eb.tar.bz2 |
Add new utility methods to rsScriptC_Lib, android.util.MathUtil and android.graphics.Color.
Fixes RS compilation.
Diffstat (limited to 'libs')
-rw-r--r-- | libs/rs/java/Grass/res/raw/grass.c | 132 | ||||
-rw-r--r-- | libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java | 81 | ||||
-rw-r--r-- | libs/rs/java/Grass/src/com/android/grass/rs/GrassView.java | 2 | ||||
-rw-r--r-- | libs/rs/rsScriptC_Lib.cpp | 57 |
4 files changed, 241 insertions, 31 deletions
diff --git a/libs/rs/java/Grass/res/raw/grass.c b/libs/rs/java/Grass/res/raw/grass.c index b1b89f0..3cedaba 100644 --- a/libs/rs/java/Grass/res/raw/grass.c +++ b/libs/rs/java/Grass/res/raw/grass.c @@ -1,21 +1,19 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - +// Copyright (C) 2009 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #pragma version(1) -#pragma stateVertex(default) +#pragma stateVertex(PVBackground) #pragma stateFragment(PFBackground) #pragma stateFragmentStore(PFSBackground) @@ -23,7 +21,8 @@ #define WVGA_PORTRAIT_HEIGHT 762.0f #define RSID_STATE 0 -#define RSID_FRAMECOUNT 0 +#define RSID_FRAME_COUNT 0 +#define RSID_BLADES_COUNT 1 #define RSID_SKY_TEXTURES 1 #define RSID_SKY_TEXTURE_NIGHT 0 @@ -31,6 +30,21 @@ #define RSID_SKY_TEXTURE_NOON 2 #define RSID_SKY_TEXTURE_SUNSET 3 +#define RSID_BLADES 2 +#define BLADE_STRUCT_FIELDS_COUNT 12 +#define BLADE_STRUCT_DEGREE 0 +#define BLADE_STRUCT_SIZE 1 +#define BLADE_STRUCT_XPOS 2 +#define BLADE_STRUCT_YPOS 3 +#define BLADE_STRUCT_OFFSET 4 +#define BLADE_STRUCT_SCALE 5 +#define BLADE_STRUCT_LENGTHX 6 +#define BLADE_STRUCT_LENGTHY 7 +#define BLADE_STRUCT_HARDNESS 8 +#define BLADE_STRUCT_H 9 +#define BLADE_STRUCT_S 10 +#define BLADE_STRUCT_B 11 + #define MIDNIGHT 0.0f #define MORNING 0.375f #define AFTERNOON 0.6f @@ -38,6 +52,8 @@ #define SECONDS_IN_DAY 24.0f * 3600.0f +#define PI 3.1415926f + #define REAL_TIME 0 float time(int frameCount) { @@ -76,8 +92,78 @@ void drawSunset() { drawRect(0.0f, 0.0f, WVGA_PORTRAIT_WIDTH, WVGA_PORTRAIT_HEIGHT, 0.0f); } +void drawBlade(int index, float now) { + float h = loadF(RSID_BLADES, index + BLADE_STRUCT_H); + float s = loadF(RSID_BLADES, index + BLADE_STRUCT_S); + float b = loadF(RSID_BLADES, index + BLADE_STRUCT_B); + + float newB = 1.0f; + if (now >= MIDNIGHT && now < MORNING) { + newB = now / MORNING; + } + + if (now >= AFTERNOON && now < DUSK) { + newB = 1.0f - ((now - AFTERNOON) / (DUSK - AFTERNOON)); + } + + if (now >= DUSK) { + newB = 0.0f; + } + + hsb(h, s, lerpf(0, b, newB), 1.0f); + + float scale = loadF(RSID_BLADES, index + BLADE_STRUCT_SCALE); + float degree = loadF(RSID_BLADES, index + BLADE_STRUCT_DEGREE); + float hardness = loadF(RSID_BLADES, index + BLADE_STRUCT_HARDNESS); + + float targetDegree = 0.0f; // TODO Compute + degree += (targetDegree - degree) * 0.3f; + + float xpos = loadF(RSID_BLADES, index + BLADE_STRUCT_XPOS); + float ypos = loadF(RSID_BLADES, index + BLADE_STRUCT_YPOS); + + float lengthX = loadF(RSID_BLADES, index + BLADE_STRUCT_LENGTHX); + float lengthY = loadF(RSID_BLADES, index + BLADE_STRUCT_LENGTHY); + + float angle = PI / 2.0f; + + float currentX = xpos; + float currentY = ypos; + + int size = loadF(RSID_BLADES, index + BLADE_STRUCT_SIZE); + int i = size; + + for ( ; i > 0; i--) { + float nextX = currentX - cosf(angle) * size * lengthX; + float nextY = currentY - sinf(angle) * size * lengthY; + angle += degree * hardness; + + drawQuad(nextX + (i - 1) * scale, nextY, 0.0f, + nextX - (i - 1) * scale, nextY, 0.0f, + currentX - i * scale, currentY + 0.7f, 0.0f, + currentX + i * scale, currentY + 0.7f, 0.0f); + + currentX = nextX; + currentY = nextY; + } + + storeF(RSID_BLADES, index + BLADE_STRUCT_DEGREE, degree); +} + +void drawBlades(float now) { + bindTexture(NAMED_PFBackground, 0, 0); + + int bladesCount = loadI32(RSID_STATE, RSID_BLADES_COUNT); + int count = bladesCount * BLADE_STRUCT_FIELDS_COUNT; + + int i = 0; + for ( ; i < count; i += BLADE_STRUCT_FIELDS_COUNT) { + drawBlade(i, now); + } +} + int main(int launchID) { - int frameCount = loadI32(RSID_STATE, RSID_FRAMECOUNT); + int frameCount = loadI32(RSID_STATE, RSID_FRAME_COUNT); float now = time(frameCount); alpha(1.0f); @@ -100,13 +186,15 @@ int main(int launchID) { } if (now >= DUSK) { - drawSunset(); - alpha(norm(now, DUSK, 1.0f)); drawNight(); + alpha(1.0f - norm(now, DUSK, 1.0f)); + drawSunset(); } + + drawBlades(now); frameCount++; - storeI32(RSID_STATE, RSID_FRAMECOUNT, frameCount); + storeI32(RSID_STATE, RSID_FRAME_COUNT, frameCount); return 1; } diff --git a/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java b/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java index 70cb012..435b5ce 100644 --- a/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java +++ b/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java @@ -23,20 +23,45 @@ import static android.renderscript.RenderScript.EnvMode.*; import static android.renderscript.RenderScript.DepthFunc.*; import static android.renderscript.RenderScript.BlendSrcFunc; import static android.renderscript.RenderScript.BlendDstFunc; - import android.renderscript.RenderScript; import android.renderscript.Element; import android.renderscript.Allocation; +import android.renderscript.ProgramVertexAlloc; +import static android.renderscript.Element.*; + +import static android.util.MathUtils.*; import java.util.TimeZone; class GrassRS { private static final int RSID_STATE = 0; + private static final int RSID_STATE_FRAMECOUNT = 0; + private static final int RSID_STATE_BLADES_COUNT = 1; + private static final int RSID_SKY_TEXTURES = 1; private static final int SKY_TEXTURES_COUNT = 4; - + + private static final int RSID_BLADES = 2; + private static final int BLADES_COUNT = 100; + private static final int BLADE_STRUCT_FIELDS_COUNT = 12; + private static final int BLADE_STRUCT_DEGREE = 0; + private static final int BLADE_STRUCT_SIZE = 1; + private static final int BLADE_STRUCT_XPOS = 2; + private static final int BLADE_STRUCT_YPOS = 3; + private static final int BLADE_STRUCT_OFFSET = 4; + private static final int BLADE_STRUCT_SCALE = 5; + private static final int BLADE_STRUCT_LENGTHX = 6; + private static final int BLADE_STRUCT_LENGTHY = 7; + private static final int BLADE_STRUCT_HARDNESS = 8; + private static final int BLADE_STRUCT_H = 9; + private static final int BLADE_STRUCT_S = 10; + private static final int BLADE_STRUCT_B = 11; + private Resources mResources; private RenderScript mRS; + + private final int mWidth; + private final int mHeight; @SuppressWarnings({"FieldCanBeLocal"}) private RenderScript.Script mScript; @@ -46,6 +71,10 @@ class GrassRS { private RenderScript.ProgramFragment mPfBackground; @SuppressWarnings({"FieldCanBeLocal"}) private RenderScript.ProgramFragmentStore mPfsBackground; + @SuppressWarnings({"FieldCanBeLocal"}) + private RenderScript.ProgramVertex mPvBackground; + @SuppressWarnings({"FieldCanBeLocal"}) + private ProgramVertexAlloc mPvOrthoAlloc; @SuppressWarnings({"FieldCanBeLocal"}) private Allocation mSkyTexturesIDs; @@ -55,8 +84,12 @@ class GrassRS { private int[] mSkyBufferIDs; @SuppressWarnings({"FieldCanBeLocal"}) private Allocation mState; + @SuppressWarnings({"FieldCanBeLocal"}) + private Allocation mBlades; - public GrassRS() { + public GrassRS(int width, int height) { + mWidth = width; + mHeight = height; } public void init(RenderScript rs, Resources res) { @@ -82,20 +115,46 @@ class GrassRS { loadSkyTextures(); mScript.bindAllocation(mState, RSID_STATE); mScript.bindAllocation(mSkyTexturesIDs, RSID_SKY_TEXTURES); + mScript.bindAllocation(mBlades, RSID_BLADES); mRS.contextBindRootScript(mScript); } private void createScriptStructures() { - mState = Allocation.createSized(mRS, Element.USER_I32, 1); - mState.data(new int[1]); + final int[] data = new int[2]; + mState = Allocation.createSized(mRS, USER_I32, data.length); + data[RSID_STATE_FRAMECOUNT] = 0; + data[RSID_STATE_BLADES_COUNT] = BLADES_COUNT; + mState.data(data); + + final float[] blades = new float[BLADES_COUNT * BLADE_STRUCT_FIELDS_COUNT]; + mBlades = Allocation.createSized(mRS, USER_FLOAT, blades.length); + for (int i = 0; i < blades.length; i+= BLADE_STRUCT_FIELDS_COUNT) { + createBlade(blades, i); + } + mBlades.data(blades); + } + + private void createBlade(float[] blades, int index) { + //noinspection PointlessArithmeticExpression + blades[index + BLADE_STRUCT_DEGREE] = 0.0f; + blades[index + BLADE_STRUCT_SIZE] = random(4.0f) + 4.0f; + blades[index + BLADE_STRUCT_XPOS] = random(mWidth); + blades[index + BLADE_STRUCT_YPOS] = mHeight; + blades[index + BLADE_STRUCT_OFFSET] = random(0.2f) - 0.1f; + blades[index + BLADE_STRUCT_SCALE] = random(0.6f) + 0.2f; + blades[index + BLADE_STRUCT_LENGTHX] = random(4.5f) + 3.0f; + blades[index + BLADE_STRUCT_LENGTHY] = random(5.5f) + 2.0f; + blades[index + BLADE_STRUCT_HARDNESS] = random(1.0f) + 0.2f; + blades[index + BLADE_STRUCT_H] = (51.0f + random(5.0f)) / 255.0f; + blades[index + BLADE_STRUCT_S] = (200.0f + random(55.0f)) / 255.0f; + blades[index + BLADE_STRUCT_B] = (90.0f + random(165.0f)) / 255.0f; } private void loadSkyTextures() { mSkyBufferIDs = new int[SKY_TEXTURES_COUNT]; mSkyTextures = new Allocation[SKY_TEXTURES_COUNT]; - mSkyTexturesIDs = Allocation.createSized( - mRS, Element.USER_FLOAT, SKY_TEXTURES_COUNT); + mSkyTexturesIDs = Allocation.createSized(mRS, USER_FLOAT, SKY_TEXTURES_COUNT); final Allocation[] textures = mSkyTextures; textures[0] = loadTexture(R.drawable.night, "night"); @@ -149,5 +208,13 @@ class GrassRS { } private void createProgramVertex() { + mPvOrthoAlloc = new ProgramVertexAlloc(mRS); + mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight); + + mRS.programVertexBegin(null, null); + mRS.programVertexSetTextureMatrixEnable(true); + mPvBackground = mRS.programVertexCreate(); + mPvBackground.bindAllocation(0, mPvOrthoAlloc.mAlloc); + mPvBackground.setName("PVBackground"); } } diff --git a/libs/rs/java/Grass/src/com/android/grass/rs/GrassView.java b/libs/rs/java/Grass/src/com/android/grass/rs/GrassView.java index a641e1e..44d5b08 100644 --- a/libs/rs/java/Grass/src/com/android/grass/rs/GrassView.java +++ b/libs/rs/java/Grass/src/com/android/grass/rs/GrassView.java @@ -30,7 +30,7 @@ class GrassView extends RSSurfaceView { super.surfaceChanged(holder, format, w, h); RenderScript RS = createRenderScript(); - GrassRS render = new GrassRS(); + GrassRS render = new GrassRS(w, h); render.init(RS, getResources()); } } diff --git a/libs/rs/rsScriptC_Lib.cpp b/libs/rs/rsScriptC_Lib.cpp index 21c9753..e4979ea 100644 --- a/libs/rs/rsScriptC_Lib.cpp +++ b/libs/rs/rsScriptC_Lib.cpp @@ -566,6 +566,59 @@ static void SC_color(float r, float g, float b, float a) glColor4f(r, g, b, a); } +static void SC_hsb(float h, float s, float b, float a) +{ + float red = 0.0f; + float green = 0.0f; + float blue = 0.0f; + + float x = h; + float y = s; + float z = b; + + float hf = (x - (int) x) * 6.0f; + int ihf = (int) hf; + float f = hf - ihf; + float pv = z * (1.0f - y); + float qv = z * (1.0f - y * f); + float tv = z * (1.0f - y * (1.0f - f)); + + switch (ihf) { + case 0: // Red is the dominant color + red = z; + green = tv; + blue = pv; + break; + case 1: // Green is the dominant color + red = qv; + green = z; + blue = pv; + break; + case 2: + red = pv; + green = z; + blue = tv; + break; + case 3: // Blue is the dominant color + red = pv; + green = qv; + blue = z; + break; + case 4: + red = tv; + green = pv; + blue = z; + break; + case 5: // Red is the dominant color + red = z; + green = pv; + blue = qv; + break; + } + + glColor4f(red, green, blue, a); +} + /* extern "C" void materialDiffuse(float r, float g, float b, float a) { @@ -650,7 +703,7 @@ ScriptCState::SymbolTable_t ScriptCState::gSyms[] = { { "atanf", (void *)&atanf, "float", "(float)" }, { "atan2f", (void *)&atan2f, - "float", "(floatm float)" }, + "float", "(float, float)" }, { "fabsf", (void *)&fabsf, "float", "(float)" }, { "randf", (void *)&SC_randf, @@ -772,6 +825,8 @@ ScriptCState::SymbolTable_t ScriptCState::gSyms[] = { "void", "(float, float, float, float)" }, { "color", (void *)&SC_color, "void", "(float, float, float, float)" }, + { "hsb", (void *)&SC_hsb, + "void", "(float, float, float, float)" }, { "uploadToTexture", (void *)&SC_uploadToTexture, "void", "(int, int)" }, |