PDA

View Full Version : transparency



snow_master
04-04-2002, 12:14 PM
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->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;

any ideas ????
thanks in advance

chowe6685
04-04-2002, 05:41 PM
hmmm, nothing specific, but don't use GL_COMPILE_AND_EXECUTE, I seem to rememeber it being very broken. Instead use GL_COMPILE, and then call the list with glCallList();