Skip to main content
GameDev.net gamedev.net
🔒 Locked

IDIRECT3DTEXTURE9 Question

Started by choffstein Feb 25, 2005 at 7:21 PM 4 replies 950+ views
Original Post
choffstein
choffstein
Is there any way for me to retrieve a specific pixel color (r,g,b) from a texture at a specific location (x,y)? Is there any way to set a color on the texture? I scoured MSDN and couldn't find anything. Basically, I was looking through a book on terrain rendering, and am going to try interpolating different texture colors at different height locations to give the look of flowing terrain, while still only using "one" texture (everything will be pre-done). If there is no way to do what I asked above, does anyone have a better method for this? Thanks!
circlesoft
circlesoft
Yep, this is possible. This thread goes into detail on how to do it. Be aware though, that locking a texture is *very* slow - it should be done at load-time, preferably.
choffstein
choffstein
Yep. The idea was to simply do this once at loading, and never again. This is just for my first terrain demo. Hopefully I will figure out better methods down the line. Thanks a bunch!
choffstein
choffstein
So I tried to get a specific pixel out and go stuck. Here is my code:
void CTexture::GetColor(int x, int y, float *r, float *g, float *b){	D3DLOCKED_RECT data;	HRESULT hr;	hr = texture->LockRect(0, &data, NULL, D3DLOCK_READONLY);	if FAILED(hr)		return;	BYTE* bits = (BYTE *)data.pBits;	int offset = ((data.Pitch/sizeof(BYTE)) * y)) + x;	BYTE rgb = bits[offset];        /* This is where the issue is.  How do I get the specific colors?	*r = rgb >> 8;	*g = rgb & 0x00FF00 >> 4;	*b = rgb & 0x0000FF;        */	texture->UnlockRect(0);}


Also, after loading a texture as a LPDIRECT3DTEXTURE9, is there a way for me to find out the height and the width of the texture? I cant figure it out. I suppose I could LockRect(), and probably figure it out that way...but I cant quite get it.

Thoughts?

Thanks!
load_bitmap_file
load_bitmap_file
You need to use D3DSURFACE_DESC and GetLevelDesc, see the documentation for more information.

Where "texture" is a LPDIRECT3DTEXTURE9:

D3DSURFACE_DESC textureDesc;
//the first parameter 0 here specifies the level
texture->GetLevelDesc(0, &textureDesc);

And you can access the width/height like so:
textureDesc.width
textureDesc.height

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.