diff options
author | Romain Guy <romainguy@android.com> | 2009-08-07 15:40:32 -0700 |
---|---|---|
committer | Romain Guy <romainguy@android.com> | 2009-08-07 15:40:32 -0700 |
commit | 6c0cc6db9966bda47d04ecdd1a4692b11cf3775a (patch) | |
tree | 8d7513e2ea2b13550f5507ac794adbbbb11f9e75 /libs/rs/java/Fall/res/raw/fall.c | |
parent | 4c35e2c59afa28f9ed3fab1788570ef933f29b1a (diff) | |
download | frameworks_base-6c0cc6db9966bda47d04ecdd1a4692b11cf3775a.zip frameworks_base-6c0cc6db9966bda47d04ecdd1a4692b11cf3775a.tar.gz frameworks_base-6c0cc6db9966bda47d04ecdd1a4692b11cf3775a.tar.bz2 |
Add refraction and bettr normals computation to FallRS
Diffstat (limited to 'libs/rs/java/Fall/res/raw/fall.c')
-rw-r--r-- | libs/rs/java/Fall/res/raw/fall.c | 122 |
1 files changed, 112 insertions, 10 deletions
diff --git a/libs/rs/java/Fall/res/raw/fall.c b/libs/rs/java/Fall/res/raw/fall.c index 267b3ea..ba9dd25 100644 --- a/libs/rs/java/Fall/res/raw/fall.c +++ b/libs/rs/java/Fall/res/raw/fall.c @@ -27,6 +27,7 @@ #define RSID_RIPPLE_INDEX 6 #define RSID_DROP_X 7 #define RSID_DROP_Y 8 +#define RSID_RUNNING 9 #define RSID_TEXTURES 1 @@ -35,7 +36,7 @@ #define RSID_REFRACTION_MAP 3 #define REFRACTION 1.333f -#define DAMP 4 +#define DAMP 3 #define DROP_RADIUS 2 // The higher, the smaller the ripple @@ -113,6 +114,20 @@ void updateRipples() { } } +int refraction(int d, int wave) { + int* map = loadArrayI32(RSID_REFRACTION_MAP, 0); + int i = d; + if (i < 0) i = -i; + if (i > 512) i = 512; + int w = (wave + 0x10000) >> 8; + w &= ~(w >> 31); + int r = (map[i] * w) >> 3; + if (d < 0) { + return -r; + } + return r; +} + void generateRipples() { int rippleMapSize = loadI32(RSID_STATE, RSID_RIPPLE_MAP_SIZE); int width = loadI32(RSID_STATE, RSID_MESH_WIDTH); @@ -135,6 +150,19 @@ void generateRipples() { int dx = nextWave - wave; int dy = current[b] - wave; + int offsetx = refraction(dx, wave) >> 16; + int u = (width - w) + offsetx; + u &= ~(u >> 31); + if (u >= width) u = width - 1; + + int offsety = refraction(dy, wave) >> 16; + int v = (height - h) + offsety; + v &= ~(v >> 31); + if (v >= height) v = height - 1; + + vertices[(offset + w) * 8 + 3] = u / (float) width; + vertices[(offset + w) * 8 + 4] = v / (float) height; + // Update Z coordinate of the vertex vertices[(offset + w) * 8 + 7] = (dy / 512.0f) / RIPPLE_HEIGHT; @@ -187,13 +215,67 @@ void generateRipples() { n3x /= len; n3y /= len; n3z /= len; + + // V2 + v2x = vertices[(yOffset + width + x + 1) * 8 + 5]; + v2y = vertices[(yOffset + width + x + 1) * 8 + 6]; + v2z = vertices[(yOffset + width + x + 1) * 8 + 7]; + + // N1 + n1x = v2x - v1x; + n1y = v2y - v1y; + n1z = v2z - v1z; - vertices[(yOffset + x) * 8 + 0] = -n3x; - vertices[(yOffset + x) * 8 + 1] = -n3y; + // N2 + n2x = v3x - v1x; + n2y = v3y - v1y; + n2z = v3z - v1z; + + // Avegare of previous normal and N1 x N2 + n3x = n3x / 2.0f + (n1y * n2z - n1z * n2y) / 2.0f; + n3y = n3y / 2.0f + (n1z * n2x - n1x * n2z) / 2.0f; + n3z = n3z / 2.0f + (n1x * n2y - n1y * n2x) / 2.0f; + + // Normalize + len = magf3(n3x, n3y, n3z); + n3x /= len; + n3y /= len; + n3z /= len; + + vertices[(yOffset + x) * 8 + 0] = n3x; + vertices[(yOffset + x) * 8 + 1] = n3y; vertices[(yOffset + x) * 8 + 2] = -n3z; + + // reset Z + vertices[(yOffset + x) * 8 + 7] = 0.0f; } } -} +} + +void drawNormals() { + int width = loadI32(RSID_STATE, RSID_MESH_WIDTH); + int height = loadI32(RSID_STATE, RSID_MESH_HEIGHT); + + float *vertices = loadTriangleMeshVerticesF(NAMED_mesh); + + bindProgramVertex(NAMED_PVLines); + color(1.0f, 0.0f, 0.0f, 1.0f); + + int y = 0; + for ( ; y < height; y++) { + int yOffset = y * width; + int x = 0; + for ( ; x < width; x++) { + float vx = vertices[(yOffset + x) * 8 + 5]; + float vy = vertices[(yOffset + x) * 8 + 6]; + float vz = vertices[(yOffset + x) * 8 + 7]; + float nx = vertices[(yOffset + x) * 8 + 0]; + float ny = vertices[(yOffset + x) * 8 + 1]; + float nz = vertices[(yOffset + x) * 8 + 2]; + drawLine(vx, vy, vz, vx + nx / 10.0f, vy + ny / 10.0f, vz + nz / 10.0f); + } + } +} int main(int index) { int dropX = loadI32(RSID_STATE, RSID_DROP_X); @@ -204,13 +286,33 @@ int main(int index) { storeI32(RSID_STATE, RSID_DROP_Y, -1); } - updateRipples(); - generateRipples(); - updateTriangleMesh(NAMED_mesh); + int isRunning = loadI32(RSID_STATE, RSID_RUNNING); + if (isRunning) { + updateRipples(); + generateRipples(); + updateTriangleMesh(NAMED_mesh); + } - ambient(0.0f, 0.1f, 0.9f, 1.0f); - diffuse(0.0f, 0.1f, 0.9f, 1.0f); - drawTriangleMesh(NAMED_mesh); + bindTexture(NAMED_PFBackground, 0, NAMED_TRiverbed); + drawTriangleMesh(NAMED_mesh); + + ambient(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); + shininess(40.0f); + bindProgramFragment(NAMED_PFLighting); + drawTriangleMesh(NAMED_mesh); + +// bindProgramVertex(NAMED_PVSky); +// bindProgramFragment(NAMED_PFSky); +// bindProgramFragmentStore(NAMED_PFSSky); +// color(1.0f, 1.0f, 1.0f, 0.30f); +// bindTexture(NAMED_PFSky, 0, NAMED_TSky); +// drawTriangleMesh(NAMED_mesh); + + if (!isRunning) { + drawNormals(); + } return 1; } |