Change GlSurfaceView renderer

假如想象 提交于 2020-01-05 17:41:42

问题


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.


回答1:


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);
    }
}


来源:https://stackoverflow.com/questions/25369099/change-glsurfaceview-renderer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!