我正在尝试对 android OpenGLES 3.0 中位于不同 Z 的两个三角形进行遮挡查询。
这是我的使用方式:
In onSurfaceCreated():
GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GLES30.glEnable(GLES30.GL_DEPTH_TEST); // enabling Deoth test
In onDrawFrame():
GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, 1.0f); // translating at some differnt z = 1.0
drawTriangle(mTriangle1Vertices); // method for drawing traingle1
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, 0.0f); // other triangle at Z = 0.0
// Occlusion Query for Triangle2
IntBuffer testBox = IntBuffer.allocate(1);
IntBuffer hasBeenTested = IntBuffer.allocate(1);
IntBuffer theParams = IntBuffer.allocate(1);
GLES30.glGenQueries(1, testBox);
int retid = testBox.get(0);
GLES30.glBeginQuery(GLES30.GL_ANY_SAMPLES_PASSED, retid);
drawTriangle(mTriangle2Vertices); // drawing traingle 2 ie at Z = 0.0
GLES30.glEndQuery(retid);
int retQuerry = testBox.get(0);
GLES30.glGetQueryObjectuiv(retid, GLES30.GL_QUERY_RESULT, theParams);
int isHidden = theParams.get(0); // THIS IS ALWAYS ZERO even if I REVERSE the Z- translation of the triangles
Log.i("occ", " isHiddedn " + isHidden);
GLES30.glDeleteQueries(1, testBox);
在drawTriangle()
方法中,我只是将颜色、顶点和 MVP uinform 传递给顶点着色器。
在我的设备屏幕上,我可以看到 TRAINGLE 2 完全被 TRIANGLE 1 遮挡。另外,如果我反转 Z 平移,其他三角形完全被遮挡。但是测试结果(isHidden)仍然保持不变吗?我哪里错了?如何进行正确的遮挡测试?