1 /*
2  * Copyright (C) 2011 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 
17 package com.android.modelviewer;
18 
19 import java.io.Writer;
20 
21 import android.content.res.Resources;
22 import android.renderscript.*;
23 import android.renderscript.ProgramStore.DepthFunc;
24 import android.util.Log;
25 
26 
27 public class SimpleModelRS {
28 
SimpleModelRS()29     public SimpleModelRS() {
30     }
31 
init(RenderScriptGL rs, Resources res)32     public void init(RenderScriptGL rs, Resources res) {
33         mRS = rs;
34         mRes = res;
35         initRS();
36     }
37 
surfaceChanged()38     public void surfaceChanged() {
39         mRS.getWidth();
40         mRS.getHeight();
41     }
42 
43     private Resources mRes;
44     private RenderScriptGL mRS;
45     private Sampler mSampler;
46     private ProgramStore mPSBackground;
47     private ProgramFragment mPFBackground;
48     private ProgramVertex mPVBackground;
49     private ProgramVertexFixedFunction.Constants mPVA;
50 
51     private Allocation mGridImage;
52     private Allocation mAllocPV;
53 
54     private Font mItalic;
55     private Allocation mTextAlloc;
56 
57     private ScriptField_MeshInfo mMeshes;
58     private ScriptC_simplemodel mScript;
59 
60 
onActionDown(float x, float y)61     public void onActionDown(float x, float y) {
62         mScript.invoke_onActionDown(x, y);
63     }
64 
onActionScale(float scale)65     public void onActionScale(float scale) {
66         mScript.invoke_onActionScale(scale);
67     }
68 
onActionMove(float x, float y)69     public void onActionMove(float x, float y) {
70         mScript.invoke_onActionMove(x, y);
71     }
72 
onPostureChanged(Matrix4f posture)73     public void onPostureChanged(Matrix4f posture) {
74         mScript.set_gPostureMatrix(posture);
75     }
76 
initPFS()77     private void initPFS() {
78         ProgramStore.Builder b = new ProgramStore.Builder(mRS);
79 
80         b.setDepthFunc(ProgramStore.DepthFunc.LESS);
81         b.setDitherEnabled(false);
82         b.setDepthMaskEnabled(true);
83         mPSBackground = b.create();
84 
85         mScript.set_gPFSBackground(mPSBackground);
86     }
87 
initPF()88     private void initPF() {
89         Sampler.Builder bs = new Sampler.Builder(mRS);
90         bs.setMinification(Sampler.Value.LINEAR);
91         bs.setMagnification(Sampler.Value.LINEAR);
92         bs.setWrapS(Sampler.Value.CLAMP);
93         bs.setWrapT(Sampler.Value.CLAMP);
94         mSampler = bs.create();
95 
96         ProgramFragmentFixedFunction.Builder b = new ProgramFragmentFixedFunction.Builder(mRS);
97         b.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
98                      ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
99         mPFBackground = b.create();
100         mPFBackground.bindSampler(mSampler, 0);
101 
102         mScript.set_gPFBackground(mPFBackground);
103     }
104 
initPV()105     private void initPV() {
106         ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
107         mPVBackground = pvb.create();
108 
109         mPVA = new ProgramVertexFixedFunction.Constants(mRS);
110         ((ProgramVertexFixedFunction)mPVBackground).bindConstants(mPVA);
111 
112         mScript.set_gPVBackground(mPVBackground);
113     }
114 
loadImage()115     private void loadImage() {
116         mGridImage = Allocation.createFromBitmapResource(mRS, mRes, R.drawable.robot,
117                                                          Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
118                                                          Allocation.USAGE_GRAPHICS_TEXTURE);
119         mScript.set_gTGrid(mGridImage);
120     }
121 
initTextAllocation(String fileName)122     private void initTextAllocation(String fileName) {
123         String allocString = "Displaying file: " + fileName;
124         mTextAlloc = Allocation.createFromString(mRS, allocString, Allocation.USAGE_SCRIPT);
125         mScript.set_gTextAlloc(mTextAlloc);
126     }
127 
initMeshes(FileA3D model)128     private void initMeshes(FileA3D model) {
129         int numEntries = model.getIndexEntryCount();
130         int numMeshes = 0;
131         for (int i = 0; i < numEntries; i ++) {
132             FileA3D.IndexEntry entry = model.getIndexEntry(i);
133             if (entry != null && entry.getEntryType() == FileA3D.EntryType.MESH) {
134                 numMeshes ++;
135             }
136         }
137 
138         if (numMeshes > 0) {
139             mMeshes = new ScriptField_MeshInfo(mRS, numMeshes);
140 
141             for (int i = 0; i < numEntries; i ++) {
142                 FileA3D.IndexEntry entry = model.getIndexEntry(i);
143                 if (entry != null && entry.getEntryType() == FileA3D.EntryType.MESH) {
144                     Mesh mesh = entry.getMesh();
145                     mMeshes.set_mMesh(i, mesh, false);
146                     mMeshes.set_mNumIndexSets(i, mesh.getPrimitiveCount(), false);
147                 }
148             }
149             mMeshes.copyAll();
150         } else {
151             throw new RSRuntimeException("No valid meshes in file");
152         }
153 
154         mScript.bind_gMeshes(mMeshes);
155         mScript.invoke_updateMeshInfo();
156     }
157 
loadA3DFile(String path)158     public void loadA3DFile(String path) {
159         FileA3D model = FileA3D.createFromFile(mRS, path);
160         initMeshes(model);
161         initTextAllocation(path);
162     }
163 
initRS()164     private void initRS() {
165 
166         mScript = new ScriptC_simplemodel(mRS, mRes, R.raw.simplemodel);
167 
168         initPFS();
169         initPF();
170         initPV();
171 
172         loadImage();
173 
174         FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.robot);
175         initMeshes(model);
176 
177         mItalic = Font.create(mRS, mRes, "serif", Font.Style.ITALIC, 8);
178         mScript.set_gItalic(mItalic);
179 
180         initTextAllocation("R.raw.robot");
181 
182         mRS.bindRootScript(mScript);
183     }
184 }
185 
186 
187 
188