# ID3DXSprite Scaling and Rotation

## Recommended Posts

Hi everyone. I am having a problem with ID3DXSprite. I'm trying to set the scaling matrix to be used to transform the texture that is going to be rendered. I'm trying to make it so the user can specify the width and height they want the texture rendered at. DirectX's sprite class doesn't do that internally though, I have to find the value at which to scale the original texture width and height to get the width and height the user wants.
bgeSprite::Draw(bgeVector &pos, bgeVector &Center, float width, float height, float rotation, BGECOLOR VisibleColor)
{
ID3DXSprite * sprite = g_Sprites.at(this->m_nID);
IDirect3DTexture9 * texture = g_Textures.at(this->m_pTexture->GetID());
D3DXVECTOR2 SpriteCenter(Center.GetX(), Center.GetY());
D3DXVECTOR2 SpriteTrans(pos.GetX(), pos.GetY());
D3DXVECTOR2 SpriteScale((this->m_pTexture->GetWidth())*1/width, (this->m_pTexture->GetHeight())*1/height);
D3DXMATRIX res;

if (FAILED(sprite->Begin(D3DXSPRITE_ALPHABLEND))) {
return BGE_FAIL;
}

D3DXMatrixTransformation2D(&res, NULL, 0.0f, &SpriteScale, &SpriteCenter, rotation, &SpriteTrans);
sprite->SetTransform(&res);

HRESULT hr = sprite->Draw(texture, NULL, NULL, NULL, VisibleColor);
if (FAILED(hr)) { return BGE_FAIL; }

sprite->End();
return BGE_OK;
}
scaling code:
D3DXVECTOR2 SpriteScale((this->m_pTexture->GetWidth())*1/width, (this->m_pTexture->GetHeight())*1/height);
That is one of the "draw" functions that renders the sprite on the screen. As you can see I am trying to set the "SpriteScale" vector to the values I need it to be scaled by. But for some reason, it draws it larger than I originally want, which sets the rotation off. Here is an example of what I mean. Both of these textures are different sizes but I am rendering them both at 160x160. The white lines represent the real 160x160 position. http://img517.imageshack.us/img517/9595/scrsm6.jpg http://img293.imageshack.us/img293/6776/scr2zs5.jpg Any ideas of why it is scaling it incorrectly? Is my "formula" for getting the scalar incorrect? TIA :D

##### Share on other sites
Quote:
 Original post by chris95219Any ideas of why it is scaling it incorrectly? Is my "formula" for getting the scalar incorrect?

Yes and no. (x * y / z) is different to (x * (y / z)). But in your case, it's the same anyway (since you're multiplying by 1).

It actually looks like a problem of powers of two - I suspect that when you load your texture (presumably with D3DX) it resized it to the nearest power of two, which is default behaviour. This means that the image may be unexpectedly larger, causing the problem you're seeing. Check your texture loading function, and see the DirectX documentation for the appropriate flag to disable POT resizing.

##### Share on other sites
Hi, I really appreciate you helping me but I can't find out how to disable powers of two. I figured looking at SetSamplerState() and the closing thing I saw was to set the mag and min filters to POINT, but that is already done by default. :(

Any ideas?

##### Share on other sites
If you're using D3DX to load your textures, the function D3DXCreateTextureFromFile() will do the POT resizing. I don't actually know if this is the problem, but it's worth checking out.

##### Share on other sites
yeah, but there is no option to disable it from there.. (as far as I know). I've been google'ing for a while now and haven't found *anything* on disabling POT resizing..

##### Share on other sites
It's in the DirectX documentation.

Quote:
 D3DXCreateTextureFromFileEx[...]Width [in] Width in pixels. If this value is zero or D3DX_DEFAULT, the dimensions are taken from the file and rounded up to a power of two. If the device supports non-power of 2 textures and D3DX_DEFAULT_NONPOW2 is specified, the size will not be rounded. Height [in] Height, in pixels. If this value is zero or D3DX_DEFAULT, the dimensions are taken from the file and rounded up to a power of two. If the device supports non-power of 2 textures and D3DX_DEFAULT_NONPOW2 is sepcified, the size will not be rounded.