Implement D3DOrtho
This commit is contained in:
@@ -94,6 +94,18 @@ namespace LinearAlgebra {
|
||||
SetMatrixRotatePart(*this, q);
|
||||
}
|
||||
|
||||
void Matrix4x4::SetMatrixRotatePart(Matrix4x4 &m, const Quaternion& q)
|
||||
{
|
||||
// See e.g. http://www.geometrictools.com/Documentation/LinearAlgebraicQuaternions.pdf .
|
||||
const float x = q.x;
|
||||
const float y = q.y;
|
||||
const float z = q.z;
|
||||
const float w = q.w;
|
||||
elems[0][0] = 1 - 2*(y*y + z*z); elems[0][1] = 2*(x*y - z*w); elems[0][2] = 2*(x*z + y*w);
|
||||
elems[1][0] = 2*(x*y + z*w); elems[1][1] = 1 - 2*(x*x + z*z); elems[1][2] = 2*(y*z - x*w);
|
||||
elems[2][0] = 2*(x*z - y*w); elems[2][1] = 2*(y*z + x*w); elems[2][2] = 1 - 2*(x*x + y*y);
|
||||
}
|
||||
|
||||
void Matrix4x4::SetRow(int row, const Vector3 &rowVector, float m_r3) {
|
||||
SetRow(row, rowVector.x, rowVector.y, rowVector.z, m_r3);
|
||||
}
|
||||
@@ -114,4 +126,46 @@ namespace LinearAlgebra {
|
||||
SetTranslatePart(translation);
|
||||
SetRow(3, 0, 0, 0, 1);
|
||||
}
|
||||
|
||||
Matrix4x4 Matrix4x4::D3DOrthoProjLH(float n, float f, float h, float v) {
|
||||
float p00 = 2.f / h; float p01 = 0; float p02 = 0; float p03 = 0.f;
|
||||
float p10 = 0; float p11 = 2.f / v; float p12 = 0; float p13 = 0.f;
|
||||
float p20 = 0; float p21 = 0; float p22 = 1.f / (f-n); float p23 = n / (n-f);
|
||||
float p30 = 0; float p31 = 0; float p32 = 0.f; float p33 = 1.f;
|
||||
|
||||
return {p00, p01, p02, p03, p10, p11, p12, p13, p20, p21, p22, p23, p30, p31, p32, p33};
|
||||
}
|
||||
|
||||
/** This function generates an orthographic projection matrix that maps from
|
||||
the Direct3D view space to the Direct3D normalized viewport space as follows:
|
||||
|
||||
In Direct3D view space, we assume that the camera is positioned at the origin (0,0,0).
|
||||
The camera looks directly towards the positive Z axis (0,0,1).
|
||||
The -X axis spans to the left of the screen, +X goes to the right.
|
||||
-Y goes to the bottom of the screen, +Y goes to the top.
|
||||
|
||||
After the transformation, we're in the Direct3D normalized viewport space as follows:
|
||||
|
||||
(-1,-1,0) is the bottom-left corner of the viewport at the near plane.
|
||||
(1,1,0) is the top-right corner of the viewport at the near plane.
|
||||
(0,0,0) is the center point at the near plane.
|
||||
Coordinates with z=1 are at the far plane.
|
||||
|
||||
Examples:
|
||||
(0,0,n) maps to (0,0,0).
|
||||
(0,0,f) maps to (0,0,1).
|
||||
(-h/2, -v/2, n) maps to (-1, -1, 0).
|
||||
(h/2, v/2, f) maps to (1, 1, 1).
|
||||
*/
|
||||
Matrix4x4 Matrix4x4::D3DOrthoProjRH(float n, float f, float h, float v)
|
||||
{
|
||||
// D3DOrthoProjLH and D3DOrthoProjRH differ from each other in that the third column is negated.
|
||||
// This corresponds to LH = RH * In, where In is a diagonal matrix with elements [1 1 -1 1].
|
||||
float p00 = 2.f / h; float p01 = 0; float p02 = 0; float p03 = 0.f;
|
||||
float p10 = 0; float p11 = 2.f / v; float p12 = 0; float p13 = 0.f;
|
||||
float p20 = 0; float p21 = 0; float p22 = 1.f / (n-f); float p23 = n / (n-f);
|
||||
float p30 = 0; float p31 = 0; float p32 = 0.f; float p33 = 1.f;
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user