SimpleAppRS.java revision d2dc9838961f945c402e5d810bc715196240be6f
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.testapp;
18
19import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.Map;
22import java.util.Vector;
23
24import com.android.scenegraph.*;
25import com.android.scenegraph.SceneManager.SceneLoadedCallback;
26
27import android.content.res.Resources;
28import android.graphics.Bitmap;
29import android.graphics.BitmapFactory;
30import android.os.AsyncTask;
31import android.renderscript.*;
32import android.renderscript.Program.TextureType;
33import android.util.Log;
34
35// This is where the scenegraph and the rendered objects are initialized and used
36public class SimpleAppRS {
37
38    private static String TAG = "SimpleAppRS";
39
40    SceneManager mSceneManager;
41
42    RenderScriptGL mRS;
43    Resources mRes;
44
45    Scene mScene;
46    Mesh mSimpleMesh;
47
48    public void init(RenderScriptGL rs, Resources res, int width, int height) {
49        mRS = rs;
50        mRes = res;
51        mSceneManager = SceneManager.getInstance();
52        mSceneManager.initRS(mRS, mRes, width, height);
53
54        mScene = new Scene();
55
56        setupGeometry();
57        setupRenderables();
58        setupCamera();
59        setupRenderPass();
60
61        mSceneManager.setActiveScene(mScene);
62
63        mScene.initRS();
64        mRS.bindRootScript(mSceneManager.getRenderLoop());
65    }
66
67    private void setupGeometry() {
68        Mesh.TriangleMeshBuilder tmb = new Mesh.TriangleMeshBuilder(mRS, 3,
69                                                         Mesh.TriangleMeshBuilder.TEXTURE_0);
70
71        tmb.setTexture(0.0f, 1.0f).addVertex(-1.0f, 1.0f, 0.0f);
72        tmb.setTexture(0.0f, 0.0f).addVertex(-1.0f, -1.0f, 0.0f);
73        tmb.setTexture(1.0f, 0.0f).addVertex(1.0f, -1.0f, 0.0f);
74        tmb.setTexture(1.0f, 1.0f).addVertex(1.0f, 1.0f, 0.0f);
75
76        tmb.addTriangle(0, 1, 2);
77        tmb.addTriangle(2, 3, 0);
78        mSimpleMesh = tmb.create(true);
79    }
80
81    private void setupRenderables() {
82        // Built-in shader that provides position, texcoord and normal
83        VertexShader genericV = SceneManager.getDefaultVS();
84        // Built-in shader that displays a color
85        FragmentShader colorF = SceneManager.getColorFS();
86        // Built-in shader that displays a texture
87        FragmentShader textureF = SceneManager.getTextureFS();
88        RenderState colorRS = new RenderState(genericV, colorF, null, null);
89        ProgramStore alphaBlend = ProgramStore.BLEND_ALPHA_DEPTH_TEST(mRS);
90        RenderState texRS = new RenderState(genericV, textureF, alphaBlend, null);
91
92        // Draw a simple colored quad
93        Renderable quad = mScene.appendNewRenderable();
94        quad.setMesh(mSimpleMesh);
95        quad.appendSourceParams(new Float4Param("color", 0.2f, 0.3f, 0.4f));
96        quad.setRenderState(colorRS);
97
98        // Draw a textured quad
99        quad = mScene.appendNewRenderable();
100        quad.setMesh(mSimpleMesh);
101        // Make a transform to position the quad
102        CompoundTransform t = mScene.appendNewCompoundTransform();
103        t.addTranslate("position", new Float3(2, 2, 0));
104        quad.setTransform(t);
105        quad.appendSourceParams(new TextureParam("color", new Texture2D(R.drawable.icon)));
106        quad.setRenderState(texRS);
107    }
108
109    private void setupCamera() {
110        Camera camera = mScene.appendNewCamera();
111        camera.setFar(200);
112        camera.setNear(0.1f);
113        camera.setFOV(60);
114        CompoundTransform cameraTransform = mScene.appendNewCompoundTransform();
115        cameraTransform.addTranslate("camera", new Float3(0, 0, 10));
116        camera.setTransform(cameraTransform);
117    }
118
119    private void setupRenderPass() {
120        RenderPass mainPass = mScene.appendNewRenderPass();
121        mainPass.setClearColor(new Float4(1.0f, 1.0f, 1.0f, 1.0f));
122        mainPass.setShouldClearColor(true);
123        mainPass.setClearDepth(1.0f);
124        mainPass.setShouldClearDepth(true);
125        mainPass.setCamera(mScene.getCameras().get(0));
126        ArrayList<RenderableBase> allRender = mScene.getRenderables();
127        for (RenderableBase renderable : allRender) {
128            mainPass.appendRenderable((Renderable)renderable);
129        }
130    }
131}
132