Drawing a round point with BGL

blender_2019-12-23_14-27-04

Hey guys, my bgl point comes out looking like a square. Is there any way I can make a circle out of this via smoothing or whatever?

I know I can use def draw circle 2d to create my own circle and then just place it where i want, but I don’t know how to make a filled solid circle… So honestly just having the point drawn as a circle would be the simplest way. Either way is fine

If you can define a circle in coordinates, you can fill it using the TRI_FAN method where every triangle of a shape share a common vertex. This gives the best prospects for anti-aliasing the shape later.

from math import pi, cos, sin

def circle(x, y, radius, segments):
    coords = []
    m = (1.0 / (segments - 1)) * (pi * 2)

    for p in range(segments):
        p1 = x + cos(m * p) * radius
        p2 = y + sin(m * p) * radius
        coords.append((p1, p2))
    return coords

Then for the batch:

circle_co = circle(200, 200, 50, 32)
batch = batch_for_shader(shader, 'TRI_FAN', {"pos":  circle_co})
2 Likes

Thank you so much!

How would I go about making this work for all the vertices?

This is how I get the coordinates btw:

def get_ob_coords():
        return [obj.matrix_world @ v.co for v in bm.verts]
    

This is what i am doing

for i in range(len(self.mouse_path)):
     shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
     pos = self.mouse_path[i]
     circle_co = draw_circle_2d(pos[0], pos[1], self.drawsize * self.pressure[i])
     batch = batch_for_shader(shader, 'TRI_FAN', {"pos":  circle_co})
     shader.bind()
     shader.uniform_float("color", (0.6, 0.6, 0.6, 1.0))
     batch.draw(shader)

Is there no other way to do this? I can’t seem to get it working with my modal. I’d like to draw for selected vertex.

just do it with a pixel shader… adding extra triangles to make a 2D circle when you have access to shaders is a bad idea to begin with. https://thebookofshaders.com/07/

Is there an example available?

there are dozens of examples on the page I linked… it’s even GLSL.

Do you know how to update viewport?

I am trying to use this:

obj.update_from_editmode()

but when drawing vertex point and changing selection, it doesnt update the shader. Just crashes a lot…

Update: Just include it in ‘MOUSEMOVE’ event instead of the the draw function.

yeah, if you tried to push edit mode changes to the object every single frame that would probably crash blender.

to refresh the viewport just use area.tag_redraw()