2023-08-16 00:20:26 +10:00
|
|
|
# SPDX-FileCopyrightText: 2018-2023 Blender Authors
|
2023-06-15 13:09:04 +10:00
|
|
|
#
|
2022-02-11 09:07:11 +11:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2018-10-25 21:04:38 +11:00
|
|
|
|
2022-08-03 09:38:51 +12:00
|
|
|
def draw_circle_2d(position, color, radius, *, segments=None):
|
2018-11-14 16:43:09 +01:00
|
|
|
"""
|
|
|
|
Draw a circle.
|
|
|
|
|
|
|
|
:arg position: Position where the circle will be drawn.
|
|
|
|
:type position: 2D Vector
|
|
|
|
:arg color: Color of the circle. To use transparency GL_BLEND has to be enabled.
|
|
|
|
:type color: tuple containing RGBA values
|
|
|
|
:arg radius: Radius of the circle.
|
|
|
|
:type radius: float
|
|
|
|
:arg segments: How many segments will be used to draw the circle.
|
2022-08-03 09:38:51 +12:00
|
|
|
Higher values give better results but the drawing will take longer.
|
|
|
|
If None or not specified, an automatic value will be calculated.
|
|
|
|
:type segments: int or None
|
2018-11-14 16:43:09 +01:00
|
|
|
"""
|
2022-08-03 09:38:51 +12:00
|
|
|
from math import sin, cos, pi, ceil, acos
|
2018-10-25 21:04:38 +11:00
|
|
|
import gpu
|
|
|
|
from gpu.types import (
|
|
|
|
GPUBatch,
|
|
|
|
GPUVertBuf,
|
|
|
|
GPUVertFormat,
|
|
|
|
)
|
|
|
|
|
2022-08-03 09:38:51 +12:00
|
|
|
if segments is None:
|
2022-08-03 13:27:13 +12:00
|
|
|
max_pixel_error = 0.25 # TODO: multiply 0.5 by display dpi
|
2022-08-03 09:38:51 +12:00
|
|
|
segments = int(ceil(pi / acos(1.0 - max_pixel_error / radius)))
|
|
|
|
segments = max(segments, 8)
|
|
|
|
segments = min(segments, 1000)
|
|
|
|
|
2018-11-12 17:54:20 +01:00
|
|
|
if segments <= 0:
|
|
|
|
raise ValueError("Amount of segments must be greater than 0.")
|
|
|
|
|
2018-10-25 21:04:38 +11:00
|
|
|
with gpu.matrix.push_pop():
|
|
|
|
gpu.matrix.translate(position)
|
|
|
|
gpu.matrix.scale_uniform(radius)
|
2018-11-12 17:54:20 +01:00
|
|
|
mul = (1.0 / (segments - 1)) * (pi * 2)
|
|
|
|
verts = [(sin(i * mul), cos(i * mul)) for i in range(segments)]
|
2018-10-26 08:06:05 +11:00
|
|
|
fmt = GPUVertFormat()
|
2018-10-25 21:04:38 +11:00
|
|
|
pos_id = fmt.attr_add(id="pos", comp_type='F32', len=2, fetch_mode='FLOAT')
|
|
|
|
vbo = GPUVertBuf(len=len(verts), format=fmt)
|
|
|
|
vbo.attr_fill(id=pos_id, data=verts)
|
|
|
|
batch = GPUBatch(type='LINE_STRIP', buf=vbo)
|
2022-09-02 09:33:29 -03:00
|
|
|
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
|
2018-10-25 21:04:38 +11:00
|
|
|
batch.program_set(shader)
|
|
|
|
shader.uniform_float("color", color)
|
|
|
|
batch.draw()
|
2018-11-13 16:20:16 +01:00
|
|
|
|
|
|
|
|
2021-04-30 10:50:50 -03:00
|
|
|
def draw_texture_2d(texture, position, width, height):
|
2018-11-14 16:43:09 +01:00
|
|
|
"""
|
|
|
|
Draw a 2d texture.
|
|
|
|
|
2021-04-30 10:50:50 -03:00
|
|
|
:arg texture: GPUTexture to draw (e.g. gpu.texture.from_image(image) for :class:`bpy.types.Image`).
|
|
|
|
:type texture: :class:`gpu.types.GPUTexture`
|
2018-11-14 16:43:09 +01:00
|
|
|
:arg position: Position of the lower left corner.
|
|
|
|
:type position: 2D Vector
|
|
|
|
:arg width: Width of the image when drawn (not necessarily
|
|
|
|
the original width of the texture).
|
|
|
|
:type width: float
|
|
|
|
:arg height: Height of the image when drawn.
|
|
|
|
:type height: float
|
|
|
|
"""
|
2018-11-13 16:20:16 +01:00
|
|
|
import gpu
|
|
|
|
from . batch import batch_for_shader
|
|
|
|
|
|
|
|
coords = ((0, 0), (1, 0), (1, 1), (0, 1))
|
|
|
|
|
2022-09-02 09:33:29 -03:00
|
|
|
shader = gpu.shader.from_builtin('IMAGE')
|
2018-11-13 16:20:16 +01:00
|
|
|
batch = batch_for_shader(
|
|
|
|
shader, 'TRI_FAN',
|
|
|
|
{"pos": coords, "texCoord": coords},
|
|
|
|
)
|
|
|
|
|
|
|
|
with gpu.matrix.push_pop():
|
|
|
|
gpu.matrix.translate(position)
|
|
|
|
gpu.matrix.scale((width, height))
|
|
|
|
|
2022-09-02 09:33:29 -03:00
|
|
|
shader = gpu.shader.from_builtin('IMAGE')
|
2021-04-30 10:50:50 -03:00
|
|
|
|
|
|
|
if isinstance(texture, int):
|
|
|
|
# Call the legacy bgl to not break the existing API
|
|
|
|
import bgl
|
|
|
|
bgl.glActiveTexture(bgl.GL_TEXTURE0)
|
|
|
|
bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture)
|
|
|
|
shader.uniform_int("image", 0)
|
|
|
|
else:
|
|
|
|
shader.uniform_sampler("image", texture)
|
|
|
|
|
2018-11-13 16:20:16 +01:00
|
|
|
batch.draw(shader)
|