How to use antialiasing with bgl drawn elements?

by Dobis   Last Updated August 10, 2018 13:15 PM

When using a draw_handler to draw bgl elements to a screen as with GL_POLYGON or GL_LINE_LOOP etc... are there any builtin in bgl flags which can be enabled to invoke antialiasing or will this always require a much more complicated solution?

I'm aware of GL_POLYGON_SMOOTH, GL_POLYGON_SMOOTH_HINT, GL_LINE_SMOOTH, and GL_LINE_SMOOTH_HINT but enabling them before or after using GL_POLYGON or GL_LINE_LOOP doesn't have any affect. Has anyone personally accomplished antialiasing using bgl in Blender Python? If so, could you please show me how?

import bpy,bgl,blf


def draw_callback_px(self, context):

    #Can anything go here to invoke antialiasing?
    bgl.glEnable(bgl.GL_POLYGON_SMOOTH)
    bgl.glEnable(bgl.GL_POLYGON_SMOOTH_HINT)

    bgl.glBegin(bgl.GL_POLYGON)

    #Should flags go here?
    for x, y in self.mouse_path:
        bgl.glVertex2i(x, y)

    #How about here?
    bgl.glEnd()    


#How about an antialiasing function involving
#multi-sampling, bgl texture (or some other) 
#functions etc.. Anyone know how to do this?

class ModalDrawOperator(bpy.types.Operator):
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"

    def modal(self, context, event):
        context.area.tag_redraw()

        if event.type == 'MOUSEMOVE':
            self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))

        if event.type in ['LEFTMOUSE','RIGHTMOUSE','ESC']:
            context.space_data.draw_handler_remove(self._handle, 'WINDOW')
            return {'FINISHED'}

        return {'RUNNING_MODAL'}


    def invoke(self, context, event):
        self.mouse_path = []

        args = (self, context)
        self._handle = context.space_data.draw_handler_add(
        draw_callback_px, args, 'WINDOW', 'POST_PIXEL')

        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}


bpy.utils.register_class(ModalDrawOperator)
bpy.ops.object.modal_operator('INVOKE_DEFAULT')


Related Questions


Antialiasing for far object and reflected object

Updated May 06, 2015 10:17 AM

Blender viewport no anti aliasing

Updated January 29, 2018 23:15 PM

Looking for a usable solution for Z-pass aliasing

Updated June 27, 2016 08:06 AM

Color is bleeding if AA is enabled

Updated November 06, 2016 08:06 AM

Matching Cycles and Blender Internal AA settings

Updated August 13, 2018 09:15 AM