Archived

This topic is now archived and is closed to further replies.

Chaucer

Updating a texture

Recommended Posts

I''ve got a source texture that is created as a rendertarget and drawn to. I later would like to copy the data from this texture, darken it, and set its value to a new texture. I''m getting an access violation when I try to read the texture. Since this texture is required to be a rendertarget and not dynamic, I''m not sure how to handle. Here''s my source for updating the texture. Can anyone help?
  
void DarkenTexture(LPDIRECT3DTEXTURE8 *pSourceTexture, LPDIRECT3DTEXTURE8 *pDestTexture, int textureSize, int value)
{
	if(textureSize<0) return;
	if(!pSourceTexture) return;
	if(!pDestTexture) return;

	BYTE *pSourceData = new BYTE[textureSize*textureSize];

	//read data from source texture

	D3DLOCKED_RECT d3dlr;
	(*pSourceTexture)->LockRect(0, &d3dlr,0, D3DLOCK_READONLY);
	BYTE *pSourceBits = (BYTE*)d3dlr.pBits;
	int dataIndex=0;
	int bitIndex=0;
	for(int x=0;x<textureSize;x++)
	{
		for(int y=0;y<textureSize;y++)
			pSourceData[dataIndex++] = pSourceBits[bitIndex++];
		bitIndex += d3dlr.Pitch - (textureSize);
	}
	(*pSourceTexture)->UnlockRect(0);

	//read data from source texture

	(*pDestTexture)->LockRect(0, &d3dlr,0, 0);
	BYTE *pDestBits = (BYTE*)d3dlr.pBits;
	dataIndex=0;
	bitIndex=0;
	for(x=0;x<textureSize;x++)
	{
		for(int y=0;y<textureSize;y++)
		{
			if(pSourceData[dataIndex++] - value > 0)
				pDestBits[bitIndex++] = pSourceData[dataIndex++] - value;
			else
				pDestBits[bitIndex++] = 0;
		}
		bitIndex += d3dlr.Pitch - (textureSize);
	}
	(*pDestTexture)->UnlockRect(0);

}
  

Share this post


Link to post
Share on other sites