4

I have a pointer to loacation in memory which contains my pixel information. I want to display this on screen using textures or any other form.Is it possible to create texture from memory in directx 9? Thanks

Here is the code sample

D3DLOCKED_RECT r;
HRESULT hr;
DWORD c0=D3DCOLOR_ARGB(255,0,0,0), c1=D3DCOLOR_ARGB(255,255,255,255);
DWORD *pData, 
Image[64]={c0,c0,c0,c1,c0,c0,c0,c0,
   c0,c0,c0,c1,c0,c0,c0,c0,
   c0,c0,c0,c1,c0,c0,c0,c0,
       c1,c1,c1,c1,c1,c1,c1,c0,
       c0,c0,c0,c1,c0,c0,c0,c0,
   c0,c0,c0,c1,c0,c0,c0,c0,
   c0,c0,c0,c1,c0,c0,c0,c0,
   c0,c0,c0,c0,c0,c0,c0,c0};
if (m_pDevice->CreateTexture(8,8,1,D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,   &m_pPointTexture,NULL)!=D3D_OK)
    return false;
if (m_pPointTexture->LockRect(0,&r,NULL, D3DLOCK_DISCARD |D3DLOCK_NOOVERWRITE)!=D3D_OK)
    return false;
for (int y=0; y<8; y++)
{
 pData=(DWORD*)((BYTE*)r.pBits + r.Pitch);
 for (int x=0; x<8; x++)
 pData[x]=Image[y*8+x];
}
m_pDevice->SetTexture(0,m_pPointTexture);

Then in i render it by drawing rectangle primitive Here before setTexture if i save my texture to a BMP file by D3DXSaveTextureToFile() and then create texture using D3DXCreateTextureFromFile(). Then i get expected output

1 Answers1

4

D3DXCreateTextureFromFileInMemory (doc) if it's a imagefile in memory.

However, if it's only the color data just create a texture with D3DXCreateTexture (doc), lock it and write the data to the texture manually.

Nerfboy
  • 160
  • 3
  • 8
Gnietschow
  • 2,831
  • 1
  • 14
  • 23
  • I tried doing it but the result is displayed as blank black screen where as wen i write my created texture to bmp file using D3DXSaveTextureToFile() and then load the created BMP into texture using D3DXCreateTextureFromFile() i get the expected output.. Any idea where im going wrong –  Feb 08 '13 at 09:42
  • Can you add some code of your loading and rendering to your question? – Gnietschow Feb 08 '13 at 11:05
  • `D3DLOCK_NOOVERWRITE` can only be used for vertexbuffers not for textures. `D3DLOCK_DISCARD` is sufficient. And you have to unlock the texture with `UnlockRect`. Apart from that I don't see other problems. – Gnietschow Feb 09 '13 at 10:47
  • oh yea.. i have used unlockRect also forgot to mention that..Now changed to D3DLOCK_DISCARD also but still same problem.. –  Feb 09 '13 at 10:56
  • If you could give me an example of how to create/render texture from memory that would be a great help to me. –  Feb 09 '13 at 11:00
  • I searched for some code, where I created a texture and manipulated it afterwards. The main difference to your code is that I created it with usage 0 and `D3DPOOL_MANAGED` and at the lockRect I didn't used any flags. Apart from that shouldn't it be `pData=(DWORD*)((BYTE*)r.pBits + r.Pitch * y);` in your code, otherwise you overwrite your data, because there's no correct offset. – Gnietschow Feb 09 '13 at 11:28
  • ThANX A TONNE.. IT WORKED NOW THE PROBLEM WAS WITHHE USAGE PARAMETER..ONCE I CHANGED IT.. I GOT THE EXPECTED DISPLAY.. THNX AGAIN –  Feb 11 '13 at 02:14
  • What usage parameter did you use?? – alDiablo Apr 10 '17 at 08:02
  • I used no usage parameter, so a `0`. – Gnietschow Apr 10 '17 at 11:27