Advertisement
Ember

shite

Nov 11th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. // Vector transform
  2. static Float4 Multiply(const Float4& vector, const Float4x4& matrix)
  3. {
  4.     Float4 result;
  5.     // Simple stuff
  6.     result.x = Dot(vector, matrix.x);
  7.     result.y = Dot(vector, matrix.y);
  8.     result.z = Dot(vector, matrix.z);
  9.     result.w = Dot(vector, matrix.w);
  10.     return result;
  11. }
  12.  
  13. // Matrix multiplication --------------------------------------------------------------
  14. static inline Float4x4 Multiply(const Float4x4& matrixA, const Float4x4& matrixB)
  15. {
  16.     // Transpose B so everything becomes dot products
  17.     const Float4x4 matrixBtranspose = Transpose(matrixB);
  18.  
  19.     Float4x4 result;
  20.     // Dot products infinity
  21.     result.x.x = Dot(matrixA.x, matrixBtranspose.x);
  22.     result.x.y = Dot(matrixA.x, matrixBtranspose.y);
  23.     result.x.z = Dot(matrixA.x, matrixBtranspose.z);
  24.     result.x.w = Dot(matrixA.x, matrixBtranspose.w);
  25.     // -- //
  26.     result.y.x = Dot(matrixA.y, matrixBtranspose.x);
  27.     result.y.y = Dot(matrixA.y, matrixBtranspose.y);
  28.     result.y.z = Dot(matrixA.y, matrixBtranspose.z);
  29.     result.y.w = Dot(matrixA.y, matrixBtranspose.w);
  30.     // -- //
  31.     result.z.x = Dot(matrixA.z, matrixBtranspose.x);
  32.     result.z.y = Dot(matrixA.z, matrixBtranspose.y);
  33.     result.z.z = Dot(matrixA.z, matrixBtranspose.z);
  34.     result.z.w = Dot(matrixA.z, matrixBtranspose.w);
  35.     // -- //
  36.     result.w.x = Dot(matrixA.w, matrixBtranspose.x);
  37.     result.w.y = Dot(matrixA.w, matrixBtranspose.y);
  38.     result.w.z = Dot(matrixA.w, matrixBtranspose.z);
  39.     result.w.w = Dot(matrixA.w, matrixBtranspose.w);
  40.  
  41.     return result;
  42. }
  43.  
  44. // Matrix view look to ----------------------------------------------------------------
  45. static Float4x4 LookTo(const Float3& position, const Float3& direction, const Float3& up)
  46. {
  47.     Float4x4 result;
  48.  
  49.     // Rotation matrix
  50.     result.x.xyz = direction;
  51.     result.y.xyz = Normalize(Cross(up, result.x.xyz));
  52.     result.z.xyz = Cross(result.x.xyz, result.y.xyz);
  53.  
  54.     // Computing the position
  55.     Float3 negativePosition = -position;
  56.     result.x.w = Dot(result.x.xyz, negativePosition);
  57.     result.y.w = Dot(result.y.xyz, negativePosition);
  58.     result.z.w = Dot(result.z.xyz, negativePosition);
  59.     result.w.w = 1.0f;
  60.  
  61.     return result;
  62. }
  63. // -- //
  64. static Float4x4 LookAt(const Float3& position, const Float3& target, const Float3& up)
  65. {
  66.     return LookTo(position, Normalize(target - position), up);
  67. }
  68.  
  69. // Matrix perspective projection ------------------------------------------------------
  70. static Float4x4 PerspectiveLH(Float fov, Float aspectRatio, Float near, Float far)
  71. {
  72.     // [cot(fov / 2) / aspect, 0,            0,                         0]
  73.     // [0,                     cot(fov / 2), 0,                         0]
  74.     // [0,                     0,            far / (far - near),        1]
  75.     // [0,                     0,            near * far / (far - near), 0]
  76.  
  77.     // cotangent(x) == cosine(x) / sine(x)
  78.     Float2 angles = SinCos(0.5f * fov);
  79.  
  80.     Float height = angles.y / angles.x;
  81.     Float width = height / aspectRatio;
  82.     Float range = far / (far - near);
  83.  
  84.     Float4x4 matrix;
  85.     matrix.x.x = width;
  86.     matrix.y.y = height;
  87.     matrix.z.z = range;
  88.     matrix.z.w = 1.0f;
  89.     matrix.w.z = -range * near;
  90.  
  91.     Float4x4 fixup =
  92.     {
  93.         { 0.0f, 0.0f, 1.0f, 0.0f },
  94.         { 1.0f, 0.0f, 0.0f, 0.0f },
  95.         { 0.0f, 1.0f, 0.0f, 0.0f },
  96.         { 0.0f, 0.0f, 0.0f, 1.0f }
  97.     };
  98.  
  99.     //return matrix;
  100.     return Multiply(fixup, matrix);
  101. }
  102.  
  103. // The code in question
  104. Float4x4 world = Identity(); world.z.w = -50.0f;
  105. Float4x4 view = LookAt(Float3(-100.0f, -100.0f, 1.0f), Float3(0.0, 0.0f, 0.0f), Float3(0.0f, 0.0f, 1.0f));
  106. view = Inverse(view);
  107. Float4x4 projection = PerspectiveLH(pi / 4.0f, (Float)window.Width / window.Height, 0.01f, 1000.0f);
  108. // -- //
  109. Float4 vertex(5, 10, 55, 1);
  110. vertex = Multiply(vertex, Multiply(Multiply(world, view), projection));
  111. vertex = vertex / vertex.w;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement