3
votes

I'm a little new to OpenGL. I am making a 2D application, and I defined a Quad class which defines a square with a texture on it. It loads these textures from a texture atlas, and it does this correctly. Everything works with regular textures, and the textures display correctly, but doesn't display correctly when the texture image is not a square.

For example, I want a Quad to have a star texture, and have the star to show up, and the area around the star image that still lies in the Quad to be transparent. But what ends up happening is that the star shows up fine, and then behind it is another texture from my texture atlas that fills the Quad. I assume the texture behind it is just the last texture I loaded into the system? Either way, I don't want that texture to show up.

Here's what I mean. I want the star but not the cloud-ish texture behind it showing up:
enter image description here

The important part of my render function is:

glDisable(GL_CULL_FACE);
glVertexPointer(vertexStride, GL_FLOAT, 0, vertexes);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(colorStride, GL_FLOAT, 0, colors);   
glEnableClientState(GL_COLOR_ARRAY);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, uvCoordinates);

//render
glDrawArrays(renderStyle, 0, vertexCount);  
3
What about non-square texture? It seems a blending related problem. The question title is misleading...Luca

3 Answers

3
votes

It seems like the obvious choice would be to use an RGBA texture, and make everything but the star transparent by setting the alpha channel to zero for those pixels (and enable alpha blending for the texture unit).

1
votes

Use an image manipulation program. Photoshop is a great one, gimp is a free one. You don't really use OpenGL to crop your textures. Rather, your textures need to be prepared beforehand for your program.

There should be some sort of very easy tool to remove everything outside of the star. By remove, I mean make it transparent, which will require an alpha channel. This means you need to make sure that the way you load your textures in your program takes into account 32-bit colors (RGBA - red, green, blue, alpha), not just 24-bit colors (RGB - red, green, blue).

This will make everything behind your star see-through, or transparent.

Also, just an afterthought, it looks like you could be taking a copyrighted image off the internet and using it in your game/program. If you're doing anything commercial, I'd strongly recommend creating your own textures.

0
votes

You want to make a call to glBindTexture(GL_TEXTURE_2D,0); after you have mapped your texture here is an example from some code ive written

         // Bind the texture
    glBindTexture(GL_TEXTURE_2D, image.getID());

    // Draw a QUAD with setting texture coordinates
    glBegin(GL_QUADS);
    {
        // Top left corner of the texture
        glTexCoord2f(0, 0);
        glVertex2f(x, y);

        // Top right corner of the texture
        glTexCoord2f(image.getRelativeWidth(), 0);
        glVertex2f(x+image.getImageWidth(), y);

        // Bottom right corner of the texture
        glTexCoord2f(image.getRelativeWidth(), image.getRelativeHeight());
        glVertex2f(x+image.getImageWidth()-20, y+image.getImageHeight());

        // Bottom left corner of the texture
        glTexCoord2f(0, image.getRelativeHeight());
        glVertex2f(x+20, y+image.getImageHeight());
    }
    glEnd();
    glBindTexture(GL_TEXTURE_2D, 0);

I am no expert but this certainly solved what you are experiencing for me.