欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UE4 视图矩阵

程序员文章站 2022-06-10 23:24:54
...

UE4 视图矩阵

左手系的视图矩阵,与右手系只差一个负号,如下取负即为右手系的视图矩阵。

const FVector ZAxis = (LookAtPosition - EyePosition).GetSafeNormal();
struct FLookAtMatrix : FMatrix
{
	/** 
	 * Creates a view matrix given an eye position, a position to look at, and an up vector. 
	 * This does the same thing as D3DXMatrixLookAtLH.
	 */
	FLookAtMatrix(const FVector& EyePosition, const FVector& LookAtPosition, const FVector& UpVector);
};


FORCEINLINE FLookAtMatrix::FLookAtMatrix(const FVector& EyePosition, const FVector& LookAtPosition, const FVector& UpVector)
{
	const FVector ZAxis = (LookAtPosition - EyePosition).GetSafeNormal();
	const FVector XAxis = (UpVector ^ ZAxis).GetSafeNormal();
	const FVector YAxis = ZAxis ^ XAxis;

	for (uint32 RowIndex = 0; RowIndex < 3; RowIndex++)
	{
		M[RowIndex][0] = (&XAxis.X)[RowIndex];
		M[RowIndex][1] = (&YAxis.X)[RowIndex];
		M[RowIndex][2] = (&ZAxis.X)[RowIndex];
		M[RowIndex][3] = 0.0f;
	}
	M[3][0] = -EyePosition | XAxis;
	M[3][1] = -EyePosition | YAxis;
	M[3][2] = -EyePosition | ZAxis;
	M[3][3] = 1.0f;
}

参考链接

UE4 视图矩阵