我在GLES20上遇到深度测试问题。它根本不起作用。症状如下:
当我尝试渲染2个点时:NEAR = (0,0,-0.3)
和FAR = (0,0,-0.8)
,我得到了错误的结果 - 我总是看到这个点,它被渲染成第二个。即,当我进行渲染调用时(1):
drawBlueNear(300.0f); // draws blue point (0,0,-0.3,1.0) with size 300
drawRedFar(500.0f); // draws red point (0,0,-0.8,1.0) with size 500
我获得的结果与我拨打电话时的结果不同(2):
drawRedFar(500.0f);
drawBlueNear(300.0f);
public class MainActivity extends Activity {
private GLSurfaceView mGLSurfaceView;
private DepthTestRenderer mRenderer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGLSurfaceView = new GLSurfaceView(this);
mGLSurfaceView.setEGLContextClientVersion(2);
mGLSurfaceView.setEGLConfigChooser(true);
mRenderer = new DepthTestRenderer(this);
mGLSurfaceView.setRenderer(mRenderer);
setContentView(mGLSurfaceView);
}
}
public class DepthTestRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(0.2f, 0.9f, 0.6f, 1.0f);
GLES20.glClearDepthf(1.0f);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glDepthFunc(GLES20.GL_LEQUAL);
GLES20.glDepthMask(true);
GLES20.glDepthRangef(0.0f, -1.0f);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
}
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
drawRedFar(500.0f);
drawBlueNear(300.0f);
// drawBlueNear(300.0f);
// drawRedFar(500.0f);
}
}
此外,我已检查以下配置:
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(0.2f, 0.9f, 0.6f, 1.0f);
GLES20.glClearDepthf(-0.3f);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glDepthFunc(GLES20.GL_EQUAL);
GLES20.glDepthMask(true);
}
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
drawRedFar(500.0f);
drawBlueNear(300.0f);
}
奇怪的是,我在屏幕上看到了相同的视图,在图片(1)中,因此看起来,我的两个点的深度都等于0.3f ,但我&#39 ;我很确定它不应该是这样的:我渲染点的方式是我为着色器程序分配统一值。
你有什么想法,可能是什么问题以及如何解决它?
修改 参考评论,我添加了着色器和绘制方法代码。我的透视属性是默认属性,我这里没有使用任何MVP矩阵。因此,近平面为0.0f,远平面为-1.0f。
private float mCoorNear = -0.3f;
private float mCoorFar = -0.8f;
private void drawBlueNear(float size) {
GLES20.glUseProgram(mProgram);
GLES20.glUniform1f(h_u_size, size);
GLES20.glUniform3f(h_u_point, 0.0f, 0.0f, mCoorNear);
GLES20.glUniform3f(h_u_color, 0.0f, 0.0f, 1.0f);
GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
}
private void drawRedFar(float size) {
GLES20.glUseProgram(mProgram);
GLES20.glUniform1f(h_u_size, size);
GLES20.glUniform3f(h_u_point, 0.0f, 0.0f, mCoorFar);
GLES20.glUniform3f(h_u_color, 1.0f, 0.0f, 0.0f);
GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
}
顶点着色器:
uniform vec3 u_point;
uniform float u_size;
void main() {
gl_Position = vec4(u_point, 1.0);
gl_PointSize = u_size;
}
片段着色器:
uniform vec3 u_color;
void main() {
gl_FragColor = vec4(u_color, 1.0);
}