Nativ
Nativ

Reputation: 3150

Change GlSurfaceView renderer

I looked all over the net in order to find out if its possible to change the renderer of a GLSurfaceView on the flight. The reason is that I want to change the OpenGl program, and initiate all the attributes and unified params from its vertex and fragment shader and I don't want the any change would require to create a brand new GLSurfaceView with a brand new Renderer.

It seems like reasonable operation that should be doable.

Upvotes: 1

Views: 2198

Answers (1)

Stefan Hanke
Stefan Hanke

Reputation: 3528

Note: I haven't implemented the following.

GLSurfaceView.Renderer is an interface. Implement it three times. Twice for your different OpenGL renderers, and one time attached to the GLSurfaceView. The latter only dispatches to one of the former, and allows to change the renderer to which it dispatches. The code must hold a reference to this renderer, and eventually must be synchronized to the draw calls (though I don't know).

Be aware that you cannot easily switch OpenGLES context data. It is shared between all renderer instances.

class DispatchingRenderer implements GLSurfaceView.Renderer {
    private class Renderer1 implements GLSurfaceView.Renderer {
       ...
    }
    private class Renderer2 implements GLSurfaceView.Renderer {
       ...
    }

    public DispatchingRenderer() {
        this.r1 = new Renderer1();
        this.r2 = new Renderer2();

        this.currentRenderer = this.r1;
    }
    public void ToggleRenderer() {
        if(this.currentRenderer == this.r1) {
            this.currentRenderer = this.r2;
        } else if (this.currentRenderer == this.r2) {
            this.currentRenderer = this.r1;
        }
    }
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // do one-time setup
    }
    public void onSurfaceChanged(GL10 gl, int w, int h) {
        this.currentRenderer.onSurfaceChanged(gl, w, h);
    }
    public void onDrawFrame(GL10 gl) {
        this.currentRenderer.onDrawFrame(gl);
    }
}

Upvotes: 4

Related Questions