Jump to content
  • Advertisement
Sign in to follow this  

Creating Textures - Direct3D

This topic is 4862 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

You can't use memcpy on a d3d texture. The reason for this is that when you create a texture, it is not always created with the exact dimensions you specify. There is often more memory allocated than is actually used for performance reasons. You're ignoring the pitch of the locked surface. You can't ignore this value. The pitch is what tells you how many bytes are in each row of the texture.

Here is the right way way to do this:

m_pTex->GetLevelDesc(0, &desc);
m_pTex->LockRect(0, &rect, 0, 0);

// each channel is 8 bits, so we use an unsigned char* to access surface
unsigned char *pSurfBits = static_cast<unsigned char*>(rect.pBits);
for( DWORD dwRow = 0; dwRow < desc.Height; dwRow++ )
DWORD dwRowStart = dwRow * rect.Pitch;

// You could copy the pixels individually...
for( DWORD dwCol = 0; dwCol < desc.Width; dwCol++ )
DWORD dwColStart = dwCol * 4; // 4 channels per pixel
pSurfBits[dwRowStart + dwColStart + 0] = ;// Blue
pSurfBits[dwRowStart + dwColStart + 1] = ;// Green
pSurfBits[dwRowStart + dwColStart + 2] = ;// Red
pSurfBits[dwRowStart + dwColStart + 3] = ;// Alpha

// Or you could copy one row at a time.
memcpy(pSurfBits + dwRowStart, lpbi->biSizeImage + /*some offset for the row*/, lpbi->Width*sizeof(unsigned char));

Hope this helps,

Share this post

Link to post
Share on other sites
Original post by iddqd42
Ya, that help alot, always felt like something was missing...

However, it still aint displaying it properly, long time since i slept so im probably doing something really lame ;) cant find it tho...

(...snipped code...)

pBitmapData (unsigned char *) points to the first imagedatabyte of a 24bit bmp.

That memcpy line will only work if both the D3D surface and the image data you're copying are the same format. So if you've created the D3D surface as a D3DFMT_A8R8G8B8 or D3DFMT_X8R8G8B8 surface, then copying a row of the 24bit bmp using memcpy will not work because part of the 24bit image data will be copied into the alpha channel of the D3D surface.

If that is the case, you'll need to copy the individual elements like in the inner loop of the code I posted.


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!