transparency problem

Hi,
i am drawing two objects the first one is opaque , and the second one is semi transparent on a black
background.
the alpha factor of the opaque object is 1.0 and of the transparent one is 0.5
i have the following problem :
if i use the blending functions :
glBlendFunc (GL_SRC_ALPHA, GL_ONE);
or
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
I see only the opaque object and not the transparent one.
if i use :
glBlendFunc(GL_ONE ,GL_ONE);
both objects appear but when they are one in front of the other
the blended pixels turns white, and the transparent object turns white also in some angles when is just
against the black background.(as if it reflects to much light even that the specular component is disabled)
if i use:
glBlendFunc (GL_SRC_ALPHA, GL_SRC_COLOR);
I get good results when the transparent object is in front of the opaque object, but when the transparent
object is on the black background it disappears !!! (if i change the color of the background to white
it works great, but I need it black…)
the display function is :

GLfloat SurfaceColor[4];

// Erase last list
if (m_ListOpenGL)
if (glIsList(m_ListOpenGL))
::glDeleteLists(m_ListOpenGL,1);

// Search for a new list
m_ListOpenGL = ::glGenLists(1);
if(m_ListOpenGL == 0)
return 0;

// Start list
unsigned int NbVertex = (unsigned int)m_ArrayVertex.GetSize();
if(!NbVertex)
return 0;

unsigned int NbFace = (unsigned int)m_ArrayFace.GetSize();
if(!NbFace)
return 0;

CFace3d* pFace;
CVector3d* pVector;
CColor* pColorPrevious;

::glNewList(m_ListOpenGL,GL_COMPILE_AND_EXECUTE);

//the alph value of the mesh
if(m_fTransperentLevel< 1.0)
{
//both objects appear but when are one in fron of the
//other
//the blendind turns white, and the transparent object is
//white in some angles.
//glBlendFunc(GL_ONE ,GL_ONE);
//only the solid object appear, the transperent isn’t
//glBlendFunc (GL_SRC_ALPHA, GL_ONE);
//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//works good but dissapear in black backround
glBlendFunc (GL_SRC_ALPHA, GL_SRC_COLOR);
glEnable(GL_BLEND);
//turn off the depth buffer
glDepthMask (GL_FALSE);
}

// Init color
pFace = m_ArrayFace[0];
pColorPrevious = pFace->GetColor();

::glBegin(GL_TRIANGLE_STRIP);
// form the first two vertices of the N-2 triangle strip.
SurfaceColor[0] = (float)1 / (float)255 * (float)pFace->v(0)->GetColor()->r();
SurfaceColor[1] = (float)1 / (float)255 * (float)pFace->v(0)->GetColor()->g();
SurfaceColor[2] = (float)1 / (float)255 * (float)pFace->v(0)->GetColor()->b();
SurfaceColor[3] = m_fTransperentLevel;
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, SurfaceColor);

pVector = pFace->v(0)->GetNormal();
::glNormal3f(pVector->x(),pVector->y(),pVector->z());
::glVertex3f(pFace->v(0)->x(),pFace->v(0)->y(),pFace->v(0)->z());
pVector = pFace->v(1)->GetNormal();
::glNormal3f(pVector->x(),pVector->y(),pVector->z());
::glVertex3f(pFace->v(1)->x(),pFace->v(1)->y(),pFace->v(1)->z());

for(unsigned int i=0; i< NbFace-1; i++)
{
pFace = m_ArrayFace[i];
// Normal (per face)
if(m_NormalBinding == NORMAL_PER_FACE)
{
pVector = pFace->GetNormal();
::glNormal3f(pVector->x(),pVector->y
(),pVector->z());
}
// Color (per face)
if(m_ColorBinding == COLOR_PER_FACE &&
pColorPrevious != pFace->GetColor())
pColorPrevious = pFace->GetColor();
// Normal
if(m_NormalBinding == NORMAL_PER_VERTEX)
{
pVector = pFace->v(2)->GetNormal
();
::glNormal3f(pVector->x(),pVector-
>y(),pVector->z());
}
// Color (per vertex)
if(m_ColorBinding == COLOR_PER_VERTEX &&
pColorPrevious != pFace->v(2)->GetColor())
{
SurfaceColor[0] = (float)1 /
(float)255 * (float)pFace->v(2)->GetColor()->r();
SurfaceColor[1] = (float)1 /
(float)255 * (float)pFace->v(2)->GetColor()->g();
SurfaceColor[2] = (float)1 /
(float)255 * (float)pFace->v(2)->GetColor()->b();
SurfaceColor[3] =
if(m_fTransperentLevel< 1.0)
{
//change the transperent
//object color from the
//original color
SurfaceColor[0] -= 0.4;
if(SurfaceColor[0] < 0)
SurfaceColor[0] = 0;
SurfaceColor[1] -= 0.3;
if(SurfaceColor[1] < 0)
SurfaceColor[1] = 0;
SurfaceColor[2] -= 0.1;
if(SurfaceColor[2] < 0)
SurfaceColor[2] = 0;

           }
           glMaterialfv(GL_FRONT, 
                      GL_AMBIENT_AND_DIFFUSE, 
                      SurfaceColor);

      pColorPrevious = pFace-&gt;v(2)-&gt;GetColor();
      }

      
      // Vertex     
      ::glVertex3f(pFace-&gt;v(2)-&gt;x(),pFace-&gt;v(2)-
             &gt;y(),pFace-&gt;v(2)-&gt;z());
           
 }
 //complete the last ring triangle strip
 pFace = m_ArrayFace[i-39];
 pVector = pFace-&gt;v(1)-&gt;GetNormal();
 ::glNormal3f(pVector-&gt;x(),pVector-&gt;y(),pVector-&gt;z
   ());
 ::glVertex3f(pFace-&gt;v(1)-&gt;x(),pFace-&gt;v(1)-&gt;y
   (),pFace-&gt;v(1)-&gt;z());
 pVector = pFace-&gt;v(2)-&gt;GetNormal();
 ::glNormal3f(pVector-&gt;x(),pVector-&gt;y(),pVector-&gt;z
   ());
 ::glVertex3f(pFace-&gt;v(2)-&gt;x(),pFace-&gt;v(2)-&gt;y
   (),pFace-&gt;v(2)-&gt;z());
 pFace = m_ArrayFace[i-37];
 pVector = pFace-&gt;v(1)-&gt;GetNormal();
 ::glNormal3f(pVector-&gt;x(),pVector-&gt;y(),pVector-&gt;z
   ());
 ::glVertex3f(pFace-&gt;v(1)-&gt;x(),pFace-&gt;v(1)-&gt;y
   (),pFace-&gt;v(1)-&gt;z());
 
      
 ::glEnd();


 if(m_fTransperentLevel&lt; 1.0)
 {
      glDepthMask (GL_TRUE);
      glDisable(GL_BLEND);
 }
 ::glEndList();

 // List is done now
 m_ListDone = TRUE;
 m_Modified = FALSE;
 
 return 1;

any ideas ???
thanks in advance

wow… lovely knacky…
are you in love? or got payed holidays at work? or what?

yeah, listen to knacky he tells you all you need to know. good luck on reordering your stuff.

>>I don’t mean to offend you
you didn’t
>>you have to draw transparent objects >>AFTER opaque ones, and they must be drawn >>in strict order from the far plane to the >>near plane, with glDepthMask(GL_FALSE);
DA, this is the paint function of an object of type 3dMesh, and if you would bother to read the code you would have seen that of the transparency level is < 1.0 (that is the alpha level of the mesh) I transform the depth buffer to a read only mode using glDepthMask(GL_FALSE)
since there are two mesh objects I draw the
opaque one first and then the transparent one and therefore the if condition is being entered during the rendering of the second mesh.

>>Wasn’t I nice, everyone?
actually Not really.

Originally posted by snow_master:
[b][snip]
and if you would bother to read the code you would have seen that …[snip]
[b]

LOL geeze Knackered… can’t you read?

I want that code debugged and ready to go in an hour! Get to it, before we send you to n00bie land!

p.s. Sleep? Whats that? Dunno, but if you don’t get any, it sure makes you say strange things!

>>I didn’t bother to read your code because it’s almost unreadable

you probably meant you couldn’t understand it because things I have long forgotten regarding programming you will probably never learn…

>>and I shouldn’t have even replied to you

so don’t.
you bother me and spend my valuable (and very expensive) time, and except bitching you aren’t being helpful, so just spare me your “wane be” smart answers.

>>You are also very ungrateful

you got to be kidding…I should be grateful for what ?

thank god for the internet…otherwise people like you would have never had the chance to interact with people like me…I find it amusing…but you live in this sad comedy…I pity you.
p.s
if you are such a good programmer (I hope you are because judging your inter relationship with human beans, you probably haven’t got any friends…) instead of answering intelligently insulting replays…try to debug the problem and earn some respect…

As a vegetarian, I object to the whole
concept of these “human beans”.

Originally posted by gumby:
As a vegetarian, I object to the whole
concept of these “human beans”.

It was a typo. He meant “humans and beans”.

again if you would have bother to read the question and the code…you would have noticed that i tryed many blending functions (including some that are not writen) it didn’t made any sence to me either why when the src factor of the blending function is src_alpha (which make sence)that the whight of the closer transparent pixel alpha value will be multiplied, but the dest factor is GL_SRC_COLOR which dosen’t make sence at all, but this is the only way i got good results…(when the background isn’t black), the reasnobele choise was to put ONE_MINUS_SRC_ALPHA, but as i wrote earlier this method made only the opaque object to appear.
>>God, your spelling is almost as bad as
i appologize for my bad spelling, but english isn’t my first language, and actualy i speak read and write in 7 languages.(i bet you do to…)

now, dosen’t any one else read this fourum ? can someone please try to be more helpfull…?..?

please do go away already !!!
now doesn’t any one else can please be kind and try to suggest a solution ?

What the heck is your problem knackered?

This question is on topic and not purely beginners. His code lacks indents because of the UBB system.

Are you deliberately trying to intimidate people learning OpenGL to stop them posting here? People post here to learn, if you don’t want to help them then don’t intimidate them.

I’ll take the liberty to try and restore some indentation for better readability:

the display function is :

GLfloat SurfaceColor[4];

// Erase last list
if (m_ListOpenGL)
     if (glIsList(m_ListOpenGL))
          ::glDeleteLists(m_ListOpenGL,1);

// Search for a new list
m_ListOpenGL = ::glGenLists(1);
if(m_ListOpenGL == 0) return 0;

// Start list
unsigned int NbVertex = (unsigned int)m_ArrayVertex.GetSize();
if(!NbVertex) return 0;

unsigned int NbFace = (unsigned int)m_ArrayFace.GetSize();
if(!NbFace) return 0;

CFace3d* pFace;
CVector3d* pVector;
CColor* pColorPrevious;

::glNewList(m_ListOpenGL,GL_COMPILE_AND_EXECUTE);

//the alph value of the mesh
if(m_fTransperentLevel< 1.0)
{
    //both objects appear but when are one in fron of the other the blending
    //turns white, and the transparent object is white in some angles.
    //glBlendFunc(GL_ONE ,GL_ONE);
    //only the solid object appear, the transperent isn't
    //glBlendFunc (GL_SRC_ALPHA, GL_ONE);
    //glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    //works good but dissapear in black backround
    glBlendFunc (GL_SRC_ALPHA, GL_SRC_COLOR);
    glEnable(GL_BLEND);
    //turn off the depth buffer
    glDepthMask (GL_FALSE);
}

// Init color
pFace = m_ArrayFace[0];
pColorPrevious = pFace->GetColor();
     
::glBegin(GL_TRIANGLE_STRIP);
// form the first two vertices of the N-2 triangle strip.
SurfaceColor[0] = 1.0f/255*pFace->v(0)->GetColor()->r();
SurfaceColor[1] = 1.0f/255*pFace->v(0)->GetColor()->g();
SurfaceColor[2] = 1.0f/255*pFace->v(0)->GetColor()->b();
SurfaceColor[3] = m_fTransperentLevel;
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, SurfaceColor);
               
     
pVector = pFace->v(0)->GetNormal();
::glNormal3f(pVector->x(),pVector->y(),pVector->z());
::glVertex3f(pFace->v(0)->x(),pFace->v(0)->y(),pFace->v(0)->z());
pVector = pFace->v(1)->GetNormal();
::glNormal3f(pVector->x(),pVector->y(),pVector->z());
::glVertex3f(pFace->v(1)->x(),pFace->v(1)->y(),pFace->v(1)->z());

for(unsigned int i=0; i< NbFace-1; i++)
{
     pFace = m_ArrayFace[i];
     // Normal (per face)
     if(m_NormalBinding == NORMAL_PER_FACE)
     {
          pVector = pFace->GetNormal();
          ::glNormal3f(pVector->x(),pVector->y   
               (),pVector->z());
     }
     // Color (per face)
     if(m_ColorBinding == COLOR_PER_FACE && 
        pColorPrevious != pFace->GetColor())
          pColorPrevious = pFace->GetColor();
          // Normal
          if(m_NormalBinding == NORMAL_PER_VERTEX)
          {
               pVector = pFace->v(2)->GetNormal
                       ();
               ::glNormal3f(pVector->x(),pVector-
                       >y(),pVector->z());
          }
          // Color (per vertex)
          if(m_ColorBinding == COLOR_PER_VERTEX && 
               pColorPrevious != pFace->v(2)->GetColor())
          {
               SurfaceColor[0] = (float)1 / 
        (float)255 * (float)pFace->v(2)->GetColor()->r();
               SurfaceColor[1] = (float)1 / 
        (float)255 * (float)pFace->v(2)->GetColor()->g();
               SurfaceColor[2] = (float)1 / 
        (float)255 * (float)pFace->v(2)->GetColor()->b();
               SurfaceColor[3] = m_fTransperentLevel;
               if(m_fTransperentLevel< 1.0)
               {
                           //change the transperent object color from the original color
                    SurfaceColor[0] -= 0.4;
                    if(SurfaceColor[0] < 0)
                         SurfaceColor[0] = 0;
                    SurfaceColor[1] -= 0.3;
                    if(SurfaceColor[1] < 0)
                         SurfaceColor[1] = 0;
                    SurfaceColor[2] -= 0.1;
                    if(SurfaceColor[2] < 0)
                         SurfaceColor[2] = 0;
               

               }
               glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,SurfaceColor);

          pColorPrevious = pFace->v(2)->GetColor();
          }
          
          // Vertex     
          ::glVertex3f(pFace->v(2)->x(),pFace->v(2)->y(),pFace->v(2)->z()); 
     }
     //complete the last ring triangle strip
     pFace = m_ArrayFace[i-39];
     pVector = pFace->v(1)->GetNormal();
     ::glNormal3f(pVector->x(),pVector->y(),pVector->z
       ());
     ::glVertex3f(pFace->v(1)->x(),pFace->v(1)->y(),pFace->v(1)->z());
     pVector = pFace->v(2)->GetNormal();
     ::glNormal3f(pVector->x(),pVector->y(),pVector->z());
     ::glVertex3f(pFace->v(2)->x(),pFace->v(2)->y(),pFace->v(2)->z());
     pFace = m_ArrayFace[i-37];
     pVector = pFace->v(1)->GetNormal();
      ::glNormal3f(pVector->x(),pVector->y(),pVector->z());
      ::glVertex3f(pFace->v(1)->x(),pFace->v(1)->y(),pFace->v(1)->z());
     ::glEnd();

 if(m_fTransperentLevel< 1.0)
 {
      glDepthMask (GL_TRUE);
      glDisable(GL_BLEND);
 }
 ::glEndList();

 // List is done now
 m_ListDone = TRUE;
 m_Modified = FALSE;
 
 return 1;

