2012-01-01 9 views
0

私はこれを理解できません。 私は古い固定機能のパイプラインからプログラム可能なパイプラインに変換して、私が使ってきた廃止された関数を取り除くOpenGLアプリケーションを持っています... 私はそれが数学になると最高です行列はかなり新しいものです。なぜこのパースペクティブ射影行列計算で正しい結果が得られないのですか?

しかし、私のModelViewマトリックスはうまくいきましたが、私はPerspectiveマトリックスを正しく動作させることができません。

私は、固定機能の廃止予定関数を使用して、行列の値を取得し、これを私が生成した計算値に一致させることができます... ModelView?問題ない。展望?それは一致しない、なぜ私は理解していない。

二つの方法は、私が直接観察により、及びマトリックス値を比較することにより、両方の私の値をチェックしていプリプロセッサの#define USEPROJMATRIX


typedef float vec_t; 

const static vec_t identityMatrix[16] = { 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 }; 

const static vec_t zeroMatrix[16] = { 0.0, 0.0, 0.0, 0.0, 
             0.0, 0.0, 0.0, 0.0, 
             0.0, 0.0, 0.0, 0.0, 
             0.0, 0.0, 0.0, 0.0 }; 

const double pi = 3.1415926535897932384626433832795; 

float Utils::Radians(const float& degrees) 
{ 
    return degrees * (pi/180); 
} 

vec_t* Utils::FrustumMatrix(vec_t* out_matrix, const vec_t& left, const vec_t& right, const vec_t& bottom, const vec_t& top, const vec_t& znear, const vec_t& zfar) // Replaced glFrustum() 
{ 
    memcpy(out_matrix, zeroMatrix, 16*sizeof(vec_t)); 

    out_matrix[0] = (2.0 * znear)/(right - left); 
    out_matrix[5] = (2.0 * znear)/(top - bottom); 
    out_matrix[8] = (right + left)/(right - left); 
    out_matrix[9] = (top + bottom)/(top - bottom); 
    out_matrix[10] = -((zfar + znear)/(zfar - znear)); 
    out_matrix[11] = -1.0; 
    out_matrix[14] = -((2.0 * zfar * znear)/(zfar - znear)); 

    return out_matrix; 
} 

vec_t* Utils::PerspectiveMatrix(vec_t* out_matrix, const vec_t& yFOVDegrees, const vec_t& aspectRatio, const vec_t& znear, const vec_t& zfar) // Replaces gluPerspective() 
{ 
    vec_t range, left, right, bottom, top; 

    range = tan(Radians(yFOVDegrees/2)) * znear; 
    left = -range * aspectRatio; 
    right = range * aspectRatio; 
    bottom = -range; 
    top = range; 

    return FrustumMatrix(out_matrix, left, right, bottom, top, znear, zfar); 
} 

#define USEPROJMATRIX 
void GameLogic::OnResize(int width = 640, int height = 480) 
{ 
    glViewport(0, 0, width, height); 
    glMatrixMode(GL_PROJECTION); 
#ifndef USEPROJMATRIX 
    glLoadMatrixf(identityMatrix); 
    gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f); 
#else 
    vec_t pMatrix[16]; 
    memcpy(pMatrix, identityMatrix, sizeof(vec_t) * 16); 
    Utils::PerspectiveMatrix(pMatrix, 45.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f); 
    glLoadMatrixf(pMatrix); 
#endif 

    vec_t projectionmatrix[16]; 
    glGetFloatv(GL_PROJECTION_MATRIX, projectionmatrix); 

    /* Matrix calculation results in projectionmatrix equalling this: 
     1.8106601, 0.00000000, 0.00000000, 0.00000000, 
     0.00000000, 2.4142134, 0.00000000, 0.00000000, 
     0.00000000, 0.00000000, -1.0020020, -1.0000000, 
     0.00000000, 0.00000000, -2.0020020, 0.0000000 
    */ 
    /* GL calculation 
     1.8106601, 0.00000000, 0.00000000, 0.00000000, 
     0.00000000, 2.4142137, 0.00000000, 0.00000000, 
     0.00000000, 0.00000000, -1.0020020, -1.0000000, 
     0.00000000, 0.00000000, -2.0020020, 0.00000000 
    */ 

    glMatrixMode(GL_MODELVIEW); 
    glLoadMatrixf(identityMatrix); 
} 

をコメントアウトすることで切り替えることができます。 結果の値は上記のコメントに記載されていますが、なぜ一致しないのか分かりません。手動で計算された行列は、レンダリングされたシーン(中央の左下にある単純な立方体のオフセット)を視覚的に歪ませ、正しく配置しないようにします。

編集 Nicol Bolasの答えによると愚かな間違いを修正しました。しかし、これはまだ正しく動作しません。コメントの値も更新されています。 edit2 上記のコードは問題なく解決しました。私の愚かな間違い。

答えて

3

これは、あなたのgluPerspectiveコールに使用するものです。

(GLfloat)width/(GLfloat)height 

これはあなたのUtils::PerspectiveMatrixコールに使用するものです。

width/height 

最初のものは、浮動小数点値を生成します。 2番目の要素は整数を生成します。私はあなたが最初を望んでいたと思います。

+0

それは私のところで本当に愚かな間違いでした。しかし、問題は残っている。番号はまだ一致していません。私は、projectionmatrix [5]要素のわずかな不一致は単なる丸め誤差またはpiの有効数字であると考えていますが、projectionmatrix [15]は完全に間違っています。 – Azuvector

+0

Annnnd、かわいこ。問題が見つかりました。私の行列を間違って初期化していた。問題を修正してコードを修正しました。私が目が見えないことを明らかにしてくれてありがとう。 :) – Azuvector

関連する問題