Sign in to follow this  
advance-software

Flipping a texture upside down (DX9)

Recommended Posts

Hi all,

I need to flip textures vertically on load & am using D3DX functions to load.

D3DXCreateTextureFromFileEx does not appear to have an option to invert on load (unless I missed something), so I create a secondary surface, lock both & copy the bits from one to the other a line at a time to invert.

This works fine on uncompressed surfaces but seems to screw up DXT1 surfaces.

Does anyone know how to flip a DXT1 surface upside down using DX9 ?

Best regards,
Steve.

Share this post


Link to post
Share on other sites
DXT surfaces are compressed in 4x4 pixel blocks, so if you just flip Pitch bytes, you'll get 4 lines the correct way up, but the 4 pixel blocks vertically flipped.

For flipping a DXT1 surface, you'll need to re-order the bits in the 4x4 block; a 4x4 block is compressed into 8 bytes (See Here), with the first 4 bytes representing two 16-bit colours, and the last 4 bytes representing the pixel data with 2 bits per pixel.

So to vertically flip a 4x4 block, you'd want something like this (untested):

void flipBlock(BYTE* pBlock)
{
// Skip past the two 16-bit colours
pBlock += 4;

// Copy the pixel bits
DWORD dwMask = *(DWORD*)pBlock;

// Row 0 = bits 24..32
// Row 1 = bits 16..23
// Row 2 = bits 8..15
// Row 3 = bits 0..7
DWORD dwFlipped = 0;
dwFlipped |= (dwMask & 0xff000000) >> 24; // Row 0 -> row 3
dwFlipped |= (dwMask & 0x00ff0000) >> 8; // Row 1 -> row 2
dwFlipped |= (dwMask & 0x0000ff00) << 8; // Row 2 -> row 1
dwFlipped |= (dwMask & 0x000000ff) << 24; // Row 3 -> row 0

// Put the pixel data back into the buffer
*(DWORD*)pBlock = dwFlipped;
}


So you'd use your current code for flipping each scanline (Pitch bytes), and then go through each scanline and flip each block.

It'll mess up if your texture isn't a multiple of 4 pixels in height (Since you'll have padded blocks in your texture, with blank space you won't want to flip), but it's still possible.

Is there any reason you can't just flip the texture coordinates of the object you're applying the texture to? That'd be much easier...

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