1/*
2 * Copyright (C) 2015 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.vr.loaders;
18
19import android.renderscript.Allocation;
20import android.renderscript.RenderScript;
21import android.renderscript.Type;
22import android.util.Log;
23
24import com.example.android.rs.vr.engine.ScriptC_mandelbulb;
25import com.example.android.rs.vr.engine.Volume;
26
27/**
28 * Provides a simple an example of a computed data set and allows the application to
29 * be run without any data sets.
30 */
31public class Mandelbulb {
32    private static final String LOGTAG = "RawLoader";
33    private static final String simpleLook = "green";
34    private static final int[][] simpleOpacity = {{120, 0x0}, {140, 0xFF}};
35    private static final int[][] simpleColor = {
36            {144, 0xA4C639, 10, 80, 0},
37            {155, 0xA4C639, 10, 60, 0},
38            {300, 0xAA5555, 40, 60, 0},
39            {255, 0xAAAAAA, 10, 80, 0}};
40
41
42    private static final String tranlLook = "purple";
43    private static final int[][] tranOpacity = {{110, 0x0},{140, 0x13},{143, 0x0}, {400, 0xFF}};
44    private static final int[][] tranColor = {
45            {144, 0xA4C639, 70, 30, 0},
46            {230, 0xAA44AA, 70, 30, 0},
47            {300, 0xAA5555, 70, 30, 20},
48            {400, 0xAAAAAA, 70, 30, 20}};
49
50    private static final int SIZE = 256;
51    public static final String NAME = "A Mandelbulb";
52
53    public static Volume buildRSVolume(RenderScript rs,
54                                       final VolumeLoader.ProgressListener listener) {
55        ScriptC_mandelbulb scriptC_mandelbulb = new ScriptC_mandelbulb(rs);
56
57        Volume v = new Volume();
58        v.mDimx = v.mDimy = v.mDimz = SIZE;
59        v.mVoxelDim[0] = v.mVoxelDim[1] = v.mVoxelDim[2] = 1.f;
60
61        v.addLook(simpleLook, simpleColor, simpleOpacity);
62        v.addLook(tranlLook, tranColor, tranOpacity);
63
64        Type.Builder b = new Type.Builder(rs, android.renderscript.Element.I16(rs));
65        b.setX(v.mDimx).setY(v.mDimy);
66        Allocation tmp = Allocation.createTyped(rs, b.create(), Allocation.USAGE_SCRIPT);
67        b.setZ(v.mDimz);
68        b.setX(v.mDimx).setY(v.mDimy).setZ(v.mDimz);
69        v.mVolumeAllocation = Allocation.createTyped(rs, b.create(), Allocation.USAGE_SCRIPT);
70
71        scriptC_mandelbulb.set_volume(v.mVolumeAllocation);
72        scriptC_mandelbulb.set_size(SIZE);
73        long time = System.nanoTime();
74        for (int z = 0; z < v.mDimz; z++) {
75            scriptC_mandelbulb.set_z(z);
76            scriptC_mandelbulb.forEach_mandelbulb(tmp);
77            scriptC_mandelbulb.forEach_copy(tmp);
78            rs.finish();
79            listener.progress(z, v.mDimz);
80        }
81
82        Log.v(LOGTAG, "compute Mandelbulb in" + ((System.nanoTime() - time) / 1E9f) + "seconds");
83        tmp.destroy();
84        scriptC_mandelbulb.destroy();
85        return v;
86    }
87
88}
89