0

足を濡らすためのシンプルなAndroidOpenGL2.0ゲームを作成しようとしています。私はOpenGLのAndroidチュートリアルを参照して起動し、正方形を目的の場所に移動しました。今はタッチで翻訳しようとしています。

現在の正方形の投影を解除する必要があることを読みました...しかし、これを理解していません。正方形で翻訳を実行するのに助けがあれば、以下は私のコードです...

 private float mPreviousY;

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        // MotionEvent reports input details from the touch screen
        // and other input controls. In this case, you are only
        // interested in events where the touch position changed.
    float y = e.getY();

    switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:

            float dy = y - mPreviousY;

            // reverse direction of rotation to left of the mid-line
            if (y < getHeight() / 2) {
              dy = dy * -1 ;
            }

            mRenderer.mOffSet += dy;
            requestRender();
    }

    mPreviousY = y;
    return true;
}

私のonDrawFrame:

    @Override
public void onDrawFrame(GL10 unused) {

    // Draw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -50, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    Matrix.translateM(mModleViewProjMatrix, 0, 0, mOffSet, 0);

    // Calculate the projection and view transformation
    Matrix.multiplyMM( mModleViewProjMatrix, 0, mProjMatrix, 0, mViewMatrix, 0);

    // Draw square
    mPaddle.draw(mModleViewProjMatrix);
}
4

1 に答える 1

1

非投影とは、変換時に頂点が受けるプロセスを逆にすることを意味します。順変換は

v_eye = Modelview · v

v_clip = Projection · v_eye

v_ndc = v_clip / v_clip.w

今あなたがしなければならないことは、このプロセスを逆にすることです. Mesa の GLU 関数 gluUnProject のソースコードを参照することをお勧めします

アップデート

アンプロジェクションは、基本的にプロセスを逆にします。

Mesa の GLU gluUnProject コードを見てみましょう。

GLint GLAPIENTRY
gluUnProject(GLdouble winx, GLdouble winy, GLdouble winz,
        const GLdouble modelMatrix[16], 
        const GLdouble projMatrix[16],
                const GLint viewport[4],
            GLdouble *objx, GLdouble *objy, GLdouble *objz)
{
    double finalMatrix[16];
    double in[4];
    double out[4];

最初に複合変換Projection · Modelviewが評価されます…</p>

    __gluMultMatricesd(modelMatrix, projMatrix, finalMatrix);

…そして反転、つまり反転。

    if (!__gluInvertMatrixd(finalMatrix, finalMatrix)) return(GL_FALSE);

    in[0]=winx;
    in[1]=winy;
    in[2]=winz;
    in[3]=1.0;

次に、ウィンドウ/ビューポートの座標が NDC 座標にマップされます

    /* Map x and y from window coordinates */
    in[0] = (in[0] - viewport[0]) / viewport[2];
    in[1] = (in[1] - viewport[1]) / viewport[3];

    /* Map to range -1 to 1 */
    in[0] = in[0] * 2 - 1;
    in[1] = in[1] * 2 - 1;
    in[2] = in[2] * 2 - 1;

そして、複合射影モデルビューの逆を掛けます

    __gluMultMatrixVecd(finalMatrix, in, out);

最後に、いわゆる均質成分がゼロでないことがチェックされます

    if (out[3] == 0.0) return(GL_FALSE);

そして同質の分裂は逆転した。

    out[0] /= out[3];
    out[1] /= out[3];
    out[2] /= out[3];

投影プロセス前の元の頂点位置になります

    *objx = out[0];
    *objy = out[1];
    *objz = out[2];
    return(GL_TRUE);
}
于 2012-12-23T23:19:48.323 に答える