2015-04-12 56 views
0

所以,这里是我的3D立方体代码:GL_DEPTH_TEST好好尝试一下工作

Mesh cube = new Mesh(false, 8, 36, 
     new VertexAttribute(VertexAttribute.POSITION, 3, ShaderProgram.POSITION_ATTRIBUTE), 
     new VertexAttribute(VertexAttribute.COLOR_PACKED, 4, ShaderProgram.COLOR_ATTRIBUTE) 
    ); 

float x = -100; 
float y = -50; 
float z = 250; 

float w = 50; 
float h = 50; 
float l = 50; 

float r = Color.RED.toFloatBits(); 
float g = Color.GREEN.toFloatBits(); 
float b = Color.BLUE.toFloatBits(); 

float[] vertices = { 
    -w/2f + x, y, l/2f + z, // 0 
    r, 
    -w/2f + x, h + y, l/2f + z, // 1 
    r, 
    w/2f + x, h + y, l/2f + z, // 2 
    b, 
    w/2f + x, y, l/2f + z, // 3 
    b, 

    w/2f + x, y, -l/2f + z, // 4 
    g, 
    w/2f + x, y + h, -l/2f + z, // 5 
    g, 
    -w/2f + x, y + h, -l/2f + z, // 6 
    r, 
    -w/2f + x, y, -l/2f + z, // 7 
    r 
}; 

short[] indices = { 
    0, 1, 2, 2, 3, 0, 
    3, 2, 5, 5, 4, 3, 
    4, 5, 6, 6, 7, 4, 
    7, 6, 1, 1, 0, 7, 
    0, 3, 4, 4, 7, 0, 
    1, 6, 5, 5, 2, 1 
}; 

cube.setIndices(indices); 
cube.setVertices(vertices); 

“原型”: enter image description here

网类是LibGDX并且它是由我修改(不要太多)。所有的 首先,我打电话

GLES20.glEnable(GLES20.GL_DEPTH_TEST); 

在初始化(当然初始化上下文之后,)。

和渲染方法是这样的:

GLES20.glClearColor(0f, 0f, 0f, 1f); 
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 

shaderProgram.begin(); 
cube.render(shaderProgram, GLES20.GL_TRIANGLES, 0, 36); 
shaderProgram.end(); 

而最终的结果是: enter image description here

我也打了glDepthFunc,但我没有得到期望的结果。

我在做什么错?

+0

你叫glEnable后得到一个错误? – TameHog

+0

@TameHog nope .. –

+0

znear和zfar的值是什么? – DanP

回答

0

答案是,我忘了设置在创建的Open GL背景下深度缓存大小:

setEGLConfigChooser(8, 8, 8, 8, 16, 0); 

此外,near值应> 0,所以我将它设置为1。

0

原因只有两个,

1)如果启用了剔除功能,并且三角形的面法线方向相反,则不会渲染三角形。使用叉积计算三角形的面法线。 2)如果深度函数错误。

要面对计算/表面法线三角形的参考http://fullonsoftware.co.uk/snippets/content/Math_-_Calculating_Face_Normals.pdf

+0

另外,'near'应该> 0 –