UE4投影矩阵[通俗易懂]

UE4投影矩阵[通俗易懂]UE4投影矩阵正交投影classFOrthoMatrix :publicFMatrix{public: /** *Constructor * *@paramWidthviewspacewidth *@paramHeightviewspaceheight *@paramZScalescaleintheZaxis *@paramZOffsetoffsetintheZaxis */ FOrthoMatrix(flo

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

UE4投影矩阵

正交投影

class FOrthoMatrix
	: public FMatrix
{ 
   
public:

	/** * Constructor * * @param Width view space width * @param Height view space height * @param ZScale scale in the Z axis * @param ZOffset offset in the Z axis */
	FOrthoMatrix(float Width,float Height,float ZScale,float ZOffset);
};


class FReversedZOrthoMatrix : public FMatrix
{ 
   
public:
	FReversedZOrthoMatrix(float Width,float Height,float ZScale,float ZOffset);
};


FORCEINLINE FOrthoMatrix::FOrthoMatrix(float Width,float Height,float ZScale,float ZOffset)
	: FMatrix(
		FPlane((Width)? (1.0f / Width) : 1.0f,	0.0f,								0.0f,				0.0f),
		FPlane(0.0f,							(Height)? (1.0f / Height) : 1.f,	0.0f,				0.0f),
		FPlane(0.0f,							0.0f,								ZScale,				0.0f),
		FPlane(0.0f,							0.0f,								ZOffset * ZScale,	1.0f)
	)
{ 
    }


FORCEINLINE FReversedZOrthoMatrix::FReversedZOrthoMatrix(float Width,float Height,float ZScale,float ZOffset)
	: FMatrix(
		FPlane((Width)? (1.0f / Width) : 1.0f,	0.0f,								0.0f,					0.0f),
		FPlane(0.0f,							(Height)? (1.0f / Height) : 1.f,	0.0f,					0.0f),
		FPlane(0.0f,							0.0f,								-ZScale,				0.0f),
		FPlane(0.0f,							0.0f,								1.0 - ZOffset * ZScale,	1.0f)
	)
{ 
    }

透视投影

class FPerspectiveMatrix
	: public FMatrix
{ 
   
public:

// Note: the value of this must match the mirror in Common.usf!
#define Z_PRECISION 0.0f

	/** * Constructor * * @param HalfFOVX Half FOV in the X axis * @param HalfFOVY Half FOV in the Y axis * @param MultFOVX multiplier on the X axis * @param MultFOVY multiplier on the y axis * @param MinZ distance to the near Z plane * @param MaxZ distance to the far Z plane */
	FPerspectiveMatrix(float HalfFOVX, float HalfFOVY, float MultFOVX, float MultFOVY, float MinZ, float MaxZ);

	/** * Constructor * * @param HalfFOV half Field of View in the Y direction * @param Width view space width * @param Height view space height * @param MinZ distance to the near Z plane * @param MaxZ distance to the far Z plane * @note that the FOV you pass in is actually half the FOV, unlike most perspective matrix functions (D3DXMatrixPerspectiveFovLH). */
	FPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ, float MaxZ);

	/** * Constructor * * @param HalfFOV half Field of View in the Y direction * @param Width view space width * @param Height view space height * @param MinZ distance to the near Z plane * @note that the FOV you pass in is actually half the FOV, unlike most perspective matrix functions (D3DXMatrixPerspectiveFovLH). */
	FPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ);
};


class FReversedZPerspectiveMatrix : public FMatrix
{ 
   
public:
	FReversedZPerspectiveMatrix(float HalfFOVX, float HalfFOVY, float MultFOVX, float MultFOVY, float MinZ, float MaxZ);
	FReversedZPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ, float MaxZ);
	FReversedZPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ);
};


#if _MSC_VER
#pragma warning (push)
// Disable possible division by 0 warning
#pragma warning (disable : 4723)
#endif


