android opengl app behaves differently on device, z-far is different?
I have a simple opengl app that renders arbitrary 3d objects. On the emulator things work great. On my nexus on, not so good.
The model is rendered, but it's clipped to almost nothing in the far z. I use the model's bounding sphere diameter as my far Z value. The code seems pretty boilerplate, it's included below for reference.
The problem must be with the call to gluPerspective(), but I can't understand what about that would differ between devices. the only difference is that the aspect ratio is slightly different. I tried hard coding the value of farZ to something real big and that fixed the problem. any ideas?
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0f, 0f, 2f*mesh.diameter, 0f, 0f, 0f, 0f, 1f, 0f);
gl.glPushMatrix();
gl.glRotatef(mesh.rx, 1, 0, 0);
gl.glRotatef(mesh.ry, 0, 1, 0);
mesh.draw(gl);
gl.glPopMatrix();
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
float zNear = 0.1f;
float zFar = zNear + mesh.diameter;
float aspect = (float) width / (float) height;
float left = mesh.mid.x - mesh.diameter;
float right = mesh.mid.x + mesh.diameter;
float bottom = mesh.mid.y - mesh.diameter;
float top = mesh.mid.y + mesh.diameter;
/*
if (aspect < 1.0) { // window taller than wide
bottom /= aspect;
top /= aspect;
} else {
bottom *= aspect;
top *= aspect;
}
*/
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 90.0f, aspect, zNear, zFar);
gl.glMatrixMode(GL1开发者_高级运维0.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(left, right, bottom, top, zNear, zFar);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
Simply increasing the zFar solved the issue. I still don't have an explanation as to why one zFar worked on the emulator but did not on the device.
精彩评论