core: Define VK_USE_PLATFORM_XCB_KHR before including vkd3d_utils.h.
[vkmodelviewer.git] / Core / VectorMath.h
blobb0a2b0430900f543cdc264d5cc2849eefb1b2a16
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // This code is licensed under the MIT License (MIT).
4 // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5 // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6 // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7 // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
8 //
9 // Developed by Minigraph
11 // Author: James Stanard
13 // This is a system of classes that wrap DirectXMath in a more object-oriented and concise (readable) way. While these
14 // classes are not designed to maximize throughput on the instruction level, they are designed to be easily understood.
15 // I believe that the source of most math library inefficiency is in their being too confusing and not making the preferred
16 // way of doing things obvious. This leaves programmers constantly finding usage patterns that "work for them" but are
17 // ultimately inefficient and don't use the API the "way it was intended". The goal of this wrapper is to be cogent and
18 // familiar.
20 // Note that DirectXMath treats vectors like [1x4] matrices (rows) rather than [4x1] matrices (columns). Likewise, it
21 // treats matrices like they are transposed, so that you would multiply a vector and a matrix like so:
23 // Vector [1x4] = Vector [1x4] * Matrix [4x4]
25 // Applying multiple transforms to a vector involves concatenating on the outside, or right of the previous transform:
27 // ProjectedPosition = ModelPosition * ModelToWorld * WorldToView * ViewToProj
29 // This is *not* how this API works because it is needlessly contrary to Math textbooks. It's not "wrong", per se,
30 // but it's a paradigm I'd like to see changed. A vector is four floats. A matrix is four consecutive vectors. Whether
31 // you think of them as row or column vectors of a matrix is just a matter of perspective. In this library, you will see:
33 // Vector [4x1] = Matrix4 [4x4] * Vector [4x1]
35 // and
37 // ProjectedPosition = ViewToProj * WorldToView * ModelToWorld * ModelPosition
39 // One very happy result of this is that you can stop transposing every matrix you set in a shader constant buffer. They
40 // were always in the right format, you were just multiplying them backwards. In the shader you should have been calling
41 // mul( matrix, vector ) rather than mul( vector, matrix ). Then you wouldn't have needed to transpose the matrix.
43 // It's possible to work in a transposed space: (B*A*x)ᵀ = xᵀ*Aᵀ*Bᵀ but why would you want to?
45 // Oh, and we use right-handed coordinate systems because that's what you learned in your Linear Algebra, Calculus, and
46 // Physics classes.
48 // Peace,
49 // James
52 #pragma once
54 #include "Math/Scalar.h"
55 #include "Math/Vector.h"
56 #include "Math/Quaternion.h"
57 #include "Math/Matrix3.h"
58 #include "Math/Transform.h"
59 #include "Math/Matrix4.h"
60 #include "Math/Functions.inl"