iDevGames Forums
Simple textures with GLUT? - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Simple textures with GLUT? (/thread-3374.html)

Simple textures with GLUT? - Canter - Apr 2, 2007 09:11 PM

I have this code to make a simple box. This box acts as more or less a sprite on the screen. This is a really n00bish question, but how do I get a texture to display on this object so it has an appearance? How do I get this texture to have transparency, like a GIF? I've only ever worked on consoles like the GBA before where there was a color that was transparent, this is completely different deal, heh.


Simple textures with GLUT? - StealthyCoin - Apr 2, 2007 11:00 PM

Steal someone's texture code. Probably OSC's. I can't find it, sorry, but I stole his.

Simple textures with GLUT? - OneSadCookie - Apr 3, 2007 12:00 AM

It's unfortunately not a very simple question. The transparency thing is easy; use a texture with an alpha channel (eg. a PNG file), and do


before you draw. Remember to turn blending off when you don't need it Smile

For the texture-loading thing, you have a bunch of options. Easiest is probably to grab the code from , but the warning is there for a reason. Using ImageIO/CoreGraphics is also relatively simple (search the boards and you'll find examples). If you need cross-platform, you need libpng or libjpeg. Source for something that uses both is here:

Simple textures with GLUT? - PowerMacX - Apr 3, 2007 06:07 AM

Also, if you are going to use partial transparency, you'll need to render your quads back-to-front for them to show correctly.

Simple textures with GLUT? - DesertPenguin - Apr 3, 2007 06:19 AM

Here is the texture mapping tutorial from NeHe

Basically, you supply texture coordinates (u,v) for each vertex. The hardware then looks up the color at that location from the active texture unit in order to color the vertex.

glTexCoord2f(0.0f, 1.0f);glVertex2f(0.0f,0.0f);
glTexCoord2f(1.0f, 1.0f);glVertex2f(box2W,0.0f);
glTexCoord2f(1.0f, 0.0f);glVertex2f(box2W,box2W);
glTexCoord2f(0.0f, 0.0f);glVertex2f(0.0f,box2W);

You must also load an image and store it in a texture unit with:
glBindTexture(GL_TEXTURE_2D, texture[0]);

and activate texture mapping with:

Hope that helps...

Simple textures with GLUT? - Canter - Apr 3, 2007 09:18 AM

Thanks to all who have helped so far!

However, this code, as mentioned from the "NeHe" tutorial, appears to not work...

AUX_RGBImageRec *LoadBMP(char *Filename)
    FILE *File=NULL;
        if (!Filename)
            return NULL;
        if (File)
            return auxDIBImageLoad(Filename);
    return NULL;

int LoadGLTextures()
    int Status=FALSE;
    AUX_REGImageRec *TextureImage[1];
    memset(TextureImage,0,sizeof(void *)*1);
    if (TextureImage[0]=LoadBMP("/ball.bmp"))
        glGenTextures(1, &texture[0]);
        glBindTexture(GL_TEXTURE_2D, texture[0]);
        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
        if (TextureImage[0]->data)
    return Status;

Well, that returned 3 errors and 2 warnings, and provided (obviously) no function:

errors Wrote:error: parse error before '*' token
error: 'AUX_REGImageRec' undeclared (first use in this function)
error: 'TextureImage' undeclared (first use in this function)

About the "undeclared" stuff, it wasn't declared in the example, so I wasn't sure wether to just make an int or whatever at the top.

I'm not sure what to do, like I said, I've never dealt with this before.

Simple textures with GLUT? - OneSadCookie - Apr 3, 2007 12:33 PM

The NeHe texture loading code is Windows-specific and ancient; it won't work on a Mac. Follow my links.

Simple textures with GLUT? - Canter - Apr 3, 2007 01:05 PM

Sorry, it's probably great stuff, but it's not really context-sensitive, I mean, what do I do with it? I'm sorry, it's probably great code, I just have no idea what I'm doing...

Simple textures with GLUT? - DesertPenguin - Apr 3, 2007 05:35 PM

One Sad Cookie is right - the NeHe tutorials are both ancient and window specific.

I posted them solely for the discussion regarding OpenGL texture loading.

Your problem stems from the need to load a texture from some kind of graphic file - like png, bmp,tiff,jpg. OpenGL does not provide a facility for this - it expects the raw image pixel data.

So basically you need some way to load a file and get the raw image data. Once you have that, just call something like:

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
w, h,

I personally use both SDL and SDL_image to read a png.

Hope that helps.

Simple textures with GLUT? - bronxbomber92 - Apr 3, 2007 05:52 PM

The links that OneSadCookie posted are the loaders that extract the raw pixel data for OpenGL to use, once you have that you can do what DesertPenguin said.

Simple textures with GLUT? - arekkusu - Apr 3, 2007 07:45 PM

The question of "how do I get a texture to display on this object" is different from "how do I load an image from disk to use as a texture." The first question is all about OpenGL. The second question is about your file system and your image libraries.

Both of these have been answered many times on this forum before. But to succinctly answer the first question, separate from the second:

// let's hardcode an image to illustrate texture application
// I'll use 4-bit per component values (16 bits per pixel) for a few rgba colors
#define TEX_TYPE     GL_UNSIGNED_SHORT_4_4_4_4

// here's a few hardcoded RGBA color values
#define R 0xf30f
#define W 0xffff
#define X 0x0000
#define G 0x5c6c
#define B 0x111f

// and here's a hardcoded image
unsigned short mushroom_texture [(16 * 16)] = {
// now that we have a buffer containing an image, let's pass it to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, TEX_INTERNAL, 16, 16, 0, TEX_FORMAT, TEX_TYPE, mushroom_texture);

// by default, OpenGL uses mipmaps. We must disable that for this texture to be "complete"

// now draw a quad with the texture (this assumes your projection etc is already setup)
    glTexCoord2f(0, 0); glVertex2f(0,  0);
    glTexCoord2f(1, 0); glVertex2f(16, 0);
    glTexCoord2f(1, 1); glVertex2f(16,16);
    glTexCoord2f(0, 1); glVertex2f(0, 16);

Make sense? Not that much to it.

Simple textures with GLUT? - Canter - Apr 4, 2007 08:14 AM

Yeah, arekkusu, that's great, it worked for me. Now I need to get it to load the images, I'll try to work with OneSadCookie's code now that I know how it works. Smile

EDIT: I still can't figure it out... >_<

Is there some sort of a tutorial?