Merge branch 'blender-v2.92-release'
[blender-addons.git] / blenderkit / ui_bgl.py
blob2165cdc5dca3ad0249603386b26e62bf92566d4b
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 import bgl, blf
21 import bpy, blf
22 import gpu
23 from gpu_extras.batch import batch_for_shader
26 def draw_rect(x, y, width, height, color):
27 xmax = x + width
28 ymax = y + height
29 points = [[x, y], # [x, y]
30 [x, ymax], # [x, y]
31 [xmax, ymax], # [x, y]
32 [xmax, y], # [x, y]
34 indices = ((0, 1, 2), (2, 3, 0))
36 shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
37 batch = batch_for_shader(shader, 'TRIS', {"pos": points}, indices=indices)
39 shader.bind()
40 shader.uniform_float("color", color)
41 bgl.glEnable(bgl.GL_BLEND)
42 batch.draw(shader)
45 def draw_line2d(x1, y1, x2, y2, width, color):
46 coords = (
47 (x1, y1), (x2, y2))
49 indices = (
50 (0, 1),)
51 bgl.glEnable(bgl.GL_BLEND)
53 shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
54 batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices)
55 shader.bind()
56 shader.uniform_float("color", color)
57 batch.draw(shader)
60 def draw_lines(vertices, indices, color):
61 bgl.glEnable(bgl.GL_BLEND)
63 shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
64 batch = batch_for_shader(shader, 'LINES', {"pos": vertices}, indices=indices)
65 shader.bind()
66 shader.uniform_float("color", color)
67 batch.draw(shader)
70 def draw_rect_3d(coords, color):
71 indices = [(0, 1, 2), (2, 3, 0)]
72 shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
73 batch = batch_for_shader(shader, 'TRIS', {"pos": coords}, indices=indices)
74 shader.uniform_float("color", color)
75 batch.draw(shader)
78 def draw_image(x, y, width, height, image, transparency, crop=(0, 0, 1, 1)):
79 # draw_rect(x,y, width, height, (.5,0,0,.5))
81 coords = [
82 (x, y), (x + width, y),
83 (x, y + height), (x + width, y + height)]
85 uvs = [(crop[0], crop[1]),
86 (crop[2], crop[1]),
87 (crop[0], crop[3]),
88 (crop[2], crop[3]),
91 indices = [(0, 1, 2), (2, 1, 3)]
93 shader = gpu.shader.from_builtin('2D_IMAGE')
94 batch = batch_for_shader(shader, 'TRIS',
95 {"pos": coords,
96 "texCoord": uvs},
97 indices=indices)
99 # send image to gpu if it isn't there already
100 if image.gl_load():
101 raise Exception()
103 # texture identifier on gpu
104 texture_id = image.bindcode
106 # in case someone disabled it before
107 bgl.glEnable(bgl.GL_BLEND)
109 # bind texture to image unit 0
110 bgl.glActiveTexture(bgl.GL_TEXTURE0)
111 bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture_id)
113 shader.bind()
114 # tell shader to use the image that is bound to image unit 0
115 shader.uniform_int("image", 0)
116 batch.draw(shader)
118 bgl.glDisable(bgl.GL_TEXTURE_2D)
121 def draw_text(text, x, y, size, color=(1, 1, 1, 0.5)):
122 font_id = 0
123 # bgl.glColor4f(*color)
124 blf.color(font_id, color[0], color[1], color[2], color[3])
125 blf.position(font_id, x, y, 0)
126 blf.size(font_id, size, 72)
127 blf.draw(font_id, text)