OpenGL alpha blending issue with back face visible.

Posted by Max on Stack Overflow See other posts from Stack Overflow or by Max
Published on 2011-01-12T19:34:44Z Indexed on 2011/01/12 19:54 UTC
Read the original article Hit count: 262

I'm trying to display "transparent" surfaces (not closed volumes) with both the front face and back face are visible (not culled).

For example displaying a cone or cylinder where the transparency is applied on both sides. There are some visible artifacts where some part of the surface does not seems to be handling the alpha values correctly.

The issue it seems is when I (opengl) is trying to apply the alpha from the front side of the surface to the backside of the surface. (when both the inside/outside of the surface is visible).

void init()
{
    glMatrixMode(GL_PROJECTION);
    gluPerspective( /* field of view in degree */ 40.0,
        /* aspect ratio */ 1.0,
        /* Z near */ 1.0, /* Z far */ 10.0);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
        0.0, 0.0, 0.0,      /* center is at (0,0,0) */
        0.0, 1.0, 0.);      /* up is in positive Y direction */
    glTranslatef(0.0, 0.6, -1.0);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
    glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
    glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
    glLightfv(GL_LIGHT2, GL_POSITION, light2_position);

    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    //glEnable(GL_CULL_FACE);
    glFrontFace( GL_CW );

    glShadeModel(GL_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

}
void draw ()
{
    static GLfloat amb[] = {0.4f, 0.4f, 0.4f, 0.0f};
    static GLfloat dif[] = {1.0f, 1.0f, 1.0f, 0.0f};
    static GLfloat back_amb[] = {0.4f, 0.4f, 0.4f, 1.0f};
    static GLfloat back_dif[] = {1.0f, 1.0f, 1.0f, 1.0f};

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_LIGHT1);
    glDisable(GL_LIGHT2);
    amb[3] = dif[3] = 0.5f;// cos(s) / 2.0f + 0.5f;
    glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
    glMaterialfv(GL_BACK, GL_AMBIENT, back_amb);
    glMaterialfv(GL_BACK, GL_DIFFUSE, back_dif);
    glPushMatrix();
    glTranslatef(-0.3f, -0.3f, 0.0f);
    glRotatef(angle1, 1.0f, 5.0f, 0.0f);
    glutSolidCone(1.0, 1.0, 50, 2 ); 
    glPopMatrix();

    ///...
    SwapBuffers(wglGetCurrentDC()); // glutSwapBuffers();

}

The code is based on : http://www.sgi.com/products/software/opengl/examples/glut/examples/source/blender.c

tinyurled links to 2 images on flickr showing the issue (but from out production code, not the above code, but both have the same kind of problems): http://flic.kr/p/99soxy and http://flic.kr/p/99pg18

Thanks. Max.

© Stack Overflow or respective owner

Related posts about opengl

Related posts about windows-vista