🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

GIF Textures (ADD: And others)

Started by
10 comments, last by Nathan Baum 16 years, 11 months ago
First off, just like any new member, I have to say that NeHe is a great man... He's helped me with GL programming, but anywho... I've skimmed around his lessons and I haven't found much on this at all... atleast from what I have looked at... And I've also 'tried' to search these forums but failed... But I have two questions... first off on his Lesson 6, I'm using his BMP loader and it works fine for simple textures that I'm using just to tell things apart from... However... This program is going to be big... and I want to use GIF's... also, not just ^2's... For the instance of I'm going to be using 100x96 images or what not... I'm Alright with C++ and I know what I'm doing if I have a guide that I can make sure I'm using the right commands and whatnot... and I know a few other Web / Programming languages so a hardcore breakdown isn't needed... I'm basically wondering if I could modify his Lesson 6 code a little and get what I'm after, or if I have to write a whole new thing... Either way, I have no idea what I'm doing when it comes to loading and rendering Textures in OpenGL / Glut... Which that reminded myself of some important information.. .I Am using GLUT aswell... Anyway, I'll stop typin now... Thanks for any help in advance! [Edited by - PorkChop Conrad on July 19, 2007 10:10:48 PM]
Advertisement
1) Loading GIF files isn't trivial, you need some library like giflib, CImg or many others.

2) There are OpenGL extensions for funny-size textures, but they have are various complications and limitations: probably you want to use regular textures anyway.
You can simply pad individual images to the next power of 2 (100x96 -> 128x128); since it's a GIF file, adding runs of background-coloured pixels doesn't increase file size significantly.
If the number of textures and corresponding files or the wasted texture memory are excessive you can also consolidate some of your images into texture atlases.

Omae Wa Mou Shindeiru

I've tried CImg... and I tried to use it to render, however, it seems like it's probably more used for image software... I just opens a whole new window with the image, and I searched around and found that if you want it to work with OpenGL that you have to write you own function...

I can see using it just for loading... but wouldn't that be pointless to imclude something like CImg and just use 5% of it?

Guess I just like to not waste or somethin... I'll try it out and see if I can get it this time with that, Thanks for the help.

any other suggestions?
You can use glu to scale the image data before creating a texture from it. I believe it is gluScaleImage() or something to that affect. You specify the image data, original dimensions, new dimensions (which must be a power of 2), and a buffer for the scaled data. Then you can use this new data to create your texture.

As far as loading GIFs, you might try creating your own GIF loader if you don't need the other 95% of an image library. I am pretty sure that this is a complicated image format though (I've never actually looked into it, but I've heard it is difficult).

If you are just looking for an alpha channel, you might try using TGAs or PNGs, both of which have ample documentation and source code available. Plus they have the added benefit of being lossless formats.
I've tried TGA, and I was accually going to use those originally... I'm accually really comfortable with TGA's sense I used to mod just about every Medal of Honor game... in which they primaraly used TGA's...

I just haven't found a way that can load a compressed TGA... Which other then than Alpha Channel, an uncompressed TGA is just the same as a BMP.

I've just google'd myself out on it, and I don't have any books on OpenGL, GLUT, or even DirectX other then this book that's about 600 pages long and just says "Microsoft DirectX is the Best!" and in the back of the book it has the different DirectX books from "Getting DirectX to trim your toe nails!" to "How to make DirectX pop-up on your screen randomly to tell you 'you are pretty!'"
Quote: Original post by PorkChop Conrad
I've tried CImg... and I tried to use it to render, however, it seems like it's probably more used for image software... I just opens a whole new window with the image, and I searched around and found that if you want it to work with OpenGL that you have to write you own function...

You need to convert from palette to RGB or RGBA, possibly convert to another color depth, and iterate through pixels to copy them to a correctly ordered and aligned buffer; CImg has obvious functions for each of the three steps.
Even if a library offered a complicated general function to create an OpenGL texture from a file, figuring out the appropriate parameters would be as difficult as assembling smaller pieces.
Quote:
I can see using it just for loading... but wouldn't that be pointless to imclude something like CImg and just use 5% of it?
Guess I just like to not waste or somethin...

It's the normal way to use libraries: the library is general, the user's needs are narrow.
You aren't "wasting" anything; features you don't use right now are patiently waiting to help you if the need arises, they are indirectly useful because they make your project more flexible.
For example, what if you decide to use some JPEG image? Would you write another loader?

And writing a GIF loader or any other commodity component you want to use, if you already have a good enough one, is an horrible waste of your time and energies.






Omae Wa Mou Shindeiru

I just want to through in that GIFs really aren't great for games. It's a highly lossy format. It's rather awkward to see GIF compression in games. I, personally, like PNGs. It's lossless and has all the features you'd need.

