我想使用gluUnProject进行3D拾取。它返回错误的值。我认为原因是ModelView&投影矩阵生成错误。要抓住它们,我使用API演示中的MatrixGrabber,MatrixStack和MatrixTrackingGL类:
MatrixGrabber mg = new MatrixGrabber();
mg.getCurrentProjection(gl);
mg.getCurrentModelView(gl);
无论我放置相机,Projection& ModelView矩阵始终如一:
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
投影
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
一些代码:
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);
}
非常感谢任何建议!