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.android.fountain;
18
19import android.content.res.Resources;
20import android.renderscript.*;
21import android.util.Log;
22
23
24public class FountainRS {
25    public static final int PART_COUNT = 20000;
26
27    static class SomeData {
28        public int x;
29        public int y;
30        public int rate;
31        public int count;
32        public float r;
33        public float g;
34        public float b;
35    }
36
37    public FountainRS() {
38    }
39
40    public void init(RenderScriptGL rs, Resources res, int width, int height) {
41        mRS = rs;
42        mRes = res;
43        initRS();
44    }
45
46    public void newTouchPosition(int x, int y, int rate) {
47        if (mSD.rate == 0) {
48            mSD.r = ((x & 0x1) != 0) ? 0.f : 1.f;
49            mSD.g = ((x & 0x2) != 0) ? 0.f : 1.f;
50            mSD.b = ((x & 0x4) != 0) ? 0.f : 1.f;
51            if ((mSD.r + mSD.g + mSD.b) < 0.9f) {
52                mSD.r = 0.8f;
53                mSD.g = 0.5f;
54                mSD.b = 1.f;
55            }
56        }
57        mSD.rate = rate;
58        mSD.x = x;
59        mSD.y = y;
60        mIntAlloc.data(mSD);
61    }
62
63
64    /////////////////////////////////////////
65
66    private Resources mRes;
67
68    private RenderScriptGL mRS;
69    private Allocation mIntAlloc;
70    private SimpleMesh mSM;
71    private SomeData mSD;
72    private Type mSDType;
73
74    private void initRS() {
75        mSD = new SomeData();
76        mSDType = Type.createFromClass(mRS, SomeData.class, 1, "SomeData");
77        mIntAlloc = Allocation.createTyped(mRS, mSDType);
78        mSD.count = PART_COUNT;
79        mIntAlloc.data(mSD);
80
81        Element.Builder eb = new Element.Builder(mRS);
82        eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "delta");
83        eb.add(Element.createAttrib(mRS, Element.DataType.FLOAT_32, Element.DataKind.POSITION, 2), "position");
84        eb.add(Element.createAttrib(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.COLOR, 4), "color");
85        Element primElement = eb.create();
86
87
88        SimpleMesh.Builder smb = new SimpleMesh.Builder(mRS);
89        int vtxSlot = smb.addVertexType(primElement, PART_COUNT);
90        smb.setPrimitive(Primitive.POINT);
91        mSM = smb.create();
92        mSM.setName("PartMesh");
93
94        Allocation partAlloc = mSM.createVertexAllocation(vtxSlot);
95        partAlloc.setName("PartBuffer");
96        mSM.bindVertexAllocation(partAlloc, 0);
97
98        // All setup of named objects should be done by this point
99        // because we are about to compile the script.
100        ScriptC.Builder sb = new ScriptC.Builder(mRS);
101        sb.setScript(mRes, R.raw.fountain);
102        sb.setRoot(true);
103        sb.setType(mSDType, "Control", 0);
104        sb.setType(mSM.getVertexType(0), "point", 1);
105        Script script = sb.create();
106        script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
107
108        script.bindAllocation(mIntAlloc, 0);
109        script.bindAllocation(partAlloc, 1);
110        mRS.contextBindRootScript(script);
111    }
112
113}
114
115
116