Merge change 21471 into eclair
* changes: Add Galaxy wallpaper
This commit is contained in:
@@ -16,9 +16,6 @@
|
|||||||
|
|
||||||
package android.renderscript;
|
package android.renderscript;
|
||||||
|
|
||||||
import android.util.Config;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -193,9 +190,7 @@ public class Element extends BaseObj {
|
|||||||
void addEntry(Entry e) {
|
void addEntry(Entry e) {
|
||||||
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] = e;
|
mEntries[mEntryCount] = e;
|
||||||
|
|||||||
25
libs/rs/java/Galaxy/Android.mk
Normal file
25
libs/rs/java/Galaxy/Android.mk
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
|
|
||||||
|
LOCAL_PATH := $(call my-dir)
|
||||||
|
include $(CLEAR_VARS)
|
||||||
|
|
||||||
|
LOCAL_SRC_FILES := $(call all-java-files-under, src)
|
||||||
|
#LOCAL_STATIC_JAVA_LIBRARIES := android.renderscript
|
||||||
|
|
||||||
|
LOCAL_PACKAGE_NAME := GalaxyRS
|
||||||
|
|
||||||
|
include $(BUILD_PACKAGE)
|
||||||
21
libs/rs/java/Galaxy/AndroidManifest.xml
Normal file
21
libs/rs/java/Galaxy/AndroidManifest.xml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="com.android.galaxy.rs">
|
||||||
|
|
||||||
|
<application android:label="GalaxyRS">
|
||||||
|
|
||||||
|
<activity
|
||||||
|
android:screenOrientation="portrait"
|
||||||
|
android:name="Galaxy"
|
||||||
|
android:theme="@android:style/Theme.NoTitleBar">
|
||||||
|
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
</intent-filter>
|
||||||
|
|
||||||
|
</activity>
|
||||||
|
|
||||||
|
</application>
|
||||||
|
|
||||||
|
</manifest>
|
||||||
BIN
libs/rs/java/Galaxy/res/drawable-hdpi/flares.png
Normal file
BIN
libs/rs/java/Galaxy/res/drawable-hdpi/flares.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.9 KiB |
BIN
libs/rs/java/Galaxy/res/drawable-hdpi/light1.png
Normal file
BIN
libs/rs/java/Galaxy/res/drawable-hdpi/light1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 191 KiB |
BIN
libs/rs/java/Galaxy/res/drawable-hdpi/light2.png
Normal file
BIN
libs/rs/java/Galaxy/res/drawable-hdpi/light2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 126 KiB |
BIN
libs/rs/java/Galaxy/res/drawable-hdpi/space.jpg
Normal file
BIN
libs/rs/java/Galaxy/res/drawable-hdpi/space.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 134 KiB |
181
libs/rs/java/Galaxy/res/raw/galaxy.c
Normal file
181
libs/rs/java/Galaxy/res/raw/galaxy.c
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
// 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(PVBackground)
|
||||||
|
#pragma stateFragment(PFBackground)
|
||||||
|
#pragma stateFragmentStore(PFSBackground)
|
||||||
|
|
||||||
|
#define RSID_STATE 0
|
||||||
|
#define RSID_FRAME_COUNT 0
|
||||||
|
#define RSID_WIDTH 1
|
||||||
|
#define RSID_HEIGHT 2
|
||||||
|
#define RSID_PARTICLES_COUNT 3
|
||||||
|
#define RSID_GALAXY_RADIUS 4
|
||||||
|
|
||||||
|
#define RSID_PARTICLES 1
|
||||||
|
|
||||||
|
#define PARTICLE_STRUCT_FIELDS_COUNT 7
|
||||||
|
#define PARTICLE_STRUCT_ANGLE 0
|
||||||
|
#define PARTICLE_STRUCT_DISTANCE 1
|
||||||
|
#define PARTICLE_STRUCT_SPEED 2
|
||||||
|
#define PARTICLE_STRUCT_Z 3
|
||||||
|
#define PARTICLE_STRUCT_RADIUS 4
|
||||||
|
#define PARTICLE_STRUCT_U1 5
|
||||||
|
#define PARTICLE_STRUCT_U2 6
|
||||||
|
|
||||||
|
#define RSID_PARTICLES_BUFFER 2
|
||||||
|
#define PARTICLE_BUFFER_COMPONENTS_COUNT 6
|
||||||
|
|
||||||
|
#define PARTICLES_TEXTURES_COUNT 2
|
||||||
|
|
||||||
|
#define ELLIPSE_RATIO 0.86f
|
||||||
|
#define ELLIPSE_TWIST 0.02333333333f
|
||||||
|
|
||||||
|
void drawSpace(int width, int height) {
|
||||||
|
bindTexture(NAMED_PFBackground, 0, NAMED_TSpace);
|
||||||
|
drawQuadTexCoords(
|
||||||
|
0.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 1.0f,
|
||||||
|
width, 0.0f, 0.0f,
|
||||||
|
2.0f, 1.0f,
|
||||||
|
width, height, 0.0f,
|
||||||
|
2.0f, 0.0f,
|
||||||
|
0.0f, height, 0.0f,
|
||||||
|
0.0f, 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawLights(int width, int height) {
|
||||||
|
bindProgramFragment(NAMED_PFBackground);
|
||||||
|
bindProgramFragmentStore(NAMED_PFSLights);
|
||||||
|
|
||||||
|
float x = (width - 512.0f) / 2.0f;
|
||||||
|
float y = (height - 512.0f) / 2.0f;
|
||||||
|
|
||||||
|
bindTexture(NAMED_PFBackground, 0, NAMED_TLight1);
|
||||||
|
drawQuad(x + 512.0f, y , 0.0f,
|
||||||
|
x , y , 0.0f,
|
||||||
|
x , y + 512.0f, 0.0f,
|
||||||
|
x + 512.0f, y + 512.0f, 0.0f);
|
||||||
|
|
||||||
|
bindTexture(NAMED_PFBackground, 0, NAMED_TLight2);
|
||||||
|
drawQuad(x + 512.0f, y , 0.0f,
|
||||||
|
x , y , 0.0f,
|
||||||
|
x , y + 512.0f, 0.0f,
|
||||||
|
x + 512.0f, y + 512.0f, 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawParticle(int index, int bufferIndex, int width, int height, int radius) {
|
||||||
|
float *particle = loadArrayF(RSID_PARTICLES, index);
|
||||||
|
|
||||||
|
float distance = particle[PARTICLE_STRUCT_DISTANCE];
|
||||||
|
float angle = particle[PARTICLE_STRUCT_ANGLE];
|
||||||
|
float speed = particle[PARTICLE_STRUCT_SPEED];
|
||||||
|
float r = particle[PARTICLE_STRUCT_RADIUS];
|
||||||
|
|
||||||
|
int red;
|
||||||
|
int green;
|
||||||
|
int blue;
|
||||||
|
|
||||||
|
if (distance < radius / 3.0f) {
|
||||||
|
red = 220 + (distance / (float) radius) * 35;
|
||||||
|
green = 220;
|
||||||
|
blue = 220;
|
||||||
|
} else {
|
||||||
|
red = 180;
|
||||||
|
green = 180;
|
||||||
|
blue = clamp(140 + (distance / (float) radius) * 115, 140, 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
int color = 0xFF000000 | red | green << 8 | blue << 16;
|
||||||
|
|
||||||
|
float a = angle + speed * (0.5f + (0.5f * radius / distance));
|
||||||
|
float x = distance * sinf(a);
|
||||||
|
float y = distance * cosf(a) * ELLIPSE_RATIO;
|
||||||
|
float z = distance * ELLIPSE_TWIST;
|
||||||
|
float s = cosf(z);
|
||||||
|
float t = sinf(z);
|
||||||
|
|
||||||
|
float sX = t * x + s * y + width / 2.0f;
|
||||||
|
float sY = s * x - t * y + height / 2.0f;
|
||||||
|
float sZ = particle[PARTICLE_STRUCT_Z];
|
||||||
|
|
||||||
|
float u1 = particle[PARTICLE_STRUCT_U1];
|
||||||
|
float u2 = particle[PARTICLE_STRUCT_U2];
|
||||||
|
|
||||||
|
// lower left vertex of the particle's triangle
|
||||||
|
storeI32(RSID_PARTICLES_BUFFER, bufferIndex, color); // ABGR
|
||||||
|
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 1, sX - r); // X
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 2, sY + r); // Y
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 3, sZ); // Z
|
||||||
|
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 4, u1); // S
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 5, 1.0f); // T
|
||||||
|
|
||||||
|
// lower right vertex of the particle's triangle
|
||||||
|
bufferIndex += PARTICLE_BUFFER_COMPONENTS_COUNT;
|
||||||
|
storeI32(RSID_PARTICLES_BUFFER, bufferIndex, color);
|
||||||
|
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 1, sX + r);
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 2, sY + r);
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 3, sZ);
|
||||||
|
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 4, u2);
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 5, 1.0f);
|
||||||
|
|
||||||
|
// upper middle vertex of the particle's triangle
|
||||||
|
bufferIndex += PARTICLE_BUFFER_COMPONENTS_COUNT;
|
||||||
|
storeI32(RSID_PARTICLES_BUFFER, bufferIndex, color);
|
||||||
|
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 1, sX);
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 2, sY - r);
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 3, sZ);
|
||||||
|
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 4, u1 + (u2 - u1) / 2.0f);
|
||||||
|
storeF(RSID_PARTICLES_BUFFER, bufferIndex + 5, 0.0f);
|
||||||
|
|
||||||
|
particle[PARTICLE_STRUCT_ANGLE] = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawParticles(int width, int height) {
|
||||||
|
bindProgramFragment(NAMED_PFLighting);
|
||||||
|
bindTexture(NAMED_PFLighting, 0, NAMED_TFlares);
|
||||||
|
|
||||||
|
int radius = loadI32(RSID_STATE, RSID_GALAXY_RADIUS);
|
||||||
|
int particlesCount = loadI32(RSID_STATE, RSID_PARTICLES_COUNT);
|
||||||
|
int count = particlesCount * PARTICLE_STRUCT_FIELDS_COUNT;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
int bufferIndex = 0;
|
||||||
|
for ( ; i < count; i += PARTICLE_STRUCT_FIELDS_COUNT) {
|
||||||
|
drawParticle(i, bufferIndex, width, height, radius);
|
||||||
|
// each particle is a triangle (3 vertices) of 6 properties (ABGR, X, Y, Z, S, T)
|
||||||
|
bufferIndex += 3 * PARTICLE_BUFFER_COMPONENTS_COUNT;
|
||||||
|
}
|
||||||
|
|
||||||
|
uploadToBufferObject(NAMED_BParticles);
|
||||||
|
drawSimpleMeshRange(NAMED_MParticles, 0, particlesCount * 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int index) {
|
||||||
|
int width = loadI32(RSID_STATE, RSID_WIDTH);
|
||||||
|
int height = loadI32(RSID_STATE, RSID_HEIGHT);
|
||||||
|
|
||||||
|
drawSpace(width, height);
|
||||||
|
drawParticles(width, height);
|
||||||
|
drawLights(width, height);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
46
libs/rs/java/Galaxy/src/com/android/galaxy/rs/Galaxy.java
Normal file
46
libs/rs/java/Galaxy/src/com/android/galaxy/rs/Galaxy.java
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.galaxy.rs;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
public class Galaxy extends Activity {
|
||||||
|
private GalaxyView mView;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle icicle) {
|
||||||
|
super.onCreate(icicle);
|
||||||
|
|
||||||
|
mView = new GalaxyView(this);
|
||||||
|
setContentView(mView);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
mView.onResume();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
mView.onPause();
|
||||||
|
|
||||||
|
Runtime.getRuntime().exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
332
libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java
Normal file
332
libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java
Normal file
@@ -0,0 +1,332 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.galaxy.rs;
|
||||||
|
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.renderscript.RenderScript;
|
||||||
|
import android.renderscript.ScriptC;
|
||||||
|
import android.renderscript.ProgramFragment;
|
||||||
|
import android.renderscript.ProgramStore;
|
||||||
|
import android.renderscript.ProgramVertex;
|
||||||
|
import android.renderscript.Allocation;
|
||||||
|
import android.renderscript.Sampler;
|
||||||
|
import android.renderscript.Element;
|
||||||
|
import android.renderscript.SimpleMesh;
|
||||||
|
import android.renderscript.Primitive;
|
||||||
|
import static android.renderscript.Sampler.Value.LINEAR;
|
||||||
|
import static android.renderscript.Sampler.Value.CLAMP;
|
||||||
|
import static android.renderscript.Sampler.Value.WRAP;
|
||||||
|
import static android.renderscript.ProgramStore.DepthFunc.*;
|
||||||
|
import static android.renderscript.ProgramStore.BlendDstFunc;
|
||||||
|
import static android.renderscript.ProgramStore.BlendSrcFunc;
|
||||||
|
import static android.renderscript.ProgramFragment.EnvMode.*;
|
||||||
|
import static android.renderscript.Element.*;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import static android.util.MathUtils.*;
|
||||||
|
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
class GalaxyRS {
|
||||||
|
private static final int GALAXY_RADIUS = 300;
|
||||||
|
private static final int PARTICLES_COUNT = 12000;
|
||||||
|
private static final float GALAXY_HEIGHT = 0.1f;
|
||||||
|
|
||||||
|
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_PARTICLES_COUNT = 3;
|
||||||
|
private static final int RSID_STATE_GALAXY_RADIUS = 4;
|
||||||
|
|
||||||
|
private static final int TEXTURES_COUNT = 4;
|
||||||
|
private static final int PARTICLES_TEXTURES_COUNT = 2;
|
||||||
|
private static final int RSID_TEXTURE_SPACE = 0;
|
||||||
|
private static final int RSID_TEXTURE_LIGHT1 = 1;
|
||||||
|
private static final int RSID_TEXTURE_LIGHT2 = 2;
|
||||||
|
private static final int RSID_TEXTURE_FLARES = 3;
|
||||||
|
|
||||||
|
private static final int RSID_PARTICLES = 1;
|
||||||
|
private static final int PARTICLE_STRUCT_FIELDS_COUNT = 7;
|
||||||
|
private static final int PARTICLE_STRUCT_ANGLE = 0;
|
||||||
|
private static final int PARTICLE_STRUCT_DISTANCE = 1;
|
||||||
|
private static final int PARTICLE_STRUCT_SPEED = 2;
|
||||||
|
private static final int PARTICLE_STRUCT_Z = 3;
|
||||||
|
private static final int PARTICLE_STRUCT_RADIUS = 4;
|
||||||
|
private static final int PARTICLE_STRUCT_U1 = 5;
|
||||||
|
private static final int PARTICLE_STRUCT_U2 = 6;
|
||||||
|
|
||||||
|
private static final int RSID_PARTICLES_BUFFER = 2;
|
||||||
|
|
||||||
|
private Resources mResources;
|
||||||
|
private RenderScript mRS;
|
||||||
|
|
||||||
|
private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();
|
||||||
|
|
||||||
|
private final int mWidth;
|
||||||
|
private final int mHeight;
|
||||||
|
|
||||||
|
private ScriptC mScript;
|
||||||
|
private Sampler mSampler;
|
||||||
|
private Sampler mLightSampler;
|
||||||
|
private ProgramFragment mPfBackground;
|
||||||
|
private ProgramFragment mPfLighting;
|
||||||
|
private ProgramStore mPfsBackground;
|
||||||
|
private ProgramStore mPfsLights;
|
||||||
|
private ProgramVertex mPvBackground;
|
||||||
|
private ProgramVertex.MatrixAllocation mPvOrthoAlloc;
|
||||||
|
|
||||||
|
private Allocation[] mTextures;
|
||||||
|
|
||||||
|
private Allocation mState;
|
||||||
|
private Allocation mParticles;
|
||||||
|
private Allocation mParticlesBuffer;
|
||||||
|
private SimpleMesh mParticlesMesh;
|
||||||
|
|
||||||
|
public GalaxyRS(int width, int height) {
|
||||||
|
mWidth = width;
|
||||||
|
mHeight = height;
|
||||||
|
mOptionsARGB.inScaled = false;
|
||||||
|
mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init(RenderScript rs, Resources res) {
|
||||||
|
mRS = rs;
|
||||||
|
mResources = res;
|
||||||
|
initRS();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void destroy() {
|
||||||
|
mScript.destroy();
|
||||||
|
mSampler.destroy();
|
||||||
|
mLightSampler.destroy();
|
||||||
|
mPfBackground.destroy();
|
||||||
|
mPfsBackground.destroy();
|
||||||
|
mPvBackground.destroy();
|
||||||
|
mPvOrthoAlloc.mAlloc.destroy();
|
||||||
|
for (Allocation a : mTextures) {
|
||||||
|
a.destroy();
|
||||||
|
}
|
||||||
|
mState.destroy();
|
||||||
|
mPfLighting.destroy();
|
||||||
|
mParticles.destroy();
|
||||||
|
mPfsLights.destroy();
|
||||||
|
mParticlesMesh.destroy();
|
||||||
|
mParticlesBuffer.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void finalize() throws Throwable {
|
||||||
|
try {
|
||||||
|
destroy();
|
||||||
|
} finally {
|
||||||
|
super.finalize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initRS() {
|
||||||
|
createProgramVertex();
|
||||||
|
createProgramFragmentStore();
|
||||||
|
createProgramFragment();
|
||||||
|
createScriptStructures();
|
||||||
|
loadTextures();
|
||||||
|
|
||||||
|
ScriptC.Builder sb = new ScriptC.Builder(mRS);
|
||||||
|
sb.setScript(mResources, R.raw.galaxy);
|
||||||
|
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);
|
||||||
|
mScript.bindAllocation(mParticles, RSID_PARTICLES);
|
||||||
|
mScript.bindAllocation(mParticlesBuffer, RSID_PARTICLES_BUFFER);
|
||||||
|
|
||||||
|
mRS.contextBindRootScript(mScript);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createScriptStructures() {
|
||||||
|
createState();
|
||||||
|
createParticles();
|
||||||
|
createParticlesMesh();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createParticlesMesh() {
|
||||||
|
final Element.Builder elementBuilder = new Element.Builder(mRS);
|
||||||
|
elementBuilder.add(Element.DataType.UNSIGNED, Element.DataKind.RED, true, 8);
|
||||||
|
elementBuilder.add(Element.DataType.UNSIGNED, Element.DataKind.GREEN, true, 8);
|
||||||
|
elementBuilder.add(Element.DataType.UNSIGNED, Element.DataKind.BLUE, true, 8);
|
||||||
|
elementBuilder.add(Element.DataType.UNSIGNED, Element.DataKind.ALPHA, true, 8);
|
||||||
|
elementBuilder.add(Element.DataType.FLOAT, Element.DataKind.X, false, 32);
|
||||||
|
elementBuilder.add(Element.DataType.FLOAT, Element.DataKind.Y, false, 32);
|
||||||
|
elementBuilder.add(Element.DataType.FLOAT, Element.DataKind.Z, false, 32);
|
||||||
|
elementBuilder.add(Element.DataType.FLOAT, Element.DataKind.S, false, 32);
|
||||||
|
elementBuilder.add(Element.DataType.FLOAT, Element.DataKind.T, false, 32);
|
||||||
|
final Element vertexElement = elementBuilder.create();
|
||||||
|
|
||||||
|
final SimpleMesh.Builder meshBuilder = new SimpleMesh.Builder(mRS);
|
||||||
|
final int vertexSlot = meshBuilder.addVertexType(vertexElement, PARTICLES_COUNT * 3);
|
||||||
|
meshBuilder.setPrimitive(Primitive.TRIANGLE);
|
||||||
|
mParticlesMesh = meshBuilder.create();
|
||||||
|
mParticlesMesh.setName("MParticles");
|
||||||
|
|
||||||
|
mParticlesBuffer = mParticlesMesh.createVertexAllocation(vertexSlot);
|
||||||
|
mParticlesBuffer.setName("BParticles");
|
||||||
|
mParticlesMesh.bindVertexAllocation(mParticlesBuffer, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createParticles() {
|
||||||
|
final float[] particles = new float[PARTICLES_COUNT * PARTICLE_STRUCT_FIELDS_COUNT];
|
||||||
|
mParticles = Allocation.createSized(mRS, USER_FLOAT, particles.length);
|
||||||
|
for (int i = 0; i < particles.length; i += PARTICLE_STRUCT_FIELDS_COUNT) {
|
||||||
|
createParticle(particles, i);
|
||||||
|
}
|
||||||
|
mParticles.data(particles);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createState() {
|
||||||
|
final int[] data = new int[5];
|
||||||
|
|
||||||
|
mState = Allocation.createSized(mRS, USER_I32, data.length);
|
||||||
|
data[RSID_STATE_FRAMECOUNT] = 0;
|
||||||
|
data[RSID_STATE_WIDTH] = mWidth;
|
||||||
|
data[RSID_STATE_HEIGHT] = mHeight;
|
||||||
|
data[RSID_STATE_PARTICLES_COUNT] = PARTICLES_COUNT;
|
||||||
|
data[RSID_STATE_GALAXY_RADIUS] = GALAXY_RADIUS;
|
||||||
|
mState.data(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({"PointlessArithmeticExpression"})
|
||||||
|
private void createParticle(float[] particles, int index) {
|
||||||
|
int sprite = random(PARTICLES_TEXTURES_COUNT);
|
||||||
|
float d = abs(randomGauss()) * GALAXY_RADIUS / 2.0f;
|
||||||
|
|
||||||
|
particles[index + PARTICLE_STRUCT_ANGLE] = random(0.0f, (float) (Math.PI * 2.0));
|
||||||
|
particles[index + PARTICLE_STRUCT_DISTANCE] = d;
|
||||||
|
particles[index + PARTICLE_STRUCT_SPEED] = random(0.0015f, 0.0025f);
|
||||||
|
particles[index + PARTICLE_STRUCT_Z] = randomGauss() * GALAXY_HEIGHT *
|
||||||
|
(GALAXY_RADIUS - d) / (float) GALAXY_RADIUS;
|
||||||
|
particles[index + PARTICLE_STRUCT_RADIUS] = random(3.0f, 7.5f);
|
||||||
|
particles[index + PARTICLE_STRUCT_U1] = sprite / (float) PARTICLES_TEXTURES_COUNT;
|
||||||
|
particles[index + PARTICLE_STRUCT_U2] = (sprite + 1) / (float) PARTICLES_TEXTURES_COUNT;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float randomGauss() {
|
||||||
|
float x1;
|
||||||
|
float x2;
|
||||||
|
float w;
|
||||||
|
|
||||||
|
do {
|
||||||
|
x1 = 2.0f * random(0.0f, 1.0f) - 1.0f;
|
||||||
|
x2 = 2.0f * random(0.0f, 1.0f) - 1.0f;
|
||||||
|
w = x1 * x1 + x2 * x2;
|
||||||
|
} while (w >= 1.0f);
|
||||||
|
|
||||||
|
w = (float) Math.sqrt(-2.0 * log(w) / w);
|
||||||
|
return x1 * w;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadTextures() {
|
||||||
|
mTextures = new Allocation[TEXTURES_COUNT];
|
||||||
|
|
||||||
|
final Allocation[] textures = mTextures;
|
||||||
|
textures[RSID_TEXTURE_SPACE] = loadTexture(R.drawable.space, "TSpace");
|
||||||
|
textures[RSID_TEXTURE_LIGHT1] = loadTextureARGB(R.drawable.light1, "TLight1");
|
||||||
|
textures[RSID_TEXTURE_LIGHT2] = loadTextureARGB(R.drawable.light2, "TLight2");
|
||||||
|
textures[RSID_TEXTURE_FLARES] = loadTextureARGB(R.drawable.flares, "TFlares");
|
||||||
|
|
||||||
|
final int count = textures.length;
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
final Allocation texture = textures[i];
|
||||||
|
texture.uploadToTexture(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Allocation loadTexture(int id, String name) {
|
||||||
|
final Allocation allocation = Allocation.createFromBitmapResource(mRS, mResources,
|
||||||
|
id, RGB_565, false);
|
||||||
|
allocation.setName(name);
|
||||||
|
return allocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Allocation loadTextureARGB(int id, String name) {
|
||||||
|
Bitmap b = BitmapFactory.decodeResource(mResources, id, mOptionsARGB);
|
||||||
|
final Allocation allocation = Allocation.createFromBitmap(mRS, b, RGBA_8888, false);
|
||||||
|
allocation.setName(name);
|
||||||
|
return allocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createProgramFragment() {
|
||||||
|
Sampler.Builder sampleBuilder = new Sampler.Builder(mRS);
|
||||||
|
sampleBuilder.setMin(LINEAR);
|
||||||
|
sampleBuilder.setMag(LINEAR);
|
||||||
|
sampleBuilder.setWrapS(WRAP);
|
||||||
|
sampleBuilder.setWrapT(WRAP);
|
||||||
|
mSampler = sampleBuilder.create();
|
||||||
|
|
||||||
|
ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS, null, null);
|
||||||
|
builder.setTexEnable(true, 0);
|
||||||
|
builder.setTexEnvMode(REPLACE, 0);
|
||||||
|
mPfBackground = builder.create();
|
||||||
|
mPfBackground.setName("PFBackground");
|
||||||
|
mPfBackground.bindSampler(mSampler, 0);
|
||||||
|
|
||||||
|
sampleBuilder = new Sampler.Builder(mRS);
|
||||||
|
sampleBuilder.setMin(LINEAR);
|
||||||
|
sampleBuilder.setMag(LINEAR);
|
||||||
|
sampleBuilder.setWrapS(CLAMP);
|
||||||
|
sampleBuilder.setWrapT(CLAMP);
|
||||||
|
mLightSampler = sampleBuilder.create();
|
||||||
|
|
||||||
|
builder = new ProgramFragment.Builder(mRS, null, null);
|
||||||
|
builder.setTexEnable(true, 0);
|
||||||
|
builder.setTexEnvMode(MODULATE, 0);
|
||||||
|
mPfLighting = builder.create();
|
||||||
|
mPfLighting.setName("PFLighting");
|
||||||
|
mPfLighting.bindSampler(mLightSampler, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createProgramFragmentStore() {
|
||||||
|
ProgramStore.Builder builder = new ProgramStore.Builder(mRS, null, null);
|
||||||
|
builder.setDepthFunc(ALWAYS);
|
||||||
|
builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
|
||||||
|
builder.setDitherEnable(false);
|
||||||
|
builder.setDepthMask(true);
|
||||||
|
mPfsBackground = builder.create();
|
||||||
|
mPfsBackground.setName("PFSBackground");
|
||||||
|
|
||||||
|
builder = new ProgramStore.Builder(mRS, null, null);
|
||||||
|
builder.setDepthFunc(ALWAYS);
|
||||||
|
builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE);
|
||||||
|
builder.setDitherEnable(false);
|
||||||
|
builder.setDepthMask(true);
|
||||||
|
mPfsLights = builder.create();
|
||||||
|
mPfsLights.setName("PFSLights");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createProgramVertex() {
|
||||||
|
mPvOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
|
||||||
|
//mPvOrthoAlloc.setupProjectionNormalized(mWidth, mHeight);
|
||||||
|
mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
|
||||||
|
|
||||||
|
ProgramVertex.Builder builder = new ProgramVertex.Builder(mRS, null, null);
|
||||||
|
builder.setTextureMatrixEnable(true);
|
||||||
|
mPvBackground = builder.create();
|
||||||
|
mPvBackground.bindAllocation(mPvOrthoAlloc);
|
||||||
|
mPvBackground.setName("PVBackground");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.galaxy.rs;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.view.SurfaceHolder;
|
||||||
|
import android.renderscript.RenderScript;
|
||||||
|
import android.renderscript.RSSurfaceView;
|
||||||
|
|
||||||
|
class GalaxyView extends RSSurfaceView {
|
||||||
|
private GalaxyRS mRender;
|
||||||
|
|
||||||
|
public GalaxyView(Context context) {
|
||||||
|
super(context);
|
||||||
|
setFocusable(true);
|
||||||
|
setFocusableInTouchMode(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
|
||||||
|
super.surfaceChanged(holder, format, w, h);
|
||||||
|
|
||||||
|
RenderScript RS = createRenderScript();
|
||||||
|
mRender = new GalaxyRS(w, h);
|
||||||
|
mRender.init(RS, getResources());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
|
if (mRender != null) mRender.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user