1

我想使用 gluUnProject 进行 3D 拾取。它返回错误的值。我认为原因是 ModelView & Projection 矩阵生成不正确。为了获取它们,我使用 API 演示中的 MatrixGrabber、MatrixStack 和 MatrixTrackingGL 类:

MatrixGrabber mg = new MatrixGrabber();  
mg.getCurrentProjection(gl);  
mg.getCurrentModelView(gl);  

无论我把相机放在哪里,Projection 和 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);
}

非常感谢您的任何建议!

4

0 回答 0