Sign in to follow this  
aarddave74

loading a texture from file

Recommended Posts

i've now sorted out my problem regarding getting textures to display, now i have a whole other and hugely strange problem. i have an example image which came on the cd from the book i'm working from, that works fine. i try to display an image that i create, and it gets stretched. using screengrabs, the tutorial image is 512 pixels wide, i load it into my app, both windowed and fullscreen mode.. it is 512 pixels wide the image i create is 200 pixels wide. i load it into my app, both windowed and fullscreen mode, and it becomes 256 pixels wide. i have tried numerous graphics formats (jpg/bmp/dds), using both paintshop pro and the directx texture tool and all images i create are being stretched by the same amount each time. am i missing something totally obvious? do texture dimensions need to be multiples of 16/32 etc?

Share this post


Link to post
Share on other sites
Read this, particularly where width and height are concerned.

Basically, yes, DX will stretch your image to a power of 2 unless you tell it otherwise.

Hope that helps.

[Edit]: Typo.

Share this post


Link to post
Share on other sites
D3DX will pad a texture so that it's a power of 2 width and height. You can use GetLevelDesc() on the first level of the texture to find out what size D3DX created the texture as, and if you use the Ex versions of D3DXCreateTextureFrom[...]() you can get it to tell you the size of the image in the file. Using that information, you can set your texture coordinates up so that the image isn't stretched.

I'd recommend using power of two image dimensions unless you have a good reason, since it's sometimes more efficient, and will allow you to target lower end hardware (Older cards can't do non-power-of-two textures).
If you really need non-power-of-2 textures, then make sure you check your caps to check the card can support them (Or gracefully handle errors fom creating textures at least), then you can create the texture manually with IDirect3DDevice9::CreateTexture(), then D3DXLoadSurfaceFrom[..][Ex] to load the file into each mip level of the texture. specify D3DX_DEFAULT_NONPOW2 for the width and height.

This is all assuming C++, unmanaged DX, but it should be similar for other languages.

EDIT: FOrgot about D3DX_DEFAULT_NONPOW2...

Share this post


Link to post
Share on other sites
Hi,

try this...

Load your texture (tx)....

and get desc surface.

D3DSURFACE_DESC surfDesc;
tx->GetTextureDev()->GetLevelDesc(0, &surfDesc);

float left = xpos;
float top = ypos;
float right = left + surfDesc.Width; // - 1;
float bottom = top + surfDesc.Height; // - 1;

and.... set vertex positions...

Verts[0].posiz = D3DXVECTOR2(left,top);
Verts[1].posiz = D3DXVECTOR2(right,top);
Verts[2].posiz = D3DXVECTOR2(right,bottom);
Verts[3].posiz = D3DXVECTOR2(left,bottom);

bye.
Luca

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