The DX texture you are creating may be wider than what you
requested. This is usually a power of 2 and relates to the
way different video cards optimise things.
This is called the "pitch" (Cronus3D mentioned this.)
The pitch may be (probably will be) different for the all various
different graphics card chipsets. The pitch can also differ on the
same graphics card when you change resolution.
You need to do a row, by row copy, adjusting the pointer to the
DirectX Texture at the end of each row.
+---------------------+---------------------+
| | |
| Raw data | DXSurface |
| | |
+--------------------+ |
| |
+-------------------------------------------+
The pseudo code is as follows:
(Assumes you have 24bit R8G8B8 rawdata and
32bit A8R8G8B8 texture.)
Create new DX texture
Clean contents (zero) new DX texture
Create a D3DLOCKED_RECT structure
Call ->LockRect() on the texture
D3DLOCKED_RECT.Pitch now holds the width of the DX Surface
Store the pointer to DX Surface in a variable
BYTE *lpbDXSurfCurrent = (BYTE *)D3DLOCKED_RECT.pBits
Store a pointer to the start of the rawdata
BYTE *lpbRawDataCurrent = (BYTE *)start of rawdata
*lpbDXTexCurrent = 255; // set first byte of DXSurface (alpha
channel 255 = fully solid)
lpbDXTexCurrent ++; // move to second byte of DXSurface (first Red)
for (i=0 to height)
{
for (j=0 to width of rawdata)
{
memcpy(lpbDXSurfCurrent, lpbRawDataCurrent, sizeof (BYTE) *3);
// Copy the RGB from raw to RGB of DXSurface
lpbDXSurfCurrent+=3; // Move to A value of next destination pixel
*lpbDXSurfCurrent = 255; // set to solid alpha
lpbDXSurfCurrent++; // Move to R value of next destination pixel
lpbRawDataCurrent+=3; // Move to R value of next source pixel
}
lpbDXSurfCurrent += (D3DLOCKED_RECT.pitch - width of rawdata);
// Move to start of next row of DXSurface
lpbDXSurfCurrent = NULL;
lpbRawDataCurrent = NULL;
I am assuming that the "raw RGB bits" are actually "raw."
If this was originally a BMP you probably have another
issue with the "raw data" actually being "padded" to a
32bit boundary. BMP's have header fields which must
be skipped. BMP's are also "upside down" if the height
field is positive.