Sign in to follow this  
Erik1234

Create a solid color IDirect3DTexture9

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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
}
}

// Snip

DWORD dwWidth = 256;
DWORD dwHeight = 256;
LPDIRECT3DTEXTURE9 pTexture; // Your created dwWidth by dwheight texture
D3DFORMAT fmtTexture = D3DFMT_A8R8G8B8; // Format of pTexture
D3DCOLOR dwColour = D3DCOLOR_ARGB(0.5, 0.5, 0.5, 0.0); // Your colour
DWORD 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

Share this post


Link to post
Share on other sites
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 D3DXFillTexture
if (FAILED (hr = D3DXFillTexture (m_pTexture, ColorFill, NULL)))
{
return hr;
}


Jack

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

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