FORCEINLINE FPerspectiveMatrix::FPerspectiveMatrix(float HalfFOVX, float HalfFOVY, float MultFOVX, float MultFOVY, float MinZ, float MaxZ)
	: FMatrix(
		FPlane(MultFOVX / FMath::Tan(HalfFOVX),	0.0f,								0.0f,																	0.0f),
		FPlane(0.0f,							MultFOVY / FMath::Tan(HalfFOVY),	0.0f,																	0.0f),
		FPlane(0.0f,							0.0f,								((MinZ == MaxZ) ? (1.0f - Z_PRECISION) : MaxZ / (MaxZ - MinZ)),			1.0f),
		FPlane(0.0f,							0.0f,								-MinZ * ((MinZ == MaxZ) ? (1.0f - Z_PRECISION) : MaxZ / (MaxZ - MinZ)),	0.0f)
	)
{ 
    }


FORCEINLINE FPerspectiveMatrix::FPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ, float MaxZ)
	: FMatrix(
		FPlane(1.0f / FMath::Tan(HalfFOV),	0.0f,									0.0f,							0.0f),
		FPlane(0.0f,						Width / FMath::Tan(HalfFOV) / Height,	0.0f,							0.0f),
		FPlane(0.0f,						0.0f,									((MinZ == MaxZ) ? (1.0f - Z_PRECISION) : MaxZ / (MaxZ - MinZ)),			1.0f),
		FPlane(0.0f,						0.0f,									-MinZ * ((MinZ == MaxZ) ? (1.0f - Z_PRECISION) : MaxZ / (MaxZ - MinZ)),	0.0f)
	)
{ 
    }


FORCEINLINE FPerspectiveMatrix::FPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ)
	: FMatrix(
		FPlane(1.0f / FMath::Tan(HalfFOV),	0.0f,									0.0f,							0.0f),
		FPlane(0.0f,						Width / FMath::Tan(HalfFOV) / Height,	0.0f,							0.0f),
		FPlane(0.0f,						0.0f,									(1.0f - Z_PRECISION),			1.0f),
		FPlane(0.0f,						0.0f,									-MinZ * (1.0f - Z_PRECISION),	0.0f)
	)
{ 
    }


FORCEINLINE FReversedZPerspectiveMatrix::FReversedZPerspectiveMatrix(float HalfFOVX, float HalfFOVY, float MultFOVX, float MultFOVY, float MinZ, float MaxZ)
	: FMatrix(
		FPlane(MultFOVX / FMath::Tan(HalfFOVX),	0.0f,								0.0f,													0.0f),
		FPlane(0.0f,							MultFOVY / FMath::Tan(HalfFOVY),	0.0f,													0.0f),
		FPlane(0.0f,							0.0f,								((MinZ == MaxZ) ? 0.0f : MinZ / (MinZ - MaxZ)),			1.0f),
		FPlane(0.0f,							0.0f,								((MinZ == MaxZ) ? MinZ : -MaxZ * MinZ / (MinZ - MaxZ)),	0.0f)
	)
{ 
    }


FORCEINLINE FReversedZPerspectiveMatrix::FReversedZPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ, float MaxZ)
	: FMatrix(
		FPlane(1.0f / FMath::Tan(HalfFOV),	0.0f,									0.0f,													0.0f),
		FPlane(0.0f,						Width / FMath::Tan(HalfFOV) / Height,	0.0f,													0.0f),
		FPlane(0.0f,						0.0f,									((MinZ == MaxZ) ? 0.0f : MinZ / (MinZ - MaxZ)),			1.0f),
		FPlane(0.0f,						0.0f,									((MinZ == MaxZ) ? MinZ : -MaxZ * MinZ / (MinZ - MaxZ)),	0.0f)
	)
{ 
    }


FORCEINLINE FReversedZPerspectiveMatrix::FReversedZPerspectiveMatrix(float HalfFOV, float Width, float Height, float MinZ)
	: FMatrix(
		FPlane(1.0f / FMath::Tan(HalfFOV),	0.0f,									0.0f, 0.0f),
		FPlane(0.0f,						Width / FMath::Tan(HalfFOV) / Height,	0.0f, 0.0f),
		FPlane(0.0f,						0.0f,									0.0f, 1.0f),
		FPlane(0.0f,						0.0f,									MinZ, 0.0f)
	)
{ 
    }

使用

FMinimalViewInfo::CalculateProjectionMatrixGivenView(ControllingActorViewInfo, AspectRatioAxisConstraint, Viewport, /*inout*/ ViewInitOptions);
{ 
   
if (ViewInfo.ProjectionMode == ECameraProjectionMode::Orthographic)
		{ 
   
			const float YScale = 1.0f / ViewInfo.AspectRatio;
	
			const float OrthoWidth = ViewInfo.OrthoWidth / 2.0f;
			const float OrthoHeight = ViewInfo.OrthoWidth / 2.0f * YScale;

			const float NearPlane = ViewInfo.OrthoNearClipPlane;
			const float FarPlane = ViewInfo.OrthoFarClipPlane;

			const float ZScale = 1.0f / (FarPlane - NearPlane);
			const float ZOffset = -NearPlane;

			InOutProjectionData.ProjectionMatrix = FReversedZOrthoMatrix(
				OrthoWidth,
				OrthoHeight,
				ZScale,
				ZOffset
				);
		}
		else
		{ 
   
			// Avoid divide by zero in the projection matrix calculation by clamping FOV
			InOutProjectionData.ProjectionMatrix = FReversedZPerspectiveMatrix(
				FMath::Max(0.001f, ViewInfo.FOV) * (float)PI / 360.0f,
				ViewInfo.AspectRatio,
				1.0f,
				GNearClippingPlane );
		}
}

参考链接

UE4 投影矩阵

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/185758.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • case label does not reduce to_class not loaded

    case label does not reduce to_class not loadedClassCastException出现原因解决办法出现原因抛出以指示代码已尝试将对象强制转换为它不是实例的子类。例如,以下代码生成一个ClassCastException:对象x=新整数(0);System.out.println((String)x);解决办法这种异常我也不知道有什么能够完全解决的办法,但是可以同instanseof来避免例如:Pet宠物类   Dog狗狗类(继承Pet)  Penguin企鹅类(继承Pet)  Dogdog=(Dog)pet

  • 各种常用不等式汇总「建议收藏」

    各种常用不等式汇总「建议收藏」对数学中常用的不等式进行了汇总,目前只有结论,没有证明

  • .sln文件

    .sln文件VisualStudio.NET采用两种文件类型(.sln和.suo)来存储特定于解决方案的设置,它们总称为解决方案文件。为解决方案资源管理器提供显示管理文件的图形接口所需的信息,从而在每次继续开发任务时,不会因开发环境而分散精力;*.sln:(VisualStudio.Solution)通过为环境提供对项目、项目项和解决方案项在磁盘上位置的引用,可将它们组织到解决方案中。比如是生成Debug模式,还是Release模式,是通用CPU还是专用的等.ps:…

  • matlab plotyy 坐标轴设置,[转载]Matlab plotyy画双纵坐标图实例[通俗易懂]

    matlab plotyy 坐标轴设置,[转载]Matlab plotyy画双纵坐标图实例[通俗易懂]Matlabplotyy画双纵坐标图实例x=0:0.01:20;y1=200*exp(-0.05*x).*sin(x);y2=0.8*exp(-0.5*x).*sin(10*x);[AX,H1,H2]=plotyy(x,y1,x,y2,’plot’);set(AX(1),’XColor’,’k’,’YColor’,’b’);set(AX(2),’XColor’,’k’,’YCol…

  • hadoop使用教程_hadoop入门

    hadoop使用教程_hadoop入门Hadoop【大数据Hadoop系列】Hadoop2.X单机环境搭建【大数据Hadoop系列】Hadoop2.X集群环境搭建MapReduce【大数据Hadoop系列】分布式计算框架——MapReduceYARN【大数据Hadoop系列】Hadoop-YARN知识体系总结(2021版)【大数据Hadoop系列】集群资源管理器——YARNHDFS【大数据Hadoop系列】Hadoop-HDFS知识体系总结(2021版)【大数据Hadoop系列】Hadoop分

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号