Simple textures with GLUT?

Canter
Unregistered
 
Post: #1
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.

Code:
    glPushMatrix();
    glTranslatef(box2X,box2Y,0.0f);
    glColor3f(1.0f,1.0f,1.0f);
    glBegin(GL_QUADS);
    glVertex2f(0.0f,0.0f);
    glVertex2f(box2W,0.0f);
    glVertex2f(box2W,box2W);
    glVertex2f(0.0f,box2W);
    glEnd();
    glPopMatrix();
Quote this message in a reply
Member
Posts: 67
Joined: 2006.06
Post: #2
Steal someone's texture code. Probably OSC's. I can't find it, sorry, but I stole his.

The machine does not run without the coin.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
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

Code:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

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 http://onesadcookie.com/trac/browser/QTValuePak , 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: http://onesadcookie.com/trac/browser/SimpleImage
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #4
Also, if you are going to use partial transparency, you'll need to render your quads back-to-front for them to show correctly.
Quote this message in a reply
DesertPenguin
Unregistered
 
Post: #5
Here is the texture mapping tutorial from NeHe
http://nehe.gamedev.net/data/lessons/les...?lesson=06

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.

glBegin(GL_QUADS);
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);
glEnd();

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:
glEnable(GL_TEXTURE_2D);


Hope that helps...
Quote this message in a reply
Canter
Unregistered
 
Post: #6
Thanks to all who have helped so far!

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

Code:
AUX_RGBImageRec *LoadBMP(char *Filename)
{
    FILE *File=NULL;
        if (!Filename)
        {
            return NULL;
        }
        
        File=fopen(Filename,"r");
        
        if (File)
        {
            fclose(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"))
    {
        Status=TRUE;
        
        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);
        
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    }
    if(TextureImage[0])
    {
        if (TextureImage[0]->data)
        {
            free(TextureImage[0]->data);
        }
        free(TextureImage[0]);
    }
    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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
The NeHe texture loading code is Windows-specific and ancient; it won't work on a Mac. Follow my links.
Quote this message in a reply
Canter
Unregistered
 
Post: #8
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...
Quote this message in a reply
DesertPenguin
Unregistered
 
Post: #9
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
w, h,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
image->pixels);


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

Hope that helps.
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #10
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.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #11
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:

Code:
// 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_INTERNAL GL_RGBA4
#define TEX_FORMAT   GL_RGBA
#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)] = {
    X,X,X,B,B,B,B,B,B,B,B,B,B,X,X,X,
    X,X,B,B,W,W,W,W,W,W,W,W,B,B,X,X,
    X,X,B,W,W,W,W,W,W,W,W,W,W,B,X,X,
    X,B,B,W,W,W,R,W,W,R,W,W,W,B,B,X,
    B,B,B,B,W,W,R,W,W,R,W,W,B,B,B,B,
    B,W,W,B,B,B,B,B,B,B,B,B,B,W,W,B,
    B,W,W,W,W,W,G,G,G,G,W,W,W,W,W,B,
    B,G,G,W,W,G,G,G,G,G,G,W,W,G,G,B,
    B,G,G,G,W,G,G,G,G,G,G,W,G,G,G,B,
    B,G,G,G,W,G,G,G,G,G,G,W,G,G,G,B,
    B,B,G,G,W,W,G,G,G,G,W,W,G,G,B,B,
    X,B,G,W,W,W,W,W,W,W,W,W,W,G,B,X,
    X,B,B,W,G,G,W,W,W,W,G,G,W,B,B,X,
    X,X,B,B,G,G,G,W,W,G,G,G,B,B,X,X,
    X,X,X,B,B,B,G,W,W,G,B,B,B,X,X,X,
    X,X,X,X,X,B,B,B,B,B,B,X,X,X,X,X,
};
    
// 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"
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

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

Make sense? Not that much to it.
Quote this message in a reply
Canter
Unregistered
 
Post: #12
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?
Quote this message in a reply
Post Reply