I want to use gluUnProject for 3D picking. It returns wrong values. I think the reason is that ModelView & Projection matrices are generated incorrectly. To grab them, I use MatrixGrabber, MatrixStack and MatrixTrackingGL classes from API demos:
MatrixGrabber mg = new MatrixGrabber();
mg.getCurrentProjection(gl);
mg.getCurrentModelView(gl);
Wherever I place the camera, Projection & ModelView matrices are the same all the way:
ModelView
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
Projection
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
Some code:
private Game game;
private final float boardSize = 25f;
private float[] eyeV;
private float[] centerV;
private float[] upV;
private GL10 gl;
private MatrixGrabber mg;
public GLRenderer() {
game = new Game();
eyeV = new float[] { boardSize / 2f, boardSize / 5f, boardSize * 1.2f };
centerV = new float[] { boardSize / 2f, boardSize / 2f, 0f };
upV = new float[] { 0f, 1f, 0f };
mg = new MatrixGrabber();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
this.gl = gl;
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glDisable(GL10.GL_DITHER);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(.2f, .2f, .2f, 1f);
gl.glClearDepthf(1f);
gl.glEnable(GL10.GL_TEXTURE_2D);
game.LoadTextures(gl);
}
public void onDrawFrame(GL10 gl) {
gl.glDisable(GL10.GL_DITHER);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, eyeV[0], eyeV[1], eyeV[2], centerV[0], centerV[1], centerV[2], upV[0], upV[1], upV[2]);
game.draw(gl);
mg.getCurrentProjection(gl);
mg.getCurrentModelView(gl);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float ratio = (float) width / (float) height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 100);
}
Thanks a lot for any advice!