Home | History | Annotate | Download | only in balls
      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 
     17 package com.example.android.rs.balls;
     18 
     19 import android.content.res.Resources;
     20 import android.renderscript.*;
     21 import android.util.Log;
     22 
     23 public class BallsRS {
     24     public static final int PART_COUNT = 900;
     25 
     26     public BallsRS() {
     27     }
     28 
     29     private Resources mRes;
     30     private RenderScriptGL mRS;
     31     private ScriptC_balls mScript;
     32     private ScriptC_ball_physics mPhysicsScript;
     33     private ProgramFragment mPFLines;
     34     private ProgramFragment mPFPoints;
     35     private ProgramVertex mPV;
     36     private ScriptField_Point mPoints;
     37     private ScriptField_VpConsts mVpConsts;
     38 
     39     void updateProjectionMatrices() {
     40         mVpConsts = new ScriptField_VpConsts(mRS, 1,
     41                                              Allocation.USAGE_SCRIPT |
     42                                              Allocation.USAGE_GRAPHICS_CONSTANTS);
     43         ScriptField_VpConsts.Item i = new ScriptField_VpConsts.Item();
     44         Matrix4f mvp = new Matrix4f();
     45         mvp.loadOrtho(0, mRS.getWidth(), mRS.getHeight(), 0, -1, 1);
     46         i.MVP = mvp;
     47         mVpConsts.set(i, 0, true);
     48     }
     49 
     50     private void createProgramVertex() {
     51         updateProjectionMatrices();
     52 
     53         ProgramVertex.Builder sb = new ProgramVertex.Builder(mRS);
     54         String t =  "varying vec4 varColor;\n" +
     55                     "void main() {\n" +
     56                     "  vec4 pos = vec4(0.0, 0.0, 0.0, 1.0);\n" +
     57                     "  pos.xy = ATTRIB_position;\n" +
     58                     "  gl_Position = UNI_MVP * pos;\n" +
     59                     "  varColor = vec4(1.0, 1.0, 1.0, 1.0);\n" +
     60                     "  gl_PointSize = ATTRIB_size;\n" +
     61                     "}\n";
     62         sb.setShader(t);
     63         sb.addConstant(mVpConsts.getType());
     64         sb.addInput(mPoints.getElement());
     65         ProgramVertex pvs = sb.create();
     66         pvs.bindConstants(mVpConsts.getAllocation(), 0);
     67         mRS.bindProgramVertex(pvs);
     68     }
     69 
     70     private Allocation loadTexture(int id) {
     71         final Allocation allocation =
     72             Allocation.createFromBitmapResource(mRS, mRes,
     73                 id, Allocation.MipmapControl.MIPMAP_NONE,
     74                 Allocation.USAGE_GRAPHICS_TEXTURE);
     75         return allocation;
     76     }
     77 
     78     ProgramStore BLEND_ADD_DEPTH_NONE(RenderScript rs) {
     79         ProgramStore.Builder builder = new ProgramStore.Builder(rs);
     80         builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
     81         builder.setBlendFunc(ProgramStore.BlendSrcFunc.ONE, ProgramStore.BlendDstFunc.ONE);
     82         builder.setDitherEnabled(false);
     83         builder.setDepthMaskEnabled(false);
     84         return builder.create();
     85     }
     86 
     87     public void init(RenderScriptGL rs, Resources res, int width, int height) {
     88         mRS = rs;
     89         mRes = res;
     90 
     91         ProgramFragmentFixedFunction.Builder pfb = new ProgramFragmentFixedFunction.Builder(rs);
     92         pfb.setPointSpriteTexCoordinateReplacement(true);
     93         pfb.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.MODULATE,
     94                            ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
     95         pfb.setVaryingColor(true);
     96         mPFPoints = pfb.create();
     97 
     98         pfb = new ProgramFragmentFixedFunction.Builder(rs);
     99         pfb.setVaryingColor(true);
    100         mPFLines = pfb.create();
    101 
    102         android.util.Log.e("rs", "Load texture");
    103         mPFPoints.bindTexture(loadTexture(R.drawable.flares), 0);
    104 
    105         mPoints = new ScriptField_Point(mRS, PART_COUNT, Allocation.USAGE_SCRIPT);
    106 
    107         Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
    108         smb.addVertexAllocation(mPoints.getAllocation());
    109         smb.addIndexSetType(Mesh.Primitive.POINT);
    110         Mesh smP = smb.create();
    111 
    112         mPhysicsScript = new ScriptC_ball_physics(mRS, mRes, R.raw.ball_physics);
    113 
    114         mScript = new ScriptC_balls(mRS, mRes, R.raw.balls);
    115         mScript.set_partMesh(smP);
    116         mScript.set_physics_script(mPhysicsScript);
    117         mScript.bind_point(mPoints);
    118         mScript.bind_balls1(new ScriptField_Ball(mRS, PART_COUNT, Allocation.USAGE_SCRIPT));
    119         mScript.bind_balls2(new ScriptField_Ball(mRS, PART_COUNT, Allocation.USAGE_SCRIPT));
    120 
    121         mScript.set_gPFLines(mPFLines);
    122         mScript.set_gPFPoints(mPFPoints);
    123         createProgramVertex();
    124 
    125         mRS.bindProgramStore(BLEND_ADD_DEPTH_NONE(mRS));
    126 
    127         mPhysicsScript.set_gMinPos(new Float2(5, 5));
    128         mPhysicsScript.set_gMaxPos(new Float2(width - 5, height - 5));
    129 
    130         mScript.invoke_initParts(width, height);
    131 
    132         mRS.bindRootScript(mScript);
    133     }
    134 
    135     public void newTouchPosition(float x, float y, float pressure, int id) {
    136         mPhysicsScript.invoke_touch(x, y, pressure, id);
    137     }
    138 
    139     public void setAccel(float x, float y) {
    140         mPhysicsScript.set_gGravityVector(new Float2(x, y));
    141     }
    142 
    143 }
    144