记录一下,
在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: