开发者

Differences in the depth buffer between Android versions using OpenGL ES 1.1

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 fun开发者_Python百科ction 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.


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:

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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