Sign in to follow this  
Trillian

Backwards-stored texture formats.

Recommended Posts

Trillian    410
I have been using D3DXCreateTextureFromFile for a while but I always get the texture to be loaded backwards. I heard that the BMP file format was stored backwards so the problem would be there, not in the D3DXCreateTextureFromFile function. First: Could you explain me how I cant access the texture data of a IDirect3dTextre9 interface and switch it (by using a temp buffer I guess)? Second:I plan to load the following formats: BMP,JPG,PNG,RAW,DDS which of them are stored backwards? Can I assume I can always reverse the texture after having it loaded?

Share this post


Link to post
Share on other sites
While some formats are stored inverted, with the bottom stored first, and the top stored last, that's just how it is on disk. When D3DX loads a texture, it puts the top on top, no matter what the format is. If you texture appears backwards, upside down, or both, it's more likely that your geometry is flipped. Drawing a quad with the following UV layout will work, no matter what format the texture file was.
(0,0)-(1,0)
| /--|
| /-- |
|/-- |
(0,1)-(1,1)


Not that you need, or should consider, swapping the pixels manually, I'd explain a bit about it as it's useful to know how to access texture data.

If you want to manipulate the texture data, call pTexture->LockRect(0, &lr, 0, 0);
The lr structure will be filled with a pointer to the data, and the pitch. Pitch is the number of bytes from the start of one line, to the start of the next. This will be the top mip-level only. You can ask D3D to regenerate miplevels after modifying the data. Remember to unlock after you finish reading or writing the texture data.

You may also want to call GetSurfaceDesc() to find the pixel format, as copying an 8 bit, 16 bit, 24 bit, or 32 bit texture will have some impact on how you access the pixels. DXT compressed textures will require a bit more work to swap around.

Share this post


Link to post
Share on other sites
Trillian    410
Thanks for your help!

I'm still gonna tell more about my problem:
I am loading a .3ds file (sample from spacesimulator.net)
So the texture coordinates should be alright I guess (In a previous openGL app it worked alright)
But then in D3D I get to load all the vertices/indices/UV all right but ten when applying the texture it doesn't work as it's supposed, badly mapped.
My supposition was that the file was loaded in memory upside-down so the UV would not represent the right texture triangles but that's a guess.

Still, I keep getting that texture badly loaded/mapped.

Share this post


Link to post
Share on other sites
There is difference in how OpenGL and D3D map UVs. OpenGL's quad would look like this.

(0,1)-(1,1)
| --/|
| --/ |
|--/ |
(0,0)-(1,0)


You *could* flip the texture, but then any geometry made for D3D sharing the texture would have a flipped texture. On load, or during preprocessing, I'd flip the V coordinates of any mesh that need them flipped. You could use a texture coordinate transform, but that will add overhead to each draw call, instead of a small bit of work when loading.

edit: Open GL textured quad info

Share this post


Link to post
Share on other sites
Trillian    410
Ah!

Thanks now I understand, D3D has a different UV thing than openGL and since that exemple was optimized for openGL, the UV are upside down.

Ok cool.

Just one thing : Does the maya/softimage/3dsmax and all these save textures UV in openGL-style or in D3D style? Or can we change it?

Share this post


Link to post
Share on other sites
I'm not sure which way the UVs work in Max, Maya, SoftImage, Milkshape or anything. The data I use to render has always come from a custom exporter that another developer at our studio wrote. I know nothing about actually getting the data out of any modelling packages, so I can't say which way any of them work.

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