Sign in to follow this  
Florian22222

[DX9]Probem with texture->lockrect()

Recommended Posts

Florian22222    618
Hello!

I have a problem with my dx9 texture.
I am loading the texture:
[code]

if( FAILED(D3DXCreateTextureFromFile( m_pd3dDevice,m_TextureFilename, &m_pMeshTextures[0])) )
{
MessageBox(NULL,L"Texture missing",L"Error",NULL);
exit(1);
}
[/code]


and then i want to get the color of each pixel to compute the height of each vertex:

[code]
#define To1Dim(a,b) ((a)+((m_Width+1)*(b)))

D3DLOCKED_RECT locked;
if(FAILED(m_pMeshTextures[0]->LockRect(0,&locked,NULL,0)))
{
MessageBox(NULL,L"Texture locking error",L"Error",NULL);
exit(1);
}

BYTE *bytePointer=(BYTE*)locked.pBits;
int sizePerPixel = 3; //convert to bytes

for(UINT z = 0;z<=m_Height;z++)
{
for(UINT x = 0;x<=m_Width*sizePerPixel;x+=sizePerPixel)
{
int index = To1Dim(x/sizePerPixel,z);

int sumColor = bytePointer[index] + bytePointer[index+1] + bytePointer[index+2]; //sum of rgb; max = 255 * 3
float y = (float)sumColor / (255 *3); //convert to value between 0.0f and 1.0f

D3DXVECTOR3 pos((x/sizePerPixel),y,z);

m_pVertices[index].position = pos;
}
}

m_pMeshTextures[0]->UnlockRect(0);
[/code]

but there is much more information in the texture then I read. I have a 10x10 bmp and there are only 10x10x3 bytes for pixels.
But when i read out of the texture in an infinite loop i can do the z-loop over 4000 times till i get an violent access. (4000*10*3 = 120000bytes)
the format of the bmp is R8G8B8 if this is important.

Please help me ;)

Share this post


Link to post
Share on other sites
mhagain    13430
There are two formats here; the first is the format of the image on disk and the second is the format of the texture in GPU memory. If an image on disk is in a format that is not available to the GPU, the D3DX function will convert it in order to create a texture from it.

Now, there is - in D3D9 - [i]no such thing[/i] as a R8G8B8 format for use on the GPU, so the D3DX function is converting it to a 32-bit format (most likely X8R8G8B8) when creating the texture. It may also resize the texture if needed (which might be the case if your hardware doesn't support non-power-of-two textures), so your usage is quite unsafe. You should call GetLevelDesc on the texture and check the format and size members, because what gets created for the GPU is in no way guaranteed to exactly match the source image.

Share this post


Link to post
Share on other sites
Florian22222    618
Thank you! :)

now i have a problem with the bitmap. I´ve now created a bitmap with 16x16 and set my heightfield to this too.
the whole bitmap is only white. but when i read I get this as debug output in an loop(about 256/4 = 64 times):


255|255|255
255|255|0
255|0|255
0|255|255

then i tried to skip every fourth pixel(alpha/x byte) but i got the same issue.

I hope you can help once more ;)

Share this post


Link to post
Share on other sites
Florian22222    618
nearly the same as above:

[code]

if(desc.Format != 22) //X8R8G8B8
{
MessageBox(NULL,TEXT("Texture format in heightfield is not supported"),TEXT("Heightfield error"),MB_OK);
exit(1);
}

D3DLOCKED_RECT locked;
if(FAILED(m_pMeshTextures[0]->LockRect(0,&locked,NULL,0)))
{
MessageBox(NULL,L"Texture locking error",L"Error",NULL);
exit(1);
}

BYTE *bytePointer=(BYTE*)locked.pBits;
int sizePerPixel = 4; //convert to bytes

for(UINT z = 0;z<=m_Height;z++)
{
for(UINT x = 0;x<=m_Width*sizePerPixel;x+=sizePerPixel)
{
int index = To1Dim(x/sizePerPixel,z);

int sumColor = bytePointer[index] + bytePointer[index+1] + bytePointer[index+2]; //sum of rgb; max = 255 * 3
//tried this too: int sumColor = bytePointer[index+1] + bytePointer[index+2] + bytePointer[index+3];

float y = (float)sumColor / (255 *3); //convert to value between 0.0f and 1.0f

std::wstringstream s;
s << (float)bytePointer[index] <<"|"<< (float)bytePointer[index+1] <<"|"<< (float)bytePointer[index+2] << " sumColor: " << sumColor <<" -> y: "<< y <<"\n";
OutputDebugString(s.str().c_str());

D3DXVECTOR3 pos((x/sizePerPixel),y,z);

m_pVertices[index].position = pos;
}
}

m_pMeshTextures[0]->UnlockRect(0);
[/code]


output(the whole bitmap is white):
[code]

255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
255|255|255 sumColor: 765 -> y: 1
255|255|0 sumColor: 510 -> y: 0.666667
255|0|255 sumColor: 510 -> y: 0.666667
0|255|255 sumColor: 510 -> y: 0.666667
[/code]

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