15b539461dcc159bd89297443780d635ccc5e3564John Hoford/*
25b539461dcc159bd89297443780d635ccc5e3564John Hoford * Copyright (C) 2015 The Android Open Source Project
35b539461dcc159bd89297443780d635ccc5e3564John Hoford *
45b539461dcc159bd89297443780d635ccc5e3564John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
55b539461dcc159bd89297443780d635ccc5e3564John Hoford * you may not use this file except in compliance with the License.
65b539461dcc159bd89297443780d635ccc5e3564John Hoford * You may obtain a copy of the License at
75b539461dcc159bd89297443780d635ccc5e3564John Hoford *
85b539461dcc159bd89297443780d635ccc5e3564John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
95b539461dcc159bd89297443780d635ccc5e3564John Hoford *
105b539461dcc159bd89297443780d635ccc5e3564John Hoford * Unless required by applicable law or agreed to in writing, software
115b539461dcc159bd89297443780d635ccc5e3564John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
125b539461dcc159bd89297443780d635ccc5e3564John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135b539461dcc159bd89297443780d635ccc5e3564John Hoford * See the License for the specific language governing permissions and
145b539461dcc159bd89297443780d635ccc5e3564John Hoford * limitations under the License.
155b539461dcc159bd89297443780d635ccc5e3564John Hoford */
165b539461dcc159bd89297443780d635ccc5e3564John Hoford
175b539461dcc159bd89297443780d635ccc5e3564John Hofordpackage com.example.android.rs.vr.loaders;
185b539461dcc159bd89297443780d635ccc5e3564John Hoford
195b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.renderscript.Allocation;
205b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.renderscript.RenderScript;
215b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.renderscript.Type;
225b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.util.Log;
235b539461dcc159bd89297443780d635ccc5e3564John Hoford
245ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hofordimport com.example.android.rs.vr.engine.ScriptC_mandelbulb;
255b539461dcc159bd89297443780d635ccc5e3564John Hofordimport com.example.android.rs.vr.engine.Volume;
265b539461dcc159bd89297443780d635ccc5e3564John Hoford
275b539461dcc159bd89297443780d635ccc5e3564John Hoford/**
285b539461dcc159bd89297443780d635ccc5e3564John Hoford * Provides a simple an example of a computed data set and allows the application to
295b539461dcc159bd89297443780d635ccc5e3564John Hoford * be run without any data sets.
305b539461dcc159bd89297443780d635ccc5e3564John Hoford */
315b539461dcc159bd89297443780d635ccc5e3564John Hofordpublic class Mandelbulb {
325b539461dcc159bd89297443780d635ccc5e3564John Hoford    private static final String LOGTAG = "RawLoader";
335ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford    private static final String simpleLook = "green";
34c81b64d6800dfdb339afe8ad2e89af149ea92fd5John Hoford    private static final int[][] simpleOpacity = {{120, 0x0}, {140, 0xFF}};
355b539461dcc159bd89297443780d635ccc5e3564John Hoford    private static final int[][] simpleColor = {
365ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            {144, 0xA4C639, 10, 80, 0},
375ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            {155, 0xA4C639, 10, 60, 0},
385ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            {300, 0xAA5555, 40, 60, 0},
395ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            {255, 0xAAAAAA, 10, 80, 0}};
405ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford
415ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford
425ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford    private static final String tranlLook = "purple";
435ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford    private static final int[][] tranOpacity = {{110, 0x0},{140, 0x13},{143, 0x0}, {400, 0xFF}};
445ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford    private static final int[][] tranColor = {
455ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            {144, 0xA4C639, 70, 30, 0},
46c81b64d6800dfdb339afe8ad2e89af149ea92fd5John Hoford            {230, 0xAA44AA, 70, 30, 0},
475ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            {300, 0xAA5555, 70, 30, 20},
485ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            {400, 0xAAAAAA, 70, 30, 20}};
495b539461dcc159bd89297443780d635ccc5e3564John Hoford
50c81b64d6800dfdb339afe8ad2e89af149ea92fd5John Hoford    private static final int SIZE = 256;
515b539461dcc159bd89297443780d635ccc5e3564John Hoford    public static final String NAME = "A Mandelbulb";
525b539461dcc159bd89297443780d635ccc5e3564John Hoford
535b539461dcc159bd89297443780d635ccc5e3564John Hoford    public static Volume buildRSVolume(RenderScript rs,
545b539461dcc159bd89297443780d635ccc5e3564John Hoford                                       final VolumeLoader.ProgressListener listener) {
555ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford        ScriptC_mandelbulb scriptC_mandelbulb = new ScriptC_mandelbulb(rs);
565b539461dcc159bd89297443780d635ccc5e3564John Hoford
575b539461dcc159bd89297443780d635ccc5e3564John Hoford        Volume v = new Volume();
585b539461dcc159bd89297443780d635ccc5e3564John Hoford        v.mDimx = v.mDimy = v.mDimz = SIZE;
595b539461dcc159bd89297443780d635ccc5e3564John Hoford        v.mVoxelDim[0] = v.mVoxelDim[1] = v.mVoxelDim[2] = 1.f;
605ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford
615b539461dcc159bd89297443780d635ccc5e3564John Hoford        v.addLook(simpleLook, simpleColor, simpleOpacity);
625ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford        v.addLook(tranlLook, tranColor, tranOpacity);
635b539461dcc159bd89297443780d635ccc5e3564John Hoford
645b539461dcc159bd89297443780d635ccc5e3564John Hoford        Type.Builder b = new Type.Builder(rs, android.renderscript.Element.I16(rs));
655b539461dcc159bd89297443780d635ccc5e3564John Hoford        b.setX(v.mDimx).setY(v.mDimy);
665b539461dcc159bd89297443780d635ccc5e3564John Hoford        Allocation tmp = Allocation.createTyped(rs, b.create(), Allocation.USAGE_SCRIPT);
675b539461dcc159bd89297443780d635ccc5e3564John Hoford        b.setZ(v.mDimz);
685b539461dcc159bd89297443780d635ccc5e3564John Hoford        b.setX(v.mDimx).setY(v.mDimy).setZ(v.mDimz);
695b539461dcc159bd89297443780d635ccc5e3564John Hoford        v.mVolumeAllocation = Allocation.createTyped(rs, b.create(), Allocation.USAGE_SCRIPT);
705b539461dcc159bd89297443780d635ccc5e3564John Hoford
715ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford        scriptC_mandelbulb.set_volume(v.mVolumeAllocation);
725ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford        scriptC_mandelbulb.set_size(SIZE);
735b539461dcc159bd89297443780d635ccc5e3564John Hoford        long time = System.nanoTime();
745b539461dcc159bd89297443780d635ccc5e3564John Hoford        for (int z = 0; z < v.mDimz; z++) {
755ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            scriptC_mandelbulb.set_z(z);
765ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            scriptC_mandelbulb.forEach_mandelbulb(tmp);
775ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            scriptC_mandelbulb.forEach_copy(tmp);
785b539461dcc159bd89297443780d635ccc5e3564John Hoford            rs.finish();
795b539461dcc159bd89297443780d635ccc5e3564John Hoford            listener.progress(z, v.mDimz);
805b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
815b539461dcc159bd89297443780d635ccc5e3564John Hoford
825b539461dcc159bd89297443780d635ccc5e3564John Hoford        Log.v(LOGTAG, "compute Mandelbulb in" + ((System.nanoTime() - time) / 1E9f) + "seconds");
835b539461dcc159bd89297443780d635ccc5e3564John Hoford        tmp.destroy();
845ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford        scriptC_mandelbulb.destroy();
855b539461dcc159bd89297443780d635ccc5e3564John Hoford        return v;
865b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
875b539461dcc159bd89297443780d635ccc5e3564John Hoford
885b539461dcc159bd89297443780d635ccc5e3564John Hoford}
89