Question

As I mentioned in title do you know how to flip an ID3D10Texture2D object horizontal/vertical ?

I used this code to take screenshot and save it to a file.

ID3D10Resource *backbufferRes;
renderTargetView->GetResource(&backbufferRes);
ID3D10Texture2D *mRenderedTexture;

// Create our texture
D3D10_TEXTURE2D_DESC texDesc;
texDesc.ArraySize = 1;
texDesc.BindFlags = 0;
texDesc.CPUAccessFlags = 0;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.Width = 640;  // must be same as backbuffer
texDesc.Height = 480; // must be same as backbuffer
texDesc.MipLevels = 1;
texDesc.MiscFlags = 0;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D10_USAGE_DEFAULT;

d3d10Device->CreateTexture2D(&texDesc, 0, &mRenderedTexture); 

d3d10Device->CopyResource(mRenderedTexture, backbufferRes);

D3DX10FilterTexture(mRenderedTexture, 0, D3DX10_FILTER_MIRROR_U);

D3DX10SaveTextureToFile(mRenderedTexture, D3DX10_IFF_PNG, L"test.png");

D3DX10FilterTexture(mRenderedTexture, 0, D3DX10_FILTER_MIRROR_U); line doesnt mirror my texture. Any suggestions ?

Was it helpful?

Solution

In your shader do 1-u to flip horizontally or 1-v to flip vertically.

Edit: If you aren't actually doing any rendering then there are far better ways to do image manipulation. However if you want to do it manually you will have to use map and flip the data round yourself.

You could do that as follows (The code is not tested so please excuse any compile errors):

D3D10Resource *backbufferRes;
renderTargetView->GetResource(&backbufferRes);
ID3D10Texture2D *mRenderedTexture;

// Create our texture
D3D10_TEXTURE2D_DESC texDesc;
texDesc.ArraySize = 1;
texDesc.BindFlags = 0;
texDesc.CPUAccessFlags = 0;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.Width = 640;  // must be same as backbuffer
texDesc.Height = 480; // must be same as backbuffer
texDesc.MipLevels = 1;
texDesc.MiscFlags = 0;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D10_USAGE_DEFAULT;

d3d10Device->CreateTexture2D(&texDesc, 0, &mRenderedTexture); 

d3d10Device->CopyResource(mRenderedTexture, backbufferRes);

D3D10_MAPPED_TEXTURE2D d3d10MT = { 0 };
mRenderedTexture->Map( 0, D3D10_MAP_READ_WRITE, 0, &d3d10MT );

unsigned int* pPix = (unsigned int)d3d10MT.pData;

int rows    = 0;
int rowsMax = height;
while( rows < rowsMax )
{
    unsigned int* pRowStart = pPix + (rows * width);
    unsigned int* pRowEnd   = pRowStart + width;
    std::reverse( pRowStart, pRowEnd );
    rows++;
}

mRenderedTexture->Unmap();

D3DX10SaveTextureToFile(mRenderedTexture, D3DX10_IFF_PNG, L"test.png");

From the doc btw:

D3DX10_FILTER_MIRROR_U Pixels off the edge of the texture on the u-axis should be mirrored, not wrapped.

So that only counts for the pixels round the edge when you are filtering the image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top