Texture transparency problems

No matter what I try, I can’t get the transparent pixels to actually be transparent.

I’m initializing with these:

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glEnable(GL_TEXTURE_2D)
glEnable(GL_ALPHA_TEST)
glAlphaFunc(GL_GREATER,0.4)
glEnable(GL_BLEND)
glColor4f(1.0,1.0,1.0,1.0)
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)

And initializing textures with this:

glBindTexture(GL_TEXTURE_2D, each.index)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, each.w, each.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, each.image)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)

But no matter what the alpha values are, the output screen is the same.

The order of rendering object is very important when you use blending. In general, opaque object are rendered first then transparent object are rendered.

I do something like this


 //draw opaque object ...
  glDepthMask(GL_FALSE);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
 //draw transparent object ...
   glDisable(GL_BLEND);
   glDepthMask(GL_TRUE);   

It still doesn’t work :frowning:
Did I do something wrong when initializing the textures, because no transparency is being shown at all.
I read in .bmp files and added alpha values to them. I’m pretty sure I did it right because it reads them correctly as GL_RGBA, but not GL_RGB.

Try that with a custom 2x2 pixels texture first.


 texel.push_back(0); texel.push_back(0); texel.push_back(255);texel.push_back(128);
  texel.push_back(0); texel.push_back(255); texel.push_back(0);texel.push_back(200);
  texel.push_back(255); texel.push_back(255);texel.push_back(0);texel.push_back(64);
  texel.push_back(0); texel.push_back(255);texel.push_back(255);texel.push_back(32);

  glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  glGenTextures(1,&texture_id);
  glBindTexture(GL_TEXTURE_2D,texture_id);
  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_MAG_FILTER,GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  glTexEnvf(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_DECAL);
   glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,2,2,0,GL_RGBA,GL_UNSIGNED_BYTE,&texel[0]);


This draw a transparent square and a opaque square behind.


  glLoadIdentity();
  gluLookAt(0.0,0.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);

  glPushMatrix();
  glDisable(GL_TEXTURE_2D);
  glColor4f(1.0,1.0,1.0,1.0);
  glTranslatef(-2.5,-2.5,-0.25);
  glBegin(GL_TRIANGLE_STRIP);
  glVertex3f(-5.0,-5.0,0.0);
  glVertex3f(5.0,-5.0,0.0);
  glVertex3f(-5.0,5.0,0.0);
  glVertex3f(5.0,5.0,0.0); 
  glEnd();
  glPopMatrix(); 

glPushMatrix();
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D,texture_id);
   
  glDepthMask(GL_FALSE);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
 
  glBegin(GL_TRIANGLE_STRIP);
  glTexCoord2f(0.0,0.0);
  glVertex3f(-5.0,-5.0,0.0);
  glTexCoord2f(1.0,0.0);
  glVertex3f(5.0,-5.0,0.0);
  glTexCoord2f(0.0,1.0);
  glVertex3f(-5.0,5.0,0.0);
  glTexCoord2f(1.0,1.0);
  glVertex3f(5.0,5.0,0.0); 
  glEnd();
  glDisable(GL_BLEND);
   glDepthMask(GL_TRUE);   
  glPopMatrix();

That also doesn’t work, but it shouldn’t be blurry, it should be pixelated right?

The pixels are blurred, but I’m not sure why.

oh, the blurriness was just caused by using GL_LINEAR instead of GL_NEAREST in one of the texture parameters

it’s easier to see now and transparency is definately not working

Ok, I have replaced the texture env mode from decal mode to modulate. So the transparency is not defined only by the image of the texture but also defined by the color of the quad just for testing purpose.

Here is my test code:


#include <iostream>
#include <vector>
#include <GL/glut.h>


void display(void);
void init(void);
void reshape(int w,int h);

GLuint texture_id;

int main(int argc,char **argv)
{
  glutInit(&argc,argv);
  glutInitWindowSize(512,512);
  glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
  glutCreateWindow("Transparency test");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  init();
  glutMainLoop();
  
  return(EXIT_SUCCESS);
}


/*---------------------------------------------------*/
void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  
  gluLookAt(0.0,0.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);

  glPushMatrix();
  glDisable(GL_TEXTURE_2D);
  glColor4f(1.0,1.0,1.0,1.0);
  glTranslatef(-1.5,-1.5,-0.25);
  glBegin(GL_TRIANGLE_STRIP);
  glVertex3f(-5.0,-5.0,0.0);
  glVertex3f(5.0,-5.0,0.0);
  glVertex3f(-5.0,5.0,0.0);
  glVertex3f(5.0,5.0,0.0); 
  glEnd();
  glPopMatrix(); 

  glPushMatrix();
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D,texture_id);
  glTexEnvf(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_MODULATE);
  glDepthMask(GL_FALSE);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  glColor4f(1.0,1.0,1.0,0.5);//Replace this alpha for transparency
  glBegin(GL_TRIANGLE_STRIP);
  glTexCoord2f(0.0,0.0);
  glVertex3f(-5.0,-5.0,0.0);
  glTexCoord2f(1.0,0.0);
  glVertex3f(5.0,-5.0,0.0);
  glTexCoord2f(0.0,1.0);
  glVertex3f(-5.0,5.0,0.0);
  glTexCoord2f(1.0,1.0);
  glVertex3f(5.0,5.0,0.0); 
  glEnd();
  glDisable(GL_BLEND);
  glDepthMask(GL_TRUE);   
  glPopMatrix();

  
  glutSwapBuffers();
}


/*------------------------------------------------------*/
void init(void)
{
  glEnable(GL_DEPTH_TEST);
   
  std::vector<GLubyte> texel;
  
  texel.push_back(255); texel.push_back(0); texel.push_back(0);texel.push_back(255);
  texel.push_back(0); texel.push_back(255); texel.push_back(0);texel.push_back(255);
  texel.push_back(0); texel.push_back(0);texel.push_back(255);texel.push_back(255);
  texel.push_back(0); texel.push_back(255);texel.push_back(255);texel.push_back(255);

  glEnable(GL_TEXTURE_2D);
  glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  glGenTextures(1,&texture_id);
  glBindTexture(GL_TEXTURE_2D,texture_id);
  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_MAG_FILTER,GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,2,2,0,GL_RGBA,GL_UNSIGNED_BYTE,&texel[0]);
  glDisable(GL_TEXTURE_2D);
}



/*-------------------------------------------------------*/
void reshape(int w,int h)
{
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(-5.0,5.0,-5.0,5.0,1.0,100.0);
  glMatrixMode(GL_MODELVIEW);
}


This code work for me. But if it does not work for you, can you post a screen shot to see what happen.

Thanks so much, I finally got around to taking another shot at it and it works. Using modulate instead of decal is the key.

I’ve been troubled by this for months, thanks!

omg u just saved my life
I LOVE U MAN!!! <3

i jus started particles today for my impact smoke puffs
my first time doing particles so i started with nehe #19
then i added simple up axis billboarding
but then i was kind of getting pissed that the alpha was hiding the polygons behind it
so even tho it was transparent they were still ugly squares
so i googled this and now my puffs are so smooth and tga is crispy!
btw im using stb_image lib to load all my images
i realy like that lib

does anyone have an explanation to why that command makes it totally transparent?

thx