diff options
Diffstat (limited to 'java/Fountain/src')
3 files changed, 147 insertions, 57 deletions
diff --git a/java/Fountain/src/com/android/fountain/FountainRS.java b/java/Fountain/src/com/android/fountain/FountainRS.java index 93565796..d193134c 100644 --- a/java/Fountain/src/com/android/fountain/FountainRS.java +++ b/java/Fountain/src/com/android/fountain/FountainRS.java @@ -24,16 +24,6 @@ import android.util.Log; public class FountainRS { public static final int PART_COUNT = 20000; - static class SomeData { - public int x; - public int y; - public int rate; - public int count; - public float r; - public float g; - public float b; - } - public FountainRS() { } @@ -43,21 +33,28 @@ public class FountainRS { initRS(); } + Float4 tmpColor = new Float4(); + boolean holdingColor = false; public void newTouchPosition(int x, int y, int rate) { - if (mSD.rate == 0) { - mSD.r = ((x & 0x1) != 0) ? 0.f : 1.f; - mSD.g = ((x & 0x2) != 0) ? 0.f : 1.f; - mSD.b = ((x & 0x4) != 0) ? 0.f : 1.f; - if ((mSD.r + mSD.g + mSD.b) < 0.9f) { - mSD.r = 0.8f; - mSD.g = 0.5f; - mSD.b = 1.f; + if (rate > 0) { + if (true/*!holdingColor*/) { + tmpColor.x = ((x & 0x1) != 0) ? 0.f : 1.f; + tmpColor.y = ((x & 0x2) != 0) ? 0.f : 1.f; + tmpColor.z = ((x & 0x4) != 0) ? 0.f : 1.f; + if ((tmpColor.x + tmpColor.y + tmpColor.z) < 0.9f) { + tmpColor.x = 0.8f; + tmpColor.y = 0.5f; + tmpColor.z = 1.0f; + } + android.util.Log.e("rs", "set color " + tmpColor.x + ", " + tmpColor.y + ", " + tmpColor.z); + mScript.set_partColor(tmpColor); } + mScript.invokable_addParticles(rate, x, y); + holdingColor = true; + } else { + holdingColor = false; } - mSD.rate = rate; - mSD.x = x; - mSD.y = y; - mIntAlloc.data(mSD); + } @@ -65,49 +62,26 @@ public class FountainRS { private Resources mRes; + private ScriptField_Point mPoints; + private ScriptC_Fountain mScript; private RenderScriptGL mRS; - private Allocation mIntAlloc; private SimpleMesh mSM; - private SomeData mSD; - private Type mSDType; private void initRS() { - mSD = new SomeData(); - mSDType = Type.createFromClass(mRS, SomeData.class, 1, "SomeData"); - mIntAlloc = Allocation.createTyped(mRS, mSDType); - mSD.count = PART_COUNT; - mIntAlloc.data(mSD); - - Element.Builder eb = new Element.Builder(mRS); - eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "delta"); - eb.add(Element.createAttrib(mRS, Element.DataType.FLOAT_32, Element.DataKind.POSITION, 2), "position"); - eb.add(Element.createAttrib(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.COLOR, 4), "color"); - Element primElement = eb.create(); - + mPoints = new ScriptField_Point(mRS, PART_COUNT); SimpleMesh.Builder smb = new SimpleMesh.Builder(mRS); - int vtxSlot = smb.addVertexType(primElement, PART_COUNT); + int vtxSlot = smb.addVertexType(mPoints.getType()); smb.setPrimitive(Primitive.POINT); mSM = smb.create(); - mSM.setName("PartMesh"); - - Allocation partAlloc = mSM.createVertexAllocation(vtxSlot); - partAlloc.setName("PartBuffer"); - mSM.bindVertexAllocation(partAlloc, 0); - - // All setup of named objects should be done by this point - // because we are about to compile the script. - ScriptC.Builder sb = new ScriptC.Builder(mRS); - sb.setScript(mRes, R.raw.fountain); - sb.setRoot(true); - sb.setType(mSDType, "Control", 0); - sb.setType(mSM.getVertexType(0), "point", 1); - Script script = sb.create(); - script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f); - - script.bindAllocation(mIntAlloc, 0); - script.bindAllocation(partAlloc, 1); - mRS.contextBindRootScript(script); + mSM.bindVertexAllocation(mPoints.getAllocation(), vtxSlot); + + mScript = new ScriptC_Fountain(mRS, mRes, true); + mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f); + mScript.set_partMesh(mSM); + mScript.set_partBuffer(mPoints.getAllocation()); + mScript.bind_point(mPoints); + mRS.contextBindRootScript(mScript); } } diff --git a/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java b/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java new file mode 100644 index 00000000..7cff390c --- /dev/null +++ b/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java @@ -0,0 +1,49 @@ + +package com.android.fountain; + +import android.content.res.Resources; +import android.renderscript.*; +import android.util.Log; + +public class ScriptC_Fountain + extends android.renderscript.ScriptC +{ + public ScriptC_Fountain(RenderScript rs, Resources resources, boolean isRoot) { + super(rs, resources, R.raw.fountain_bc, isRoot); + } + + public void set_partColor(Float4 v) { + FieldPacker fp = new FieldPacker(16); + fp.addF32(v); + setVar(0, fp); + } + public void set_partMesh(SimpleMesh v) { + setVar(1, v.getID()); + } + public void set_partBuffer(Allocation v) { + setVar(2, v.getID()); + } + + private ScriptField_Point mField_point; + public void bind_point(ScriptField_Point f) { + mField_point = f; + if (f == null) { + bindAllocation(null, 3); + } else { + bindAllocation(f.getAllocation(), 3); + } + } + public ScriptField_Point get_point() { + return mField_point; + } + + + public void invokable_addParticles(int count, int x, int y) { + FieldPacker fp = new FieldPacker(12); + fp.addI32(count); + fp.addI32(x); + fp.addI32(y); + invokeV(0, fp); + } +} + diff --git a/java/Fountain/src/com/android/fountain/ScriptField_Point.java b/java/Fountain/src/com/android/fountain/ScriptField_Point.java new file mode 100644 index 00000000..edd18d58 --- /dev/null +++ b/java/Fountain/src/com/android/fountain/ScriptField_Point.java @@ -0,0 +1,67 @@ + +package com.android.fountain; + +import android.content.res.Resources; +import android.renderscript.*; +import android.util.Log; + +public class ScriptField_Point + extends android.renderscript.Script.FieldBase +{ + + static public class Item { + Item() { + delta = new Float2(); + pos = new Float2(); + color = new Short4(); + } + + public static final int sizeof = (5*4); + Float2 delta; + Float2 pos; + Short4 color; + } + private Item mItemArray[]; + + + public ScriptField_Point(RenderScript rs, int count) { + // Allocate a pack/unpack buffer + mIOBuffer = new FieldPacker(Item.sizeof * count); + mItemArray = new Item[count]; + + Element.Builder eb = new Element.Builder(rs); + eb.add(Element.createVector(rs, Element.DataType.FLOAT_32, 2), "delta"); + eb.add(Element.createAttrib(rs, Element.DataType.FLOAT_32, Element.DataKind.POSITION, 2), "pos"); + eb.add(Element.createAttrib(rs, Element.DataType.UNSIGNED_8, Element.DataKind.COLOR, 4), "color"); + mElement = eb.create(); + + init(rs, count); + } + + private void copyToArray(Item i, int index) { + mIOBuffer.reset(index * Item.sizeof); + mIOBuffer.addF32(i.delta); + mIOBuffer.addF32(i.pos); + mIOBuffer.addU8(i.color); + } + + public void set(Item i, int index, boolean copyNow) { + mItemArray[index] = i; + if (copyNow) { + copyToArray(i, index); + mAllocation.subData1D(index * Item.sizeof, Item.sizeof, mIOBuffer.getData()); + } + } + + public void copyAll() { + for (int ct=0; ct < mItemArray.length; ct++) { + copyToArray(mItemArray[ct], ct); + } + mAllocation.data(mIOBuffer.getData()); + } + + + private FieldPacker mIOBuffer; + + +} |