Sign in to follow this  
odyodyodys

transformation matrix went wrong.. (I NEED some HELP!!)

Recommended Posts

Hi all, I am trying to use ID3DXSprite to draw images on the screen. I want to do something like the life and power bar in games, 2d graphics. I've managed to do it, but i'm failing at the transformation matrix that will scale the image as desired. I've loaded a texture and the code below is inside the BeginScene-EndScene What I do is the following:
//Load the texture (this is in the initialization method)
D3DXCreateTextureFromFile(device, imageFilename.c_str(), &imageTexture);

//------------------------- inside the BeginScene/EndScene method

sprite->Begin(D3DXSPRITE_ALPHABLEND);

D3DXVECTOR3 position(0, 0, 0); // Set the image to start from top left

// My image is 1440*1050 and my monitor resolution is 1440*900
// I've set those values by testing it, and like this it is stretched for this resolution only
imageStretchMatrix = D3DXMATRIX( 0.703f,  0.0f,  0.0f,  0.0f,
                                 0.0f,  0.439f,  0.0f,  0.0f,
                                 0.0f,  0.0f,  1.0f,  0.0f,
                                 0.0f,  0.0f,  0.0f,  1.0f);

// Transform using the imageStrechMatrix
sprite->SetTransform(&imageStretchMatrix);

sprite->Draw(imageTexture, NULL, NULL, &position, D3DCOLOR_ARGB(255, 255, 255, 255));

sprite->End();


What should I do so the image is stretched in the monitor? (to cover the whole screen) and how should I compute the x and y transformation factors? Thanks in advance, every bit of info is treasure for me!

Share this post


Link to post
Share on other sites
The X scaling factor you need in this case is simply screenWidth / imageWidth. The Y scaling factor is screenHeight / imageHeight. Once you have these values, you can simply give them to D3DXMatrixScaling


// Get the texture dimensions
D3DSURFACE_DESC imageDesc;
imageTexture->GetLevelDesc(0, &imageDesc);

// Get the backbuffer dimensions (you should probably have this stored anyway)
LPDIRECT3DSURFACE9 backBuffer;
device->GetRenderTarget(0, &backBuffer);
D3DSURFACE_DESC backbufferDesc;
backBuffer->GetDesc(&backbufferDesc);

// If you're not going to hold onto a pointer to the backbuffer, make sure you
// Release it!
backBuffer->Release();

// Figure out the scaling values
float scaleX = backbufferDesc.Width / (float)imageDesc.Width;
float scaleY = backbufferDesc.Height / (float)imageDesc.Height ;

// Make the scaling matrix
D3DXMATRIX scaleMatrix;
D3DXMatrixScaling(&scaleMatrix, scaleX, scaleY, 1.0f);


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