Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to make DirectX display a sprite from a sprite sheet I found on the internet but it doesn't seem to be displaying the texture in the correct position as I am specifying it.

D3DXVECTOR2 scaling = D3DXVECTOR2(1.0,1.0);
D3DXVECTOR2 center = D3DXVECTOR2(64,64);
D3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,NULL,rot,&position);
sprite->SetTransform(&mat);
sprite->Draw(zombieTexture,&srcRect,NULL,NULL,0xFFFFFFFF);

srcRect is defined as follows:

srcRect.top = 384;
srcRect.bottom = 512;
srcRect.left = 512;
srcRect.right = 640;

which instead of giving this texture:

enter image description here

It gets this one: (ignore the rotation)

enter image description here

What am I doing wrong?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I'm guessing a bit here, but unless the coordinates are just wrong, this is probably why they are off.

One thing that can happen when you specify coordinates in pixels rather than as 0-1 UV coordinates is that if the texture is resized the coordinates go wrong.

By default the D3DX image loading functions will resize images under some circumstances. For example if they exceed the maximum supported texture size, or the dimensions aren't powers of two.

The solution is either to resize the texture, or use D3DX_DEFAULT_NONPOW2.

share|improve this answer
    
Ah, thanks, this looks like its the issue but ill check when im home later. So would it be better to be using UV coordinates for specifying the texture location? If so how do I go about finding the coordinates at a certain point? –  Jack Dec 5 '11 at 13:44
    
You divide the pixel coordinates by the texture size. For example in a 100x1000 texture the point 50, 500 would be 0.5, 0.5. You may find msdn.microsoft.com/en-us/library/windows/desktop/… useful too as there's some additional complexity with D3D9. –  Adam Dec 6 '11 at 0:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.