danginsburg / opengles3-book

OpenGL ES 3.0 Programming Guide Sample Code
MIT License
1.94k stars 747 forks source link

Does rotMat in esRotate() should be the tranposed? #31

Closed JieX closed 8 years ago

JieX commented 8 years ago

After compared with mesa lib, i got confused with these matrixes. As the "frust" in esFrustum(), "result" in esTranslate() and esScale() are using the transpose of standard transform matrix, does "rotMat" in esRotate() should be the tranposed(as below)?

      rotMat.m[0][0] = ( oneMinusCos * xx ) + cosAngle;
      rotMat.m[1][0] = ( oneMinusCos * xy ) - zs;
      rotMat.m[2][0] = ( oneMinusCos * zx ) + ys;
      rotMat.m[3][0] = 0.0F;

      rotMat.m[0][1] = ( oneMinusCos * xy ) + zs;
      rotMat.m[1][1] = ( oneMinusCos * yy ) + cosAngle;
      rotMat.m[2][1] = ( oneMinusCos * yz ) - xs;
      rotMat.m[3][1] = 0.0F;

      rotMat.m[0][2] = ( oneMinusCos * zx ) - ys;
      rotMat.m[1][2] = ( oneMinusCos * yz ) + xs;
      rotMat.m[2][2] = ( oneMinusCos * zz ) + cosAngle;
      rotMat.m[3][2] = 0.0F;

      rotMat.m[0][3] = 0.0F;
      rotMat.m[1][3] = 0.0F;
      rotMat.m[2][3] = 0.0F;
      rotMat.m[3][3] = 1.0F;

but the orignal "rotMat" in esRotate() works:

      rotMat.m[0][0] = ( oneMinusCos * xx ) + cosAngle;
      rotMat.m[0][1] = ( oneMinusCos * xy ) - zs;
      rotMat.m[0][2] = ( oneMinusCos * zx ) + ys;
      rotMat.m[0][3] = 0.0F;

      rotMat.m[1][0] = ( oneMinusCos * xy ) + zs;
      rotMat.m[1][1] = ( oneMinusCos * yy ) + cosAngle;
      rotMat.m[1][2] = ( oneMinusCos * yz ) - xs;
      rotMat.m[1][3] = 0.0F;

      rotMat.m[2][0] = ( oneMinusCos * zx ) - ys;
      rotMat.m[2][1] = ( oneMinusCos * yz ) + xs;
      rotMat.m[2][2] = ( oneMinusCos * zz ) + cosAngle;
      rotMat.m[2][3] = 0.0F;

      rotMat.m[3][0] = 0.0F;
      rotMat.m[3][1] = 0.0F;
      rotMat.m[3][2] = 0.0F;
      rotMat.m[3][3] = 1.0F;
danginsburg commented 8 years ago

So in the book source code the matrices are a 2-D array. In mesa, they are a 1-D array stored in column major order.

Mesa defines:

define M(row,col) m[col*4+row]

So, for example, M(1,3) would be m[3 * 4+1] or m[13]. In our matrices, m[3][1] would be in linear address m[3*4+1] which is the same thing.

I think what is throwing you for a loop is that the matrices are in column major order and the M macro is row,col and when you look at our code it is always [col][row].