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
17package com.example.android.rs.fountainfbo;
18
19import android.content.res.Resources;
20import android.renderscript.Allocation;
21import android.renderscript.Element;
22import android.renderscript.Mesh;
23import android.renderscript.ProgramFragment;
24import android.renderscript.ProgramFragmentFixedFunction;
25import android.renderscript.RenderScriptGL;
26import android.renderscript.Type;
27
28public class FountainFboRS {
29    public static final int PART_COUNT = 50000;
30
31    public FountainFboRS() {
32    }
33
34    private Resources mRes;
35    private RenderScriptGL mRS;
36    private ScriptC_fountainfbo mScript;
37    private Allocation mColorBuffer;
38    private ProgramFragment mProgramFragment;
39    private ProgramFragment mTextureProgramFragment;
40    public void init(RenderScriptGL rs, Resources res) {
41      mRS = rs;
42      mRes = res;
43
44      ScriptField_Point points = new ScriptField_Point(mRS, PART_COUNT);
45
46      Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
47      smb.addVertexAllocation(points.getAllocation());
48      smb.addIndexSetType(Mesh.Primitive.POINT);
49      Mesh sm = smb.create();
50
51      mScript = new ScriptC_fountainfbo(mRS, mRes, R.raw.fountainfbo);
52      mScript.set_partMesh(sm);
53      mScript.bind_point(points);
54
55      ProgramFragmentFixedFunction.Builder pfb = new ProgramFragmentFixedFunction.Builder(rs);
56      pfb.setVaryingColor(true);
57      mProgramFragment = pfb.create();
58      mScript.set_gProgramFragment(mProgramFragment);
59
60      /* Second fragment shader to use a texture (framebuffer object) to draw with */
61      pfb.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
62          ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
63
64      /* Set the fragment shader in the Renderscript runtime */
65      mTextureProgramFragment = pfb.create();
66      mScript.set_gTextureProgramFragment(mTextureProgramFragment);
67
68      /* Create the allocation for the color buffer */
69      Type.Builder colorBuilder = new Type.Builder(mRS, Element.RGBA_8888(mRS));
70      colorBuilder.setX(256).setY(256);
71      mColorBuffer = Allocation.createTyped(mRS, colorBuilder.create(),
72      Allocation.USAGE_GRAPHICS_TEXTURE |
73      Allocation.USAGE_GRAPHICS_RENDER_TARGET);
74
75      /* Set the allocation in the Renderscript runtime */
76      mScript.set_gColorBuffer(mColorBuffer);
77
78      mRS.bindRootScript(mScript);
79  }
80
81    boolean holdingColor[] = new boolean[10];
82    public void newTouchPosition(float x, float y, float pressure, int id) {
83        if (id >= holdingColor.length) {
84            return;
85        }
86        int rate = (int)(pressure * pressure * 500.f);
87        if (rate > 500) {
88            rate = 500;
89        }
90        if (rate > 0) {
91            mScript.invoke_addParticles(rate, x, y, id, !holdingColor[id]);
92            holdingColor[id] = true;
93        } else {
94            holdingColor[id] = false;
95        }
96
97    }
98}
99
100