Ok. I looked at as many of the code snipets that had anything to do with normal/bump mapping that I can stand. Thanks for the point in the right direction though.
I remembered back in my DX days there was an old example of how to make a bump map. Here is my code. It's not doing what I want, but it's a start. I would like for someone to help me figure out what I'm doing wrong.
int MakeBump(int image){
int BumpImage=FindFreeImage();
dbMakeMemblockFromImage(1,image);
int iHeight=dbGetImageHeight(image);
int iWidth=dbGetImageWidth(image);
dbMakeImage(BumpImage,iWidth,iHeight);
dbMakeMemblockFromImage(2,BumpImage);
dbDeleteImage(BumpImage);
for (int H=0;H<iHeight;H++){
for (int W=0;W<iWidth;W++){
LONG C,Cu,Cd,Cl,Cr;
int pos;
pos=(H*iWidth+W)*4+12;
C=dbMemblockDword(1,pos);
//up
if (H==0) pos=((iHeight-1)*iWidth+W)*4+12;
else pos=((H-1)*iWidth+W)*4+12;
Cu=dbMemblockDword(1,pos);
//down
if (H==iHeight-1) pos=W*4+12;
else pos=((H+1)*iWidth+W)*4+12;
Cd=dbMemblockDword(1,pos);
//left
if (W==0) pos=(H*iWidth+iWidth-1)*4+12;
else pos=(H*iWidth+W-1)*4+12;
Cl=dbMemblockDword(1,pos);
//right
if (W==iWidth-1) pos=(H*iWidth)*4+12;
else pos=(H*iWidth+W+1)*4+12;
Cr=dbMemblockDword(1,pos);
LONG iDu=(Cl-Cr);
LONG iDv=(Cu-Cd);
WORD uL=(C>1) ? 63 : 127;
dbWriteMemblockByte(2,(H*iWidth+W)*4+12+0,byte(iDu));
dbWriteMemblockByte(2,(H*iWidth+W)*4+12+1,byte(iDv));
dbWriteMemblockByte(2,(H*iWidth+W)*4+12+2,byte(uL));
dbWriteMemblockByte(2,(H*iWidth+W)*4+12+3,0);
}
}
dbMakeImageFromMemblock(BumpImage,2);
dbDeleteMemblock(1);
dbDeleteMemblock(2);
return BumpImage;
}
There's a function in it that you will need to make. FindFreeImage();
The rest will port into whatever you want to test with. I got the code from a DX example. Here is the function in it's original form:
//-----------------------------------------------------------------------------
// Name: InitBumpMap()
// Desc: Converts data from m_pEarthBumpTexture into the type of bump map requested
// as m_BumpMapFormat into m_psBumpMap.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitBumpMap()
{
LPDIRECT3DTEXTURE9 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, NULL ) ) )
{
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;
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);
return S_OK;
}
I MUST be doing something wrong because the bump texture doesn't come out right.
The fastest code is the code never written.