|
|

楼主 |
发表于 2005-7-26 14:02:00
|
显示全部楼层
Re:我正在学习凹凸贴图技术,请各位指点
DX SDK 下有一个函数 InitBumpMap,它的代码如下:
LPDIRECT3DTEXTURE8 psBumpSrc = m_pEarthBumpTexture;
D3DSURFACE_DESC d3dsd;
D3DLOCKED_RECT d3dlr;
psBumpSrc->GetLevelDesc( 0, &d3dsd );
// Create the bumpmap's surface and texture objects
if( FAILED( m_pd3dDevice->CreateTexture( d3dsd.Width, d3dsd.Height, 1, 0,
m_BumpMapFormat, D3DPOOL_MANAGED, &m_psBumpMap ) ) )
{
return E_FAIL;
}
// Fill the bits of the new texture surface with bits from
// a private format.
psBumpSrc->LockRect( 0, &d3dlr, 0, 0 );
DWORD dwSrcPitch = (DWORD)d3dlr.Pitch;
BYTE* pSrcTopRow = (BYTE*)d3dlr.pBits;
BYTE* pSrcCurRow = pSrcTopRow;
BYTE* pSrcBotRow = pSrcTopRow + (dwSrcPitch * (d3dsd.Height - 1) );
m_psBumpMap->LockRect( 0, &d3dlr, 0, 0 );
DWORD dwDstPitch = (DWORD)d3dlr.Pitch;
BYTE* pDstTopRow = (BYTE*)d3dlr.pBits;
BYTE* pDstCurRow = pDstTopRow;
BYTE* pDstBotRow = pDstTopRow + (dwDstPitch * (d3dsd.Height - 1) );
for( DWORD y=0; y<d3dsd.Height; y++ )
{
BYTE* pSrcB0; // addr of current pixel
BYTE* pSrcB1; // addr of pixel below current pixel, wrapping to top if necessary
BYTE* pSrcB2; // addr of pixel above current pixel, wrapping to bottom if necessary
BYTE* pDstT; // addr of dest pixel;
pSrcB0 = pSrcCurRow;
if( y == d3dsd.Height - 1)
pSrcB1 = pSrcTopRow;
else
pSrcB1 = pSrcCurRow + dwSrcPitch;
if( y == 0 )
pSrcB2 = pSrcBotRow;
else
pSrcB2 = pSrcCurRow - dwSrcPitch;
pDstT = pDstCurRow;
for( DWORD x=0; x<d3dsd.Width; x++ )
{
LONG v00; // Current pixel
LONG v01; // Pixel to the right of current pixel, wrapping to left edge if necessary
LONG vM1; // Pixel to the left of current pixel, wrapping to right edge if necessary
LONG v10; // Pixel one line below.
LONG v1M; // Pixel one line above.
v00 = *(pSrcB0+0);
if( x == d3dsd.Width - 1 )
v01 = *(pSrcCurRow);
else
v01 = *(pSrcB0+4);
if( x == 0 )
vM1 = *(pSrcCurRow + (4 * (d3dsd.Width - 1)));
else
vM1 = *(pSrcB0-4);
v10 = *(pSrcB1+0);
v1M = *(pSrcB2+0);
LONG iDu = (vM1-v01); // The delta-u bump value
LONG iDv = (v1M-v10); // The delta-v bump value
// The luminance bump value (land masses are less shiny)
WORD uL = ( v00>1 ) ? 63 : 127;
switch( m_BumpMapFormat )
{
case D3DFMT_V8U8:
*pDstT++ = (BYTE)iDu;
*pDstT++ = (BYTE)iDv;
break;
case D3DFMT_L6V5U5:
*(WORD*)pDstT = (WORD)( ( (iDu>>3) & 0x1f ) << 0 );
*(WORD*)pDstT |= (WORD)( ( (iDv>>3) & 0x1f ) << 5 );
*(WORD*)pDstT |= (WORD)( ( ( uL>>2) & 0x3f ) << 10 );
pDstT += 2;
break;
case D3DFMT_X8L8V8U8:
*pDstT++ = (BYTE)iDu;
*pDstT++ = (BYTE)iDv;
*pDstT++ = (BYTE)uL;
*pDstT++ = (BYTE)0L;
break;
}
// Move one pixel to the right (src is 32-bpp)
pSrcB0+=4;
pSrcB1+=4;
pSrcB2+=4;
}
// Move to the next line
pSrcCurRow += dwSrcPitch;
pDstCurRow += dwDstPitch;
}
m_psBumpMap->UnlockRect(0);
psBumpSrc->UnlockRect(0);
这一段的作用是什么?把深度图转换成法线图吗? |
|