Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Vector transform
- static Float4 Multiply(const Float4& vector, const Float4x4& matrix)
- {
- Float4 result;
- // Simple stuff
- result.x = Dot(vector, matrix.x);
- result.y = Dot(vector, matrix.y);
- result.z = Dot(vector, matrix.z);
- result.w = Dot(vector, matrix.w);
- return result;
- }
- // Matrix multiplication --------------------------------------------------------------
- static inline Float4x4 Multiply(const Float4x4& matrixA, const Float4x4& matrixB)
- {
- // Transpose B so everything becomes dot products
- const Float4x4 matrixBtranspose = Transpose(matrixB);
- Float4x4 result;
- // Dot products infinity
- result.x.x = Dot(matrixA.x, matrixBtranspose.x);
- result.x.y = Dot(matrixA.x, matrixBtranspose.y);
- result.x.z = Dot(matrixA.x, matrixBtranspose.z);
- result.x.w = Dot(matrixA.x, matrixBtranspose.w);
- // -- //
- result.y.x = Dot(matrixA.y, matrixBtranspose.x);
- result.y.y = Dot(matrixA.y, matrixBtranspose.y);
- result.y.z = Dot(matrixA.y, matrixBtranspose.z);
- result.y.w = Dot(matrixA.y, matrixBtranspose.w);
- // -- //
- result.z.x = Dot(matrixA.z, matrixBtranspose.x);
- result.z.y = Dot(matrixA.z, matrixBtranspose.y);
- result.z.z = Dot(matrixA.z, matrixBtranspose.z);
- result.z.w = Dot(matrixA.z, matrixBtranspose.w);
- // -- //
- result.w.x = Dot(matrixA.w, matrixBtranspose.x);
- result.w.y = Dot(matrixA.w, matrixBtranspose.y);
- result.w.z = Dot(matrixA.w, matrixBtranspose.z);
- result.w.w = Dot(matrixA.w, matrixBtranspose.w);
- return result;
- }
- // Matrix view look to ----------------------------------------------------------------
- static Float4x4 LookTo(const Float3& position, const Float3& direction, const Float3& up)
- {
- Float4x4 result;
- // Rotation matrix
- result.x.xyz = direction;
- result.y.xyz = Normalize(Cross(up, result.x.xyz));
- result.z.xyz = Cross(result.x.xyz, result.y.xyz);
- // Computing the position
- Float3 negativePosition = -position;
- result.x.w = Dot(result.x.xyz, negativePosition);
- result.y.w = Dot(result.y.xyz, negativePosition);
- result.z.w = Dot(result.z.xyz, negativePosition);
- result.w.w = 1.0f;
- return result;
- }
- // -- //
- static Float4x4 LookAt(const Float3& position, const Float3& target, const Float3& up)
- {
- return LookTo(position, Normalize(target - position), up);
- }
- // Matrix perspective projection ------------------------------------------------------
- static Float4x4 PerspectiveLH(Float fov, Float aspectRatio, Float near, Float far)
- {
- // [cot(fov / 2) / aspect, 0, 0, 0]
- // [0, cot(fov / 2), 0, 0]
- // [0, 0, far / (far - near), 1]
- // [0, 0, near * far / (far - near), 0]
- // cotangent(x) == cosine(x) / sine(x)
- Float2 angles = SinCos(0.5f * fov);
- Float height = angles.y / angles.x;
- Float width = height / aspectRatio;
- Float range = far / (far - near);
- Float4x4 matrix;
- matrix.x.x = width;
- matrix.y.y = height;
- matrix.z.z = range;
- matrix.z.w = 1.0f;
- matrix.w.z = -range * near;
- Float4x4 fixup =
- {
- { 0.0f, 0.0f, 1.0f, 0.0f },
- { 1.0f, 0.0f, 0.0f, 0.0f },
- { 0.0f, 1.0f, 0.0f, 0.0f },
- { 0.0f, 0.0f, 0.0f, 1.0f }
- };
- //return matrix;
- return Multiply(fixup, matrix);
- }
- // The code in question
- Float4x4 world = Identity(); world.z.w = -50.0f;
- Float4x4 view = LookAt(Float3(-100.0f, -100.0f, 1.0f), Float3(0.0, 0.0f, 0.0f), Float3(0.0f, 0.0f, 1.0f));
- view = Inverse(view);
- Float4x4 projection = PerspectiveLH(pi / 4.0f, (Float)window.Width / window.Height, 0.01f, 1000.0f);
- // -- //
- Float4 vertex(5, 10, 55, 1);
- vertex = Multiply(vertex, Multiply(Multiply(world, view), projection));
- vertex = vertex / vertex.w;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement