Sign in to follow this  
Odoacer

D3DX Sprites appear bigger

Recommended Posts

Sorry if this question is dumb, but whenever I try to render a sprite using D3DX, it appears bigger onscreen (roughly one and a half the size it should be). Is there a reason for this? Or am I perhaps initializing something incorrectly in my code?
	if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, "images/snakeHeaddown.png", &g_pTexture)))
		return false;
	if(FAILED(D3DXCreateSprite(g_pd3dDevice, &g_pSprite)))
		return false;
/**/
	g_pSprite->Begin(NULL);
	D3DXVECTOR3 foo(16, 16, 1);

	if(D3DXERR_INVALIDDATA == (g_pSprite->Draw(g_pTexture, NULL, NULL, &foo, 0xFFFFFFFF )))
		msgboxnumber(100);

Share this post


Link to post
Share on other sites
you need to disable texture filtering using D3DXCreateTextureFromFileEx

D3DXCreateTextureFromFileEx(device, texture.c_str(), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &m_texture);

D3DX_FILTER_NONE
No scaling or filtering will take place. Pixels outside the bounds of the source image are assumed to be transparent black.

Share this post


Link to post
Share on other sites
Quote:
Original post by eFoDay
you need to disable texture filtering using D3DXCreateTextureFromFileEx

D3DXCreateTextureFromFileEx(device, texture.c_str(), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &m_texture);

D3DX_FILTER_NONE
No scaling or filtering will take place. Pixels outside the bounds of the source image are assumed to be transparent black.


That worked... the image is not being resized, but now there is a black border (transparent, eh?) on the left and bottom sides of the picture, so it is still pretty much the same size as it was before.

I'm one step closer, but not quite there yet. Why is D3D trying to render pixels outside the bounds of the source image? I'm not specifying any sizes.

Quote:
Stupid question, but is it due to the fact you have your application in a lower resolution than your OS?
No, I'm running in windowed mode, not fullscreen.

Share this post


Link to post
Share on other sites
Is your images size not powers of 2? A lot of cards still need textures with sizes being powers of 2.

The D3DXCreateTextureFromFile will acquire a bigger texture and put your image inside with borders.

You can use D3DXCreateTextureFromFileEx and specify D3DX_DEFAULT_NONPOW2 for width and height. This will only work on a card that supports textures of non power of 2 sizes.

Share this post


Link to post
Share on other sites
you dont have to create in non powers of two. You can scale it down to original size:


scale.x = origWidth / width;
scale.y = origHeight / height;

D3DXMatrixTransformation2D (... & scale );

sprite->SetTransform(& matrix);
sprite->Draw(..)


you can calculate width by getting a surface description from the texture's highest miplevel, or you can just use the rule of thumb: round up to power of two. Eg:

124 -> 128
343 -> 512
10 -> 16

etc..

Share this post


Link to post
Share on other sites
Quote:
Original post by dhanji
you dont have to create in non powers of two. You can scale it down to original size:

If you do this your texture gets filtered multiple times (once on load and once on draw). Depending on your art this can cause nasty artifacts.

Share this post


Link to post
Share on other sites
By the way, if you should need to use sprites whose dimensions aren't powers of two, you can still render them without the border by explicitly stating the coordinates in the source-rect.
Since textures can only be stored with these dimension limits, passing an empty rect (or equivalent) will cause the whole texture to be rendered, including the black border.

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