Advertisement Jump to content
Sign in to follow this  

loading a texture from file

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.


Share this post

Link to post
Share on other sites

try this...

Load your texture (tx)....

and get desc surface.

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);


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!