blob: f76a0119c9668675bd5f3e6365ceb2fa476f5fc4 [file] [log] [blame]
Jason Sams177f8442010-10-29 10:19:21 -07001/*
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.balls;
18
19import android.content.res.Resources;
20import android.renderscript.*;
21import android.util.Log;
22
23public class BallsRS {
24 public static final int PART_COUNT = 800;
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 mPF;
34 private ProgramVertex mPV;
35 private ProgramRaster mPR;
36 private ProgramStore mPS;
37 private ScriptField_Point mPoints;
38 private ScriptField_Point mArcs;
39 private ScriptField_VpConsts mVpConsts;
40
41 void updateProjectionMatrices() {
42 mVpConsts = new ScriptField_VpConsts(mRS, 1);
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 createProgramRaster() {
51 ProgramRaster.Builder b = new ProgramRaster.Builder(mRS);
52 mPR = b.create();
53 mScript.set_gPR(mPR);
54 }
55
56 private void createProgramVertex() {
57 updateProjectionMatrices();
58
59 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
60 String t = "varying vec4 varColor;\n" +
61 "void main() {\n" +
62 " vec4 pos = vec4(0.0, 0.0, 0.0, 1.0);\n" +
63 " pos.xy = ATTRIB_position;\n" +
64 " gl_Position = UNI_MVP * pos;\n" +
65 " varColor = ATTRIB_color;\n" +
66 " gl_PointSize = ATTRIB_size;\n" +
67 "}\n";
68 sb.setShader(t);
69 sb.addConstant(mVpConsts.getType());
70 sb.addInput(mPoints.getElement());
71 ProgramVertex pvs = sb.create();
72 pvs.bindConstants(mVpConsts.getAllocation(), 0);
73 mScript.set_gPV(pvs);
74 }
75
76 private Allocation loadTexture(int id) {
77 final Allocation allocation = Allocation.createFromBitmapResource(mRS, mRes,
78 id, Element.RGB_565(mRS), false);
79 allocation.uploadToTexture(0);
80 return allocation;
81 }
82
83 public void init(RenderScriptGL rs, Resources res, int width, int height) {
84 mRS = rs;
85 mRes = res;
86
87 ProgramFragment.Builder pfb = new ProgramFragment.Builder(rs);
88 pfb.setPointSpriteTexCoordinateReplacement(true);
89 pfb.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
90 ProgramFragment.Builder.Format.RGBA, 0);
91 pfb.setVaryingColor(true);
92 mPF = pfb.create();
93 rs.contextBindProgramFragment(mPF);
94
95 mPF.bindTexture(loadTexture(R.drawable.flares), 0);
96
97 mPoints = new ScriptField_Point(mRS, PART_COUNT);
98 mArcs = new ScriptField_Point(mRS, PART_COUNT * 2);
99
100 Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
101 smb.addVertexAllocation(mPoints.getAllocation());
102 smb.addIndexType(Primitive.POINT);
103 Mesh smP = smb.create();
104
105 smb = new Mesh.AllocationBuilder(mRS);
106 smb.addVertexAllocation(mArcs.getAllocation());
107 smb.addIndexType(Primitive.LINE);
108 Mesh smA = smb.create();
109
110 mPhysicsScript = new ScriptC_ball_physics(mRS, mRes, R.raw.ball_physics, true);
111
112 mScript = new ScriptC_balls(mRS, mRes, R.raw.balls, true);
113 mScript.set_partMesh(smP);
114 mScript.set_arcMesh(smA);
115 mScript.set_physics_script(mPhysicsScript);
116 mScript.bind_point(mPoints);
117 mScript.bind_arc(mArcs);
118 mScript.bind_balls1(new ScriptField_Ball(mRS, PART_COUNT));
119 mScript.bind_balls2(new ScriptField_Ball(mRS, PART_COUNT));
120
121 mScript.set_gPF(mPF);
122 createProgramVertex();
123 createProgramRaster();
124
125 mPS = ProgramStore.BLEND_ADD_DEPTH_NO_DEPTH(mRS);
126 mScript.set_gPS(mPS);
127
128 mPhysicsScript.set_gMinPos(new Float2(5, 5));
129 mPhysicsScript.set_gMaxPos(new Float2(width - 5, height - 5));
130
131 mScript.invoke_initParts(width, height);
132
133 mRS.contextBindRootScript(mScript);
134 }
135
136 public void newTouchPosition(float x, float y, float pressure, int id) {
137 mPhysicsScript.set_touchX(x);
138 mPhysicsScript.set_touchY(y);
139 mPhysicsScript.set_touchPressure(pressure);
140 }
141
142 public void setAccel(float x, float y) {
143 mPhysicsScript.set_gGravityVector(new Float2(x, y));
144 }
145
146}