Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
ClipProjectionMatrix.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Math/Plane.h"
7#include "Math/Matrix.h"
8
9namespace UE {
10namespace Math {
11
12/**
13 * Realigns the near plane for an existing projection matrix
14 * with an arbitrary clip plane
15 * from: http://sourceforge.net/mailarchive/message.php?msg_id=000901c26324%242181ea90%24a1e93942%40firefly
16 * Updated for the fact that our FPlane uses Ax+By+Cz=D.
17 */
18template<typename T>
19struct TClipProjectionMatrix : public TMatrix<T>
20{
21public:
22 using TMatrix<T>::M;
23 /**
24 * Constructor
25 *
26 * @param SrcProjMat - source projection matrix to premultiply with the clip matrix
27 * @param Plane - clipping plane used to build the clip matrix (assumed to be in camera space)
28 */
29 TClipProjectionMatrix( const TMatrix<T>& SrcProjMat, const TPlane<T>& Plane );
30
31 // Conversion to other type.
32 template<typename FArg, TEMPLATE_REQUIRES(!std::is_same_v<T, FArg>)>
33 explicit TClipProjectionMatrix(const TClipProjectionMatrix<FArg>& From) : TMatrix<T>(From) {}
34
35private:
36 /** return sign of a number */
37 FORCEINLINE T sgn(T a );
38};
39
40
41template<typename T>
42FORCEINLINE TClipProjectionMatrix<T>::TClipProjectionMatrix( const TMatrix<T>& SrcProjMat, const TPlane<T>& Plane ) :
43TMatrix<T>(SrcProjMat)
44{
45 // Calculate the clip-space corner point opposite the clipping plane
46 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
47 // transform it into camera space by multiplying it
48 // by the inverse of the projection matrix
49 TPlane<T> CornerPlane(
50 sgn(Plane.X) / SrcProjMat.M[0][0],
51 sgn(Plane.Y) / SrcProjMat.M[1][1],
52 1.0f,
53 -(1.0f - SrcProjMat.M[2][2]) / SrcProjMat.M[3][2]
54 );
55
56 // Calculate the scaled plane vector
57 TPlane<T> ProjPlane( Plane * (1.0f / (Plane | CornerPlane)) );
58
59 // use the projected space clip plane in z column
60 // Note: (account for our negated W coefficient)
61 M[0][2] = ProjPlane.X;
62 M[1][2] = ProjPlane.Y;
63 M[2][2] = ProjPlane.Z;
64 M[3][2] = -ProjPlane.W;
65}
66
67template<typename T>
68FORCEINLINE T TClipProjectionMatrix<T>::sgn(T a)
69{
70 if (a > 0.0f) return (1.0f);
71 if (a < 0.0f) return (-1.0f);
72 return (0.0f);
73}
74
75} // namespace Math
76} // namespace UE
77
78UE_DECLARE_LWC_TYPE(ClipProjectionMatrix, 44);
79
80template<> struct TIsUECoreVariant<FClipProjectionMatrix44f> { enum { Value = true }; };
81template<> struct TIsUECoreVariant<FClipProjectionMatrix44d> { enum { Value = true }; };
#define UE_DECLARE_LWC_TYPE(...)
#define FORCEINLINE
Definition Platform.h:644
#define TEMPLATE_REQUIRES(...)