Atrus

Citro3D testing

Jun 17th, 2021
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  #include <stdbool.h>
  9.  #include <stdint.h>
  10.  #define M_PI 3.14159265358979323846 /* pi */
  11.  typedef uint8_t u8;
  12.  typedef uint32_t u32;
  13.  
  14.  typedef u32 C3D_IVec;
  15.  
  16.  static inline C3D_IVec IVec_Pack(u8 x, u8 y, u8 z, u8 w)
  17.  {
  18.      return (u32)x | ((u32)y << 8) | ((u32)z << 16) | ((u32)w << 24);
  19.  }
  20.  
  21.  typedef union
  22.  {
  23.      struct
  24.      {
  25.          float w;
  26.          float z;
  27.          float y;
  28.          float x;
  29.      };
  30.  
  31.      struct
  32.      {
  33.          float r;
  34.          float k;
  35.          float j;
  36.          float i;
  37.      };
  38.  
  39.      float c[4];
  40.  } C3D_FVec;
  41.  
  42.  typedef C3D_FVec C3D_FQuat;
  43.  
  44.  typedef union
  45.  {
  46.      C3D_FVec r[4];
  47.      float m[4*4];
  48.  } C3D_Mtx;
  49.  
  50.  
  51.  static inline C3D_FVec FVec4_New(float x, float y, float z, float w)
  52.  {
  53.      return (C3D_FVec){{ w, z, y, x }};
  54.  }
  55.  
  56.  static inline C3D_FVec FVec3_New(float x, float y, float z)
  57.  {
  58.      return FVec4_New(x, y, z, 0.0f);
  59.  }
  60.  
  61.  static inline float FVec4_Dot(C3D_FVec lhs, C3D_FVec rhs)
  62.  {
  63.      // A∙B = sum of component-wise products
  64.      return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z + lhs.w*rhs.w;
  65.  }
  66.  
  67.  static inline float FVec3_Dot(C3D_FVec lhs, C3D_FVec rhs)
  68.  {
  69.      // A∙B = sum of component-wise products
  70.      return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
  71.  }
  72.  
  73.  static inline float FVec3_Magnitude(C3D_FVec v)
  74.  {
  75.      // ‖v‖ = √(v∙v)
  76.      return sqrtf(FVec3_Dot(v,v));
  77.  }
  78.  
  79.  static inline C3D_FVec FVec3_Normalize(C3D_FVec v)
  80.  {
  81.      // get vector magnitude
  82.      float m = FVec3_Magnitude(v);
  83.  
  84.      // scale by inverse magnitude to get a unit vector
  85.      return FVec3_New(v.x/m, v.y/m, v.z/m);
  86.  }
  87.  
  88.  void Mtx_Translate(C3D_Mtx* mtx, float x, float y, float z, bool bRightSide)
  89.  {
  90.  
  91.      C3D_FVec v = FVec4_New(x, y, z, 1.0f);
  92.      int i, j;
  93.  
  94.      if (bRightSide)
  95.      {
  96.          for (i = 0; i < 4; ++i)
  97.              mtx->r[i].w = FVec4_Dot(mtx->r[i], v);
  98.      }
  99.      else
  100.      {
  101.          for (j = 0; j < 3; ++j)
  102.              for (i = 0; i < 4; ++i)
  103.                  mtx->r[j].c[i] += mtx->r[3].c[i] * v.c[3-j];
  104.      }
  105.  
  106.  }
  107.  
  108.  void gl_print_matrix(const C3D_Mtx *m) {
  109.     for (int i = 0; i < 16; i+=4) {
  110.         fprintf(stderr, "%f\t%f\t%f\t%f\n", m->m[i+3], m->m[i+2], m->m[i+1], m->m[i]);
  111.     }
  112.     fprintf(stderr, "\n");
  113. }
  114.  
  115.  void Mtx_Transpose(C3D_Mtx* out)
  116.  {
  117.      float swap;
  118.      for (int i = 0; i <= 2; i++)
  119.      {
  120.          for (int j = 2-i; j >= 0; j--)
  121.          {
  122.              swap = out->r[i].c[j];
  123.              out->r[i].c[j] = out->r[3-j].c[3-i];
  124.              out->r[3-j].c[3-i] = swap;
  125.          }
  126.      }
  127.  }
  128.  
  129.  void Mtx_Rotate(C3D_Mtx* mtx, C3D_FVec axis, float angle, bool bRightSide)
  130.  {
  131.      size_t  i;
  132.      C3D_Mtx om;
  133.  
  134.      float s = sinf(angle);
  135.      float c = cosf(angle);
  136.      float t = 1.0f - c;
  137.  
  138.      axis = FVec3_Normalize(axis);
  139.  
  140.      float x = axis.x;
  141.      float y = axis.y;
  142.      float z = axis.z;
  143.      float w;
  144.  
  145.      om.r[0].x = t*x*x + c;
  146.      om.r[1].x = t*x*y + s*z;
  147.      om.r[2].x = t*x*z - s*y;
  148.      om.r[3].x = 0.0f; //optimized out
  149.  
  150.      om.r[0].y = t*y*x - s*z;
  151.      om.r[1].y = t*y*y + c;
  152.      om.r[2].y = t*y*z + s*x;
  153.      om.r[3].y = 0.0f; //optimized out
  154.  
  155.      om.r[0].z = t*z*x + s*y;
  156.      om.r[1].z = t*z*y - s*x;
  157.      om.r[2].z = t*z*z + c;
  158.      om.r[3].z = 0.0f; //optimized out
  159.       /* optimized out*/
  160.      om.r[0].w = 0.0f;
  161.      om.r[1].w = 0.0f;
  162.      om.r[2].w = 0.0f;
  163.      om.r[3].w = 1.0f;
  164.      
  165.  
  166.      if (bRightSide)
  167.      {
  168.          for (i = 0; i < 4; ++i)
  169.          {
  170.              x = mtx->r[i].x*om.r[0].x + mtx->r[i].y*om.r[1].x + mtx->r[i].z*om.r[2].x;
  171.              y = mtx->r[i].x*om.r[0].y + mtx->r[i].y*om.r[1].y + mtx->r[i].z*om.r[2].y;
  172.              z = mtx->r[i].x*om.r[0].z + mtx->r[i].y*om.r[1].z + mtx->r[i].z*om.r[2].z;
  173.  
  174.              mtx->r[i].x = x;
  175.              mtx->r[i].y = y;
  176.              mtx->r[i].z = z;
  177.          }
  178.      }
  179.      else
  180.      {
  181.          for (i = 0; i < 3; ++i)
  182.          {
  183.              x = mtx->r[0].x*om.r[i].x + mtx->r[1].x*om.r[i].y + mtx->r[2].x*om.r[i].z;
  184.              y = mtx->r[0].y*om.r[i].x + mtx->r[1].y*om.r[i].y + mtx->r[2].y*om.r[i].z;
  185.              z = mtx->r[0].z*om.r[i].x + mtx->r[1].z*om.r[i].y + mtx->r[2].z*om.r[i].z;
  186.              w = mtx->r[0].w*om.r[i].x + mtx->r[1].w*om.r[i].y + mtx->r[2].w*om.r[i].z;
  187.  
  188.              om.r[i].x = x;
  189.              om.r[i].y = y;
  190.              om.r[i].z = z;
  191.              om.r[i].w = w;
  192.          }
  193.  
  194.          for (i = 0; i < 3; ++i)
  195.              mtx->r[i] = om.r[i];
  196.      }
  197.  }
  198.  
  199.  void Mtx_RotateZ(C3D_Mtx* mtx, float angle, bool bRightSide)
  200.  {
  201.      float  a, b;
  202.      float  cosAngle = cosf(angle);
  203.      float  sinAngle = sinf(angle);
  204.      size_t i;
  205.  
  206.      if (bRightSide)
  207.      {
  208.          for (i = 0; i < 4; ++i)
  209.          {
  210.              a = mtx->r[i].x*cosAngle + mtx->r[i].y*sinAngle;
  211.              b = mtx->r[i].y*cosAngle - mtx->r[i].x*sinAngle;
  212.              mtx->r[i].x = a;
  213.              mtx->r[i].y = b;
  214.          }
  215.      }
  216.      else
  217.      {
  218.          for (i = 0; i < 4; ++i)
  219.          {
  220.              a = mtx->r[0].c[i]*cosAngle - mtx->r[1].c[i]*sinAngle;
  221.              b = mtx->r[1].c[i]*cosAngle + mtx->r[0].c[i]*sinAngle;
  222.              mtx->r[0].c[i] = a;
  223.              mtx->r[1].c[i] = b;
  224.          }
  225.      }
  226.  }
  227.  
  228. int main() {
  229.     C3D_Mtx mtx = {12,8,4,0,13,9,5,1,14,10,6,2,15,11,7,3};
  230.     gl_print_matrix(&mtx);
  231.  
  232.     C3D_Mtx mtx2 = {12,8,4,0,13,9,5,1,14,10,6,2,15,11,7,3};
  233.     Mtx_Translate(&mtx2, 10, 20, 30, true);
  234.     gl_print_matrix(&mtx2);
  235.  
  236.     C3D_Mtx mtx3 = {12,8,4,0,13,9,5,1,14,10,6,2,15,11,7,3};
  237.     C3D_FVec vec = FVec3_New(0.5,0.8,1);
  238.     float angle = (float)(30 * (float)M_PI / 180.0);
  239.     Mtx_Rotate(&mtx3, vec, angle, true);
  240.     gl_print_matrix(&mtx3);
  241.  
  242.     C3D_Mtx mtx4 = {12,8,4,0,13,9,5,1,14,10,6,2,15,11,7,3};
  243.     Mtx_RotateZ(&mtx4, angle, true);
  244.     gl_print_matrix(&mtx4);
  245.    
  246.    
  247.     return 0;
  248.    
  249. }
Advertisement
Add Comment
Please, Sign In to add comment