I haven't used it, but DevIL is a popular image loading library. I use SDL_image, since it's very compatible with the SDL library that I use. I agree with LorenzoGatti that it's still a justified use of the library even if you only use a small portion of it. Thanks to SDL_image my game can load JPEGs, BMPs, TGAs, and more, even though all I use it for are PNGs. It's nice.
Gif's are my last effort... I've been messing around and stuff, and I got a compressed TGA to work... But I'm not happy with the fact that it has to be like 128x128, 256x256, 512x512, and so on... It kinda distorts the image unless you want to put a say 300x426 image in a 512x512 with a mask on the overlay and position it well... Which I don't want to do that for the fact that I plan to have movable menu type things... And I'd just have to write another function to see if there is any color where the mouse clicked on the image otherwise it would catch on a masked section... Which all and all I think would cause unnecessary lag in the program, even if a little.

In my mad search, I've tried CImg, ImageMagic or something(I think that one went with CImg...), and DivIL, right off the top of my head.... Along with a few others...
You don't need a mask to cut out a rectangular piece in a texture, and you don't need to distort and enlarge images to make them square; you can simply take advantage of texture coordinates to draw only part of a texture.

For example, suppose you have a 100 by 96 region of useful texels in the appropriate corner of a 128 by 128 texture; then you can draw a rectangle of 100 by 96 pixels with texture coordinates from 0 to 0.75 (i.e. 96/128) in one direction and 0 to 100/128 in the other direction. The portion of the texture corresponding to larger values of u and/or v is never used.

Omae Wa Mou Shindeiru

WEll, I solved a problem the way I could...

I have a good Compressed TGA loader thanks to NeHe's lessons and a few other resources I pieced together and made it work with my layout.

I just made a function where I can load where to start and how long and wide it is.

Also, I've now wrote my own function translating Pixels to the Percents that GL uses.

This is the Main part of the code that does all the work.

Quote:
// Window Size
// For determining the Pixel Percent
float win_height (600);
float win_width (800);

// Pixel to Percent Holders.
float x_spos; // "Start Pos"
float y_spos;
float x_imgw; // Image h*w
float y_imgh;
float x_epos; // "End Pos"
float y_epos;

void Pixelize(int x_spix, int y_spix, int x_ipix, int y_ipix) {
// First two x and y "start" Pixels
// Second two x and y "image" length

// Pixel for 200% (100% to -100%) -- 0% is GL Center
x_spos = 2 / win_width; // 1 Pixel = 0.0025% (800)
x_imgw = 2 / win_width;
y_spos = 2 / win_height; // 1 Pixel = 0.0033% (600)
y_imgh = 2 / win_height;

// Mults the Pixels by Percent
x_spos *= x_spix; // eg: 20 Pixels at .0025% each = .075%
x_imgw *= x_ipix;
y_spos *= y_spix; // eg: 20 Pixels at .0033% each = .066%
y_imgh *= y_ipix;

// Subtracts 1 (or 100%) so it is correct in GL
x_spos -= 1.0;
y_spos -= 1.0;
y_spos *= -1; // Y is backwards, so flip it
// so it can go from the Top-Left Corner
// for drawing the "End Pos"
x_epos = x_spos + x_imgw;
y_imgh *= -1; // Y is backwards again, so Flip
y_epos = y_spos + y_imgh;
y_imgh *= -1; // and again backwards so the End Pos reads right

// Add the Start Pos and Dimensions so image is square
x_imgw += x_spos;
y_imgh -= y_spos;
y_imgh *= -1; // and again to make it turn out right
}


and here is the object I call it to, it's using my third texture

Quote:
glPushMatrix();

glColor4f(1.0, 1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, ga_gametexture[3].texID);

// Call the Pixelize(Start on X, Start on Y, How Long X, How Long Y)
Pixelize(200, 300, 200, 50);

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(x_spos, y_imgh, 1.0);
glTexCoord2f(1.0, 0.0); glVertex3f(x_epos, y_epos, 1.0);
glTexCoord2f(1.0, 1.0); glVertex3f(x_imgw, y_spos, 1.0);
glTexCoord2f(0.0, 1.0); glVertex3f(x_spos, y_spos, 1.0);
glEnd();

glPopMatrix();


Comments are in the help break it down... This is the throwned together version I just did, so it may have some useless parts, or parts not needed... But hey, it works.

Posting just if anyone else needs it...


ALSO: They way the images are is that that are made EG. in 200x50 then I extend it (making it smaller causes a blured image) the a higher Power of 2. So in this case I made it 256x256 and Masked the rest of the image.

[Edited by - PorkChop Conrad on July 23, 2007 1:14:16 AM]

This topic is closed to new replies.

Advertisement