1. 程式人生 > >OpenGL中的矩陣儲存方式

OpenGL中的矩陣儲存方式

在OpenGL中,矩陣是以列優先的方式(column-major order)儲存的,而一般的數學書上是以行優先的方式(row-major order)儲存的。

列優先:

m0     m4     m8     m12

m1     m5     m9     m13

m2     m6     m10   m14

m3     m7     m11   m15

行優先:

 m0     m1     m2     m3

 m4     m5     m6     m7

 m8     m9     m10    m11

 m12   m13   m14   m15

  在OpenGL中,矩陣是用一維陣列來儲存的:m[16]。

可以用ARB_transpose_matrix來實現2者的轉換:

    New functions and tokens are added allowing application matrices
    stored in row major order rather than column major order to be
    transferred to the OpenGL implementation.  This allows an application
    to use standard C-language 2-dimensional arrays (m[row][col]) and
    have the array indices match the expected matrix row and column indexes.
    These arrays are referred to as transpose matrices since they are
    the transpose of the standard matrices passed to OpenGL.

    This extension adds an interface for transfering data to and from the
    OpenGL pipeline, it does not change any OpenGL processing or imply any
    changes in state representation.
提供的函式:

    void LoadTransposeMatrix{fd}ARB(T m[16]);
    void MultTransposeMatrix{fd}ARB(T m[16]);

提供的標記(對GetBooleanv, GetIntegerv, GetFloatv, GetDoublev函式):

 TRANSPOSE_MODELVIEW_MATRIX_ARB   
TRANSPOSE_PROJECTION_MATRIX_ARB   
TRANSPOSE_TEXTURE_MATRIX_ARB   
TRANSPOSE_COLOR_MATRIX_ARB  

LoadTransposeMatrixARB(m)相當於:

 float n[16];
transpose(m,n)
LoadMatrix(n);

MultTransposeMatrixARB(m)相當於:

 float n[16];
transpose(m,n);
MultMatrix(n);

GetFloatv(TRANSPOSE_MODELVIEW_MATRIX_ARB,m)相當於:

 float n[16];
GetFloatv(MODELVIEW_MATRIX_ARB,n);
transpose(n,m);

TRANSPOSE_PROJECTION_MATRIX_ARB,TRANSPOSE_TEXTURE_MATRIX_ARB和 TRANSPOSE_COLOR_MATRIX_ARB類似。