Matrix In OpenGL

来源:互联网 发布:数据分析师主要做什么 编辑:程序博客网 时间:2024/05/17 09:14

Got some idea with my class of matrix operation facilitated by OpenGL original pipeline.

  Notice that the matrix in OpenGL is column-major which represents the following matrix:
  a column-major model-view matrix(fetched from OpenGL):
row 0: Xx  Xy  Xz  Xw
row 1: Yx  Yy  Yz  Yw
row 2: Zx  Zy  Zz  Zw
row 3: Tx  Ty  Tz  Tw
  is the sequence in C/C++ if a model-view matrix is fetched from OpenGL because of the column-major
  sequence in OpenGL. If in a row-major matrix, such as in DirectX or C++, this matrix should be
  like this:
  a row-major matrix we usually used in C/C++:
row 0:   Xx  Yx  Zx  Tx
row 1:   Xy  Yy  Zy  Ty
row 2:   Xz  Yz  Zz  Yz
row 3:   Xw  Yw  Zw  Tw
  So, if you wish to apply a transformation with no errors or you don't want a transformation you
are not trying to have in OpenGL, use the 1st matrix form.
  In C/C++, a 1 dimension array for being the matrix can be used in OpenGL with the right form
should be like:
GLfloat modelViewMatrix = { Xx, Xy, Xz, Xw, 
                                             Yx, Yy, Yz, Yw, 
                                             Zx, Zy, Zz, Zw, 
                                             Tx, Ty, Tz, Tw}; 

Looks weird, huh? But that's it in GL, and we'd better get used to it and keep focused while applying custom transformation!
/****************************************************************************************/
But in GLSL use row-major ways to select certain element.
if a matrix is mat4 mat = {1.0,    2.0,    3.0,    1.0, 
                                        4.0,   5.0,    6.0,    7.0,
                                        8.0,   9.0,   10.0,   11.0,
                                        12.0, 13.0, 14.0,   15.0};
select 12.0 by using mat[3][0], Where 3 as column index and 0 as row index, like mat[col][row]. Column idx in front of row idx.
0 0
原创粉丝点击