daily pastebin goal
29%
SHARE
TWEET

Untitled

a guest May 9th, 2018 36 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h> // For printf
  2.  
  3. class v4
  4. {
  5. public:
  6.     double x;
  7.     double y;
  8.     double z;
  9.     double w;
  10.  
  11.     v4() {}
  12.  
  13.     v4(double a_x, double a_y, double a_z, double a_w)
  14.     {
  15.         x = a_x;
  16.         y = a_y;
  17.         z = a_z;
  18.         w = a_w;
  19.     }
  20. };
  21.  
  22. class m4
  23. {
  24. public:
  25.     v4 x;
  26.     v4 y;
  27.     v4 z;
  28.     v4 w;
  29.  
  30.     m4() {};
  31.  
  32.     m4(const double xx, const double xy, const double xz, const double xw,
  33.         const double yx, const double yy, const double yz, const double yw,
  34.         const double zx, const double zy, const double zz, const double zw,
  35.         const double wx, const double wy, const double wz, const double ww)
  36.         :
  37.         x(xx, xy, xz, xw),
  38.         y(yx, yy, yz, yw),
  39.         z(zx, zy, zz, zw),
  40.         w(wx, wy, wz, ww)
  41.     {
  42.     }
  43. };
  44.  
  45.  
  46. // Full matrix multiply A x B
  47. static m4 test_mul(const m4& matA, const m4& matB)
  48. {
  49. #if 1 //BAD
  50.     return m4
  51.     (
  52.         (matA.x.x * matB.x.x) + (matA.x.y * matB.y.x) + (matA.x.z * matB.z.x) + (matA.x.w * matB.w.x),
  53.         (matA.x.x * matB.x.y) + (matA.x.y * matB.y.y) + (matA.x.z * matB.z.y) + (matA.x.w * matB.w.y),
  54.         (matA.x.x * matB.x.z) + (matA.x.y * matB.y.z) + (matA.x.z * matB.z.z) + (matA.x.w * matB.w.z),
  55.         (matA.x.x * matB.x.w) + (matA.x.y * matB.y.w) + (matA.x.z * matB.z.w) + (matA.x.w * matB.w.w),
  56.  
  57.         (matA.y.x * matB.x.x) + (matA.y.y * matB.y.x) + (matA.y.z * matB.z.x) + (matA.y.w * matB.w.x),
  58.         (matA.y.x * matB.x.y) + (matA.y.y * matB.y.y) + (matA.y.z * matB.z.y) + (matA.y.w * matB.w.y),
  59.         (matA.y.x * matB.x.z) + (matA.y.y * matB.y.z) + (matA.y.z * matB.z.z) + (matA.y.w * matB.w.z),
  60.         (matA.y.x * matB.x.w) + (matA.y.y * matB.y.w) + (matA.y.z * matB.z.w) + (matA.y.w * matB.w.w),
  61.  
  62.         (matA.z.x * matB.x.x) + (matA.z.y * matB.y.x) + (matA.z.z * matB.z.x) + (matA.z.w * matB.w.x),
  63.         (matA.z.x * matB.x.y) + (matA.z.y * matB.y.y) + (matA.z.z * matB.z.y) + (matA.z.w * matB.w.y),
  64.         (matA.z.x * matB.x.z) + (matA.z.y * matB.y.z) + (matA.z.z * matB.z.z) + (matA.z.w * matB.w.z),
  65.         (matA.z.x * matB.x.w) + (matA.z.y * matB.y.w) + (matA.z.z * matB.z.w) + (matA.z.w * matB.w.w),
  66.  
  67.         (matA.w.x * matB.x.x) + (matA.w.y * matB.y.x) + (matA.w.z * matB.z.x) + (matA.w.w * matB.w.x),
  68.         (matA.w.x * matB.x.y) + (matA.w.y * matB.y.y) + (matA.w.z * matB.z.y) + (matA.w.w * matB.w.y),
  69.         (matA.w.x * matB.x.z) + (matA.w.y * matB.y.z) + (matA.w.z * matB.z.z) + (matA.w.w * matB.w.z),
  70.         (matA.w.x * matB.x.w) + (matA.w.y * matB.y.w) + (matA.w.z * matB.z.w) + (matA.w.w * matB.w.w)
  71.     );
  72. #endif
  73.  
  74. #if 0 //GOOD
  75.     m4 out;
  76.  
  77.     out.x.x = (matA.x.x * matB.x.x) + (matA.x.y * matB.y.x) + (matA.x.z * matB.z.x) + (matA.x.w * matB.w.x);
  78.     out.x.y = (matA.x.x * matB.x.y) + (matA.x.y * matB.y.y) + (matA.x.z * matB.z.y) + (matA.x.w * matB.w.y);
  79.     out.x.z = (matA.x.x * matB.x.z) + (matA.x.y * matB.y.z) + (matA.x.z * matB.z.z) + (matA.x.w * matB.w.z);
  80.     out.x.w = (matA.x.x * matB.x.w) + (matA.x.y * matB.y.w) + (matA.x.z * matB.z.w) + (matA.x.w * matB.w.w);
  81.  
  82.     out.y.x = (matA.y.x * matB.x.x) + (matA.y.y * matB.y.x) + (matA.y.z * matB.z.x) + (matA.y.w * matB.w.x);
  83.     out.y.y = (matA.y.x * matB.x.y) + (matA.y.y * matB.y.y) + (matA.y.z * matB.z.y) + (matA.y.w * matB.w.y);
  84.     out.y.z = (matA.y.x * matB.x.z) + (matA.y.y * matB.y.z) + (matA.y.z * matB.z.z) + (matA.y.w * matB.w.z);
  85.     out.y.w = (matA.y.x * matB.x.w) + (matA.y.y * matB.y.w) + (matA.y.z * matB.z.w) + (matA.y.w * matB.w.w);
  86.  
  87.     out.z.x = (matA.z.x * matB.x.x) + (matA.z.y * matB.y.x) + (matA.z.z * matB.z.x) + (matA.z.w * matB.w.x);
  88.     out.z.y = (matA.z.x * matB.x.y) + (matA.z.y * matB.y.y) + (matA.z.z * matB.z.y) + (matA.z.w * matB.w.y);
  89.     out.z.z = (matA.z.x * matB.x.z) + (matA.z.y * matB.y.z) + (matA.z.z * matB.z.z) + (matA.z.w * matB.w.z);
  90.     out.z.w = (matA.z.x * matB.x.w) + (matA.z.y * matB.y.w) + (matA.z.z * matB.z.w) + (matA.z.w * matB.w.w);
  91.  
  92.     out.w.x = (matA.w.x * matB.x.x) + (matA.w.y * matB.y.x) + (matA.w.z * matB.z.x) + (matA.w.w * matB.w.x);
  93.     out.w.y = (matA.w.x * matB.x.y) + (matA.w.y * matB.y.y) + (matA.w.z * matB.z.y) + (matA.w.w * matB.w.y);
  94.     out.w.z = (matA.w.x * matB.x.z) + (matA.w.y * matB.y.z) + (matA.w.z * matB.z.z) + (matA.w.w * matB.w.z);
  95.     out.w.w = (matA.w.x * matB.x.w) + (matA.w.y * matB.y.w) + (matA.w.z * matB.z.w) + (matA.w.w * matB.w.w);
  96.  
  97.     return m4(out.x.x, out.x.y, out.x.z, out.x.w,
  98.         out.y.x, out.y.y, out.y.z, out.y.w,
  99.         out.z.x, out.z.y, out.z.z, out.z.w,
  100.         out.w.x, out.w.y, out.w.z, out.w.w);
  101.  
  102. #endif
  103.  
  104. #if 0 //GOOD
  105.     m4 out;
  106.  
  107.     out.x.x = (matA.x.x * matB.x.x) + (matA.x.y * matB.y.x) + (matA.x.z * matB.z.x) + (matA.x.w * matB.w.x);
  108.     out.x.y = (matA.x.x * matB.x.y) + (matA.x.y * matB.y.y) + (matA.x.z * matB.z.y) + (matA.x.w * matB.w.y);
  109.     out.x.z = (matA.x.x * matB.x.z) + (matA.x.y * matB.y.z) + (matA.x.z * matB.z.z) + (matA.x.w * matB.w.z);
  110.     out.x.w = (matA.x.x * matB.x.w) + (matA.x.y * matB.y.w) + (matA.x.z * matB.z.w) + (matA.x.w * matB.w.w);
  111.  
  112.     out.y.x = (matA.y.x * matB.x.x) + (matA.y.y * matB.y.x) + (matA.y.z * matB.z.x) + (matA.y.w * matB.w.x);
  113.     out.y.y = (matA.y.x * matB.x.y) + (matA.y.y * matB.y.y) + (matA.y.z * matB.z.y) + (matA.y.w * matB.w.y);
  114.     out.y.z = (matA.y.x * matB.x.z) + (matA.y.y * matB.y.z) + (matA.y.z * matB.z.z) + (matA.y.w * matB.w.z);
  115.     out.y.w = (matA.y.x * matB.x.w) + (matA.y.y * matB.y.w) + (matA.y.z * matB.z.w) + (matA.y.w * matB.w.w);
  116.  
  117.     out.z.x = (matA.z.x * matB.x.x) + (matA.z.y * matB.y.x) + (matA.z.z * matB.z.x) + (matA.z.w * matB.w.x);
  118.     out.z.y = (matA.z.x * matB.x.y) + (matA.z.y * matB.y.y) + (matA.z.z * matB.z.y) + (matA.z.w * matB.w.y);
  119.     out.z.z = (matA.z.x * matB.x.z) + (matA.z.y * matB.y.z) + (matA.z.z * matB.z.z) + (matA.z.w * matB.w.z);
  120.     out.z.w = (matA.z.x * matB.x.w) + (matA.z.y * matB.y.w) + (matA.z.z * matB.z.w) + (matA.z.w * matB.w.w);
  121.  
  122.     out.w.x = (matA.w.x * matB.x.x) + (matA.w.y * matB.y.x) + (matA.w.z * matB.z.x) + (matA.w.w * matB.w.x);
  123.     out.w.y = (matA.w.x * matB.x.y) + (matA.w.y * matB.y.y) + (matA.w.z * matB.z.y) + (matA.w.w * matB.w.y);
  124.     out.w.z = (matA.w.x * matB.x.z) + (matA.w.y * matB.y.z) + (matA.w.z * matB.z.z) + (matA.w.w * matB.w.z);
  125.     out.w.w = (matA.w.x * matB.x.w) + (matA.w.y * matB.y.w) + (matA.w.z * matB.z.w) + (matA.w.w * matB.w.w);
  126.  
  127.     return out;
  128. #endif
  129. }
  130.  
  131. void print_m4(const m4& a_mat, const char* a_label)
  132. {
  133.     printf("%s\n", a_label);
  134.     printf("%f, %f, %f, %f\n", a_mat.x.x, a_mat.x.y, a_mat.x.z, a_mat.x.w);
  135.     printf("%f, %f, %f, %f\n", a_mat.y.x, a_mat.y.y, a_mat.y.z, a_mat.y.w);
  136.     printf("%f, %f, %f, %f\n", a_mat.z.x, a_mat.z.y, a_mat.z.z, a_mat.z.w);
  137.     printf("%f, %f, %f, %f\n", a_mat.w.x, a_mat.w.y, a_mat.w.z, a_mat.w.w);
  138. }
  139.  
  140.  
  141. int main()
  142. {
  143.     // REPRO:
  144.     // Build with VS 15.7.1 Debug, 64bit
  145.     // Notice the last element of
  146.     // Try comparing BAD vs GOOD #def'd blocks
  147.     // Also, with the BAD block enabled, compare Debug and Release behaviors
  148.  
  149.     const m4 mat(0, 1, 2, 3,
  150.         4, 5, 6, 7,
  151.         8, 9, 10, 11,
  152.         12, 13, 14, 15);
  153.  
  154.     const m4 id(1, 0, 0, 0,
  155.         0, 1, 0, 0,
  156.         0, 0, 1, 0,
  157.         0, 0, 0, 1);
  158.  
  159.     const m4 test = test_mul(mat, id);
  160.  
  161.     print_m4(test, "test_mul");
  162.  
  163.     getchar();
  164.  
  165.     return 0;
  166. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top