diff options
author | Alex Sakhartchouk <alexst@google.com> | 2012-02-22 11:59:12 -0800 |
---|---|---|
committer | Alex Sakhartchouk <alexst@google.com> | 2012-02-22 11:59:12 -0800 |
commit | d2dc9838961f945c402e5d810bc715196240be6f (patch) | |
tree | ad17c590d84993488a4eafc9954191970aaed926 /tests/RenderScriptTests/SceneGraph/src | |
parent | 9cc290038ca3ee06ed34d172bbd139ea13bafd1b (diff) | |
download | frameworks_base-d2dc9838961f945c402e5d810bc715196240be6f.zip frameworks_base-d2dc9838961f945c402e5d810bc715196240be6f.tar.gz frameworks_base-d2dc9838961f945c402e5d810bc715196240be6f.tar.bz2 |
Simplifying programmatic creation.
Change-Id: I792f96b5ed47d3c78976d57259074c9ae402575b
Diffstat (limited to 'tests/RenderScriptTests/SceneGraph/src')
7 files changed, 156 insertions, 54 deletions
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java index 3476e35..9266f30 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.Iterator; import com.android.scenegraph.Float4Param; +import com.android.scenegraph.MatrixTransform; import com.android.scenegraph.SceneManager; import com.android.scenegraph.ShaderParam; import com.android.scenegraph.TransformParam; @@ -89,6 +90,10 @@ public class Renderable extends RenderableBase { mMaterialName = name; } + public Transform getTransform() { + return mTransform; + } + public void setTransform(Transform t) { mTransform = t; if (mField != null) { @@ -199,12 +204,14 @@ public class Renderable extends RenderableBase { if (mRenderState == null) { mRenderState = SceneManager.getDefaultState(); } + if (mTransform == null) { + mTransform = SceneManager.getDefaultTransform(); + } updateVertexConstants(rs); updateFragmentConstants(rs); - if (mTransform != null) { - mData.transformMatrix = mTransform.getRSData().getAllocation(); - } + mData.transformMatrix = mTransform.getRSData().getAllocation(); + mData.name = getNameAlloc(rs); mData.render_state = mRenderState.getRSData().getAllocation(); mData.bVolInitialized = 0; diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java index 9bd3bf9..27336ab 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java @@ -22,6 +22,10 @@ import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.android.scenegraph.Camera; +import com.android.scenegraph.CompoundTransform; +import com.android.scenegraph.RenderPass; +import com.android.scenegraph.Renderable; import com.android.scenegraph.SceneManager; import com.android.scenegraph.TextureBase; @@ -81,6 +85,18 @@ public class Scene extends SceneGraphBase { mRootTransforms.appendChild(t); } + public CompoundTransform appendNewCompoundTransform() { + CompoundTransform t = new CompoundTransform(); + appendTransform(t); + return t; + } + + public MatrixTransform appendNewMatrixTransform() { + MatrixTransform t = new MatrixTransform(); + appendTransform(t); + return t; + } + // temporary public void addToTransformMap(Transform t) { mTransformMap.put(t.getName(), t); @@ -97,6 +113,12 @@ public class Scene extends SceneGraphBase { mRenderPasses.add(p); } + public RenderPass appendNewRenderPass() { + RenderPass p = new RenderPass(); + appendRenderPass(p); + return p; + } + public void clearRenderPasses() { mRenderPasses.clear(); } @@ -115,6 +137,12 @@ public class Scene extends SceneGraphBase { mCameras.add(c); } + public Camera appendNewCamera() { + Camera c = new Camera(); + appendCamera(c); + return c; + } + public void appendShader(FragmentShader f) { if (f == null) { throw new RuntimeException("Adding null object"); @@ -142,7 +170,15 @@ public class Scene extends SceneGraphBase { throw new RuntimeException("Adding null object"); } mRenderables.add(d); - mRenderableMap.put(d.getName(), d); + if (d.getName() != null) { + mRenderableMap.put(d.getName(), d); + } + } + + public Renderable appendNewRenderable() { + Renderable r = new Renderable(); + appendRenderable(r); + return r; } public ArrayList<RenderableBase> getRenderables() { diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/SceneManager.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/SceneManager.java index 3d1019e..4ff2c8b 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/SceneManager.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/SceneManager.java @@ -80,6 +80,7 @@ public class SceneManager extends SceneGraphBase { private VertexShader mDefaultVertex; private RenderState mDefaultState; + private Transform mDefaultTransform; private static Allocation getDefault(boolean isCube) { final int dimension = 4; @@ -161,24 +162,32 @@ public class SceneManager extends SceneGraphBase { return b; } - public static Allocation loadCubemap(String name, RenderScriptGL rs, Resources res) { - Bitmap b = loadBitmap(name, res); + static Allocation createFromBitmap(Bitmap b, RenderScriptGL rs, boolean isCube) { if (b == null) { return null; } - return Allocation.createCubemapFromBitmap(rs, b, - MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, - Allocation.USAGE_GRAPHICS_TEXTURE); + MipmapControl mip = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE; + int usage = Allocation.USAGE_GRAPHICS_TEXTURE; + if (isCube) { + return Allocation.createCubemapFromBitmap(rs, b, mip, usage); + } + return Allocation.createFromBitmap(rs, b, mip, usage); + } + + public static Allocation loadCubemap(String name, RenderScriptGL rs, Resources res) { + return createFromBitmap(loadBitmap(name, res), rs, true); + } + + public static Allocation loadCubemap(int id, RenderScriptGL rs, Resources res) { + return createFromBitmap(BitmapFactory.decodeResource(res, id), rs, true); } public static Allocation loadTexture2D(String name, RenderScriptGL rs, Resources res) { - Bitmap b = loadBitmap(name, res); - if (b == null) { - return null; - } - return Allocation.createFromBitmap(rs, b, - Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, - Allocation.USAGE_GRAPHICS_TEXTURE); + return createFromBitmap(loadBitmap(name, res), rs, false); + } + + public static Allocation loadTexture2D(int id, RenderScriptGL rs, Resources res) { + return createFromBitmap(BitmapFactory.decodeResource(res, id), rs, false); } public static ProgramStore BLEND_ADD_DEPTH_NONE(RenderScript rs) { @@ -261,6 +270,7 @@ public class SceneManager extends SceneGraphBase { } mActiveScene.appendShader(getDefaultVS()); + mActiveScene.appendTransform(getDefaultTransform()); } static RenderScriptGL getRS() { @@ -364,14 +374,15 @@ public class SceneManager extends SceneGraphBase { final String code = "\n" + "varying vec2 varTex0;\n" + "void main() {\n" + - " lowp vec4 col = texture2D(UNI_Tex0, varTex0).rgba;\n" + + " lowp vec4 col = texture2D(UNI_color, varTex0).rgba;\n" + " gl_FragColor = col;\n" + "}\n"; FragmentShader.Builder fb = new FragmentShader.Builder(rs); fb.setShader(code); - fb.addTexture(Program.TextureType.TEXTURE_2D, "Tex0"); + fb.addTexture(Program.TextureType.TEXTURE_2D, "color"); sSceneManager.mTexture = fb.create(); + sSceneManager.mTexture.mProgram.bindSampler(Sampler.CLAMP_LINEAR_MIP_LINEAR(rs), 0); } return sSceneManager.mTexture; @@ -383,10 +394,22 @@ public class SceneManager extends SceneGraphBase { } if (sSceneManager.mDefaultState == null) { sSceneManager.mDefaultState = new RenderState(getDefaultVS(), getColorFS(), null, null); + sSceneManager.mDefaultState.setName("__DefaultState"); } return sSceneManager.mDefaultState; } + static Transform getDefaultTransform() { + if (sSceneManager == null) { + return null; + } + if (sSceneManager.mDefaultTransform == null) { + sSceneManager.mDefaultTransform = new MatrixTransform(); + sSceneManager.mDefaultTransform.setName("__DefaultTransform"); + } + return sSceneManager.mDefaultTransform; + } + public static SceneManager getInstance() { if (sSceneManager == null) { sSceneManager = new SceneManager(); @@ -444,6 +467,7 @@ public class SceneManager extends SceneGraphBase { mColor = null; mTexture = null; mDefaultState = null; + mDefaultTransform = null; mExportScript = new ScriptC_export(rs, res, R.raw.export); diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Texture2D.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Texture2D.java index 8fae9d9..b53ab88 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Texture2D.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Texture2D.java @@ -30,6 +30,7 @@ import android.util.Log; public class Texture2D extends TextureBase { String mFileName; String mFileDir; + int mResourceID; public Texture2D() { super(ScriptC_export.const_TextureType_TEXTURE_2D); @@ -40,6 +41,17 @@ public class Texture2D extends TextureBase { setTexture(tex); } + public Texture2D(String dir, String file) { + super(ScriptC_export.const_TextureType_TEXTURE_CUBE); + setFileDir(dir); + setFileName(file); + } + + public Texture2D(int resourceID) { + super(ScriptC_export.const_TextureType_TEXTURE_2D); + mResourceID = resourceID; + } + public void setFileDir(String dir) { mFileDir = dir; } @@ -62,8 +74,12 @@ public class Texture2D extends TextureBase { void load() { RenderScriptGL rs = SceneManager.getRS(); Resources res = SceneManager.getRes(); - String shortName = mFileName.substring(mFileName.lastIndexOf('/') + 1); - setTexture(SceneManager.loadTexture2D(mFileDir + shortName, rs, res)); + if (mFileName != null && mFileName.length() > 0) { + String shortName = mFileName.substring(mFileName.lastIndexOf('/') + 1); + setTexture(SceneManager.loadTexture2D(mFileDir + shortName, rs, res)); + } else if (mResourceID != 0) { + setTexture(SceneManager.loadTexture2D(mResourceID, rs, res)); + } } ScriptField_Texture_s getRsData(boolean loadNow) { diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureCube.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureCube.java index 12c81c2..1269e3c 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureCube.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureCube.java @@ -31,6 +31,7 @@ import android.util.Log; public class TextureCube extends TextureBase { String mFileName; String mFileDir; + int mResourceID; public TextureCube() { super(ScriptC_export.const_TextureType_TEXTURE_CUBE); @@ -47,6 +48,11 @@ public class TextureCube extends TextureBase { setFileName(file); } + public TextureCube(int resourceID) { + super(ScriptC_export.const_TextureType_TEXTURE_2D); + mResourceID = resourceID; + } + public void setFileDir(String dir) { mFileDir = dir; } @@ -69,8 +75,12 @@ public class TextureCube extends TextureBase { void load() { RenderScriptGL rs = SceneManager.getRS(); Resources res = SceneManager.getRes(); - String shortName = mFileName.substring(mFileName.lastIndexOf('/') + 1); - setTexture(SceneManager.loadCubemap(mFileDir + shortName, rs, res)); + if (mFileName != null && mFileName.length() > 0) { + String shortName = mFileName.substring(mFileName.lastIndexOf('/') + 1); + setTexture(SceneManager.loadCubemap(mFileDir + shortName, rs, res)); + } else if (mResourceID != 0) { + setTexture(SceneManager.loadCubemap(mResourceID , rs, res)); + } } ScriptField_Texture_s getRsData(boolean loadNow) { diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/testapp/SimpleAppRS.java b/tests/RenderScriptTests/SceneGraph/src/com/android/testapp/SimpleAppRS.java index 9388838..621bfa3 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/testapp/SimpleAppRS.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/testapp/SimpleAppRS.java @@ -38,12 +38,13 @@ public class SimpleAppRS { private static String TAG = "SimpleAppRS"; SceneManager mSceneManager; - ArrayList<Renderable> geometry = new ArrayList<Renderable>(); - Scene mScene; RenderScriptGL mRS; Resources mRes; + Scene mScene; + Mesh mSimpleMesh; + public void init(RenderScriptGL rs, Resources res, int width, int height) { mRS = rs; mRes = res; @@ -53,9 +54,9 @@ public class SimpleAppRS { mScene = new Scene(); setupGeometry(); + setupRenderables(); setupCamera(); setupRenderPass(); - setupShaders(); mSceneManager.setActiveScene(mScene); @@ -63,16 +64,9 @@ public class SimpleAppRS { mRS.bindRootScript(mSceneManager.getRenderLoop()); } - private void setupShaders() { - // Built-in shader that provides position, texcoord and normal - VertexShader genericV = SceneManager.getDefaultVS(); - // Built-in shader that displays a color - FragmentShader colorF = SceneManager.getColorFS(); - mScene.assignRenderState(new RenderState(genericV, colorF, null, null)); - } private void setupGeometry() { - Mesh.TriangleMeshBuilder tmb = new Mesh.TriangleMeshBuilder(mRS, - 3, Mesh.TriangleMeshBuilder.TEXTURE_0); + Mesh.TriangleMeshBuilder tmb = new Mesh.TriangleMeshBuilder(mRS, 3, + Mesh.TriangleMeshBuilder.TEXTURE_0); tmb.setTexture(0.0f, 1.0f).addVertex(-1.0f, 1.0f, 0.0f); tmb.setTexture(0.0f, 0.0f).addVertex(-1.0f, -1.0f, 0.0f); @@ -81,40 +75,57 @@ public class SimpleAppRS { tmb.addTriangle(0, 1, 2); tmb.addTriangle(2, 3, 0); + mSimpleMesh = tmb.create(true); + } - Mesh mesh = tmb.create(true); - - Renderable quad = new Renderable(); - quad.setMesh(mesh); - quad.setTransform(new CompoundTransform()); + private void setupRenderables() { + // Built-in shader that provides position, texcoord and normal + VertexShader genericV = SceneManager.getDefaultVS(); + // Built-in shader that displays a color + FragmentShader colorF = SceneManager.getColorFS(); + // Built-in shader that displays a texture + FragmentShader textureF = SceneManager.getTextureFS(); + RenderState colorRS = new RenderState(genericV, colorF, null, null); + ProgramStore alphaBlend = ProgramStore.BLEND_ALPHA_DEPTH_TEST(mRS); + RenderState texRS = new RenderState(genericV, textureF, alphaBlend, null); + + // Draw a simple colored quad + Renderable quad = mScene.appendNewRenderable(); + quad.setMesh(mSimpleMesh); quad.appendSourceParams(new Float4Param("color", 0.2f, 0.3f, 0.4f)); - - mScene.appendRenderable(quad); - geometry.add(quad); + quad.setRenderState(colorRS); + + // Draw a textured quad + quad = mScene.appendNewRenderable(); + quad.setMesh(mSimpleMesh); + // Make a transform to position the quad + CompoundTransform t = mScene.appendNewCompoundTransform(); + t.addTranslate("position", new Float3(2, 2, 0)); + quad.setTransform(t); + quad.appendSourceParams(new TextureParam("color", new Texture2D(R.drawable.icon))); + quad.setRenderState(texRS); } private void setupCamera() { - Camera camera = new Camera(); + Camera camera = mScene.appendNewCamera(); camera.setFar(200); camera.setNear(0.1f); camera.setFOV(60); - CompoundTransform cameraTransform = new CompoundTransform(); + CompoundTransform cameraTransform = mScene.appendNewCompoundTransform(); cameraTransform.addTranslate("camera", new Float3(0, 0, 10)); - mScene.appendTransform(cameraTransform); camera.setTransform(cameraTransform); - mScene.appendCamera(camera); } private void setupRenderPass() { - RenderPass mainPass = new RenderPass(); + RenderPass mainPass = mScene.appendNewRenderPass(); mainPass.setClearColor(new Float4(1.0f, 1.0f, 1.0f, 1.0f)); mainPass.setShouldClearColor(true); mainPass.setClearDepth(1.0f); mainPass.setShouldClearDepth(true); mainPass.setCamera(mScene.getCameras().get(0)); - for (Renderable renderable : geometry) { - mainPass.appendRenderable(renderable); + ArrayList<RenderableBase> allRender = mScene.getRenderables(); + for (RenderableBase renderable : allRender) { + mainPass.appendRenderable((Renderable)renderable); } - mScene.appendRenderPass(mainPass); } } diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/testapp/TouchHandler.java b/tests/RenderScriptTests/SceneGraph/src/com/android/testapp/TouchHandler.java index c182c29..e272cc5 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/testapp/TouchHandler.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/testapp/TouchHandler.java @@ -48,7 +48,7 @@ public class TouchHandler { mPosValue = new Float3(0, 4, 0); // Make a camera transform we can manipulate - mCameraRig = new CompoundTransform(); + mCameraRig = scene.appendNewCompoundTransform(); mCameraRig.setName("CameraRig"); mPosition = mCameraRig.addTranslate("Position", mPosValue); @@ -56,10 +56,8 @@ public class TouchHandler { mRotateX = mCameraRig.addRotate("RotateX", new Float3(1, 0, 0), mRotateXValue); mDist = mCameraRig.addTranslate("Distance", mDistValue); - scene.appendTransform(mCameraRig); - mCamera = new Camera(); + mCamera = scene.appendNewCamera(); mCamera.setTransform(mCameraRig); - scene.appendCamera(mCamera); } public Camera getCamera() { |