Archived

This topic is now archived and is closed to further replies.

D3DX 8 - Loading part of a file as a texture

This topic is 6116 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''m playing around with doing some 2D sprites using textured quads in D3D8, and I need a way to load individual cells of animation from a file on disk into seperate IDirect3DTexture8 interfaces. D3DX has some marvelous loading functions for creating textures from files, just as long as you want the WHOLE file as your texture, not a specific chunk. There''s no file-to-texture loading function that allows you to specify a rect inside the file to load, instead of the whole file. There is such an argument in the D3DXLoadSurfaceFromFile series of functions, but that leaves you with an IDirect3DSurface8 interface, not an IDirect3DTexture8. So my question(finally) is, how do I load a square/rectangular piece of a file on disk into an IDirect3DTexture8 interface? Or, failing that, how do I convert an IDirect3DSurface8 interface into a renderable IDirect3DTexture8?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I believe the answer is to use GetLevel() to get a Surface
from a "level" in a texture. Then you can load into the surface,
and it will go to the corrisponding level in the texture. Basically a texture is a series of surfaces, each called a level. Typically the levels are used for mip-maps (so each one is half the dimensions of the last). When you create the texture you tell it the number of levels you want.

Another way, is to load into a temp texture, then copy rects into the real combined texture.

Make sense?

Share this post


Link to post
Share on other sites
Ah!, yeah that''s a good idea, since D3DXLoadSurfaceFromFile takes an already existing surface instead of returning a new created one. Dang, I should have thought of that! :-)

thanks.

Share this post


Link to post
Share on other sites
Hey PyroBoy,

I'm doing something very similar to what you're doing. I have 'pages' of textures (256x256) that I load as D3DTexture8 objects. I then assign a page to my sprite and use texture coordinates to get the cell for that sprite from the texture. I set my texture coordinates like this:

  // Calculate the fraction of the texture used for a cell
Celltu = 1.0 / (TextureWidth / CellWidth)
Celltv = 1.0 / (TextureHeight / CellHeight)

// Texture coords for each vertex
TopLeft.tu = CellCol * Celltu
TopLeft.tv = CellRow * Celltv
TopRight.tu = TopLeft.tu + Celltu
TopRight.tv = TopLeft.tv
BotLeft.tu = TopLeft.tu
BotLeft.tv = Topleft.tv + Celltv
BotRight.tu = TopRight.tu
BotRight.tv = BotLeft.tv

For example, assuming sprites are 32x32 and my sprite's cell was 3 cells across (CellCol = 2) and 2 cells down (CellRow = 1), the texture coordinates would be as follows:

TopLeft = (0.250,0.125)
TopRight = (0.375,0.125)
BotLeft = (0.250,0.250)
BotRight = (0.375,0.250)

Hope that helps,

Luhar

Edited by - Luhar on March 21, 2001 2:55:28 PM

Share this post


Link to post
Share on other sites
HEHE
i think the best way to do that would be to load the entire thing onto one texture Recores and change the texture Coords to meat the Frame



-VBLimits
Sorry about the Spelling..

Share this post


Link to post
Share on other sites
Re: Texture coords...

Yeah I see that suggested a lot when dealing with questions of this nature. That''s certainly a great idea with small images (like 16x16 or 32x32) and when I finally get around to writing a 2D tile engine you better believe I''m gonna be caching multiple tiles in one(or maybe several - haven''t decided yet) 256x256 texture(s)! :-)
But the particular application in this case is a regular ol'' sprite. If I need a 256x256 animated sprite I''m gonna have to create one hella big texture, concequently ditching a lot of hardware support in the process. (Damn you 3dfx! You and your tiny little textures!)
Ah well. I''ll check out that GetLevel() loading solution. That seems like a winner to me...

Share this post


Link to post
Share on other sites