Create a solid color IDirect3DTexture9

Started by
2 comments, last by Erik1234 17 years, 10 months ago
Based on red, green, blue, and alpha only, I would like to create a texture. For, example, I want a half red, and half green texture that is half tranparent, so I would use D3DCOLOR_ARGB(0.5, 0.5, 0.5, 0.0)... But, from this point on, I have no idea on how to get that D3DCOLOR structure turned into a IDirect3DTexutre9 class or interface.
Advertisement
Just lock the texture and fill in all the texels. You'll need to re-pack your ARGB value into the same format as the texture expects.

Example (Off the top of my head and without error checking):
DWORD ConvertFormat(D3DFORMAT fmtDest, DWORD dwColour, DWORD* pdwStride){   switch(fmtDest)   {      case D3DFMT_A8R8G8B8:         *pdwStride = 4;         return dwColour;      case D3DFMT_R5G6B5:         *pdwStride = 2;         return ((dwColour&0xff0000)>>0xF800) | ((dwColour&0x00ff00)>>0x7E0) | ((dwColour&0x0000ff)>>0x1F);      // Etc   }}// SnipDWORD dwWidth = 256;DWORD dwHeight = 256;LPDIRECT3DTEXTURE9 pTexture; // Your created dwWidth by dwheight textureD3DFORMAT fmtTexture = D3DFMT_A8R8G8B8; // Format of pTextureD3DCOLOR dwColour = D3DCOLOR_ARGB(0.5, 0.5, 0.5, 0.0); // Your colourDWORD dwPackedColour, dwStride;D3DLOCKED_RECT theRect;   // Convert the colour   dwPackedColour = ConvertFormat(fmtTexture, dwColour, &dwStride);   // Lock the texture   pTexture->LockRect(0, &theRect, NULL, 0);   // Fill it in   for(DWORD y=0; y<dwHeight; ++y)   {      BYTE* pBits = (BYTE*)theRect.pBits + theRect.Pitch;      for(DWORD x=0; x<dwWidth; ++x)      {         memcpy(pBits, dwPackedColour, dwStride);         pBits += dwStride;      }   }   // Unlock the texture   pTexture->Unlock();


EDIT: Why do you want to do this anyway? You could probably just use vertex colour
If you're using the texture as a render-target you can use the HW accelerated IDirect3DDevice9::ColorFill() operation...

Otherwise, just use D3DXFillTexture() - it's probably the easiest way and you don't have to mess about with pixel formats and directly locking stuff [grin]

As per the SDK documentation:
VOID WINAPI ColorFill (D3DXVECTOR4* pOut, const D3DXVECTOR2* pTexCoord, const D3DXVECTOR2* pTexelSize, LPVOID pData){    *pOut = D3DXVECTOR4( 0.5f, 0.5f, 0.5f, 0.0f ); // put your colour here, pass it in via 'pData' if necessary..}        // Fill the texture using D3DXFillTextureif (FAILED (hr = D3DXFillTexture (m_pTexture, ColorFill, NULL))){    return hr;}


Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

I hooked Counter Strike Source's Set Texture Method for D3D. I first need to learn how to make a solid color texture for the white walls effect, and secondly I need to learn how to make any texture transparent (which I forgot to ask lol)

This topic is closed to new replies.

Advertisement