summaryrefslogtreecommitdiffstats
path: root/src/com/android/galaxy4/GalaxyRS.java
blob: c568296e1d1d4ceba0618b589369d99ad3361407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.android.galaxy4;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.renderscript.Allocation;
import android.renderscript.Matrix4f;
import android.renderscript.Mesh;
import android.renderscript.ProgramFragment;
import android.renderscript.ProgramFragmentFixedFunction;
import android.renderscript.ProgramRaster;
import android.renderscript.ProgramStore;
import android.renderscript.Sampler;
import android.renderscript.ProgramStore.BlendDstFunc;
import android.renderscript.ProgramStore.BlendSrcFunc;
import android.renderscript.ProgramVertex;
import android.renderscript.ProgramVertexFixedFunction;
import android.renderscript.RenderScriptGL;
import android.renderscript.ProgramVertexFixedFunction.Builder;
import android.util.Log;
import android.renderscript.Program;
import static android.renderscript.Sampler.Value.*;

public class GalaxyRS {
    public static final int BG_STAR_COUNT = 11000;
    public static final int SPACE_CLOUDSTAR_COUNT = 25;
    private Resources mRes;
    // rendering context
    private RenderScriptGL mRS;
    private ScriptC_galaxy mScript;

    // shader constants
    private ScriptField_VpConsts mPvConsts;
    private ScriptField_Particle spaceClouds;
    private ScriptField_Particle bgStars;
    private Mesh spaceCloudsMesh;
    private Mesh bgStarsMesh;

    int mHeight;
    int mWidth;
    boolean inited = false;

    private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();

    private Allocation cloudAllocation;
    private Allocation fgStarAllocation;
    private Allocation bgAllocation;

    public void init(RenderScriptGL rs, Resources res, int width, int height) {
        if (!inited) {
            mRS = rs;
            mRes = res;

            mWidth = width;
            mHeight = height;

            mOptionsARGB.inScaled = false;
            mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888;

            spaceClouds = new ScriptField_Particle(mRS, SPACE_CLOUDSTAR_COUNT);
            Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
            smb.addVertexAllocation(spaceClouds.getAllocation());
            smb.addIndexSetType(Mesh.Primitive.POINT);
            spaceCloudsMesh = smb.create();

            bgStars = new ScriptField_Particle(mRS, BG_STAR_COUNT);
            Mesh.AllocationBuilder smb2 = new Mesh.AllocationBuilder(mRS);
            smb2.addVertexAllocation(bgStars.getAllocation());
            smb2.addIndexSetType(Mesh.Primitive.POINT);
            bgStarsMesh = smb2.create();

            mScript = new ScriptC_galaxy(mRS, mRes, R.raw.galaxy);
            mScript.set_spaceCloudsMesh(spaceCloudsMesh);
            mScript.bind_spaceClouds(spaceClouds);
            mScript.set_bgStarsMesh(bgStarsMesh);
            mScript.bind_bgStars(bgStars);

            mPvConsts = new ScriptField_VpConsts(mRS, 1);

            createProgramVertex();
            createProgramRaster();
            createProgramFragmentStore();
            createProgramFragment();

            loadTextures();

            mRS.bindRootScript(mScript);

            mScript.invoke_positionParticles();

            inited = true;
        }

    }

    private Allocation loadTexture(int id) {
        final Allocation allocation = Allocation.createFromBitmapResource(mRS, mRes, id);
        return allocation;
    }

    private Allocation loadTextureARGB(int id) {
        Bitmap b = BitmapFactory.decodeResource(mRes, id, mOptionsARGB);
        return Allocation.createFromBitmap(mRS, b,
                Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
                Allocation.USAGE_GRAPHICS_TEXTURE);
    }

    private void loadTextures() {
        fgStarAllocation = loadTexture(R.drawable.fgstar);
        cloudAllocation = loadTexture(R.drawable.cloud);
        bgAllocation = loadTexture(R.drawable.bg);
        mScript.set_textureSpaceCloud(cloudAllocation);
        mScript.set_textureFGStar(fgStarAllocation);
        mScript.set_textureBg(bgAllocation);
    }

    private Matrix4f getProjectionNormalized(int w, int h) {
        // range -1,1 in the narrow axis at z = 0.
        Matrix4f m1 = new Matrix4f();
        Matrix4f m2 = new Matrix4f();

        if (w > h) {
            float aspect = ((float) w) / h;
            m1.loadFrustum(-aspect, aspect, -1, 1, 1, 100);
        } else {
            float aspect = ((float) h) / w;
            m1.loadFrustum(-1, 1, -aspect, aspect, 1, 100);
        }

        m2.loadRotate(180, 0, 1, 0);
        m1.loadMultiply(m1, m2);

        m2.loadScale(-1, 1, 1);
        m1.loadMultiply(m1, m2);

        m2.loadTranslate(0, 0, 1);
        m1.loadMultiply(m1, m2);
        return m1;
    }

