danginsburg / opengles3-book

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

Does "rotate" function and "frustum" funtion should works in the same way when compare with mesa lib?? #33

Open JieX opened 8 years ago

JieX commented 8 years ago

As I can't reopen #31 , so i open a new issue. I think there are still something wrong in transform functions. When compare mesa code and es3 book code, in "rotate" function, M(row, col) == m[row][col]. but in "frustum" funtions, _M(row, col) == m[col][row]_. as mesa use 1-D array "M[ ]" stored in column-major, and "#define M(row,col) m[col * 4+row]"; es3 book source code use 2-D array "m[ ][ ]".

Does "rotate" function and "frustum" functions both should be either M(row, col) ==m[row][col] or M(row, col) == m[col][row]??? Or say another way, if we compare the data sequence of 2-D array "m[ ][ ]" which store row by row in the client memory, with the data sequence of 1-D array "M[ ]" in mesa in the client memory, they should be same?? And the data sequences are same when i compare "frustum" & "tranlation" & "scale" functions between mesa code and es3 book code, but "rotate" function doesn't, that's weird.

Mesa v10.6.5 rotate works:

      xx = x * x;
      yy = y * y;
      zz = z * z;
      xy = x * y;
      yz = y * z;
      zx = z * x;
      xs = x * s;
      ys = y * s;
      zs = z * s;
      one_c = 1.0F - c;

      /* We already hold the identity-matrix so we can skip some statements */
      M(0,0) = (one_c * xx) + c;
      M(0,1) = (one_c * xy) - zs;
      M(0,2) = (one_c * zx) + ys;
/*    M(0,3) = 0.0F; */

      M(1,0) = (one_c * xy) + zs;
      M(1,1) = (one_c * yy) + c;
      M(1,2) = (one_c * yz) - xs;
/*    M(1,3) = 0.0F; */

      M(2,0) = (one_c * zx) - ys;
      M(2,1) = (one_c * yz) + xs;
      M(2,2) = (one_c * zz) + c;
/*    M(2,3) = 0.0F; */
es3 book code rotate works:

      xx = x * x;
      yy = y * y;
      zz = z * z;
      xy = x * y;
      yz = y * z;
      zx = z * x;
      xs = x * sinAngle;
      ys = y * sinAngle;
      zs = z * sinAngle;
      oneMinusCos = 1.0f - cosAngle;

      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;

Then in above codes, M(row, col) == m[row][col], The single data sequence in two matrixes are not in the same sequence in the client memory.

however, in frustum function,

Mesa v10.6.5 frustum works:

   x = (2.0F*nearval) / (right-left);
   y = (2.0F*nearval) / (top-bottom);
   a = (right+left) / (right-left);
   b = (top+bottom) / (top-bottom);
   c = -(farval+nearval) / ( farval-nearval);
   d = -(2.0F*farval*nearval) / (farval-nearval);  /* error? */

#define M(row,col)  m[col*4+row]
   M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = a;      M(0,3) = 0.0F;
   M(1,0) = 0.0F;  M(1,1) = y;     M(1,2) = b;      M(1,3) = 0.0F;
   M(2,0) = 0.0F;  M(2,1) = 0.0F;  M(2,2) = c;      M(2,3) = d;
   M(3,0) = 0.0F;  M(3,1) = 0.0F;  M(3,2) = -1.0F;  M(3,3) = 0.0F;
#undef M
es3 book code frustum works:

   frust.m[0][0] = 2.0f * nearZ / deltaX;
   frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f;

   frust.m[1][1] = 2.0f * nearZ / deltaY;
   frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f;

   frust.m[2][0] = ( right + left ) / deltaX;
   frust.m[2][1] = ( top + bottom ) / deltaY;
   frust.m[2][2] = - ( nearZ + farZ ) / deltaZ;
   frust.m[2][3] = -1.0f;

   frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ;
   frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f;

in above codes, _M(row, col) == m[col][row] , as M(0,2) == m[2][0], M(1,2) == m[2][1], M(2,3) == m[3][2], ._ The single data sequence in two matrixes are in the same sequence in the client memory as 2-D array m[ ][ ] stores row by row.