使用 OpenGL ES 1.1 的 Android 版本之间深度缓冲区的差异
我正在 OpenGL ES 1.0 中编写一个 2d 游戏(在适用的情况下强制转换为 1.1 扩展)。我试图尽可能保持通用,以防我错过了一些明显的东西。
我在 Android 2.2 (Froyo) 和 Android 2.3.3 (Gingerbread) 之间的深度测试方面遇到问题。目前, onCreate 我有以下内容:
//this is called once only when the renderer is created
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, mWidth, 0, mHeight, 1.0f, 320.0f);
在使用 OES_draw_texture
扩展时我成功使用了深度缓冲区,这不是问题。然而,当指定传统顶点绘制的 Z 值时,版本之间会出现奇怪的差异。
在 2.2 中,以下代码渲染到前面(最上面):
//This array is used in the function glDrawArrays as a GL10.GL_TRIANGLE_STRIP.
//It works as intended. The specific positions are the Z positions of each point in
//the square (the triangle strips form a square).
vertices[2]=0.5f*(1+320)*-1;
vertices[5]=0.5f*(1+320)*-1;
vertices[8]=0.5f*(1+320)*-1;
vertices[11]=0.5f*(1+320)*-1;
现在,这效果很好,但在 2.3.3 中,这会将对象进一步向后推。要使用 2.3.3 获得正确的位置,我需要执行以下操作:
vertices[2]=-1;
vertices[5]=-1;
vertices[8]=-1;
vertices[11]=-1;
这将纠正问题。
我的问题有两个:
- 当我将深度缓冲区指定为 1 到 320 之间时,为什么必须将 Z 值指定为 -ve 而不是 +ve?标准(尽管非 ES)文档 明确指出 zNear 和 zFar不应该是 -ve 或 0。
- 为什么 2.2 和 2.3.3 之间会出现这种差异?我的印象是 2.3.3 是以 1 作为近值的“正确”方式,这正是我所期望的。那么为什么实际上一半的深度缓冲区被忽略了呢?
我怀疑我可能错过了 Gingerbread 隐含假设的某个命令,而 Froyo 则没有,但我不确定。任何帮助将不胜感激。
I'm writing a 2d game in OpenGL ES 1.0 (with casts to 1.1 Extensions where applicable). I'm trying to keep this as generic as possible in case I've missed something obvious.
I'm having an issue with regards to the depth testing between Android 2.2 (Froyo) and Android 2.3.3 (Gingerbread). Currently, onCreate I have the following:
//this is called once only when the renderer is created
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, mWidth, 0, mHeight, 1.0f, 320.0f);
I use the depth buffer successfully when using the OES_draw_texture
extension and this isn't a problem. However, when specifying the Z values for traditional drawing of vertices, a curious discrepancy appears between versions.
In 2.2, the following code renders to the front (topmost):
//This array is used in the function glDrawArrays as a GL10.GL_TRIANGLE_STRIP.
//It works as intended. The specific positions are the Z positions of each point in
//the square (the triangle strips form a square).
vertices[2]=0.5f*(1+320)*-1;
vertices[5]=0.5f*(1+320)*-1;
vertices[8]=0.5f*(1+320)*-1;
vertices[11]=0.5f*(1+320)*-1;
Now, this works great, but in 2.3.3 this will push the object further back. To get the correct position using 2.3.3, I need to do the following:
vertices[2]=-1;
vertices[5]=-1;
vertices[8]=-1;
vertices[11]=-1;
This will correct the problem.
My question is two-fold:
- Why must I specify the Z values as -ve instead of +ve when I have specified the depth buffer to be between 1 and 320? The standard (albeit non-ES) documentation clearly states that zNear and zFar shouldn't be -ve or 0.
- Why is there a discrepancy between 2.2 and 2.3.3 in this fashion? I am under the impression 2.3.3 is doing it the 'correct' way by having 1 as the near value, which is what I expected. Why then is effectively half the depth buffer being ignored?
I suspect I may be missing a certain command which Gingerbread is implicitly assuming, and Froyo is not, but I am unsure. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
gl.glOrthof 在 ES 2.0 中不起作用,因为它不再有投影矩阵的概念。您可能想要使用 android.opengl.Matrix.orthoM 来计算投影矩阵,然后在顶点着色器中将点乘以它,如下所示:
分配给 gl_Position 的位置位于“剪辑坐标”中,其范围为每个维度 -1..1。听起来您正在从顶点数据分配 gl_Position ,而没有将其从所需的 zNear..zFar 缩放到 -1..1 。
gl.glOrthof has no effect in ES 2.0, as it no longer has a concept of a projection matrix. You probably want to use android.opengl.Matrix.orthoM to calculate your projection matrix, then multiply your points by it in the vertex shader, like this:
The positions assigned to gl_Position are in "clip coordinates", which are in the range of -1..1 in each dimension. It sounds you're assigning gl_Position from the vertex data without scaling it from your wanted zNear..zFar to -1..1.