Random visualizations

We can switch between visualizations by adding and removing them over time. In the following example, we start with one active visualization and then every few seconds, toggle a random visualization on or off.

First, add an activate method to the abstract Visualization class, which takes a Boolean enabled parameter. The Boolean active variable is read-only:

    public boolean active = true;
    public abstract void activate(boolean enabled);

Its implementation will depend on the specific visualization. RenderBox library provides an enabled flag that's used when we render objects. The ones that instantiate a single Plane component are the easiest, such as WaveformVisualization and FFTVisualization. To each of these, add the following code:

    @Override
    public void activate(boolean enabled) {
        active = enabled;
        plane.enabled = enabled;
    }

For the GeometricVisualization class, we can enable (and disable) each of the component cubes:

    @Override
    public void activate(boolean enabled) {
        active = enabled;
        for(int i = 0; i < cubes.length; i++) {
            cubeRenderers[i].enabled = enabled;
        }
    }

Now we can control this within the MainActivity class.

Start with each of visualizations that are inactive. Add this initialization to setup() of MainActivity:

        for (Visualization viz : visualizerBox.visualizations) {
            viz.activate(false);
        }

In preDraw of MainActivity, we'll check the current time (using the Time class of RenderBox library) and toggle a random visualization after every 3 seconds. First, add a few variables to the top of the class:

    float timeToChange = 0f;
    final float CHANGE_DELAY = 3f;
    final Random rand = new Random();

Now we can modify preDraw to check the time and modify the list of visualizations:

    public void preDraw() {
        if (Time.getTime() > timeToChange) {
            int idx = rand.nextInt( visualizerBox.visualizations.size() );
            Visualization viz = visualizerBox.visualizations.get(idx);
            viz.activate(!viz.active);
            timeToChange += CHANGE_DELAY;
        }
        visualizerBox.preDraw();
    }

A similar kind of time control structure (or delta time) can be used to implement many kinds of animation, such as changing the visualization object's position, rotation, and/or scale, or evolving the geometry itself over time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset