Merge change 21926 into eclair
* changes: Move the sky
This commit is contained in:
@@ -18,10 +18,6 @@ package android.renderscript;
|
|||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
import android.renderscript.Element;
|
|
||||||
import android.util.Config;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @hide
|
* @hide
|
||||||
*
|
*
|
||||||
@@ -113,9 +109,7 @@ public class Type extends BaseObj {
|
|||||||
public void add(Dimension d, int value) {
|
public void add(Dimension d, int value) {
|
||||||
if(mEntries.length >= mEntryCount) {
|
if(mEntries.length >= mEntryCount) {
|
||||||
Entry[] en = new Entry[mEntryCount + 8];
|
Entry[] en = new Entry[mEntryCount + 8];
|
||||||
for(int ct=0; ct < mEntries.length; ct++) {
|
System.arraycopy(mEntries, 0, en, 0, mEntries.length);
|
||||||
en[ct] = mEntries[ct];
|
|
||||||
}
|
|
||||||
mEntries = en;
|
mEntries = en;
|
||||||
}
|
}
|
||||||
mEntries[mEntryCount] = new Entry();
|
mEntries[mEntryCount] = new Entry();
|
||||||
|
|||||||
BIN
libs/rs/java/Fall/res/drawable-hdpi/sky.jpg
Executable file → Normal file
BIN
libs/rs/java/Fall/res/drawable-hdpi/sky.jpg
Executable file → Normal file
Binary file not shown.
|
Before Width: | Height: | Size: 89 KiB After Width: | Height: | Size: 80 KiB |
@@ -18,25 +18,10 @@
|
|||||||
#pragma stateFragmentStore(PFSBackground)
|
#pragma stateFragmentStore(PFSBackground)
|
||||||
|
|
||||||
#define RSID_STATE 0
|
#define RSID_STATE 0
|
||||||
#define RSID_FRAME_COUNT 0
|
|
||||||
#define RSID_WIDTH 1
|
|
||||||
#define RSID_HEIGHT 2
|
|
||||||
#define RSID_MESH_WIDTH 3
|
|
||||||
#define RSID_MESH_HEIGHT 4
|
|
||||||
#define RSID_RIPPLE_MAP_SIZE 5
|
|
||||||
#define RSID_RIPPLE_INDEX 6
|
|
||||||
#define RSID_DROP_X 7
|
|
||||||
#define RSID_DROP_Y 8
|
|
||||||
#define RSID_RUNNING 9
|
|
||||||
#define RSID_LEAVES_COUNT 10
|
|
||||||
|
|
||||||
#define RSID_RIPPLE_MAP 1
|
#define RSID_RIPPLE_MAP 1
|
||||||
#define RSID_REFRACTION_MAP 2
|
#define RSID_REFRACTION_MAP 2
|
||||||
#define RSID_LEAVES 3
|
#define RSID_LEAVES 3
|
||||||
|
#define RSID_DROP 4
|
||||||
#define RSID_GL_STATE 4
|
|
||||||
#define RSID_GL_WIDTH 0
|
|
||||||
#define RSID_GL_HEIGHT 1
|
|
||||||
|
|
||||||
#define LEAF_STRUCT_FIELDS_COUNT 11
|
#define LEAF_STRUCT_FIELDS_COUNT 11
|
||||||
#define LEAF_STRUCT_X 0
|
#define LEAF_STRUCT_X 0
|
||||||
@@ -67,8 +52,8 @@ int offset(int x, int y, int width) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void dropWithStrength(int x, int y, int r, int s) {
|
void dropWithStrength(int x, int y, int r, int s) {
|
||||||
int width = loadI32(RSID_STATE, RSID_MESH_WIDTH);
|
int width = State_meshWidth;
|
||||||
int height = loadI32(RSID_STATE, RSID_MESH_HEIGHT);
|
int height = State_meshHeight;
|
||||||
|
|
||||||
if (x < r) x = r;
|
if (x < r) x = r;
|
||||||
if (y < r) y = r;
|
if (y < r) y = r;
|
||||||
@@ -77,8 +62,8 @@ void dropWithStrength(int x, int y, int r, int s) {
|
|||||||
|
|
||||||
x = width - x;
|
x = width - x;
|
||||||
|
|
||||||
int rippleMapSize = loadI32(RSID_STATE, RSID_RIPPLE_MAP_SIZE);
|
int rippleMapSize = State_rippleMapSize;
|
||||||
int index = loadI32(RSID_STATE, RSID_RIPPLE_INDEX);
|
int index = State_rippleIndex;
|
||||||
int origin = offset(0, 0, width);
|
int origin = offset(0, 0, width);
|
||||||
|
|
||||||
int* current = loadArrayI32(RSID_RIPPLE_MAP, index * rippleMapSize + origin);
|
int* current = loadArrayI32(RSID_RIPPLE_MAP, index * rippleMapSize + origin);
|
||||||
@@ -108,16 +93,16 @@ void drop(int x, int y, int r) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void updateRipples() {
|
void updateRipples() {
|
||||||
int rippleMapSize = loadI32(RSID_STATE, RSID_RIPPLE_MAP_SIZE);
|
int rippleMapSize = State_rippleMapSize;
|
||||||
int width = loadI32(RSID_STATE, RSID_MESH_WIDTH);
|
int width = State_meshWidth;
|
||||||
int height = loadI32(RSID_STATE, RSID_MESH_HEIGHT);
|
int height = State_meshHeight;
|
||||||
int index = loadI32(RSID_STATE, RSID_RIPPLE_INDEX);
|
int index = State_rippleIndex;
|
||||||
int origin = offset(0, 0, width);
|
int origin = offset(0, 0, width);
|
||||||
|
|
||||||
int* current = loadArrayI32(RSID_RIPPLE_MAP, index * rippleMapSize + origin);
|
int* current = loadArrayI32(RSID_RIPPLE_MAP, index * rippleMapSize + origin);
|
||||||
int* next = loadArrayI32(RSID_RIPPLE_MAP, (1 - index) * rippleMapSize + origin);
|
int* next = loadArrayI32(RSID_RIPPLE_MAP, (1 - index) * rippleMapSize + origin);
|
||||||
|
|
||||||
storeI32(RSID_STATE, RSID_RIPPLE_INDEX, 1 - index);
|
storeI32(RSID_STATE, OFFSETOF_WorldState_rippleIndex, 1 - index);
|
||||||
|
|
||||||
int a = 1;
|
int a = 1;
|
||||||
int b = width + 2;
|
int b = width + 2;
|
||||||
@@ -152,17 +137,17 @@ int refraction(int d, int wave, int *map) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void generateRipples() {
|
void generateRipples() {
|
||||||
int rippleMapSize = loadI32(RSID_STATE, RSID_RIPPLE_MAP_SIZE);
|
int rippleMapSize = loadI32(RSID_STATE, OFFSETOF_WorldState_rippleMapSize);
|
||||||
int width = loadI32(RSID_STATE, RSID_MESH_WIDTH);
|
int width = State_meshWidth;
|
||||||
int height = loadI32(RSID_STATE, RSID_MESH_HEIGHT);
|
int height = State_meshHeight;
|
||||||
int index = loadI32(RSID_STATE, RSID_RIPPLE_INDEX);
|
int index = State_rippleIndex;
|
||||||
int origin = offset(0, 0, width);
|
int origin = offset(0, 0, width);
|
||||||
|
|
||||||
int b = width + 2;
|
int b = width + 2;
|
||||||
|
|
||||||
int* current = loadArrayI32(RSID_RIPPLE_MAP, index * rippleMapSize + origin);
|
int* current = loadArrayI32(RSID_RIPPLE_MAP, index * rippleMapSize + origin);
|
||||||
int *map = loadArrayI32(RSID_REFRACTION_MAP, 0);
|
int *map = loadArrayI32(RSID_REFRACTION_MAP, 0);
|
||||||
float *vertices = loadTriangleMeshVerticesF(NAMED_mesh);
|
float *vertices = loadTriangleMeshVerticesF(NAMED_WaterMesh);
|
||||||
|
|
||||||
int h = height - 1;
|
int h = height - 1;
|
||||||
while (h >= 0) {
|
while (h >= 0) {
|
||||||
@@ -305,7 +290,7 @@ float averageZ(float x1, float x2, float y1, float y2, float* vertices,
|
|||||||
return 55.0f * z / vertexCount;
|
return 55.0f * z / vertexCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawLeaf(int index, int frameCount, float* vertices, int meshWidth, int meshHeight,
|
void drawLeaf(int index, float* vertices, int meshWidth, int meshHeight,
|
||||||
float glWidth, float glHeight) {
|
float glWidth, float glHeight) {
|
||||||
|
|
||||||
float *leafStruct = loadArrayF(RSID_LEAVES, index);
|
float *leafStruct = loadArrayF(RSID_LEAVES, index);
|
||||||
@@ -368,9 +353,9 @@ void drawLeaf(int index, int frameCount, float* vertices, int meshWidth, int mes
|
|||||||
leafStruct[LEAF_STRUCT_SPIN] = spin;
|
leafStruct[LEAF_STRUCT_SPIN] = spin;
|
||||||
leafStruct[LEAF_STRUCT_RIPPLED] = 1.0f;
|
leafStruct[LEAF_STRUCT_RIPPLED] = 1.0f;
|
||||||
} else {
|
} else {
|
||||||
dropWithStrength(((x + glWidth / 2.0f) / glWidth) * meshWidth,
|
// dropWithStrength(((x + glWidth / 2.0f) / glWidth) * meshWidth,
|
||||||
meshHeight - ((y + glHeight / 2.0f) / glHeight) * meshHeight,
|
// meshHeight - ((y + glHeight / 2.0f) / glHeight) * meshHeight,
|
||||||
2, 5);
|
// 2, 5);
|
||||||
}
|
}
|
||||||
leafStruct[LEAF_STRUCT_X] = x + leafStruct[LEAF_STRUCT_DELTAX];
|
leafStruct[LEAF_STRUCT_X] = x + leafStruct[LEAF_STRUCT_DELTAX];
|
||||||
leafStruct[LEAF_STRUCT_Y] = y + leafStruct[LEAF_STRUCT_DELTAY];
|
leafStruct[LEAF_STRUCT_Y] = y + leafStruct[LEAF_STRUCT_DELTAY];
|
||||||
@@ -398,23 +383,24 @@ void drawLeaf(int index, int frameCount, float* vertices, int meshWidth, int mes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawLeaves(int frameCount) {
|
void drawLeaves() {
|
||||||
bindProgramFragment(NAMED_PFBackground);
|
bindProgramFragment(NAMED_PFBackground);
|
||||||
bindProgramFragmentStore(NAMED_PFSLeaf);
|
bindProgramFragmentStore(NAMED_PFSLeaf);
|
||||||
|
bindProgramVertex(NAMED_PVSky);
|
||||||
bindTexture(NAMED_PFBackground, 0, NAMED_TLeaves);
|
bindTexture(NAMED_PFBackground, 0, NAMED_TLeaves);
|
||||||
|
|
||||||
int leavesCount = loadI32(RSID_STATE, RSID_LEAVES_COUNT);
|
int leavesCount = State_leavesCount;
|
||||||
int count = leavesCount * LEAF_STRUCT_FIELDS_COUNT;
|
int count = leavesCount * LEAF_STRUCT_FIELDS_COUNT;
|
||||||
int width = loadI32(RSID_STATE, RSID_MESH_WIDTH);
|
int width = State_meshWidth;
|
||||||
int height = loadI32(RSID_STATE, RSID_MESH_HEIGHT);
|
int height = State_meshHeight;
|
||||||
float glWidth = loadF(RSID_GL_STATE, RSID_GL_WIDTH);
|
float glWidth = State_glWidth;
|
||||||
float glHeight = loadF(RSID_GL_STATE, RSID_GL_HEIGHT);
|
float glHeight = State_glHeight;
|
||||||
|
|
||||||
float *vertices = loadTriangleMeshVerticesF(NAMED_mesh);
|
float *vertices = loadTriangleMeshVerticesF(NAMED_WaterMesh);
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for ( ; i < count; i += LEAF_STRUCT_FIELDS_COUNT) {
|
for ( ; i < count; i += LEAF_STRUCT_FIELDS_COUNT) {
|
||||||
drawLeaf(i, frameCount, vertices, width, height, glWidth, glHeight);
|
drawLeaf(i, vertices, width, height, glWidth, glHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
float matrix[16];
|
float matrix[16];
|
||||||
@@ -422,47 +408,69 @@ void drawLeaves(int frameCount) {
|
|||||||
vpLoadModelMatrix(matrix);
|
vpLoadModelMatrix(matrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int index) {
|
void drawRiverbed() {
|
||||||
int frameCount = loadI32(RSID_STATE, RSID_FRAME_COUNT);
|
|
||||||
|
|
||||||
int dropX = loadI32(RSID_STATE, RSID_DROP_X);
|
|
||||||
if (dropX != -1) {
|
|
||||||
int dropY = loadI32(RSID_STATE, RSID_DROP_Y);
|
|
||||||
drop(dropX, dropY, DROP_RADIUS);
|
|
||||||
storeI32(RSID_STATE, RSID_DROP_X, -1);
|
|
||||||
storeI32(RSID_STATE, RSID_DROP_Y, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int isRunning = loadI32(RSID_STATE, RSID_RUNNING);
|
|
||||||
if (isRunning) {
|
|
||||||
updateRipples();
|
|
||||||
generateRipples();
|
|
||||||
updateTriangleMesh(NAMED_mesh);
|
|
||||||
}
|
|
||||||
|
|
||||||
bindTexture(NAMED_PFBackground, 0, NAMED_TRiverbed);
|
bindTexture(NAMED_PFBackground, 0, NAMED_TRiverbed);
|
||||||
drawTriangleMesh(NAMED_mesh);
|
|
||||||
|
|
||||||
color(1.0f, 1.0f, 1.0f, 0.7f);
|
drawTriangleMesh(NAMED_WaterMesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawSky() {
|
||||||
|
color(1.0f, 1.0f, 1.0f, 0.8f);
|
||||||
|
|
||||||
bindProgramFragment(NAMED_PFSky);
|
bindProgramFragment(NAMED_PFSky);
|
||||||
bindProgramFragmentStore(NAMED_PFSLeaf);
|
bindProgramFragmentStore(NAMED_PFSLeaf);
|
||||||
bindTexture(NAMED_PFSky, 0, NAMED_TSky);
|
bindTexture(NAMED_PFSky, 0, NAMED_TSky);
|
||||||
drawTriangleMesh(NAMED_mesh);
|
|
||||||
|
|
||||||
|
float x = State_skyOffsetX + State_skySpeedX;
|
||||||
|
float y = State_skyOffsetY + State_skySpeedY;
|
||||||
|
|
||||||
|
if (x > 1.0f) x = 0.0f;
|
||||||
|
if (x < -1.0f) x = 0.0f;
|
||||||
|
if (y > 1.0f) y = 0.0f;
|
||||||
|
|
||||||
|
storeF(RSID_STATE, OFFSETOF_WorldState_skyOffsetX, x);
|
||||||
|
storeF(RSID_STATE, OFFSETOF_WorldState_skyOffsetY, y);
|
||||||
|
|
||||||
|
float matrix[16];
|
||||||
|
matrixLoadTranslate(matrix, x, y, 0.0f);
|
||||||
|
vpLoadTextureMatrix(matrix);
|
||||||
|
|
||||||
|
drawTriangleMesh(NAMED_WaterMesh);
|
||||||
|
|
||||||
|
matrixLoadIdentity(matrix);
|
||||||
|
vpLoadTextureMatrix(matrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawLighting() {
|
||||||
ambient(0.0f, 0.0f, 0.0f, 1.0f);
|
ambient(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
diffuse(0.0f, 0.0f, 0.0f, 1.0f);
|
diffuse(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
specular(0.44f, 0.44f, 0.44f, 1.0f);
|
specular(0.44f, 0.44f, 0.44f, 1.0f);
|
||||||
shininess(40.0f);
|
shininess(40.0f);
|
||||||
|
|
||||||
bindProgramFragmentStore(NAMED_PFSBackground);
|
bindProgramFragmentStore(NAMED_PFSBackground);
|
||||||
bindProgramFragment(NAMED_PFLighting);
|
bindProgramFragment(NAMED_PFLighting);
|
||||||
bindProgramVertex(NAMED_PVLight);
|
bindProgramVertex(NAMED_PVLight);
|
||||||
drawTriangleMesh(NAMED_mesh);
|
|
||||||
|
|
||||||
bindProgramVertex(NAMED_PVSky);
|
drawTriangleMesh(NAMED_WaterMesh);
|
||||||
drawLeaves(frameCount);
|
}
|
||||||
|
|
||||||
frameCount++;
|
int main(int index) {
|
||||||
storeI32(RSID_STATE, RSID_FRAME_COUNT, frameCount);
|
int dropX = Drop_dropX;
|
||||||
|
if (dropX != -1) {
|
||||||
|
int dropY = Drop_dropY;
|
||||||
|
drop(dropX, dropY, DROP_RADIUS);
|
||||||
|
storeI32(RSID_DROP, OFFSETOF_DropState_dropX, -1);
|
||||||
|
storeI32(RSID_DROP, OFFSETOF_DropState_dropY, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
updateRipples();
|
||||||
|
generateRipples();
|
||||||
|
updateTriangleMesh(NAMED_WaterMesh);
|
||||||
|
|
||||||
|
drawRiverbed();
|
||||||
|
drawSky();
|
||||||
|
drawLighting();
|
||||||
|
drawLeaves();
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import android.renderscript.Allocation;
|
|||||||
import android.renderscript.Sampler;
|
import android.renderscript.Sampler;
|
||||||
import android.renderscript.Element;
|
import android.renderscript.Element;
|
||||||
import android.renderscript.Light;
|
import android.renderscript.Light;
|
||||||
|
import android.renderscript.Type;
|
||||||
import static android.renderscript.Sampler.Value.LINEAR;
|
import static android.renderscript.Sampler.Value.LINEAR;
|
||||||
import static android.renderscript.Sampler.Value.WRAP;
|
import static android.renderscript.Sampler.Value.WRAP;
|
||||||
import static android.renderscript.ProgramStore.DepthFunc.*;
|
import static android.renderscript.ProgramStore.DepthFunc.*;
|
||||||
@@ -43,17 +44,6 @@ class FallRS {
|
|||||||
private static final int MESH_RESOLUTION = 48;
|
private static final int MESH_RESOLUTION = 48;
|
||||||
|
|
||||||
private static final int RSID_STATE = 0;
|
private static final int RSID_STATE = 0;
|
||||||
private static final int RSID_STATE_FRAMECOUNT = 0;
|
|
||||||
private static final int RSID_STATE_WIDTH = 1;
|
|
||||||
private static final int RSID_STATE_HEIGHT = 2;
|
|
||||||
private static final int RSID_STATE_MESH_WIDTH = 3;
|
|
||||||
private static final int RSID_STATE_MESH_HEIGHT = 4;
|
|
||||||
private static final int RSID_STATE_RIPPLE_MAP_SIZE = 5;
|
|
||||||
private static final int RSID_STATE_RIPPLE_INDEX = 6;
|
|
||||||
private static final int RSID_STATE_DROP_X = 7;
|
|
||||||
private static final int RSID_STATE_DROP_Y = 8;
|
|
||||||
private static final int RSID_STATE_RUNNING = 9;
|
|
||||||
private static final int RSID_STATE_LEAVES_COUNT = 10;
|
|
||||||
|
|
||||||
private static final int TEXTURES_COUNT = 3;
|
private static final int TEXTURES_COUNT = 3;
|
||||||
private static final int LEAVES_TEXTURES_COUNT = 4;
|
private static final int LEAVES_TEXTURES_COUNT = 4;
|
||||||
@@ -80,11 +70,7 @@ class FallRS {
|
|||||||
private static final int LEAF_STRUCT_DELTAX = 9;
|
private static final int LEAF_STRUCT_DELTAX = 9;
|
||||||
private static final int LEAF_STRUCT_DELTAY = 10;
|
private static final int LEAF_STRUCT_DELTAY = 10;
|
||||||
|
|
||||||
private static final int RSID_GL_STATE = 4;
|
private static final int RSID_DROP = 4;
|
||||||
private static final int RSID_STATE_GL_WIDTH = 0;
|
|
||||||
private static final int RSID_STATE_GL_HEIGHT = 1;
|
|
||||||
|
|
||||||
private boolean mIsRunning = true;
|
|
||||||
|
|
||||||
private Resources mResources;
|
private Resources mResources;
|
||||||
private RenderScript mRS;
|
private RenderScript mRS;
|
||||||
@@ -94,34 +80,35 @@ class FallRS {
|
|||||||
private final int mWidth;
|
private final int mWidth;
|
||||||
private final int mHeight;
|
private final int mHeight;
|
||||||
|
|
||||||
private ScriptC mScript;
|
@SuppressWarnings({"FieldCanBeLocal"})
|
||||||
private Sampler mSampler;
|
|
||||||
private ProgramFragment mPfBackground;
|
private ProgramFragment mPfBackground;
|
||||||
|
@SuppressWarnings({"FieldCanBeLocal"})
|
||||||
private ProgramFragment mPfLighting;
|
private ProgramFragment mPfLighting;
|
||||||
|
@SuppressWarnings({"FieldCanBeLocal"})
|
||||||
private ProgramFragment mPfSky;
|
private ProgramFragment mPfSky;
|
||||||
|
@SuppressWarnings({"FieldCanBeLocal"})
|
||||||
private ProgramStore mPfsBackground;
|
private ProgramStore mPfsBackground;
|
||||||
|
@SuppressWarnings({"FieldCanBeLocal"})
|
||||||
private ProgramStore mPfsLeaf;
|
private ProgramStore mPfsLeaf;
|
||||||
|
@SuppressWarnings({"FieldCanBeLocal"})
|
||||||
private ProgramVertex mPvLight;
|
private ProgramVertex mPvLight;
|
||||||
|
@SuppressWarnings({"FieldCanBeLocal"})
|
||||||
private ProgramVertex mPvSky;
|
private ProgramVertex mPvSky;
|
||||||
private ProgramVertex.MatrixAllocation mPvOrthoAlloc;
|
|
||||||
private Light mLight;
|
|
||||||
|
|
||||||
private Allocation[] mTextures;
|
|
||||||
|
|
||||||
private Allocation mState;
|
private Allocation mState;
|
||||||
private RenderScript.TriangleMesh mMesh;
|
private Allocation mDropState;
|
||||||
|
private DropState mDrop;
|
||||||
|
private Type mStateType;
|
||||||
|
private Type mDropType;
|
||||||
private int mMeshWidth;
|
private int mMeshWidth;
|
||||||
private int mMeshHeight;
|
|
||||||
|
|
||||||
|
private int mMeshHeight;
|
||||||
private Allocation mRippleMap;
|
private Allocation mRippleMap;
|
||||||
private Allocation mRefractionMap;
|
private Allocation mRefractionMap;
|
||||||
|
|
||||||
private Allocation mLeaves;
|
private Allocation mLeaves;
|
||||||
|
|
||||||
private Allocation mGlState;
|
|
||||||
private float mGlHeight;
|
private float mGlHeight;
|
||||||
|
|
||||||
private final int[] mIntData2 = new int[2];
|
|
||||||
|
|
||||||
public FallRS(int width, int height) {
|
public FallRS(int width, int height) {
|
||||||
mWidth = width;
|
mWidth = width;
|
||||||
mHeight = height;
|
mHeight = height;
|
||||||
@@ -135,38 +122,6 @@ class FallRS {
|
|||||||
initRS();
|
initRS();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void destroy() {
|
|
||||||
mScript.destroy();
|
|
||||||
mSampler.destroy();
|
|
||||||
mPfBackground.destroy();
|
|
||||||
mPfsBackground.destroy();
|
|
||||||
mPvLight.destroy();
|
|
||||||
mPvOrthoAlloc.mAlloc.destroy();
|
|
||||||
for (Allocation a : mTextures) {
|
|
||||||
a.destroy();
|
|
||||||
}
|
|
||||||
mState.destroy();
|
|
||||||
mMesh.destroy();
|
|
||||||
mLight.destroy();
|
|
||||||
mRippleMap.destroy();
|
|
||||||
mRefractionMap.destroy();
|
|
||||||
mPvSky.destroy();
|
|
||||||
mPfLighting.destroy();
|
|
||||||
mLeaves.destroy();
|
|
||||||
mPfsLeaf.destroy();
|
|
||||||
mPfSky.destroy();
|
|
||||||
mGlState.destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void finalize() throws Throwable {
|
|
||||||
try {
|
|
||||||
destroy();
|
|
||||||
} finally {
|
|
||||||
super.finalize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initRS() {
|
private void initRS() {
|
||||||
createProgramVertex();
|
createProgramVertex();
|
||||||
createProgramFragmentStore();
|
createProgramFragmentStore();
|
||||||
@@ -176,19 +131,22 @@ class FallRS {
|
|||||||
loadTextures();
|
loadTextures();
|
||||||
|
|
||||||
ScriptC.Builder sb = new ScriptC.Builder(mRS);
|
ScriptC.Builder sb = new ScriptC.Builder(mRS);
|
||||||
|
sb.setType(mStateType, "State", RSID_STATE);
|
||||||
|
sb.setType(mDropType, "Drop", RSID_DROP);
|
||||||
sb.setScript(mResources, R.raw.fall);
|
sb.setScript(mResources, R.raw.fall);
|
||||||
sb.setRoot(true);
|
sb.setRoot(true);
|
||||||
mScript = sb.create();
|
|
||||||
mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
||||||
mScript.setTimeZone(TimeZone.getDefault().getID());
|
|
||||||
|
|
||||||
mScript.bindAllocation(mState, RSID_STATE);
|
ScriptC script = sb.create();
|
||||||
mScript.bindAllocation(mRippleMap, RSID_RIPPLE_MAP);
|
script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
mScript.bindAllocation(mRefractionMap, RSID_REFRACTION_MAP);
|
script.setTimeZone(TimeZone.getDefault().getID());
|
||||||
mScript.bindAllocation(mLeaves, RSID_LEAVES);
|
|
||||||
mScript.bindAllocation(mGlState, RSID_GL_STATE);
|
|
||||||
|
|
||||||
mRS.contextBindRootScript(mScript);
|
script.bindAllocation(mState, RSID_STATE);
|
||||||
|
script.bindAllocation(mRippleMap, RSID_RIPPLE_MAP);
|
||||||
|
script.bindAllocation(mRefractionMap, RSID_REFRACTION_MAP);
|
||||||
|
script.bindAllocation(mLeaves, RSID_LEAVES);
|
||||||
|
script.bindAllocation(mDropState, RSID_DROP);
|
||||||
|
|
||||||
|
mRS.contextBindRootScript(script);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createMesh() {
|
private void createMesh() {
|
||||||
@@ -238,8 +196,8 @@ class FallRS {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mMesh = rs.triangleMeshCreate();
|
RenderScript.TriangleMesh mesh = rs.triangleMeshCreate();
|
||||||
mMesh.setName("mesh");
|
mesh.setName("WaterMesh");
|
||||||
|
|
||||||
mMeshWidth = wResolution + 1;
|
mMeshWidth = wResolution + 1;
|
||||||
mMeshHeight = hResolution + 1;
|
mMeshHeight = hResolution + 1;
|
||||||
@@ -249,7 +207,6 @@ class FallRS {
|
|||||||
final int rippleMapSize = (mMeshWidth + 2) * (mMeshHeight + 2);
|
final int rippleMapSize = (mMeshWidth + 2) * (mMeshHeight + 2);
|
||||||
|
|
||||||
createState(rippleMapSize);
|
createState(rippleMapSize);
|
||||||
createGlState();
|
|
||||||
createRippleMap(rippleMapSize);
|
createRippleMap(rippleMapSize);
|
||||||
createRefractionMap();
|
createRefractionMap();
|
||||||
createLeaves();
|
createLeaves();
|
||||||
@@ -281,29 +238,53 @@ class FallRS {
|
|||||||
mRippleMap.data(rippleMap);
|
mRippleMap.data(rippleMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createGlState() {
|
static class WorldState {
|
||||||
final float[] meshState = new float[2];
|
public int frameCount;
|
||||||
mGlState = Allocation.createSized(mRS, USER_FLOAT, meshState.length);
|
public int width;
|
||||||
meshState[RSID_STATE_GL_WIDTH] = 2.0f;
|
public int height;
|
||||||
meshState[RSID_STATE_GL_HEIGHT] = mGlHeight;
|
public int meshWidth;
|
||||||
mGlState.data(meshState);
|
public int meshHeight;
|
||||||
|
public int rippleMapSize;
|
||||||
|
public int rippleIndex;
|
||||||
|
public int leavesCount;
|
||||||
|
public float glWidth;
|
||||||
|
public float glHeight;
|
||||||
|
public float skyOffsetX;
|
||||||
|
public float skyOffsetY;
|
||||||
|
public float skySpeedX;
|
||||||
|
public float skySpeedY;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class DropState {
|
||||||
|
public int dropX;
|
||||||
|
public int dropY;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createState(int rippleMapSize) {
|
private void createState(int rippleMapSize) {
|
||||||
final int[] data = new int[11];
|
WorldState worldState = new WorldState();
|
||||||
mState = Allocation.createSized(mRS, USER_I32, data.length);
|
worldState.width = mWidth;
|
||||||
data[RSID_STATE_FRAMECOUNT] = 0;
|
worldState.height = mHeight;
|
||||||
data[RSID_STATE_WIDTH] = mWidth;
|
worldState.meshWidth = mMeshWidth;
|
||||||
data[RSID_STATE_HEIGHT] = mHeight;
|
worldState.meshHeight = mMeshHeight;
|
||||||
data[RSID_STATE_MESH_WIDTH] = mMeshWidth;
|
worldState.rippleMapSize = rippleMapSize;
|
||||||
data[RSID_STATE_MESH_HEIGHT] = mMeshHeight;
|
worldState.rippleIndex = 0;
|
||||||
data[RSID_STATE_RIPPLE_MAP_SIZE] = rippleMapSize;
|
worldState.leavesCount = LEAVES_COUNT;
|
||||||
data[RSID_STATE_RIPPLE_INDEX] = 0;
|
worldState.glWidth = 2.0f;
|
||||||
data[RSID_STATE_DROP_X] = -1;
|
worldState.glHeight = mGlHeight;
|
||||||
data[RSID_STATE_DROP_Y] = -1;
|
worldState.skySpeedX = random(-0.001f, 0.001f);
|
||||||
data[RSID_STATE_RUNNING] = 1;
|
worldState.skySpeedY = random(0.00008f, 0.0002f);
|
||||||
data[RSID_STATE_LEAVES_COUNT] = LEAVES_COUNT;
|
|
||||||
mState.data(data);
|
mStateType = Type.createFromClass(mRS, WorldState.class, 1, "WorldState");
|
||||||
|
mState = Allocation.createTyped(mRS, mStateType);
|
||||||
|
mState.data(worldState);
|
||||||
|
|
||||||
|
mDrop = new DropState();
|
||||||
|
mDrop.dropX = -1;
|
||||||
|
mDrop.dropY = -1;
|
||||||
|
|
||||||
|
mDropType = Type.createFromClass(mRS, DropState.class, 1, "DropState");
|
||||||
|
mDropState = Allocation.createTyped(mRS, mDropType);
|
||||||
|
mDropState.data(mDrop);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createLeaf(float[] leaves, int index) {
|
private void createLeaf(float[] leaves, int index) {
|
||||||
@@ -323,9 +304,7 @@ class FallRS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadTextures() {
|
private void loadTextures() {
|
||||||
mTextures = new Allocation[TEXTURES_COUNT];
|
final Allocation[] textures = new Allocation[TEXTURES_COUNT];
|
||||||
|
|
||||||
final Allocation[] textures = mTextures;
|
|
||||||
textures[RSID_TEXTURE_RIVERBED] = loadTexture(R.drawable.riverbed, "TRiverbed");
|
textures[RSID_TEXTURE_RIVERBED] = loadTexture(R.drawable.riverbed, "TRiverbed");
|
||||||
textures[RSID_TEXTURE_LEAVES] = loadTextureARGB(R.drawable.leaves, "TLeaves");
|
textures[RSID_TEXTURE_LEAVES] = loadTextureARGB(R.drawable.leaves, "TLeaves");
|
||||||
textures[RSID_TEXTURE_SKY] = loadTextureARGB(R.drawable.sky, "TSky");
|
textures[RSID_TEXTURE_SKY] = loadTextureARGB(R.drawable.sky, "TSky");
|
||||||
@@ -357,27 +336,27 @@ class FallRS {
|
|||||||
sampleBuilder.setMag(LINEAR);
|
sampleBuilder.setMag(LINEAR);
|
||||||
sampleBuilder.setWrapS(WRAP);
|
sampleBuilder.setWrapS(WRAP);
|
||||||
sampleBuilder.setWrapT(WRAP);
|
sampleBuilder.setWrapT(WRAP);
|
||||||
mSampler = sampleBuilder.create();
|
Sampler sampler = sampleBuilder.create();
|
||||||
|
|
||||||
ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS, null, null);
|
ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS, null, null);
|
||||||
builder.setTexEnable(true, 0);
|
builder.setTexEnable(true, 0);
|
||||||
builder.setTexEnvMode(REPLACE, 0);
|
builder.setTexEnvMode(REPLACE, 0);
|
||||||
mPfBackground = builder.create();
|
mPfBackground = builder.create();
|
||||||
mPfBackground.setName("PFBackground");
|
mPfBackground.setName("PFBackground");
|
||||||
mPfBackground.bindSampler(mSampler, 0);
|
mPfBackground.bindSampler(sampler, 0);
|
||||||
|
|
||||||
builder = new ProgramFragment.Builder(mRS, null, null);
|
builder = new ProgramFragment.Builder(mRS, null, null);
|
||||||
builder.setTexEnable(false, 0);
|
builder.setTexEnable(false, 0);
|
||||||
mPfLighting = builder.create();
|
mPfLighting = builder.create();
|
||||||
mPfLighting.setName("PFLighting");
|
mPfLighting.setName("PFLighting");
|
||||||
mPfLighting.bindSampler(mSampler, 0);
|
mPfLighting.bindSampler(sampler, 0);
|
||||||
|
|
||||||
builder = new ProgramFragment.Builder(mRS, null, null);
|
builder = new ProgramFragment.Builder(mRS, null, null);
|
||||||
builder.setTexEnable(true, 0);
|
builder.setTexEnable(true, 0);
|
||||||
builder.setTexEnvMode(MODULATE, 0);
|
builder.setTexEnvMode(MODULATE, 0);
|
||||||
mPfSky = builder.create();
|
mPfSky = builder.create();
|
||||||
mPfSky.setName("PFSky");
|
mPfSky.setName("PFSky");
|
||||||
mPfSky.bindSampler(mSampler, 0);
|
mPfSky.bindSampler(sampler, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createProgramFragmentStore() {
|
private void createProgramFragmentStore() {
|
||||||
@@ -399,33 +378,28 @@ class FallRS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void createProgramVertex() {
|
private void createProgramVertex() {
|
||||||
mPvOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
|
ProgramVertex.MatrixAllocation pvOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
|
||||||
mPvOrthoAlloc.setupProjectionNormalized(mWidth, mHeight);
|
pvOrthoAlloc.setupProjectionNormalized(mWidth, mHeight);
|
||||||
|
|
||||||
mLight = new Light.Builder(mRS).create();
|
Light light = new Light.Builder(mRS).create();
|
||||||
mLight.setPosition(0.0f, 2.0f, -8.0f);
|
light.setPosition(0.0f, 2.0f, -8.0f);
|
||||||
|
|
||||||
ProgramVertex.Builder builder = new ProgramVertex.Builder(mRS, null, null);
|
ProgramVertex.Builder builder = new ProgramVertex.Builder(mRS, null, null);
|
||||||
builder.setTextureMatrixEnable(true);
|
builder.addLight(light);
|
||||||
builder.addLight(mLight);
|
|
||||||
mPvLight = builder.create();
|
mPvLight = builder.create();
|
||||||
mPvLight.bindAllocation(mPvOrthoAlloc);
|
mPvLight.bindAllocation(pvOrthoAlloc);
|
||||||
mPvLight.setName("PVLight");
|
mPvLight.setName("PVLight");
|
||||||
|
|
||||||
builder = new ProgramVertex.Builder(mRS, null, null);
|
builder = new ProgramVertex.Builder(mRS, null, null);
|
||||||
|
builder.setTextureMatrixEnable(true);
|
||||||
mPvSky = builder.create();
|
mPvSky = builder.create();
|
||||||
mPvSky.bindAllocation(mPvOrthoAlloc);
|
mPvSky.bindAllocation(pvOrthoAlloc);
|
||||||
mPvSky.setName("PVSky");
|
mPvSky.setName("PVSky");
|
||||||
}
|
}
|
||||||
|
|
||||||
void addDrop(float x, float y) {
|
void addDrop(float x, float y) {
|
||||||
mIntData2[0] = (int) ((x / mWidth) * mMeshWidth);
|
mDrop.dropX = (int) ((x / mWidth) * mMeshWidth);
|
||||||
mIntData2[1] = (int) ((y / mHeight) * mMeshHeight);
|
mDrop.dropY = (int) ((y / mHeight) * mMeshHeight);
|
||||||
mState.subData1D(RSID_STATE_DROP_X, 2, mIntData2);
|
mDropState.data(mDrop);
|
||||||
}
|
|
||||||
|
|
||||||
void togglePause() {
|
|
||||||
mIsRunning = !mIsRunning;
|
|
||||||
mState.subData1D(RSID_STATE_RUNNING, 1, new int[] { mIsRunning ? 1 : 0 });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,20 +41,6 @@ class FallView extends RSSurfaceView {
|
|||||||
mRender.init(RS, getResources());
|
mRender.init(RS, getResources());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
|
||||||
if (mRender != null) mRender.destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
||||||
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER ||
|
|
||||||
keyCode == KeyEvent.KEYCODE_MENU) {
|
|
||||||
mRender.togglePause();
|
|
||||||
}
|
|
||||||
return super.onKeyDown(keyCode, event);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouchEvent(MotionEvent event) {
|
public boolean onTouchEvent(MotionEvent event) {
|
||||||
switch (event.getAction()) {
|
switch (event.getAction()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user