    private void updateProjectionMatrices() {
        Matrix4f proj = new Matrix4f();
        proj.loadOrthoWindow(mWidth, mHeight);

        Log.d("------------------- UPDATE PROJECTION MATRICES", mWidth + "  " + mHeight);

        Matrix4f projNorm = getProjectionNormalized(mWidth, mHeight);
        ScriptField_VpConsts.Item i = new ScriptField_VpConsts.Item();
        // i.Proj = projNorm;
        i.MVP = projNorm;
        mPvConsts.set(i, 0, true);

    }

    private void createProgramVertex() {

        // /////////////////// fixed function bg
        ProgramVertexFixedFunction.Constants mPvOrthoAlloc = 
            new ProgramVertexFixedFunction.Constants(mRS);
        Matrix4f proj = new Matrix4f();
        proj.loadOrthoWindow(mWidth, mHeight);
        mPvOrthoAlloc.setProjection(proj);

        ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
        ProgramVertex pv = pvb.create();
        ((ProgramVertexFixedFunction) pv).bindConstants(mPvOrthoAlloc);
        mScript.set_vertBg(pv);

        // ///////////////////////////////////////////////////////////////////////
        // //////////////////////////////////////////////////////////////////

        updateProjectionMatrices();

        // cloud
        ProgramVertex.Builder builder = new ProgramVertex.Builder(mRS);
        builder.setShader(mRes, R.raw.spacecloud_vs);
        builder.addConstant(mPvConsts.getType());
        builder.addInput(spaceCloudsMesh.getVertexAllocation(0).getType().getElement());
        ProgramVertex pvs = builder.create();
        pvs.bindConstants(mPvConsts.getAllocation(), 0);
        mRS.bindProgramVertex(pvs);

        mScript.set_vertSpaceClouds(pvs);

        // bg stars
        builder = new ProgramVertex.Builder(mRS);
        builder.setShader(mRes, R.raw.bgstar_vs);
        builder.addConstant(mPvConsts.getType());
        builder.addInput(bgStarsMesh.getVertexAllocation(0).getType().getElement());
        pvs = builder.create();
        pvs.bindConstants(mPvConsts.getAllocation(), 0);
        mRS.bindProgramVertex(pvs);
        mScript.set_vertBgStars(pvs);
    }

    private void createProgramFragment() {
        // fixed function bg

        Sampler.Builder samplerBuilder = new Sampler.Builder(mRS);
        samplerBuilder.setMinification(NEAREST);
        samplerBuilder.setMagnification(NEAREST);
        samplerBuilder.setWrapS(WRAP);
        samplerBuilder.setWrapT(WRAP);
        Sampler sn = samplerBuilder.create();
        ProgramFragmentFixedFunction.Builder builderff = 
            new ProgramFragmentFixedFunction.Builder(mRS);
        builderff = new ProgramFragmentFixedFunction.Builder(mRS);
        builderff.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
                ProgramFragmentFixedFunction.Builder.Format.RGB, 0);
        ProgramFragment pfff = builderff.create();
        mScript.set_fragBg(pfff);
        pfff.bindSampler(sn, 0);

        ////////////////////////////////////////////////////////////////////

        // cloud fragment
        ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS);

        builder.setShader(mRes, R.raw.spacecloud_fs);
        // multiple textures
        builder.addTexture(Program.TextureType.TEXTURE_2D);
        builder.addTexture(Program.TextureType.TEXTURE_2D);

        ProgramFragment pf = builder.create();
        pf.bindSampler(Sampler.CLAMP_LINEAR(mRS), 0);
        mScript.set_fragSpaceClouds(pf);

        // bg star fragment
        builder = new ProgramFragment.Builder(mRS);
        builder.setShader(mRes, R.raw.bgstar_fs);
        pf = builder.create();
        mScript.set_fragBgStars(pf);

    }

    private void createProgramRaster() {
        // Program raster is primarily used to specify whether point sprites are enabled and
        // to control the culling mode. By default, back faces are culled.
        ProgramRaster.Builder builder = new ProgramRaster.Builder(mRS);
        builder.setPointSpriteEnabled(true);
        ProgramRaster pr = builder.create();
        mRS.bindProgramRaster(pr);
    }

    private void createProgramFragmentStore() {
        // ProgramStore contains a set of parameters that control how the graphics hardware handles
        // writes to the framebuffer.
        // 
        // It could be used to:
        //     enable/disable depth testing
        //     specify wheather depth writes are performed
        //     setup various blending modes for use in effects like transparency
        //     define write masks for color components written into the framebuffer

        ProgramStore.Builder builder = new ProgramStore.Builder(mRS);
        // builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA,
        // BlendDstFunc.ONE_MINUS_SRC_ALPHA );
        builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE);
        // why alpha no work with additive blending?
        // builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE);
        mRS.bindProgramStore(builder.create());

    }

    public void start() {
        mRS.bindRootScript(mScript);
    }

    public void stop() {
        mRS.bindRootScript(null);
    }

    public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) {
        mScript.set_xOffset(xOffset);
    }

}