Adding a very simple all-code scenegraph example.

Change-Id: I776f8d8d0d3873c84141637d3479501ea92bac12
This commit is contained in:
Alex Sakhartchouk
2012-02-21 16:26:38 -08:00
parent c71343acc4
commit 57fc7109e4
10 changed files with 297 additions and 44 deletions

View File

@@ -11,6 +11,13 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="SimpleApp"
android:label="SimpleSceneGraph">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="FileSelector"
android:label="FileSelector"
android:hardwareAccelerated="true">

View File

@@ -248,17 +248,17 @@ public class ColladaParser {
String description = field.getAttribute("sid");
if (fieldName.equals("translate")) {
Float3 value = getFloat3(field);
current.addComponent(new TranslateComponent(description, value));
current.addTranslate(description, value);
//Log.v(TAG, indent + " translate " + description + toString(value));
} else if (fieldName.equals("rotate")) {
Float4 value = getFloat4(field);
//Log.v(TAG, indent + " rotate " + description + toString(value));
Float3 axis = new Float3(value.x, value.y, value.z);
current.addComponent(new RotateComponent(description, axis, value.w));
current.addRotate(description, axis, value.w);
} else if (fieldName.equals("scale")) {
Float3 value = getFloat3(field);
//Log.v(TAG, indent + " scale " + description + toString(value));
current.addComponent(new ScaleComponent(description, value));
current.addScale(description, value);
} else if (fieldName.equals("instance_geometry")) {
getRenderable(field, current);
} else if (fieldName.equals("instance_light")) {

View File

@@ -134,6 +134,24 @@ public class CompoundTransform extends Transform {
mTransformComponents = new ArrayList<Component>();
}
public TranslateComponent addTranslate(String name, Float3 translate) {
TranslateComponent c = new TranslateComponent(name, translate);
addComponent(c);
return c;
}
public RotateComponent addRotate(String name, Float3 axis, float angle) {
RotateComponent c = new RotateComponent(name, axis, angle);
addComponent(c);
return c;
}
public ScaleComponent addScale(String name, Float3 scale) {
ScaleComponent c = new ScaleComponent(name, scale);
addComponent(c);
return c;
}
public void addComponent(Component c) {
if (c.mParent != null) {
throw new IllegalArgumentException("Transform components may not be shared");

View File

@@ -248,24 +248,29 @@ public class Scene extends SceneGraphBase {
}
private void addShaders(RenderScriptGL rs, Resources res, SceneManager sceneManager) {
Allocation shaderData = Allocation.createSized(rs, Element.ALLOCATION(rs),
mVertexShaders.size());
Allocation[] shaderAllocs = new Allocation[mVertexShaders.size()];
for (int i = 0; i < mVertexShaders.size(); i ++) {
VertexShader sI = mVertexShaders.get(i);
shaderAllocs[i] = sI.getRSData().getAllocation();
if (mVertexShaders.size() > 0) {
Allocation shaderData = Allocation.createSized(rs, Element.ALLOCATION(rs),
mVertexShaders.size());
Allocation[] shaderAllocs = new Allocation[mVertexShaders.size()];
for (int i = 0; i < mVertexShaders.size(); i ++) {
VertexShader sI = mVertexShaders.get(i);
shaderAllocs[i] = sI.getRSData().getAllocation();
}
shaderData.copyFrom(shaderAllocs);
sceneManager.mRenderLoop.set_gVertexShaders(shaderData);
}
shaderData.copyFrom(shaderAllocs);
sceneManager.mRenderLoop.set_gVertexShaders(shaderData);
shaderData = Allocation.createSized(rs, Element.ALLOCATION(rs), mFragmentShaders.size());
shaderAllocs = new Allocation[mFragmentShaders.size()];
for (int i = 0; i < mFragmentShaders.size(); i ++) {
FragmentShader sI = mFragmentShaders.get(i);
shaderAllocs[i] = sI.getRSData().getAllocation();
if (mFragmentShaders.size() > 0) {
Allocation shaderData = Allocation.createSized(rs, Element.ALLOCATION(rs),
mFragmentShaders.size());
Allocation[] shaderAllocs = new Allocation[mFragmentShaders.size()];
for (int i = 0; i < mFragmentShaders.size(); i ++) {
FragmentShader sI = mFragmentShaders.get(i);
shaderAllocs[i] = sI.getRSData().getAllocation();
}
shaderData.copyFrom(shaderAllocs);
sceneManager.mRenderLoop.set_gFragmentShaders(shaderData);
}
shaderData.copyFrom(shaderAllocs);
sceneManager.mRenderLoop.set_gFragmentShaders(shaderData);
}
public void initRS() {

View File

@@ -342,7 +342,7 @@ 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 = UNI_color;\n" +
" gl_FragColor = col;\n" +
"}\n";
FragmentShader.Builder fb = new FragmentShader.Builder(rs);
@@ -360,12 +360,14 @@ public class SceneManager extends SceneGraphBase {
}
if (sSceneManager.mTexture == null) {
RenderScriptGL rs = getRS();
final String code = "\n" +
"varying vec2 varTex0;\n" +
"void main() {\n" +
" lowp vec4 col = UNI_color;\n" +
" lowp vec4 col = texture2D(UNI_Tex0, 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");
@@ -408,17 +410,10 @@ public class SceneManager extends SceneGraphBase {
Mesh.TriangleMeshBuilder tmb = new Mesh.TriangleMeshBuilder(mRS,
3, Mesh.TriangleMeshBuilder.TEXTURE_0);
tmb.setTexture(0.0f, 1.0f);
tmb.addVertex(-1.0f, 1.0f, 1.0f);
tmb.setTexture(0.0f, 0.0f);
tmb.addVertex(-1.0f, -1.0f, 1.0f);
tmb.setTexture(1.0f, 0.0f);
tmb.addVertex(1.0f, -1.0f, 1.0f);
tmb.setTexture(1.0f, 1.0f);
tmb.addVertex(1.0f, 1.0f, 1.0f);
tmb.setTexture(0.0f, 1.0f).addVertex(-1.0f, 1.0f, 1.0f);
tmb.setTexture(0.0f, 0.0f).addVertex(-1.0f, -1.0f, 1.0f);
tmb.setTexture(1.0f, 0.0f).addVertex(1.0f, -1.0f, 1.0f);
tmb.setTexture(1.0f, 1.0f).addVertex(1.0f, 1.0f, 1.0f);
tmb.addTriangle(0, 1, 2);
tmb.addTriangle(2, 3, 0);

View File

@@ -142,10 +142,14 @@ static void drawAllObjects(rs_allocation allObj) {
return;
}
rsForEach(gVertexParamsScript, nullAlloc, gVertexShaders,
gActiveCamera, sizeof(gActiveCamera));
rsForEach(gFragmentParamsScript, nullAlloc, gFragmentShaders,
gActiveCamera, sizeof(gActiveCamera));
if (rsIsObject(gVertexShaders)) {
rsForEach(gVertexParamsScript, nullAlloc, gVertexShaders,
gActiveCamera, sizeof(gActiveCamera));
}
if (rsIsObject(gFragmentShaders)) {
rsForEach(gFragmentParamsScript, nullAlloc, gFragmentShaders,
gActiveCamera, sizeof(gActiveCamera));
}
// Run the params and cull script
rsForEach(gCullScript, nullAlloc, allObj, gActiveCamera, sizeof(gActiveCamera));

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2012 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.testapp;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScript;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.Window;
import android.net.Uri;
import java.lang.Runtime;
public class SimpleApp extends Activity {
private SimpleAppView mView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create our Preview view and set it as the content of our
// Activity
mView = new SimpleAppView(this);
setContentView(mView);
}
}

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2012 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.testapp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import com.android.scenegraph.*;
import com.android.scenegraph.SceneManager.SceneLoadedCallback;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.renderscript.*;
import android.renderscript.Program.TextureType;
import android.util.Log;
// This is where the scenegraph and the rendered objects are initialized and used
public class SimpleAppRS {
private static String TAG = "SimpleAppRS";
SceneManager mSceneManager;
ArrayList<Renderable> geometry = new ArrayList<Renderable>();
Scene mScene;
RenderScriptGL mRS;
Resources mRes;
public void init(RenderScriptGL rs, Resources res, int width, int height) {
mRS = rs;
mRes = res;
mSceneManager = SceneManager.getInstance();
mSceneManager.initRS(mRS, mRes, width, height);
mScene = new Scene();
setupGeometry();
setupCamera();
setupRenderPass();
setupShaders();
mSceneManager.setActiveScene(mScene);
mScene.initRS();
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);
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);
tmb.setTexture(1.0f, 0.0f).addVertex(1.0f, -1.0f, 0.0f);
tmb.setTexture(1.0f, 1.0f).addVertex(1.0f, 1.0f, 0.0f);
tmb.addTriangle(0, 1, 2);
tmb.addTriangle(2, 3, 0);
Mesh mesh = tmb.create(true);
Renderable quad = new Renderable();
quad.setMesh(mesh);
quad.setTransform(new CompoundTransform());
quad.appendSourceParams(new Float4Param("color", 0.2f, 0.3f, 0.4f));
mScene.appendRenderable(quad);
geometry.add(quad);
}
private void setupCamera() {
Camera camera = new Camera();
camera.setFar(200);
camera.setNear(0.1f);
camera.setFOV(60);
CompoundTransform cameraTransform = new CompoundTransform();
cameraTransform.addTranslate("camera", new Float3(0, 0, 10));
mScene.appendTransform(cameraTransform);
camera.setTransform(cameraTransform);
mScene.appendCamera(camera);
}
private void setupRenderPass() {
RenderPass mainPass = new RenderPass();
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);
}
mScene.appendRenderPass(mainPass);
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2012 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.testapp;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScript;
import android.renderscript.RenderScriptGL;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SimpleAppView extends RSSurfaceView {
public SimpleAppView(Context context) {
super(context);
}
private RenderScriptGL mRS;
SimpleAppRS mRender;
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
super.surfaceChanged(holder, format, w, h);
if (mRS == null) {
RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
sc.setDepth(16, 24);
mRS = createRenderScriptGL(sc);
mRS.setSurface(holder, w, h);
mRender = new SimpleAppRS();
mRender.init(mRS, getResources(), w, h);
}
}
@Override
protected void onDetachedFromWindow() {
if (mRS != null) {
mRender = null;
mRS = null;
destroyRenderScriptGL();
}
}
}

View File

@@ -47,18 +47,15 @@ public class TouchHandler {
mDistValue = new Float3(0, 0, 45);
mPosValue = new Float3(0, 4, 0);
mRotateX = new RotateComponent("RotateX", new Float3(1, 0, 0), mRotateXValue);
mRotateY = new RotateComponent("RotateY", new Float3(0, 1, 0), mRotateYValue);
mDist = new TranslateComponent("Distance", mDistValue);
mPosition = new TranslateComponent("Distance", mPosValue);
// Make a camera transform we can manipulate
mCameraRig = new CompoundTransform();
mCameraRig.setName("CameraRig");
mCameraRig.addComponent(mPosition);
mCameraRig.addComponent(mRotateY);
mCameraRig.addComponent(mRotateX);
mCameraRig.addComponent(mDist);
mPosition = mCameraRig.addTranslate("Position", mPosValue);
mRotateY = mCameraRig.addRotate("RotateY", new Float3(0, 1, 0), mRotateYValue);
mRotateX = mCameraRig.addRotate("RotateX", new Float3(1, 0, 0), mRotateXValue);
mDist = mCameraRig.addTranslate("Distance", mDistValue);
scene.appendTransform(mCameraRig);
mCamera = new Camera();
mCamera.setTransform(mCameraRig);