2012-01-01 2 views
0

나는 이것을 알아낼 수 없다. 오래된 고정 함수 파이프 라인에서 프로그래밍 가능 파이프 라인으로 변환하는 OpenGL 응용 프로그램이 있는데 사용중인 deprecated 함수를 제거하고 있습니다 ... 수학에 관해서는 최고가 아니에요. , 그리고 행렬은 나에게 상당히 새로운 것이다.왜 원근 투영 행렬 계산이 올바른 결과를 제공하지 않습니까?

그러나 ModelView 행렬은 정상적으로 작동하지만 Perspective 행렬이 제대로 작동하지 않는 것 같습니다.

고정 된 함수가 사용되지 않는 함수를 사용할 수 있으며, 매트릭스의 값을 가져 와서 생성 된 계산 된 값과 일치시킬 수 있습니다. ModelView? 문제 없어. 원근법? 그것은 일치하지 않으며, 나는 왜 이해하지 못합니다.

두 방법은 I

직접 관찰 행렬의 값을 비교하여 두 값을 확인 내있어 전처리 타입 #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 

첫 번째는 부동 소수점 값을 생성합니다. 두 번째는 정수를 생성합니다. 나는 당신이 첫번째를 원한다고 생각합니다.

+0

사실 그건 내 바보 같은 실수였습니다. 그러나 문제는 여전히 남아 있습니다. 번호가 아직 일치하지 않습니다. 나는 projectionmatrix 요소의 약간의 불일치가 단지 반올림 오류이거나 사용중인 pi의 유효 자릿수라고 생각하지만 projectionmatrix [15]는 완전히 잘못되었습니다. – Azuvector

+0

앤드랜드, 쓰레기. 문제를 찾았습니다. 행렬을 잘못 초기화 했습니까? 코드를 수정하여 문제가 해결되었습니다. 너무 맹인으로 보았음을 분명히 지적 해 주셔서 감사합니다. :) – Azuvector