0

我正在用 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 没有,但我不确定。任何帮助,将不胜感激。

4

1 回答 1

0

gl.glOrthof 在 ES 2.0 中无效,因为它不再有投影矩阵的概念。您可能想使用 android.opengl.Matrix.orthoM 来计算您的投影矩阵,然后在顶点着色器中将您的点乘以它,如下所示:

attribute vec3 position;
uniform mat4 projection;
void main() {
    gl_Position = projection * vec4(position, 1.);
}

分配给 gl_Position 的位置在“剪辑坐标”中,每个维度的范围为 -1..1。听起来您是从顶点数据中分配 gl_Position 而不将其从您想要的 zNear..zFar 缩放到-1..1。

于 2011-09-12T23:10:36.310 回答