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