Loading a texture from a BYTE* pData

Started by
4 comments, last by Adam_42 16 years, 2 months ago
If i can get the data from a pTexture to pData, how can i get data from pData to pTexture? Shown below is a function that gets a data from the texure and puts the raw data into pData,

_Bool SaveTexToMem(IDirect3DTexture9 *pTexture, BYTE* pData)
{
	// Get the surface description for the texture, to determine size
	D3DSURFACE_DESC desc;
	D3DLOCKED_RECT rect;
	UINT i;
	HRESULT hResult;
 
	hResult = IDirect3DTexture9_GetLevelDesc(pTexture, 0, &desc);
	if(FAILED(hResult))
	{
		return FALSE;
	}
 
	// Lock the first mip-level of the texture to get access to it's bits
	hResult = IDirect3DTexture9_LockRect(pTexture,0,&rect, NULL, D3DLOCK_READONLY);
	if(FAILED(hResult))
	{
		return FALSE;
	}
 
	// Allocate output buffer - Assumes 32-bit (4 bytes per pixel) textures.
	pData = (BYTE *)malloc(desc.Width * desc.Height * 4 * sizeof(BYTE),MF_NONE);
 
	// Read data from locked rect
	for(i = 0; i < desc.Height; ++i)
	{
		// Get a pointer to this "row" of pixels
		BYTE* pBits = (BYTE*)rect.pBits + i*rect.Pitch;
		memcpy(pData, pBits, desc.Width*4); // Again, assume 32-bit
	}
 
	// Unlock the texture
	IDirect3DTexture9_UnlockRect(pTexture,0);
 
	return TRUE;
}

Advertisement
Exactly the same way, but in reverse. You copy into the lock instead of out of it (And you pass 0 instead of D3DLOCK_READONLY). Then call IDirect3DBaseTexture9::GenerateMipSubLevels() to generate the mipmaps if you want.
You can use D3DXFillTexture to do that. Just check the manual.

Other way to do this is to lock the texture then get the Device Context of the texture GetDC() then sue standar Win32 functions to fill the texture. But this may be slow.

Finally, you could ue one of these:
IDirect3DDevice9::ColorFill
IDirect3DDevice9::StretchRect
IDirect3DDevice9::UpdateSurface
IDirect3DDevice9::UpdateTexture
IDirect3DSurface9 IDirect3DSurface9::LockRect

Just be careful no to fill your texture insode the render cycle unless absolutely required as this is a slow process and will slow down your rendering pipe. These functions are intended to fill a texture before the rendering cycle starts.

Luck!
Guimo


Hi Evil steve I see what you mean but i am struggling trying to implement it,

_Bool BjGetTextureFromMem(IDirect3DTexture9 *pOutTexture, BYTE *pInData){	D3DLOCKED_RECT rect;	HRESULT hResult;	UINT i;	hResult = IDirect3DDevice9_CreateTexture(pDirect3DDevice, 1280, 720, 1, 0, D3DFMT_A8R8G8B8, 0,&pOutTexture, NULL);	if(FAILED(hResult))	{		return FALSE;	}	hResult = IDirect3DTexture9_LockRect(pOutTexture,0,&rect, NULL, 0);	if(FAILED(hResult))	{		return FALSE;	}	for(i = 0; i < 720; ++i)	{		// Get a pointer to this "row" of pixels		BYTE* pBits = (BYTE*)rect.pBits + i*rect.Pitch;		memcpy(pOutTexture, pBits, 1280*4); // Again, assume 32-bit	}	IDirect3DTexture9_UnlockRect(pOutTexture,0);	return TRUE;}


how do i use pInData to set up pOutTexture

[Edited by - Prog101 on February 17, 2008 5:17:23 PM]
You mixed up the pointers. Also your SaveTexToMem function has a bug: It does always copy to the first line of the mem buffer.

Change to this:

For SaveTexToMem:
// Read data from locked rectfor ( i = 0; i < desc.Height; ++i ){  // Get a pointer to this "row" of pixels  BYTE* pBits = (BYTE*)rect.pBits + i * rect.Pitch;  BYTE* pDataPos = pData + desc.Width * 4 * i;  memcpy( pDataPos, pBits, desc.Width * 4 ); // Again, assume 32-bit}


For BjGetTextureFromMem:

for ( i = 0; i < 720; ++i ){  // Get a pointer to this "row" of pixels  BYTE* pTargetBits = (BYTE*)rect.pBits + i * rect.Pitch;  BYTE* pSourceBits = pInData + 1280 * 4 * i;  memcpy( pTargetBits, pSourceBits, 1280 * 4 ); // Again, assume 32-bit}

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

There's also D3DXLoadSurfaceFromMemory() for loading textures from memory. You just need to call it for each surface in the mip chain.

This topic is closed to new replies.

Advertisement