1/*
2 * Copyright (C) 2008 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.modelviewer;
18
19import java.io.Writer;
20import java.util.Map;
21import java.util.Vector;
22
23import android.content.res.Resources;
24import android.renderscript.*;
25import android.renderscript.Element.Builder;
26import android.renderscript.Font.Style;
27import android.renderscript.ProgramStore.DepthFunc;
28import android.util.Log;
29
30
31public class SceneGraphRS {
32
33    private final int STATE_LAST_FOCUS = 1;
34
35    int mWidth;
36    int mHeight;
37    int mRotation;
38
39    public SceneGraphRS() {
40    }
41
42    public void init(RenderScriptGL rs, Resources res, int width, int height) {
43        mRS = rs;
44        mRes = res;
45        mWidth = width;
46        mHeight = height;
47        mRotation = 0;
48        initRS();
49    }
50
51    private Resources mRes;
52    private RenderScriptGL mRS;
53    private Sampler mSampler;
54    private ProgramStore mPSBackground;
55    private ProgramFragment mPFBackground;
56    private ProgramVertex mPVBackground;
57    private ProgramVertexFixedFunction.Constants mPVA;
58
59    private Allocation mGridImage;
60    private Allocation mAllocPV;
61
62    private Mesh mMesh;
63
64    private Font mItalic;
65    private Allocation mTextAlloc;
66
67    private ScriptC_scenegraph mScript;
68    private ScriptC_transform mTransformScript;
69
70    int mLastX;
71    int mLastY;
72
73    public void touchEvent(int x, int y) {
74        int dx = mLastX - x;
75        if (Math.abs(dx) > 50 || Math.abs(dx) < 3) {
76            dx = 0;
77        }
78
79        mRotation -= dx;
80        if (mRotation > 360) {
81            mRotation -= 360;
82        }
83        if (mRotation < 0) {
84            mRotation += 360;
85        }
86
87        mScript.set_gRotate(-(float)mRotation);
88
89        mLastX = x;
90        mLastY = y;
91    }
92
93    private void initPFS() {
94        ProgramStore.Builder b = new ProgramStore.Builder(mRS);
95
96        b.setDepthFunc(ProgramStore.DepthFunc.LESS);
97        b.setDitherEnabled(false);
98        b.setDepthMaskEnabled(true);
99        mPSBackground = b.create();
100
101        mScript.set_gPFSBackground(mPSBackground);
102    }
103
104    private void initPF() {
105        Sampler.Builder bs = new Sampler.Builder(mRS);
106        bs.setMinification(Sampler.Value.LINEAR);
107        bs.setMagnification(Sampler.Value.LINEAR);
108        bs.setWrapS(Sampler.Value.CLAMP);
109        bs.setWrapT(Sampler.Value.CLAMP);
110        mSampler = bs.create();
111
112        ProgramFragmentFixedFunction.Builder b = new ProgramFragmentFixedFunction.Builder(mRS);
113        b.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
114                     ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
115        mPFBackground = b.create();
116        mPFBackground.bindSampler(mSampler, 0);
117
118        mScript.set_gPFBackground(mPFBackground);
119    }
120
121    private void initPV() {
122        ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
123        mPVBackground = pvb.create();
124
125        mPVA = new ProgramVertexFixedFunction.Constants(mRS);
126        ((ProgramVertexFixedFunction)mPVBackground).bindConstants(mPVA);
127
128        mScript.set_gPVBackground(mPVBackground);
129    }
130
131    private void loadImage() {
132        mGridImage = Allocation.createFromBitmapResource(mRS, mRes, R.drawable.robot,
133                                                         Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
134                                                         Allocation.USAGE_GRAPHICS_TEXTURE);
135        mScript.set_gTGrid(mGridImage);
136    }
137
138    private void initTextAllocation() {
139        String allocString = "Displaying file: R.raw.robot";
140        mTextAlloc = Allocation.createFromString(mRS, allocString, Allocation.USAGE_SCRIPT);
141        mScript.set_gTextAlloc(mTextAlloc);
142    }
143
144    SgTransform mRootTransform;
145    SgTransform mGroup1;
146
147    SgTransform mRobot1;
148    SgTransform mRobot2;
149
150    void initTransformHierarchy() {
151        mRootTransform = new SgTransform(mRS);
152
153        mGroup1 = new SgTransform(mRS);
154        mRootTransform.addChild(mGroup1);
155
156        mRobot1 = new SgTransform(mRS);
157        mRobot2 = new SgTransform(mRS);
158
159        mGroup1.addChild(mRobot1);
160        mGroup1.addChild(mRobot2);
161
162        mGroup1.setTransform(0, new Float4(0.0f, 0.0f, -15.0f, 0.0f), TransformType.TRANSLATE);
163        mGroup1.setTransform(1, new Float4(0.0f, 1.0f, 0.0f, 15.0f), TransformType.ROTATE);
164
165        mRobot1.setTransform(0, new Float4(-3.0f, -0.5f, 0.0f, 0.0f), TransformType.TRANSLATE);
166        mRobot1.setTransform(1, new Float4(0.0f, 1.0f, 0.0f, 20.0f), TransformType.ROTATE);
167        mRobot1.setTransform(2, new Float4(0.2f, 0.2f, 0.2f, 0.0f), TransformType.SCALE);
168
169        mRobot2.setTransform(0, new Float4(3.0f, 0.0f, 0.0f, 0.0f), TransformType.TRANSLATE);
170        mRobot2.setTransform(1, new Float4(0.0f, 1.0f, 0.0f, -20.0f), TransformType.ROTATE);
171        mRobot2.setTransform(2, new Float4(0.3f, 0.3f, 0.3f, 0.0f), TransformType.SCALE);
172    }
173
174    private void initRS() {
175
176        mScript = new ScriptC_scenegraph(mRS, mRes, R.raw.scenegraph);
177        mTransformScript = new ScriptC_transform(mRS, mRes, R.raw.transform);
178        mTransformScript.set_transformScript(mTransformScript);
179
180        mScript.set_gTransformRS(mTransformScript);
181
182        initPFS();
183        initPF();
184        initPV();
185
186        loadImage();
187
188        FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.robot);
189        FileA3D.IndexEntry entry = model.getIndexEntry(0);
190        if (entry == null || entry.getEntryType() != FileA3D.EntryType.MESH) {
191            Log.e("rs", "could not load model");
192        } else {
193            mMesh = (Mesh)entry.getObject();
194            mScript.set_gTestMesh(mMesh);
195        }
196
197        mItalic = Font.create(mRS, mRes, "serif", Font.Style.ITALIC, 8);
198        mScript.set_gItalic(mItalic);
199
200        initTextAllocation();
201
202        initTransformHierarchy();
203
204        mScript.bind_gRootNode(mRootTransform.getField());
205
206        mScript.bind_gGroup(mGroup1.mParent.mChildField);
207        mScript.bind_gRobot1(mRobot1.mParent.mChildField);
208        mScript.set_gRobot1Index(mRobot1.mIndexInParentGroup);
209        mScript.bind_gRobot2(mRobot2.mParent.mChildField);
210        mScript.set_gRobot2Index(mRobot2.mIndexInParentGroup);
211
212        mRS.bindRootScript(mScript);
213    }
214}
215
216
217
218