如何在 Libgdx 中支持 OpenGL ES GL10、GL11 和 GL20?

2022-01-12 00:00:00 opengl-es java libgdx opengl-es-2.0

我正在编写使用 GL10 的 3d 游戏,但我希望应用程序支持 GL11 或 GL20(如果可用).支持所有 3 的最佳设计是什么?或者这是一个愚蠢的差事,我应该只专注于支持一个版本?

I am writing a 3d game that is using GL10, but I'd like the application to support GL11 or GL20 if available. What is the best design for supporting all 3? Or is this a fool's errand and I should just focus on supporting one version?

我目前的想法是将我的 render() 函数拆分为 renderGL10、renderGL11、renderGL20 并根据可用的 GL 实例调用适当的渲染函数.每个渲染函数内部都有 GL 版本的正确渲染方法;GL10 和 GL11 可能会有重叠.这是处理我的问题的适当方法还是有更好的方法?

My current idea is to split my render() function into renderGL10, renderGL11, renderGL20 and call the appropriate render function based on the available GL instance. Inside each render function is the proper way to render for the GL version; there will likely be overlap for GL10 and GL11. Is this an appropriate way to handle my problem or is there a better way?

public render(){
  if (Gdx.graphics.isGL20Available()){
    renderGL20();
  } else if (Gdx.graphics.isGL11Available()){
    renderGL11();
  } else { 
    renderGL10();
  }
}

解决方法:如果gl1.x可用,gl10也可用(且gdx.gl10不为空).所以,我的渲染代码一般应该如下结构:

Solution: If gl1.x is available, gl10 is also available (and Gdx.gl10 is not null). So, my render code should generally be structured as follows:

public render(){
  // Use Gdx.gl for any gl calls common to all versions
  if (Gdx.graphics.isGL20Available()){
    // Use Gdx.GL20 for all gl calls
  } else {
    if (Gdx.graphics.isGL11Available()){
      // Use Gdx.gl11 for any gl11 call not supported by gl10
    } 
    // Use Gdx.gl10 for all gl calls
  }
}

推荐答案

您是在 OpenGL ES 2.0 路径中实际使用着色器,还是依靠 SpriteBatch 和 consorts 为您处理脏细节?在后一种情况下,使用 GLES 2.0 并没有真正的好处(除了两个纹理的非功率,在某些设备上可能会更慢)

Do you actually use shaders in your OpenGL ES 2.0 path or do you rely on SpriteBatch and consorts to handle the dirty details for you? In the later case there's no real benefit in going GLES 2.0 (apart from non-power of two textures, which might be slower on some devices)

通常您在 GLES 1.x 或 2.0 之间进行选择.如果您使用 1.x,则无需执行任何操作.如果您想使用 2.0,您必须创建一个 AndroidApplicationConfig 并将 useGL20 标志设置为 true.如果运行应用程序的设备不支持 GLES 2.0,libgdx 将回退到 1.x.然后,您可以使用上面的渲染循环,如果可用,它将使用 GLES 2.0,否则回退到 1.x.不过,除非您直接使用 GLES 调用,否则您不太可能需要为 1.0 和 1.x 设置单独的路径.

Usually you chose between either GLES 1.x or 2.0. If you go with 1.x you don't have to do anything. If you want to use 2.0 you have to create an AndroidApplicationConfig and set the useGL20 flag to true. If GLES 2.0 is not supported on the device you run your app on, libgdx will fall back to 1.x. You can then use your above render loop which will use GLES 2.0 if available and fall back to 1.x otherwise. You are unlikely to have to have separate paths for 1.0 and 1.x though, unless you use GLES calls directly.

相关文章