多个OpenGL ES 2 Android渲染器

3

在屏幕上同时显示多个渲染画面,例如将安卓屏幕分割成4个象限,在每个象限上显示一个立方体,是否可行?我知道OpenGL可以实现这样的功能,但那是基于Windows的GLUT。目前我正在研究如何在安卓设备上实现该功能,但是还没有找到相关的资料。以下是我创建渲染主活动的代码。

public class MainActivity extends Activity
{

    private MyGLSurfaceView mGLView;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity

        // "this" is the reference to activity
        mGLView = new MyGLSurfaceView(this);
        setContentView(mGLView);
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        // The following call pauses the rendering thread.
        // If your OpenGL application is memory intensive,
        // you should consider de-allocating objects that
        // consume significant memory here.
        mGLView.onPause();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        // The following call resumes a paused rendering thread.
        // If you de-allocated graphic objects for onPause()
        // this is a good place to re-allocate them.
        mGLView.onResume();
    }
}

class MyGLSurfaceView extends GLSurfaceView
{

    private final MyGLRenderer mRenderer;
    Context context;

    public MyGLSurfaceView(Context context)
    {
        super(context);

        this.context = context;
        // Create an OpenGL ES 2.0 context.
        setEGLContextClientVersion(2);

        // Set the Renderer for drawing on the GLSurfaceView
        Log.d("Test", "GL initialized");
        mRenderer = new MyGLRenderer(context);
        setRenderer(mRenderer);

        // Render the view only when there is a change in the drawing data
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
}
2个回答

3
我觉得用一个GLSurfaceView就可以实现这个功能。通过调用glViewport将场景映射到四分之一的屏幕上,你可以将屏幕划分为四个象限。然后,你可以调用你的绘制函数,重复执行每个象限即可。
例如:
glViewport(0, 0, width/2, height/2);
< render first quadrant >

glViewport(width / 2, 0, width / 2, height / 2);
< render second quadrant >

And so on...


1
GLSurfaceView不是为此设计的,但您可以使用TextureViews来实现。请查看TextureView的在线文档。最简单的方法是创建一个线程依次渲染每个TextureView。如果您想要同时运行多个OpenGL ES上下文,则会更加复杂,但是最近在Khronos.org OpenGL ES论坛上已经讨论过这个问题。

是的,我想要同时运行它们。基本上是4个渲染器同时运行,每个在屏幕的一个象限上。 - Ion
我尝试过这种方法,但有时会出现损坏,纹理视图似乎会获取其他纹理视图的纹理而不是实际绘制的内容。我认为TextureView不可靠地支持多个实例。 - jjxtra
1
我已经有几年没有使用TextureView了,但是我看到Google终于在这里发布了一些关于它如何工作的信息:https://source.android.com/devices/graphics/architecture.html - ClayMontgomery

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接