Archived

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

Zoney

multiple textures from one file

Recommended Posts

Zoney    122
Im just a newbie to d3d and I have a question: can you load multiple textures from one texture file? For example if you want to create a 2d tile-based game with d3d you need many different texture and it would be practical to store them in on file (like maybe in a grid or something, those of you who have read "game programming for dummies" probably gets the idea)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Yes, it is possible. I''m assuming you''re familiar with the basics of d3d (setting up a quad, etc) Just use a different set of texture coordinates for each vertex in a quad with a different tile. For instance, if you have 4 textures in a single bmp-file, and have placed them in the bitmap like this:

12
34

(where 1 means the 1st texture, 2 the 2nd, etc) you can use the 2nd texture by setting the texture coordinates like this:

(0.5f, 0.0f)-------(1.0f, 0.0f)
| a quad |
(0.5f, 0.5f)-------(1.0f, 0.5f)


The texture can be of any size.

Hope i explained this clearly enough for you.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Not really. You just need to divide 1.0 by 12 and then you can make up the coordinates easily.

However I suggest that you use such a number of textures that you get even result from the division. If you have 12 textures, you get 1/12=0.833333..., and this will lead to trouble when you''re setting the texture coordinates. You may get the textures that are right next to the desired texture partially showing up, etc, and that''s really something you don''t want to happen.

Use 2x2, 4x4, 8x8, 16x16, etc as the number of textures / bitmap. If you need more, use more bitmaps. Not only do you get more precise texture coordinates, you can also use more detailed textures.

You can extend this method to your 3D meshes, and by moving the vertex coordinates get animated textures. For instance if you''re using billboards to represent the characters, monsters, in your tile-based game, you can use a texture to hold their animations. It''s faster than regular blitting and you get a variety of effects that you can use with them.


Share this post


Link to post
Share on other sites
Zoney    122
Well, what can I say... thanks a lot!
I''ve just downloaded directx 8 sdk (44 mb geez) so I''ll start coding right away.
(this messageboard really works

Share this post


Link to post
Share on other sites
Jonus    170
What that Anonymous poster has writen is not totally right.

      
typedef struct TEXTURETILES
{
FLOAT x1,x2,y1,y2;
} t_TexCor;


t_TexCor tex_tile[30];

/*
You will see artifacts if you do like that ano said.
You must point the coordinates to the centers of the
texels, not their borders. I will suppose your texture
has a resolution of 512x512. For lower resolutions
you must change this.
*/


// Function to create texture coordinates in tex_tile

#define TEXTURE_RESOLUTION 512.0f

void createtiles(int x_anz, int y_anz, t_TexCor * destination)
{
int counttiles=0;
float texeladjust = 1.0f / TEXTURE_RESOLUTION;

for(int i=0;i<y_anz;i++){
for(int j=0;j<x_anz;j++){
destination[counttiles].y1=(float)i*1/y_anz + texeladjust;
destination[counttiles].x1=(float)j*1/x_anz + texeladjust;
destination[counttiles].y2=(float)(1+i)*1/y_anz - texeladjust;
destination[counttiles].x2=(float)(1+j)*1/x_anz - texeladjust;
counttiles++;
}
}
}


Edited by - Jonus on January 28, 2001 5:21:40 PM

Share this post


Link to post
Share on other sites