I mean that I want to replace some pixels and add some alpha value.
What should I do?
How to edit a texture after I create it from file?
You can lock it - make the changes - and then unlock it again.
Here's what I usually use:
And here is an example of using the lock class:
Here's what I usually use:
//// Special class for locking surfaces and accessing pixels.// Supported pixel formats are 8, 16, 24 and 32-bit modes.//template <class PixelColor, unsigned int PITCH_DIVISOR> class LckS {public: LPDIRECT3DSURFACE9 surf; D3DLOCKED_RECT lr; union { PixelColor *d; BYTE *c; }; DWORD pitch; inline LckS( LPDIRECT3DSURFACE9 _s, RECT *srcRect ) { surf = _s; surf->LockRect( &lr, srcRect, 0 ); d = static_cast<PixelColor *>(lr.pBits); pitch =(DWORD)(lr.Pitch >> PITCH_DIVISOR); } inline ~LckS() { surf->UnlockRect( ); } inline bool isLocked() { return (d != 0); }};#define LOCALLOCK(surface, srcRect, pixelFormat, divisor) LckS< pixelFormat, divisor > __l( surface, srcRect )#define PIX __l.d#define PITCH (__l.pitch)#define PIXEL(x,y) __l.d[(x) + (y)*PITCH]#define IS_LOCKED (PIX != 0)
And here is an example of using the lock class:
LPDIRECT3DTEXTURE9 pTexture; // This is your previously loaded texture.//// Retrieve the surface image of 'pTexture'//LPDIRECT3DSURFACE9 pTexSurface;HR (pTexture->GetSurfaceLevel(0, &pTexSurface) );//// Lock and fill the surface//{ LOCALLOCK(pTexSurface, NULL, D3DCOLOR, 2); // // Fill with white all at once. // memset( PIX, 0xff, GetWidth() * GetHeight() * sizeof(D3DCOLOR) ); // // Fill with gray using a pixel-by-pixel technique. // for (UINT i = 0; i < (UINT)GetHeight(); i++) { for (UINT j = 0; j < (UINT)GetWidth(); j++) { PIXEL(j, i) = D3DCOLOR_COLORVALUE( 0.5f, 0.5f, 0.5f, 1.0f ); } }} // <========== LckS goes out of scope hereSAFE_RELEASE( pTexSurface );pTexture->GenerateMipSubLevels();
Quote:Original post by FutureCode
So I've got a D3DLOCKED_RECT.
Could you give me an example of how to replace a pixel?
My texture is using A8R8G8B8.
void SetPixel(const D3DLOCKED_RECT& rect, int x, int y, D3DCOLOR clr){ // Cast the locked bits pointer to a BYTE* so we can do some pointer arithmetic on it BYTE* pBits = (BYTE*)rect.pBits // Skip to the start of the row we want pBits += rect.Pitch * y; // Skip to the pixel we want const int cnBytesPerPixel = 4; // 32-bit format pBits += cnBytesPerPixel * x; // Cast pointer to a D3DCOLOR (32-bit value) so we can write the whole colour in one go D3DCOLOR* pPixel = (D3DCOLOR)pBits; // Replace the pixel *pPixel = clr;}
For A8R8G8B8, just take the example above and replace the '2' in LOCALLOCK with a '4', because there are 4 bytes in one pixel and the locked rect's pitch will be the number of bytes in one row of the image's surface.
Thank you, I've just made it.
And I finally found how those programmers use transparency using *.bmp images...
And I finally found how those programmers use transparency using *.bmp images...
Quote:Original post by FutureCodeI'd avoid using alpha in .bmp files, it's not officially supported, although a lot of applications allow it (Technically the other 8 bits in 32-bit BMPs are "reserved", not alpha).
Thank you, I've just made it.
And I finally found how those programmers use transparency using *.bmp images...
If you're using alpha, I'd recommend using PNG or DDS files.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement