2013-11-15 2 views
0

OpenGL glRotatef (angle, x, y, z) 함수의 사용자 정의 구현을 코딩하려고합니다. 회전 행렬을 작성했지만 사용하려고하면 원래 함수와 효과가 다릅니다. 여기 내 코드가있다.OpenGl 사용자 지정 구현 회전

void mglRotate(float angle, float x, float y, float z) 
{ 
    float angle_rad = angle * (PI/180.0f); 

    float c = cos(angle_rad); 
    float s = sin(angle_rad); 
    float t = 1 - c; 

    float m[16] = { 
      c+x*x*t,y*x*t+z*s,z*x*t-y*s,0, 
      x*y*t-z*s,c+y*y*t,z*y*t+x*s,0, 
      x*z*t+y*s,y*z*t-x*s,z*z*t+c,0, 
      0,0,0,1 
    }; 

    glMultMatrixf(m); 
} 

내 실수는 어디에서 볼 수 있습니까?

+1

이 질문은 대수 선형 대수이기 때문에 논점이없는 것처럼 보입니다. –

답변

3

glm 라이브러리가 있는데, 이는 이전의 OpenGL 기능과 완전히 똑같습니다. 당신은 GLM에서 구현 구현을 비교하고

template <typename T> 
GLM_FUNC_QUALIFIER detail::tmat4x4<T> rotate 
(
    detail::tmat4x4<T> const & m, 
    T const & angle, 
    detail::tvec3<T> const & v 
) 
{ 
    T a = radians(angle); 
    T c = cos(a); 
    T s = sin(a); 

    detail::tvec3<T> axis = normalize(v); 

    detail::tvec3<T> temp = (T(1) - c) * axis; 

    detail::tmat4x4<T> Rotate(detail::tmat4x4<T>::null); 
    Rotate[0][0] = c + temp[0] * axis[0]; 
    Rotate[0][1] = 0 + temp[0] * axis[1] + s * axis[2]; 
    Rotate[0][2] = 0 + temp[0] * axis[2] - s * axis[1]; 

    Rotate[1][0] = 0 + temp[1] * axis[0] - s * axis[2]; 
    Rotate[1][1] = c + temp[1] * axis[1]; 
    Rotate[1][2] = 0 + temp[1] * axis[2] + s * axis[0]; 

    Rotate[2][0] = 0 + temp[2] * axis[0] + s * axis[1]; 
    Rotate[2][1] = 0 + temp[2] * axis[1] - s * axis[0]; 
    Rotate[2][2] = c + temp[2] * axis[2]; 

    detail::tmat4x4<T> Result(detail::tmat4x4<T>::null); 
    Result[0] = m[0] * Rotate[0][0] + m[1] * Rotate[0][1] + m[2] * Rotate[0][2]; 
    Result[1] = m[0] * Rotate[1][0] + m[1] * Rotate[1][1] + m[2] * Rotate[1][2]; 
    Result[2] = m[0] * Rotate[2][0] + m[1] * Rotate[2][1] + m[2] * Rotate[2][2]; 
    Result[3] = m[3]; 
    return Result; 
} 

코드에서 나에게 잘못된 것 한 가지는 당신이 축을 정상화가 없다는 것입니다 :) 그것을 알아낼 수 있습니다.

+0

축을 정규화했는데 이제는 모든 것이 작동합니다! – user1071138