简体   繁体   中英

How to load a RGB format texture from memory using dx?

Here is my trying

in = cvLoadImage("24bpp_1920x1200_1.bmp", 1);

HRESULT err;
IDirect3DTexture9 * texture = NULL;
///D3DFMT_L8, D3DFMT_R8G8B8
err = D3DXCreateTexture(g_pd3dDevice, in->width, in->height, 1, 0 , D3DFMT_R8G8B8, D3DPOOL_MANAGED, &g_pTexture);
D3DLOCKED_RECT lockRect;
RECT rect;

err = g_pTexture->LockRect(0, &lockRect, NULL, 0);//I have specified the format is RGB, then why does lockRect.Picth = 7680?

memcpy(lockRect.pBits, in->imageData, in->widthStep*in->height);
if(FAILED(g_pTexture->UnlockRect(0))) 
{
    ///
}

It can't display an image in RGB format. But it can display an image in grayscale or in RGBA format.

Otherwise, I want to display high resolution image like the sample of displaying image using d3d in Dx Sdk_june10".\\DXSDK\\Samples\\InstalledSamples\\Textures" does. But, again, it can't display an image in size more than 1920x1200px approximately.

How to do ?

Two things to keep in mind:

(1) You need to check for caps that indicates support for the 24-bpp format. Not every Direct3D 9 device supports it.

(2) You have to respect the pitch returned by LockRect, so you need to do the copy scan-line by scan-line, so you can't do the whole thing in just one memcpy. Something like:

BYTE* sptr = in->imageData;
BYTE* dptr = lockRect.pBits;
for( int y = 0; y < in->height; ++y )
{
    memcpy( dptr, sptr, in->widthStep );
    sptr += in->widthStep;
    dptr += lockRect.Pitch;
}

This assumes in->widthStep is the pitch of the source image in bytes, that in->widthStep is <= lockRect.Pitch , and that the format of the source image is identical to the format of the Direct3D resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM