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 #####
23 from gpu_extras
.batch
import batch_for_shader
26 def draw_rect(x
, y
, width
, height
, color
):
29 points
= [[x
, y
], # [x, y]
31 [xmax
, ymax
], # [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
)
40 shader
.uniform_float("color", color
)
41 bgl
.glEnable(bgl
.GL_BLEND
)
45 def draw_line2d(x1
, y1
, x2
, y2
, width
, color
):
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
)
56 shader
.uniform_float("color", color
)
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
)
66 shader
.uniform_float("color", color
)
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
)
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))
82 (x
, y
), (x
+ width
, y
),
83 (x
, y
+ height
), (x
+ width
, y
+ height
)]
85 uvs
= [(crop
[0], crop
[1]),
91 indices
= [(0, 1, 2), (2, 1, 3)]
93 shader
= gpu
.shader
.from_builtin('2D_IMAGE')
94 batch
= batch_for_shader(shader
, 'TRIS',
99 # send image to gpu if it isn't there already
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
)
114 # tell shader to use the image that is bound to image unit 0
115 shader
.uniform_int("image", 0)
118 bgl
.glDisable(bgl
.GL_TEXTURE_2D
)
121 def draw_text(text
, x
, y
, size
, color
=(1, 1, 1, 0.5)):
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
)