Muli3D <9> CubeTexture的采样原理

本文详细介绍了CubeTexture的采样原理,包括Cubemap addressing的坐标映射,Dynamic cubemaps的实时更新优势,以及如何在Direct3D中实现实时预览Cubemap。通过3D向量作为纹理坐标,确定对应面并进行2D纹理采样。动态立方体贴图可以使用渲染到纹理的方式实时更新,而预览Cubemap则直接将Cubemap映射到交叉形状的顶点上,利用顶点法向量存储观察向量。
摘要由CSDN通过智能技术生成

记录一下,

在Muli3D中实现一个 采样 cubeTexture 的函数

result CMuli3DCubeTexture::SampleTexture( vector4 &o_vColor, float32 i_fU, float32 i_fV, float32 i_fW, const vector4 *i_pXGradient, const vector4 *i_pYGradient, const uint32 *i_pSamplerStates )
{
	// Determine face and local u/v coordinates ...
	// source: http://developer.nvidia.com/object/cube_map_ogl_tutorial.html

	// major axis 
	// direction     target                              sc     tc    ma 
	// ----------    ---------------------------------   ---    ---   --- 
	//  +rx          GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT   -rz    -ry   rx 
	//  -rx          GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT   +rz    -ry   rx 
	//  +ry          GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT   +rx    +rz   ry 
	//  -ry          GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT   +rx    -rz   ry 
	//  +rz          GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT   +rx    -ry   rz 
	//  -rz          GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT   -rx    -ry   rz
	float32 fCU, fCV, fInvMag;
	m3dcubefaces Face;

	const float32 fAbsU = fabsf( i_fU );
	const float32 fAbsV = fabsf( i_fV );
	const float32 fAbsW = fabsf( i_fW );

	if( fAbsU >= fAbsV && fAbsU >= fAbsW )
	{
		if( i_fU >= 0.0f )
		{
			// major axis direction: +rx
			Face = m3dcf_positive_x;
			fCU = -i_fW; fCV = -i_fV; fInvMag = 1.0f / fAbsU;
		}
		else
		{
			// major axis direction: -rx
			Face = m3dcf_negative_x;
			fCU = i_fW; fCV = -i_fV; fInvMag = 1.0f / fAbsU;
		}
	}
	else if( fAbsV >= fAbsU && fAbsV >= fAbsW )
	{
		if( i_fV >= 0.0f )
		{
			// major axis direction: +ry
			Face = m3dcf_positive_y;
			fCU = i_fU; fCV = i_fW; fInvMag = 1.0f / fAbsV;
		}
		else
		{
			// major axis direction: -ry
			Face = m3dcf_negative_y;
			fCU = i_fU; fCV = -i_fW; fInvMag = 1.0f / fAbsV;
		}
	}
	else //if( fAbsW >= fAbsU && fAbsW >= fAbsV )
	{
		if( i_fW >= 0.0f )
		{
			// major axis direction: +rz
			Face = m3dcf_positive_z;
			fCU = i_fU; fCV = -i_fV; fInvMag = 1.0f / fAbsW;
		}
		else
		{
			// major axis direction: -rz
			Face = m3dcf_negative_z;
			fCU = -i_fU; fCV = -i_fV; fInvMag = 1.0f / fAbsW;
		}
	}

	// s   =   ( sc/|ma| + 1 ) / 2 
	// t   =   ( tc/|ma| + 1 ) / 2
	fInvMag *= 0.5f;
	const float32 fU = /*fSaturate*/( fCU * fInvMag + 0.5f );
	const float32 fV = /*fSaturate*/( fCV * fInvMag + 0.5f );

	return m_ppCubeFaces[Face]->SampleTexture( o_vColor, fU, fV, 0, i_pXGradient, i_pYGradient, i_pSamplerStates );
}


这个原理主要是这样的,来自于:https://scalibq.wordpress.com/2013/06/23/cubemaps/


Cubemap addressing

A cubemap is a collection of 6 square 2D textures, each mapped to a face of a cube (hence the name). It can be visualized like this:



For a texture lookup, a 3D vector is used as a texture coordinate. Think of this vector as a ray from the center of the cube, going through one of the faces. The texture is sampled at the intersection of the ray with the face. Or more intuitively: the viewer is at the center of the cube, looking in a certain direction. The vector is that direction.

Cubemaps are often previewed in 2D in a cross arrangement, like this:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值