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 17package com.android.modelviewer; 18 19import java.io.Writer; 20 21import android.content.res.Resources; 22import android.renderscript.*; 23import android.renderscript.ProgramStore.DepthFunc; 24import android.util.Log; 25 26 27public class SimpleModelRS { 28 29 public SimpleModelRS() { 30 } 31 32 public void init(RenderScriptGL rs, Resources res) { 33 mRS = rs; 34 mRes = res; 35 initRS(); 36 } 37 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 61 public void onActionDown(float x, float y) { 62 mScript.invoke_onActionDown(x, y); 63 } 64 65 public void onActionScale(float scale) { 66 mScript.invoke_onActionScale(scale); 67 } 68 69 public void onActionMove(float x, float y) { 70 mScript.invoke_onActionMove(x, y); 71 } 72 73 public void onPostureChanged(Matrix4f posture) { 74 mScript.set_gPostureMatrix(posture); 75 } 76 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 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 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 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 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 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 158 public void loadA3DFile(String path) { 159 FileA3D model = FileA3D.createFromFile(mRS, path); 160 initMeshes(model); 161 initTextAllocation(path); 162 } 163 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