FileDocCategorySizeDatePackage
BallsRS.javaAPI DocAndroid 5.1 API5576Thu Mar 12 22:22:54 GMT 2015com.example.android.rs.balls

BallsRS

public class BallsRS extends Object

Fields Summary
public static final int
PART_COUNT
private android.content.res.Resources
mRes
private RenderScriptGL
mRS
private ScriptC_balls
mScript
private ScriptC_ball_physics
mPhysicsScript
private ProgramFragment
mPFPoints
private ScriptField_Point
mPoints
private ScriptField_VpConsts
mVpConsts
private ScriptField_BallGrid
mGrid
private ScriptField_Ball
mBalls
private Allocation
mGridCache
Constructors Summary
public BallsRS()


      
    
Methods Summary
ProgramStoreBLEND_ADD_DEPTH_NONE(RenderScript rs)

        ProgramStore.Builder builder = new ProgramStore.Builder(rs);
        builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
        builder.setBlendFunc(ProgramStore.BlendSrcFunc.ONE, ProgramStore.BlendDstFunc.ONE);
        builder.setDitherEnabled(false);
        builder.setDepthMaskEnabled(false);
        return builder.create();
    
private voidcreatePF(int width, int height)

        ProgramFragmentFixedFunction.Builder pfb = new ProgramFragmentFixedFunction.Builder(mRS);
        pfb.setPointSpriteTexCoordinateReplacement(true);
        pfb.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.MODULATE,
                           ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
        pfb.setVaryingColor(true);
        mPFPoints = pfb.create();
    
private voidcreateProgramVertex()

        updateProjectionMatrices();

        ProgramVertex.Builder sb = new ProgramVertex.Builder(mRS);
        String t =  "varying vec4 varColor;\n" +
                    "void main() {\n" +
                    "  vec4 pos = vec4(0.0, 0.0, 0.0, 1.0);\n" +
                    "  pos.xy = ATTRIB_position;\n" +
                    "  gl_Position = UNI_MVP * pos;\n" +
                    "  varColor = ATTRIB_color;\n" +
                    "  gl_PointSize = 12.0;\n" +
                    "}\n";
        sb.setShader(t);
        sb.addConstant(mVpConsts.getType());
        sb.addInput(mPoints.getElement());
        ProgramVertex pvs = sb.create();
        pvs.bindConstants(mVpConsts.getAllocation(), 0);
        mRS.bindProgramVertex(pvs);
    
public voidinit(RenderScriptGL rs, android.content.res.Resources res, int width, int height)

        mRS = rs;
        mRes = res;

        createPF(width, height);

        mPFPoints.bindTexture(loadTexture(R.drawable.flares), 0);

        mPoints = new ScriptField_Point(mRS, PART_COUNT, Allocation.USAGE_SCRIPT);

        Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
        smb.addVertexAllocation(mPoints.getAllocation());
        smb.addIndexSetType(Mesh.Primitive.POINT);
        Mesh smP = smb.create();

        mGrid = ScriptField_BallGrid.create2D(mRS, (width + 99) / 100, (height + 99) / 100);
        mGridCache = Allocation.createSized(mRS, Element.F32_2(mRS), PART_COUNT);
        mBalls = new ScriptField_Ball(mRS, PART_COUNT, Allocation.USAGE_SCRIPT);

        mPhysicsScript = new ScriptC_ball_physics(mRS);
        mPhysicsScript.set_gGridCache(mGridCache);
        mPhysicsScript.set_gBalls(mBalls.getAllocation());

        mScript = new ScriptC_balls(mRS);
        mScript.set_partMesh(smP);
        mScript.set_physics_script(mPhysicsScript);
        mScript.bind_point(mPoints);
        mScript.bind_balls(mBalls);
        mScript.set_gGrid(mGrid.getAllocation());
        mScript.bind_gGridCache(mGridCache);

        mScript.set_gPFPoints(mPFPoints);
        createProgramVertex();

        mRS.bindProgramStore(BLEND_ADD_DEPTH_NONE(mRS));

        mPhysicsScript.set_gMinPos(new Float2(5, 5));
        mPhysicsScript.set_gMaxPos(new Float2(width - 5, height - 5));
        mPhysicsScript.set_gGrid(mGrid.getAllocation());

        mScript.invoke_initParts(width, height);

        mRS.bindRootScript(mScript);
    
private AllocationloadTexture(int id)

        final Allocation allocation =
            Allocation.createFromBitmapResource(mRS, mRes,
                id, Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_GRAPHICS_TEXTURE);
        return allocation;
    
public voidnewTouchPosition(float x, float y, float pressure, int id)

        mPhysicsScript.invoke_touch(x, y, pressure * mRS.getWidth() / 1280, id);
    
public voidsetAccel(float x, float y)

        mPhysicsScript.set_gGravityVector(new Float2(x, y));
    
voidupdateProjectionMatrices()

        mVpConsts = new ScriptField_VpConsts(mRS, 1,
                                             Allocation.USAGE_SCRIPT |
                                             Allocation.USAGE_GRAPHICS_CONSTANTS);
        ScriptField_VpConsts.Item i = new ScriptField_VpConsts.Item();
        Matrix4f mvp = new Matrix4f();
        mvp.loadOrtho(0, mRS.getWidth(), mRS.getHeight(), 0, -1, 1);
        i.MVP = mvp;
        mVpConsts.set(i, 0, true);