Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Texture Transparency .BMP

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2012
    Posts
    10

    Texture Transparency .BMP

    I am drawing a polygon with texture on it as part of the HUD in my OpenGL program:

    Code :
    //Change the projection so that it is suitable for drawing HUD
    glMatrixMode(GL_PROJECTION);  //Change the projection
    glLoadIdentity();
    glOrtho(0, 800, 800, 0, -1, 1);  //2D Mode
    glMatrixMode(GL_MODELVIEW);  //Back to modeling
    glLoadIdentity();
     
    //Draw the polygon with the texture on it
    glBegin(GL_POLYGON);
    glTexCoord2f(0.0, 1.0); glVertex3f(250.0, 680, 0.0);
    glTexCoord2f(1.0, 1.0); glVertex3f(570.0, 680, 0.0);
    glTexCoord2f(1.0, 0.0); glVertex3f(570.0, 800, 0.0);
    glTexCoord2f(0.0, 0.0); glVertex3f(250.0, 800, 0.0);
    glEnd();
     
    //Change the projection back to how it was before
    glMatrixMode(GL_PROJECTION);  //Change the projection
    glLoadIdentity();
    gluPerspective(45.0, ((GLfloat)800) / GLfloat(800), 1.0, 200.0);  //3D Mode
    glMatrixMode(GL_MODELVIEW);  //Back to modeling
    glLoadIdentity();

    Properties that I am using for the textures:

    Code :
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, TextureList[i]->getSizeX(), TextureList[i]->getSizeY(), GL_RGB, GL_UNSIGNED_BYTE, TextureList[i]->getData());

    The problem is that I can't get the white "box" around the gun to blend with the tiles in the background. Here is a photo:

    img607.imageshack.us/img607/51/ogly.png

    I opened the image (.bmp) in Photoshop and deleted the pixels around the gun, but it still draws the whole rectangular image. It colors the pixels that I deleted with the last color that I used in glColor3f(), and I can get the whole image to become transparent, but I only want the pixels that I deleted in Photoshop to be fully transparent. I'm using glBlend too. From what I researched, it has to do with alpha channeling with the image, but I have no idea how. Can anyone help?

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,109
    Your image doesn't have an alpha component so it will default to 1 (opaque). You will need to use something like a .png which has an alpha component or write a shader to drop pixels with a particular colour or have a black and white mask image that
    you draw to the stencil buffer and then render the texture with stenciling enabled.

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    Or you can use shaders and assign the alpha yourself. It's almost 2013. Time to use GLSL...

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2012
    Posts
    10
    It's for a school project so GLSL will be on the list of things to learn right after this semester!
    My OpenGL program can only read .BMP images, so I think the most practical solution at the moment is to do as tonyo_au said and have a black and white mask image that I draw to the stencil buffer and then render the texture with stenciling enabled.
    Except I don't know how to do that...I imagine I would make another image of my texture with the gun being all black, and the background white, and send that to the stencil buffer before rendering the original image. Sounds like just a few lines of code...do you know of any tutorial for this by any chance?

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,109
    Always remember google is your friend
    http://en.wikibooks.org/wiki/OpenGL_...Stencil_buffer

    this is worth looking at as well

    http://ogldev.atspace.co.uk/index.html

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2012
    Posts
    10
    Thank you!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •