Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Loading texture images/animation

This topic is 4811 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

Advertisement
Quote:
Original post by The C modest god
Does direct3D support loading a jpeg image into a texture?

Yes.

Quote:
Does it support loading an animation file into several textures?
Yes.

Quote:
How is it done?

A single JPEG can be loaded using D3DXCreateTextureFromFile.

Loading an animation file depends entirely on the animation file format you're looking at.

Also, *punt*->DirectX...

Share this post


Link to post
Share on other sites
How would I load an animation file into textures in directx?
By animation I mean sprites, or a set of 2D images.
I want to load images into textures, and loading a jpeg file for each frame would seem to be slow and cumbersome.
How do I load an animation file into textures? such as mpeg?
It would also be good if I could save several jpeg images into one file and load all the images from that file into a set of textures.

Share this post


Link to post
Share on other sites
an mpeg is not an animation format, it is a motion video format.

You can also look at ID3DXSprite.

Share this post


Link to post
Share on other sites
Quote:
How would I load an animation file into textures in directx?
By animation I mean sprites, or a set of 2D images.
I want to load images into textures, and loading a jpeg file for each frame would seem to be slow and cumbersome.


Sure, you can just use the ID3DXSprite interface. This is a handy wrapper that handle everything you would ever need to do with a sprite and not to mention it has been optimized like crazy over the past few iterations of d3d.

To use a sprite sheet:
- Like superpig mentioned, use D3DXCreateTextureFromFileEx first to load in your sprite sheet into the IDirect3DTexture9* interface.
- Call ID3DXSprite::Begin when you're ready to to render
- Call ID3DXSprite::Draw specifying the sprite sheet and the rectangle of the sprite you want to be drawn.
- Call ID3DXSprite::End

*Note: Make sure your sprite sheets arn't too big, you can check how big your graphics card can support using the D3D tool called caps viewer that comes with the sdk. Check the maximum size your texture can be, in my case its 2048x2048 but a safe size would be 1024x1024


Quote:
How do I load an animation file into textures? such as mpeg?

MPEG support isn't really suited for d3d, look into other api's such as DirectShow

Share this post


Link to post
Share on other sites
Thank for the replies.
I have looked into the D3DXCreateTextureFromFileEx function, but I havnt seen how do I load a set of textures(a sprite) and not a single texture?
"This function supports the following file formats: .bmp, .dds, .dib, .jpg, .png, and .tga"
If I want a sprite of 100 images, do I need to load now a 100 jpeg files?
Also, I dont see how do I load the images into a ID3DXSprite interface?

Share this post


Link to post
Share on other sites
Hey,

What you're doing is loading in a sprite sheet, that is you need to in an artist program of your choice(such as adobe photoshop) put all your sprites together into one jpg file. Say if you have 100 sprites well, then make a sprite sheet that has a 10 rows by 10 columns containing all the sprites. Then load the file into memory.

As for your other question, when you call ID3DXSprite::Draw( ) the first parameter takes a texture.

Share this post


Link to post
Share on other sites
Edit: Better yet. I'll show you a simple app, using the D3DXSprite interface:

IDirect3D9* g_pDirect3D = 0;
IDirect3DDevice9* g_pDevice = 0;
ID3DXSprite* g_pSprite = 0;
IDirect3DTexture9* g_pTexture = 0;

UINT g_nCurrentFrame = 0;
UINT ANIM_FRAMEWIDTH = 64;
UINT ANIM_FRAMEHEIGHT = 64;
UINT ANIM_FRAMECOUNT = 3; // 4 animations, zero-based (0, 1, 2, 3)

enum LOOP_STYLE
{
REPEAT, //123 123 123 123
REVERSE, // 123 21 23 21 23
NONE, // 123
};

// Setup Windows
// Setup Direct3D
D3DXCreateSprite(g_pDevice, &g_pSprite);
// (Device, FileName, Width, Height, MipMaps, Usage, Format, Pool, Filter, MipFilter, ColorKey, ImageInfo, Palette, Texture)
D3DXCreateTextureFromFileEx(g_pDevice, "Anim.jpeg", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, 0xffff00ff, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, 0, &g_pTexture);

g_pDevice->BeginScene();
g_pSprite->Begin(D3DXSPRITE_ALPHABLEND);

RECT SrcRect;
SrcRect.left = g_nCurrentFrame * ANIM_FRAMEWIDTH;
SrcRect.top = 0;
SrcRect.right = SrcRect.left + ANIM_FRAMEWIDTH;
SrcRect.bottom = SrcRect.top + ANIM_FRAMEHEIGHT;

g_pSprite->Draw(g_pTexture, &SrcRect, 0, 0, 0xffffffff);

g_pSprite->End();
g_pDevice->EndScene();

// To udate the animation
switch(ANIM_LOOPSTYLE)
{
case REPEAT:
{
++g_nCurrentFrame;
if(g_nCurrentFrame > ANIM_FRAMECOUNT)
g_nFrameCount = 0;
break;
}

case REVERSE:
{
static UINT nDir = 1;
g_nCurrentFrame += nDir;
if(g_nCurrentFrame > ANIM_FRAMECOUNT)
nDir = -1;
else if(g_nCurrentFrame = 0)
nDir = 1;
break;
}

case NONE:
{
static bool bActive = true;

if(bActive)
{
++g_nCurrentFrame;
if(g_nCurrentFrame > ANIM_FRAMECOUNT)
{
g_nCurrentFrame = 0;
bActive = false;
}
}
break;
}
}



You'll notice that this setup only allows for 1 row of frames (as such for a side scroller or cursor), but its easy to adapt for multiple rows (for say an RPG.)

If you have any questions about the above, just ask.

Share this post


Link to post
Share on other sites
Programmer16, I used the top half of your code in a current project I'm working on just to try to load an image onto the screen. I've been having absolutely no luck in trying to display a simple image onto the screen. I'm using the DirectX9 April SDK C++ and am using the CDXUT helper classes to make a game and currently have a fully functional interface with lots of CDXUTDialogs, buttons, text boxes, etc...

I used the code you posted:

D3DXCreateSprite(g_pDevice, &g_pSprite);

// (Device, FileName, Width, Height, MipMaps, Usage, Format, Pool, Filter, MipFilter, ColorKey, ImageInfo, Palette, Texture)
D3DXCreateTextureFromFileEx(pd3dDevice, L"frigate_picture.jpg", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, 0xffff00ff, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, 0, &g_pTexture);

g_pSprite->Begin(D3DXSPRITE_ALPHABLEND);

RECT SrcRect;
SrcRect.left = 0;
SrcRect.top = 0;
SrcRect.right = 64;
SrcRect.bottom = 64;

g_pSprite->Draw(g_pTexture, &SrcRect, 0, 0, 0xffffffff);

g_pSprite->End();



I changed it just a bit, using my own d3ddevice (pd3dDevice). This call is in the middle of my OnFrameRender call, which I know is ugly programming, but I'm just trying to get it to work.


This wont draw for some reason, and is constantly printing out:

D3DX: Invalid image filter
D3DX: pTexture pointer is invalid


Do you know why this isn't working; or, what is a better, easier way to draw a simple image onto the screen?

Thanks ahead of time!
[/source]

Share this post


Link to post
Share on other sites
I don't know why that isn't working, but as for a more simple application you can shorten the call to D3DXCreateTextureFromFileEx() to:
D3DXCreateTextureFromFile(g_pDevice, "filename", &g_pTexture);
I've never worked with JPEG's before, so maybe some of the flags I'm using are incorrect. From the errors it seems that you can't use D3DX_FILTER_NONE for the first filter (try changing it to D3DX_DEFAULT), which is causing the texture to not be created. I'll work on this and get back to you in the morning.

It should work fine (I pulled it out of a working app, other than it wasn't using jpegs.)

Oh, and C modest god, if you want an image split up into several textures, you have to do this yourself. Thats why its easier to just use one texture and copy from that (as in my program.)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!