Search code examples
javaopengljogl

Triangles/lines in transparent objects in OpenGL


When I draw a plane with a transparent texture (for example the windows of a house), I see lines or triangles where they are not supposed to be. How can I fix this?

triangles
(source: troll.ws)

Here is the method I use to draw one window. I temporary enable blending to make the window transparent.

 static void drawWindow(int startX, int startY, int endX, int endY) {

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    Textures.window.bind();
    glPushMatrix();
    glTranslatef(startX, startY, 0);

    glBegin(GL_QUADS);
    glTexCoord2d(1, 1);
    glVertex3d(0, 0, 0);
    glTexCoord2d(0, 1);
    glVertex3d(endX - startX, endY - startY, 0);
    glTexCoord2d(0, 0);
    glVertex3d(endX - startX, endY - startY, 200);
    glTexCoord2d(1, 0);
    glVertex3d(0, 0, 200);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);

    glPopMatrix();
}

Solution

  • This is caused by having polygon smoothing turned on, which causes GL to render the edges of the polygons differently, causing issues with alpha-blending.

    It's an outdated form of AA, so best to turn it off, and use a full scene anti-aliasing method instead, such as MSAA or other similar technique.

    And standard advice also applies - the fixed function pipeline is antiquated and deprecated, and generally just not recommended.