BGL gluOrtho2D transformation z-coordinate

I am drawing a crosshair at a normalized screen coordinate using some OpenGL wrapper fuctions from the bgl module. The code is adapted from nilunders RTS controls tutorial (http://www.youtube.com/watch?v=-LQqOPY-N8I):


def draw():
    import bgl
    from mathutils import Vector

    def drawCrosshair():
        # getCrosshair() returns the endpoints of the two crossed lines to draw
        crosshair = getCrosshair()

        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glLoadIdentity()
        bgl.gluOrtho2D(0, 1, 1, 0)

        bgl.glMatrixMode(bgl.GL_MODELVIEW)
        bgl.glLoadIdentity()

        bgl.glColor3f(1, 0, 0)
        bgl.glLineWidth(3.0)

        bgl.glBegin(bgl.GL_LINES)
        for p in crosshair:
            bgl.glVertex2f(p.x, p.y)
        bgl.glEnd()

Further down in the same draw() function I then add drawCrosshair() to the post_draw list of an overlay scene:


    for scene_item in bge.logic.getSceneList():
        if scene_item.name == "overlay":
            overlay_scene = scene_item

    overlay_scene.post_draw = [drawCrosshair]

Drawing the crosshair works perfectly. However, it is partially covered by a plane on the overlay scene with z-position 0. How is this possible if I draw the vertices after Blender drew the scene?

getCrosshair is defined as:


    def getCrosshair():
        crosshair_radius = 0.25

        # cursor_pos is a tuple of normalized screen cordinates 
        # ie. between (0,0) and (1,1)
        h_left = Vector(cursor_pos)
        h_left.x -= crosshair_radius
        h_right = Vector(cursor_pos)
        h_right.x += crosshair_radius

        v_top = Vector(cursor_pos)
        v_top.y -= crosshair_radius
        v_bottom = Vector(cursor_pos)
        v_bottom.y += crosshair_radius

        return [h_left, h_right, v_top, v_bottom]

You need to post a simplified .blend that shows the behavior in question, so that we can run it and see for ourselves, rather than just guess.

Thanks for your quick reply and your amazing tutorials, Goran. I spent quite some time on a simplified version that still had the same issue, only to find out that I simply forgot to switch off depth buffer testing. :o

Missing line:


bgl.glDisable(GL_DEPTH_TEST)