Jump to content
  • Advertisement
Sign in to follow this  
FutureCode

How to edit a texture after I create it from file?

This topic is 2993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
You can lock it - make the changes - and then unlock it again.

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 here

SAFE_RELEASE( pTexSurface );
pTexture->GenerateMipSubLevels();


Share this post


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

That code is just an example, you normally wouldn't want to call a function to set a single colour (Since you're usually setting several pixels at once). That code also assumes that the (x, y) position passed is relative to the top left of the RECT locked (So it's in "real" coordinates if you lock the entire surface).

Share this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by FutureCode
Thank you, I've just made it.
And I finally found how those programmers use transparency using *.bmp images...
I'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).

If you're using alpha, I'd recommend using PNG or DDS files.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!