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 投影矩阵

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 电驴资源站

    电驴资源站以下为电驴资源站或者电驴资源搜索站,按推荐度排名。1、http://www.simplecd.org/新兴站点,号称山寨版的Verycd,注册就可发资源帖,下载资源无铜光盘限制!曾经据说由于某臭名

    2022年7月1日
    39
  • mysql怎么修改密码,mysql修改密码的几种方法

    mysql怎么修改密码,mysql修改密码的几种方法方法一:通过mysql控制台直接设置密码第一步我们打开mysqlconsole,输入mysql原先密码,按回车键进去,这里原密码为空,如下图所示:第二步输入“setpassword=password(‘123456’);”,按回车键之后,就成功将密码改为123456了,如果想修改密码为其他,直接将123456改成想要设置的密码就可以,如下图所示:第三步我们使用mysql图形界面工具navicat尝试一下密码是否修改成功,新建连接,输入数据库用户名和刚刚设置的密码,点击确定,如下图所示:.

    2022年7月16日
    9
  • 史上最全的Uboot常用命令汇总(超全面!超详细!)收藏这一篇就够了「建议收藏」

    史上最全的Uboot常用命令汇总(超全面!超详细!)收藏这一篇就够了「建议收藏」Linux系统要启动就必须需要一个bootloader程序,也就说芯片上电以后先运行一段bootloader程序。这段bootloader程序会先初始化DDR等外设,然后将Linux内核从flash(NAND,NORFLASH,SD,MMC等)拷贝到DDR中,最后启动Linux内核。当然了,bootloader的实际工作要复杂的多,但是它最主要的工作就是启动Linux内核,bootloader和Linux内核的关系就跟PC上的BIOS和Windows的

    2022年6月23日
    128
  • 使用std–fstream处理文件「建议收藏」

    使用std–fstream处理文件「建议收藏」fstream文件操作总结文件的操作一直在用,在此总结一下:fstream的使用std::fstream从std::ofstream继承写入文件的功能,从std::ifstream继承读取文件的功能.包含头文件#include使用open()和close()打开和关闭文件#include#includeusingnamespa

    2022年9月16日
    0
  • oracle 第一范式,数据库范式之第一范式

    oracle 第一范式,数据库范式之第一范式数据库范式(DatabaseNormalization)设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不同的规范要求被称为不同的范式,各种范式呈递次规范,越高的范式数据库冗余越小。目前关系数据库有六种范式:第一范式(1NF)、第二范式(2NF)、第三范式(3NF)、巴斯-科德范式(BCNF)、第四范式(4NF)和第五范式(5NF,还又称完美范式)。首先要明白”范式(NF)”…

    2022年5月24日
    33
  • chmod命令原理及用法详解[通俗易懂]

    chmod命令原理及用法详解[通俗易懂]Chmod命令主要用于修改、设置文件权限chmod修改文件权限主要有两种方式:字母法与数字法虽然数字法相对字母法简单,但是数字法是基于字母法,所以这里先介绍字母法。1、字母法:chmod (ugoa) (+-=) (rwx) (文件名)以上是chmod的用法,每个括号是一个参数,前三个括号主要放在一起使用即chmod+设置模式+文件名;下

    2022年6月15日
    45

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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