1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$
    – Jack
    Commented Dec 5, 2011 at 13:44
  • \$\begingroup\$ 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. \$\endgroup\$
    – Adam
    Commented Dec 6, 2011 at 0:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .