Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


[DX9]Probem with texture->lockrect()


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 IceBreaker23   Members   -  Reputation: 618

Like
0Likes
Like

Posted 23 December 2011 - 12:11 PM

Hello!

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

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


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

#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);

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 ;)

Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 8282

Like
1Likes
Like

Posted 23 December 2011 - 01:47 PM

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 - no such thing 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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 IceBreaker23   Members   -  Reputation: 618

Like
0Likes
Like

Posted 23 December 2011 - 02:27 PM

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 ;)

#4 mhagain   Crossbones+   -  Reputation: 8282

Like
0Likes
Like

Posted 23 December 2011 - 03:55 PM

Can you post some code? It will be easier to see.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 IceBreaker23   Members   -  Reputation: 618

Like
0Likes
Like

Posted 23 December 2011 - 03:59 PM

nearly the same as above:


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);


output(the whole bitmap is white):

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


#6 IceBreaker23   Members   -  Reputation: 618

Like
0Likes
Like

Posted 23 December 2011 - 04:17 PM

I got it ;)
I didn´t skip the alpha value right.

#7 mhagain   Crossbones+   -  Reputation: 8282

Like
0Likes
Like

Posted 23 December 2011 - 06:08 PM

Awesome. :)

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#8 IceBreaker23   Members   -  Reputation: 618

Like
0Likes
Like

Posted 24 December 2011 - 12:17 PM

Thank you! I forgot to say it ;)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS