我正在尝试学习OpenGL ES 2.0,我去Android上加载3d模型。我现在可以正确加载模型纹理,但我在显示深度上有一个问题。当我把我的模型放在透视图中,模型的一部分被另一部分隐藏起来,在我画另一部分之前,我碰巧看到了一两个三角形这是我通过一些部分看到的。
I try setglconfigchooser (8,8,8,8,16,0);和(8,8,8,8,24,0),但我的问题仍然是一样的,除了当我把(8,8,8,8,24,0)和显示稍微更好的定义,但当3d对象移动时,颜色产生频闪效果,这让我感到不安。
我也尝试gldepthfunction (gl_equal);与glEnable (GL_DEPTH_TEST),但这并不能解决我的问题。
这是问题的图片:
问题:链接断开
good: Link is broken
很抱歉我的链接图片,我没有超过10的声望可以在问题中发布图片。
这是我的代码我GLSurfaceViewpublic MyGLSurfaceView(Context context) {
super(context);
this.context = context;
setEGLContextClientVersion(2);
setEGLConfigChooser(true);
//setZOrderOnTop(true);
//setEGLConfigChooser(8, 8, 8, 8, 16, 0);
//setEGLConfigChooser(8, 8, 8, 8, 24, 0);
//getHolder().setFormat(PixelFormat.RGBA_8888);
mRenderer = new Renderer(context);
setRenderer(mRenderer);
}
我渲染器 @Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
mushroom = new Mushroom();
textureProgram = new TextureShaderProgram(context);
texture = TextureHelper.loadTexture(context, R.drawable.mushroom);
}
@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
glViewport(0, 0, width, height);
MatrixHelper.perspectiveM(projectionMatrix, 45, (float) width
/ (float) height, 0f, 10f);
setLookAtM(viewMatrix, 0, 0f, 1.2f, -10.2f, 0f, 0f, 0f, 0f, 1f, 0f);
}
@Override
public void onDrawFrame(GL10 glUnused) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
multiplyMM(viewProjectionMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
glDepthFunc(GL_LEQUAL);
//glDepthMask(true);
positionMushroomInScene();
textureProgram.useProgram();
textureProgram.setUniforms(modelViewProjectionMatrix, texture);
mushroom.bindData(textureProgram);
mushroom.draw();
//glDepthFunc(GL_LESS);
}
private void positionMushroomInScene() {
setIdentityM(modelMatrix, 0);
translateM(modelMatrix, 0, 0f, 0f, 5f);
rotateM(modelMatrix, 0, -yRotation, 1f, 0f, 0f);
rotateM(modelMatrix, 0, xRotation, 0f, 1f, 0f);
multiplyMM(modelViewProjectionMatrix, 0, viewProjectionMatrix,
0, modelMatrix, 0);
}
My matrix Helper
public static void perspectiveM(float[] m, float yFovInDegrees, float aspect, float n, float f) {
final float angleInRadians = (float) (yFovInDegrees * Math.PI / 180.0);
final float a = (float) (1.0 / Math.tan(angleInRadians / 2.0));
m[0] = a / aspect;
m[1] = 0f;
m[2] = 0f;
m[3] = 0f;
m[4] = 0f;
m[5] = a;
m[6] = 0f;
m[7] = 0f;
m[8] = 0f;
m[9] = 0f;
m[10] = -((f + n) / (f - n));
m[11] = -1f;
m[12] = 0f;
m[13] = 0f;
m[14] = -((2f * f * n) / (f - n));
m[15] = 0f;
}
问题很可能出在你设置投影矩阵的方式上:
MatrixHelper.perspectiveM(projectionMatrix, 45, (float) width
/ (float) height, 0f, 10f);
这个函数定义中的第四个参数是近平面。这个值永远不应该是0.0。它通常应该是远距离的一个合理的分数。选择理想值可能是一种权衡。far / near
越大,深度精度越低。另一方面,如果将near
值设置得太大,则可能会截断实际想要看到的接近几何图形。
far / near
的比率可能是100或1000,通常应该给你合理的深度精度,没有不希望的前削。如果你使用16位深度缓冲区,你需要比使用24位深度缓冲区更保守一些。
对于您的目的,尝试将near
更改为0.1,看看它如何为您工作:
MatrixHelper.perspectiveM(projectionMatrix, 45, (float) width
/ (float) height, 0.1f, 10f);