Muli3D <9> CubeTexture的采样原理

Muli3D <9> CubeTexture的采样原理记录一下 在 Muli3D 中实现一个 采样 cubeTexture 的函数 result CMuli3DCubeT SampleTextur vector4 amp o vColor float32 i fU float32 i fV float32 i fW const vector4 i pXGradient const vector4

大家好,我是讯享网,很高兴认识大家。

记录一下,

在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:

小讯
上一篇 2025-01-15 22:36
下一篇 2025-04-11 07:34

相关推荐

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