[This message has been edited by zeckensack (edited 04-04-2002).]

Whoa, I fall asleep at the keyboard, and when I woke up, and looked at this topic again, it seems that someone had their knickers in a bunch!

That can make anyone cranky!

zeckensack, nice formatting!

looking at the GL docs :
“Transparency is best implemented using blend function (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) with primitives sorted from farthest to nearest.

Now, someone pass the beer and the human beans! MMM MMM

>>“Transparency is best implemented using blend function (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) with primitives sorted from farthest to nearest.”

as I mentioned 3 times already primitives ARE sorted from farthest to nearest and when using the (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) I see only the opaque object but no trace of the transparent object !!!
and give me a break about the beans…I have spelling mistakes…big ****ing deal…I am sure each and every one fully understood what I meant.I am not an English native speaker, so I write as I phonetically hear the word, I guess that I will never get it perfect, but I am coming soon to do my masters in the states and I hope that it should do the trick…

Snow, don’t know if anyone ever told you, but a means to take it as a joke. Lighten up!!

Oh, and I didn’t see you say you sorted your stuff, but you DIDN’T say it 3 times…

Have a nice day!

What you saw can be explained when the source has an alpha of 0.0

Also, snow_master, you did not paste the whole code in your display function. In one line, half of the statement is missing.

I suspect that m_fTransperentLevel is zero or some other source fragment alpha is zero messing up your transparency. Check texture alpha, material alpha etc, and make sure it’s not getting set to zero somewhere.

Relax, some of the hostile comments were unfair, especially since your code clearly indicates you sort etc. Anyone with a clue knows you weren’t randomly trying alpha functions, it is sensible to try a few options just to see if you are getting fragments and your blending hardware & code is working. Don’t feed the trolls.

Elixer,
I am usually as light as they get, it’s just that this is the 20 replay to my original question, and except for insulting my intelligence and programming capabilities, no one had really made any real try to read the code, the question and make a wise suggestion !!!
this “beginner” question is apparently insulting the intelligence of the professional experts that read this forum.
it is so insulting that no one has a clue why the problem is occurring…
believe me that before posting the question I read the opengl doc, the open gl book, 4 articles regarding transparency and open gl and numerous code changes and transparency methods (like alpha functions instead of blending functions etc.)

Well, well, well.

If the only way to make your transparent object seen is multiplying it with a white background (glBlendFun(whatever,GL_SRC_COLOR); ) I’d conclude that your alpha value is zero or very close to it.

Have you tried to extract the actually used alpha values out of the rendering function?

No, wait, I don’t see any glColor* calls anywhere … where do you set your alpha values? Only with the glMaterialfv call?

Ok, another suggestion: try glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); again, but insert [b]glColor4f(1.0f,1.0f,1.0f,0.5f); somewhere before rendering the transparent objects. See if this helps.

m_fTransperentLevel is 1.0 in the opaque and 0.5 in the transparent object, I double checked it, this was my first guess too…

>>No, wait, I don’t see any glColor* calls anywhere … where do you set your alpha values? Only with the glMaterialfv call?

I do only use the glMaterialfv, the 4 th component of the color vector is the alpha value.

is that wrong just to use material ?
any how I tried also using the glColor4v and got the same results.

Coconut - in what line ?

dorbie - thanks.