Sign in to follow this  

how to draw dynamic bitmap buffer to texture's buffer ?

This topic is 4666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have a bitmap buffer ( with width,height and size) which was dynamic updated by a thread from a video file, how can i write this bitmap buffer to my texture buffer(IDirect3DTexture9*m_pTexture) so that it can render as as animation texture?

Share this post


Link to post
Share on other sites
Lock the texture with the LockRect() function and copy the bits in.
Something like this:

D3DLOCKED_RECT theRect;
HRESULT hResult = m_pTexture->LockRect(0,&theRect,NULL,D3DLOCK_DISCARD);
// Copy into theRect
m_pTexture->UnlockRect(0);


Share this post


Link to post
Share on other sites
Assuming that your pixels from the video source are in the same format as the texture (E.g. 8 bits alpha, 8 bits red, 8 bits green and 8 bits blue == D3DFMT_R8G8B8):

int height; // Your bitmap height
int width; // Your bitmap width
int bpp; // _Bytes_ per pixel (32-bit = 4)
BYTE* bitmapdata; // Your bitmap bits

BYTE* pBits = (BYTE*)theRect.pBits;
for(int y=0; y<height; ++y)
{
memcpy(pBits,bitmapdata,width*bpp);
pBits += theRect.Pitch;
bitmapdata += width*bpp;
}


The Pitch of a surface is the distance between two rows of pixels. It's quite often just equal to width*bpp, but there are times when it isn't, so you need to make sure you're copying width*bpp bytes, and then moving along Pitch bytes.
Writing data into the texture is done exactly the same way as a Direct3D surface, or a DirectDraw surface. You should be able to find pleanty of tutorials and code samples on Google for copying bitmap data into a DirectDraw surface, the code will be almost identical.

Share this post


Link to post
Share on other sites
ya, thanks for your help. My app worked with your suggestion but i still have problem that bitmap is not transparent with its WHITE background after i copy this this buffer to my texture:

BYTE *mybimap=GetBitmapBuffer();

// Lock the texture surface
D3DLOCKED_RECT d3dlr;
if(FAILED(m_pTexture->LockRect(0, &d3dlr, 0, 0)))
printf("lock failed");

// Get texture pitch and pointer to texture data
BYTE *pTexturePtr = (BYTE*)d3dlr.pBits;
LONG lTexturePitch = d3dlr.Pitch;

pTexturePtr += (lTexturePitch * (m_Height-1));


switch(m_Format) {
case D3DFMT_A8R8G8B8: // 32-bit

// Loop through each row, copying bytes as you go
for(y=0;y<m_iHeight;y++) {

memcpy(pTexturePtr,mybimap,m_iWidth*4);
pTexturePtr -= lTexturePitch;
mybimap += m_Width*4;

}
break;



// Unlock the Texture
if(FAILED(m_pTexture->UnlockRect(0)))
printf("unlock failed");


however, at renderring runtime , if i put this code in , my texture is transparent (i did hide this function D3DXCreateTextureFromFileInMemoryEx(...) before because it was continous called and make slow performance ) :


D3DXCreateTextureFromFileInMemoryEx(
pd3dDevice,
mybimap,
size,
Width,
Height,
D3DX_DEFAULT,
0,
D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED,
D3DX_FILTER_LINEAR,
D3DX_FILTER_LINEAR,
0xFFFFFFFF,// WHITE Color key

NULL,
NULL,
&g_pBackgroundTexture
);

g_pBackgroundSprite->Begin(NULL);

g_pBackgroundSprite->Draw(g_pBackgroundTexture,
NULL,
NULL,
NULL,
D3DCOLOR_ARGB(0xaa,0xff,0xff,0xff));
g_pBackgroundSprite->End();

can you explain me why and show me how to resolve this problem.

Share this post


Link to post
Share on other sites

This topic is 4666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this