Jump to content
  • Advertisement

Archived

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

Rhapsodus

Handling Pixel formats without 20 funtions

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

Is there a way to just directly compute the byte offset from the texture bits to a specific pixel''s Alpha component ? The code snippet below has a big flaw, it assume 32 bit textures in the A8R8G8B8 format. However, one thing I want to avoid is having to write 20 functions and a whole bunch of different structs for each Pixel Format

//--------------------------------------------------------------------

// Helper struct 

//--------------------------------------------------------------------

struct PixelColor
{
	unsigned char nBlue;
	unsigned char nGreen;
	unsigned char nRed;
	unsigned char nAlpha;
};



PixelColor *tColor;
LPDIRECT3DTEXTURE8 pTexture = getTexture( "Moo" );
D3DLOCKED_RECT locked_rect;
D3DSURFACE_DESC desc;

if( pTexture != NULL )
{
	//--------------------------------------------------------------------

	// Grab the correct location in the texture not relative to the screen

    // but to the graphic

	//--------------------------------------------------------------------

	float tX = nX - getX(); //getX returns position.x in screen coordinates

	float tY = nY - getY(); //getY returns position.y in screen coordinates


	//--------------------------------------------------------------------

	// Get the level description and lock the surface for the bits

	//--------------------------------------------------------------------

	pTexture->GetLevelDesc(0, &desc);
	pTexture->LockRect(0, &locked_rect, NULL, D3DLOCK_NOSYSLOCK );
	tColor = (PixelColor*)locked_rect.pBits;

	//--------------------------------------------------------------------

	// Transform texture coordinates to the proper byte offset

	//--------------------------------------------------------------------

	int nOffset = ( ((int)tY*desc.Width) + (int)tX );
	tColor += nOffset;
}

I appreciate any advice

Share this post


Link to post
Share on other sites
Advertisement
I recommend that you pick a few "supported" texture formats and use them (for example, A8R8G8B8, A1R5G5B5 and A4R4G4B4).
You could, of course, make a special case for every possible format, but I doubt it would be worth the trouble.


-Nik

Share this post


Link to post
Share on other sites
I fail to see why anyone would have any use (in a d3d application, anyway) for more than a handful of pixel formats. Sure, it'd be nice to have a level editor that would let you use any texture format on the market with any pixel format, but it's really unlikely that you'll encounter the need.

Edit: finish the thought...dur.

[edited by - Etnu on May 8, 2004 6:06:23 PM]

Share this post


Link to post
Share on other sites
It comes down to making sure I don''t overwrite memory and causing unforseen crashes when modifying/testing color components in a texture...

In reality there probably is only 3-4 formats to account for A8R8G8B8, A4R4G4B4, A2B10G10R10, A1R5G5B5, are the most widely used, but what guarantee is there for a card to load it in those formats? I was just curious if there was a direct way to calculate it rather than have the same 2 - 3 functions

quote:
Original post by Etnu
I fail to see why anyone would have any use (in a d3d application, anyway) for more than a handful of pixel formats. Sure, it''d be nice to have a level editor that would let you use any texture format on the market with any pixel format, but it''s really unlikely that you''ll encounter the need.



Share this post


Link to post
Share on other sites
Doesn''t the pitch of a locked rect, contain this information?


D3DLOCKED_RECT LockedRect;
pTextureSurface->LockRect(&LockedRect, 0, 0)


I''m pretty sure to access every byte of a A8R8G8B8, you have to divide the pitch by 4. Perhaps theres a similar value for the other formats?

This would create a texture, and than fill it all in with red:

if(FAILED(D3DXCreateTexture(pD3DDevice, uiWidth, uiHeight, D3DX_DEFAULT, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pTexture)))
MessageBox(0x00, "FAILED TO CREATE TEXTURE", "WOW", MB_OK);

IDirect3DSurface9 *pTextureSurface = NULL;

if(FAILED(m_pTexture->GetSurfaceLevel(0, &pTextureSurface)))
MessageBox(0x00, "FAILED TO GET SURFACE LEVEL", "WOW", MB_OK);

D3DLOCKED_RECT LockedRect;
if(FAILED(pTextureSurface->LockRect(&LockedRect, 0, 0)))
MessageBox(0x00, "FAILED TO LOCK RECT", "WOW", MB_OK);

DWORD *pData = (DWORD *)LockedRect.pBits;

for(int z = 0; z < uiHeight; ++z)
{
for(int x = 0; x < uiWidth; ++x)
{
// (uiX + uiZ * (m_uiXSize + 1))

// z + x instead of x + z

// Images are stored stupidly apparently.

pData[z + x * (LockedRect.Pitch / 4)] = D3DCOLOR_ARGB(0, 255, 0, 0);
}
}

if(FAILED(pTextureSurface->UnlockRect()))
MessageBox(0x00, "FAILED TO UNLOCK RECT", "WOW", MB_OK);

pTextureSurface->Release();

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!