Images from one file

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

Recommended Posts

I tried the gamedev search but it wasnt working? - Anyway i (think) I Know how this is done, but what i want to know is how is it managed? If there are 4 pics inside my image, and i would have to write
glTexCoord2f(0.00, 0.00);
glTexCoord2f(0.25, 0.00);
glTexCoord2f(0.25, 0.25);
glTexCoord2f(0.00, 0.25);


To pick out one pic, But what i want to know is there an easier way of doing this without having to keep track of all the points. Because the if its that way i'd have to write out a differnt function for each quad i draw?
unsigned int QuadDL;

{

glTexCoord2f( 0.0f, 0.0f ); glVertex2i( 0  ,0  );
glTexCoord2f( 1.0f, 0.0f ); glVertex2i( 32 ,0  );
glTexCoord2f( 1.0f, 1.0f ); glVertex2i( 32 ,32 );
glTexCoord2f( 0.0f, 1.0f ); glVertex2i( 0  ,32 );
glEnd();
glEndList();

}


Thats my Display List for my quad. Any help/links/tips will be appreciated ;)

Share on other sites
Well, what exactly are you doing? Making a tile engine? If it is something like that, and you have multiple images in the same file that have the same resolution, you can assign a number to each tile so that you can use it to figure out the location of each image in the texture. It is hard to explain, so lemme see if I can dig up some old code.

EDIT: Ok, forget the source, it is too messy to deal with. Instead, look at this:

Pretend we have a texture that hold 4 * 4 images. We know that there are 16 images. If we name the images 0,1,2,3...starting from the top left corner and moving right until we reach the end, then move to the next line, we would have something like this:

0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

Lets divide it up some more.
---------------- 0%
|0 | 1 | 2 | 3 |
---------------- 25%
|4 | 5 | 6 | 7 |
---------------- 50%
|8 | 9 | 10| 11|
---------------- 75%
|12| 13| 14| 15|
---------------- 100%

What I have done is divided our texture up with those horizontal and vertical lines and labeled each horizontal line with the percent of the texture that it starts at. Now, take a look at our number, 5. You can see that the top y value for it lies on the 25% line. This means that it starts at 25% of our texture. Since texture coords are all on a 1*1 scale, we know that our image starts at 25%, or, .25. This is the top y value. To get the lower one, just look at the lines...It would be at 50%, or .5
The same thing is done to get the x values. Now, all you need to do is to come up with a simple algorithm to tell which row or column your image is on based on the number.

This is how I have done it in the past...It is really simple once you get a clear picture of what is going on...My grade A ascii art should help out. If you really want to destroy your mind with my uncommented code, simply tell me. [smile]

[Edited by - tHiSiSbOb on December 5, 2004 5:41:59 PM]

Share on other sites
that is pretty much how you gonna have to do it
or when you are loading the one image file, break it apart at that point into its 4 separate pics

Share on other sites
Well i want to know how because it would be better than the way im doing it know - and when i do im going to start a new project to try it out. That code would be great, Thanks :D