2012-02-16 16 views
0

我正在做一些與iPhone上的opengl工作,我被困在一個特定的點。互聯網上的所有代碼示例都向您展示了矩陣如何圍繞x軸,y軸或z軸旋轉,但沒有人談論矩陣如何繞任意點旋轉?iPhone的開放:關於一個點旋轉矩陣

我正在使用open gl es 2.0。任何幫助,將不勝感激。

問候,

回答

1

這聽起來像你問如何構建這些軸之一左右旋轉矩陣,但在不同的點。你這樣做的方法是首先轉換到這一點,然後爲你想要的軸應用旋轉。矩陣乘法的順序取決於您是否將其視爲軸移動或幾何。

如果希望能夠在同一時間做任意的X,Y,Z角的旋轉,你可以用矩陣討論in this article

static inline void Matrix3DSetRotationByRadians(Matrix3D matrix, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) 
{ 
    GLfloat mag = sqrtf((x*x) + (y*y) + (z*z)); 
    if (mag == 0.0) 
    { 
     x = 1.0; 
     y = 0.0; 
     z = 0.0; 
    } 
    else if (mag != 1.0) 
    { 
     x /= mag; 
     y /= mag; 
     z /= mag; 
    } 

    GLfloat c = cosf(angle); 
    GLfloat s = fastSinf(angle); 
    matrix[3] = matrix[7] = matrix[11] = matrix[12] = matrix[13] = matrix[14] = 0.0; 
    matrix[15] = 1.0; 


    matrix[0] = (x*x)*(1-c) + c; 
    matrix[1] = (y*x)*(1-c) + (z*s); 
    matrix[2] = (x*z)*(1-c) - (y*s); 
    matrix[4] = (x*y)*(1-c)-(z*s); 
    matrix[5] = (y*y)*(1-c)+c; 
    matrix[6] = (y*z)*(1-c)+(x*s); 
    matrix[8] = (x*z)*(1-c)+(y*s); 
    matrix[9] = (y*z)*(1-c)-(x*s); 
    matrix[10] = (z*z)*(1-c)+c; 

} 
+0

截至目前我已經使用CA3dTransforms來做旋轉。它現在的作品,但我有一種感覺,它是一個樂隊援助解決方案。 – Abhi 2012-02-16 21:16:35

+0

好吧我想我明白了....但我有2個問題(a)當我有一個特定幾何的矩陣表示時,它究竟意味着什麼。我猜測矩陣絕對與物理屏幕座標無關。 (b)如果矩陣只是一種表示,我該如何翻譯成某一點。 – Abhi 2012-02-17 17:25:41