Loading TGA files to openGL Textures

Nibbie
Posts: 1
Joined: 2010.11
Post: #1
Does anyone have a simple bit of code lying around that loads a TGA image into a openGL texture. I have heard it is simple, and relitively painless, but I am new to C, let alone OGL and therefore clueless on how to make one myself. Can anyone post a quick function and some basic instructions on how to use it?

Thanks!

~Joseph
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #2
NeHe has a TGA loader in his texture tutorial.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #3
You already have on if you have the Dev tools installed. Go to the GLUT examples, and grab textures.c and textures.h from the NewWave demo. That's what I did first. However, If you are going to go with C grab a copy of SDL and SDL_Image. You won't regret it.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #4
I would recommend you use JPG or PNG instead of TGA these days. The difference in file size is considerably significant. I have some source code that I can upload if you are interested. It's a bit involved though, because you need to create a Quartz context, then "print" the images on the context, and then create the textures. The upside is that my code does it all for you, including auto-selecting between TGA, JPG, and PNG.

Also, other developers have been reporting problems with GLUT in OS 10.3 and later. It's best to avoid using GLUT.
Quote this message in a reply
kberg
Unregistered
 
Post: #5
Yup, certainly do...

tga.cc

Usage instructions:

Code:
TextureImage myTexture;

LoadTGA(myTexture, "My_Texture_Filename.tga");

CreateTexture(myTexture, 0);
//For the second parameter to CreateTexture, pass a 1 if you are going to be scaling the texture (ie: mapped to an object far off in the distance).

glBindTexture(GL_TEXTURE_2D, myTexture.texID);
//Draw stuff using this texture

//To delete the texture and free up all the memory:
DeleteTexture(myTexture);
DeleteBuffer(myTexture);

Now this code is only for rectangle textures (textures of size power of two). If you pass a texture that has a width or height not of a power of two (2, 4, 8, 16, 32, etc..) then the texture will show up as completely white in OpenGL if you try and use it as a texture. There is code there to read regular and RLE compressed targa files at 24 and 32 bits per pixel, as well as write uncompressed targa files to disk.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #6
"rectangle texture" means non-power-of-two sizes are allowed.
Quote this message in a reply
kberg
Unregistered
 
Post: #7
Heh, of course; sorry. I meant non-rectangular textures...
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #8
FCCovett Wrote:I would recommend you use JPG or PNG instead of TGA these days. The difference in file size is considerably significant.
actually a .sit with a TGA is smaller than a .sit with an equal PNG.
Quote:Also, other developers have been reporting problems with GLUT in OS 10.3 and later. It's best to avoid using GLUT.
Well, we'll see. I am not really using anything GLUT specific anyway, but I don't want to change if I don't have to.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #9
Um, I have a few problems kberg's is in C++, I need c. Skorche's suggestion won't work because that is a RGB loader not TGA. I'll look at nehe's tutorial, but if anyone has any more TGA loaders in C I would be gratefull. (thanks all the same kberg)
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #10
There was another thread in this forum that discussed the pros and cons of using PNG instead of TGA. The creator of the thread succeeded in demonstrating that PNG are indeed better than TGA through a number of tables that compared the final file sizes, non-compressed and compressed in several ways.

Anyway, the loader from the NeHe tutorial works just fine in C. You may need to adjust the texture struct as needed, but it works.

There's a bare-bones version of my engine on http://webpages.charter.net/utopiaplanet...Struct.dmg

You can find all the mentioned texture loaders there.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #11
Whoops, sorry. I thought it was a TGA loader. Anyway, if you are going to stick with C for a while I'd still really recommend using SDL and SDL_image. SDL_image will load several different file types with a single function call, (convenient) and SDL will allow you to easily change the pixel format to be the same as the texture format (flexible). Unless you want to fiddle with Quicktime/Quartz or Cocoa, I don't think that there is a simpler way. Not to mention the fact that SDL does pretty much everything that GLUT does, and a lot more.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #12
If you ever want to move passed C for a Mac specific implementation, I'd recommend taking advantage of CoreGraphics and Cocoa to do the image handling. Especially with Cocoa, since all you have to do is ask for a Tiff representation. My cocoa loading code is only about 40 lines, but can load any image that Cocoa can read; including handling of varying spp, bps, alpha, etc.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
kberg
Unregistered
 
Post: #13
Joseph Duchesne Wrote:Um, I have a few problems kberg's is in C++, I need c. Skorche's suggestion won't work because that is a RGB loader not TGA. I'll look at nehe's tutorial, but if anyone has any more TGA loaders in C I would be gratefull. (thanks all the same kberg)

I must admit I haven't touched pure c in years, but this should work I hope... Rasp
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #14
Your code looks ok, but it keeps giving me errors when I try to implement it
Using your code (although I changed the filename in my code)
LoadTGA(myTexture, "My_Texture_Filename.tga");
CreateTexture(myTexture, 0);

Both the above lines give me "error: incompatible type for argument 1 of 'CreateTexture'" and 'LoadTGA'

Any ideas on why?
Quote this message in a reply
kberg
Unregistered
 
Post: #15
IIRC, pass by reference works a little different in c...

try:
LoadTGA(&myTexture, "My_Texture_Filename.tga");
CreateTexture(&myTexture, 0);

instead.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenAL - Ogg files vs Caf files Fred9000 8 14,212 Aug 23, 2011 08:01 PM
Last Post: ipeku
  [SOLVED]OpenGL edges of textures mk12 2 4,436 Sep 2, 2010 08:07 PM
Last Post: mk12
  OpenGL ES Loading images techy 4 3,889 Dec 24, 2009 02:01 PM
Last Post: techy
  OpenGL Image Textures mikey 52 24,785 Jun 30, 2009 10:42 AM
Last Post: AnotherJake
  Dealing with inverted textures in OpenGL johncmurphy 7 7,069 Jun 15, 2009 08:11 AM
Last Post: Skorche