Draw untextured water mesh

This commit is contained in:
Romain Guy
2009-08-04 21:16:17 -07:00
parent 53c66b490a
commit 31858c2b84
5 changed files with 67 additions and 16 deletions

View File

@@ -0,0 +1,11 @@
#pragma version(1)
#pragma stateVertex(PVBackground)
#pragma stateFragment(PFBackground)
#pragma stateFragmentStore(PSBackground)
int main(int index) {
color(1.0f, 0.0f, 0.0f, 1.0f);
drawTriangleMesh(NAMED_mesh);
return 1;
}

View File

@@ -24,7 +24,7 @@ import android.renderscript.ProgramStore;
import android.renderscript.ProgramVertex;
import android.renderscript.Allocation;
import android.renderscript.Sampler;
import android.renderscript.ProgramVertex;
import android.renderscript.Element;
import static android.renderscript.Sampler.Value.LINEAR;
import static android.renderscript.Sampler.Value.CLAMP;
import static android.renderscript.ProgramStore.DepthFunc.*;
@@ -38,7 +38,7 @@ import android.graphics.Bitmap;
import java.util.TimeZone;
class FallRS {
private static final int MESH_RESOLUTION = 64;
private static final int MESH_RESOLUTION = 32;
private static final int RSID_STATE = 0;
private static final int RSID_STATE_FRAMECOUNT = 0;
@@ -67,6 +67,7 @@ class FallRS {
private int[] mTextureBufferIDs;
private Allocation mState;
private RenderScript.TriangleMesh mMesh;
public FallRS(int width, int height) {
mWidth = width;
@@ -94,6 +95,7 @@ class FallRS {
}
mState.destroy();
mTextureBufferIDs = null;
mMesh.destroy();
}
@Override
@@ -127,7 +129,44 @@ class FallRS {
}
private void createMesh() {
final RenderScript rs = mRS;
rs.triangleMeshBegin(Element.XYZ_F32, Element.INDEX_16);
int wResolution;
int hResolution;
final int width = mWidth;
final int height = mHeight;
if (width < height) {
wResolution = MESH_RESOLUTION;
hResolution = (int) (MESH_RESOLUTION * height / (float) width);
} else {
wResolution = (int) (MESH_RESOLUTION * width / (float) height);
hResolution = MESH_RESOLUTION;
}
final float quadWidth = width / (float) wResolution;
final float quadHeight = height / (float) hResolution;
for (int y = 0; y <= hResolution; y++) {
final float yOffset = y * quadHeight;
for (int x = 0; x <= wResolution; x++) {
rs.triangleMeshAddVertex_XYZ(x * quadWidth, yOffset, 0.0f);
}
}
for (int y = 0; y < hResolution; y++) {
for (int x = 0; x < wResolution; x++) {
final int index = y * (wResolution + 1) + x;
final int iWR1 = index + wResolution + 1;
rs.triangleMeshAddTriangle(index, index + 1, iWR1);
rs.triangleMeshAddTriangle(index + 1, iWR1, iWR1 + 1);
}
}
mMesh = rs.triangleMeshCreate();
mMesh.setName("mesh");
}
private void createScriptStructures() {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

After

Width:  |  Height:  |  Size: 75 KiB

View File

@@ -32,6 +32,8 @@ import static android.util.MathUtils.*;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.renderscript.ScriptC;
import android.renderscript.Type;
import android.renderscript.Dimension;
import static android.renderscript.Sampler.Value.*;
import java.util.TimeZone;
@@ -64,7 +66,6 @@ class GrassRS {
private Resources mResources;
private RenderScript mRS;
private final BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
private final int mWidth;
private final int mHeight;
@@ -89,8 +90,6 @@ class GrassRS {
public GrassRS(int width, int height) {
mWidth = width;
mHeight = height;
mBitmapOptions.inScaled = false;
mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
}
public void init(RenderScript rs, Resources res) {
@@ -190,7 +189,7 @@ class GrassRS {
textures[1] = loadTexture(R.drawable.sunrise, "sunrise");
textures[2] = loadTexture(R.drawable.sky, "sky");
textures[3] = loadTexture(R.drawable.sunset, "sunset");
textures[4] = loadTextureARGB(R.drawable.aa, "aa");
textures[4] = generateTextureAlpha(4, 1, new int[] { 0x00FFFF00 }, "aa");
final int[] bufferIds = mTextureBufferIDs;
final int count = textures.length;
@@ -204,18 +203,20 @@ class GrassRS {
mTexturesIDs.data(bufferIds);
}
private Allocation loadTexture(int id, String name) {
final Allocation allocation = Allocation.createFromBitmapResource(mRS, mResources,
id, RGB_565, false);
private Allocation generateTextureAlpha(int width, int height, int[] data, String name) {
final Type.Builder builder = new Type.Builder(mRS, A_8);
builder.add(Dimension.X, width);
builder.add(Dimension.Y, height);
final Allocation allocation = Allocation.createTyped(mRS, builder.create());
allocation.data(data);
allocation.setName(name);
return allocation;
}
private Allocation loadTextureARGB(int id, String name) {
// Forces ARGB 32 bits, because pngcrush sometimes optimize our PNGs to
// indexed pictures, which are not well supported
final Bitmap b = BitmapFactory.decodeResource(mResources, id, mBitmapOptions);
final Allocation allocation = Allocation.createFromBitmap(mRS, b, RGBA_8888, false);
private Allocation loadTexture(int id, String name) {
final Allocation allocation = Allocation.createFromBitmapResource(mRS, mResources,
id, RGB_565, false);
allocation.setName(name);
return allocation;
}
@@ -236,7 +237,7 @@ class GrassRS {
mPfBackground.setName("PFBackground");
mPfBackground.bindSampler(mSampler, 0);
b.setTexEnvMode(MODULATE, 0);
b.setTexEnvMode(REPLACE, 0);
mPfGrass = b.create();
mPfGrass.setName("PFGrass");
mPfGrass.bindSampler(mSampler, 0);
@@ -253,7 +254,7 @@ class GrassRS {
mPfsBackground = b.create();
mPfsBackground.setName("PFSBackground");
b.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
b.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
mPfsGrass = b.create();
mPfsGrass.setName("PFSGrass");
}