diff --git a/nodes/number/easing.py b/nodes/number/easing.py index db40273d06..4a6599130a 100644 --- a/nodes/number/easing.py +++ b/nodes/number/easing.py @@ -1,27 +1,15 @@ -# ##### BEGIN GPL LICENSE BLOCK ##### -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software Foundation, -# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -# -# ##### END GPL LICENSE BLOCK ##### +# This file is part of project Sverchok. It's copyrighted by the contributors +# recorded in the version control history of the file, available from +# its original location https://github.com/nortikin/sverchok/commit/master +# +# SPDX-License-Identifier: GPL3 +# License-Filename: LICENSE + from mathutils import Vector import bpy from bpy.props import FloatProperty, EnumProperty, StringProperty, BoolProperty -import blf -import bgl import gpu from gpu_extras.batch import batch_for_shader diff --git a/nodes/solid/solid_viewer.py b/nodes/solid/solid_viewer.py index dc3e207c77..df87cad6a9 100644 --- a/nodes/solid/solid_viewer.py +++ b/nodes/solid/solid_viewer.py @@ -7,7 +7,6 @@ from math import pi import numpy as np -import bgl import bpy import gpu from gpu_extras.batch import batch_for_shader @@ -23,6 +22,7 @@ from sverchok.utils.sv_shader_sources import dashed_vertex_shader, dashed_fragment_shader from sverchok.utils.sv_bmesh_utils import bmesh_from_pydata from sverchok.utils.modules.geom_utils import obtain_normal3 as normal +from sverchok.utils.modules.drawing_abstractions import drawing, shading_3d from sverchok.dependencies import FreeCAD if FreeCAD is not None: @@ -83,9 +83,9 @@ def generate_normals_data(verts, faces): def draw_uniform(GL_KIND, coords, indices, color, width=1, dashed_data=None): if GL_KIND == 'LINES': - bgl.glLineWidth(width) + drawing.set_line_width(width) elif GL_KIND == 'POINTS': - bgl.glPointSize(width) + drawing.set_point_size(width) params = dict(indices=indices) if indices else {} @@ -102,9 +102,7 @@ def draw_uniform(GL_KIND, coords, indices, color, width=1, dashed_data=None): batch.draw(shader) else: - # print(GL_KIND,coords) - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}UNIFORM_COLOR' - shader = gpu.shader.from_builtin(shader_name) + shader = gpu.shader.from_builtin(shading_3d.UNIFORM_COLOR) batch = batch_for_shader(shader, GL_KIND, {"pos" : coords}, **params) shader.bind() shader.uniform_float("color", color) @@ -112,14 +110,13 @@ def draw_uniform(GL_KIND, coords, indices, color, width=1, dashed_data=None): batch.draw(shader) if GL_KIND == 'LINES': - bgl.glLineWidth(1) + drawing.reset_line_width() elif GL_KIND == 'POINTS': - bgl.glPointSize(1) + drawing.reset_point_size() def draw_smooth(coords, vcols, indices=None): - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}SMOOTH_COLOR' - shader = gpu.shader.from_builtin(shader_name) + shader = gpu.shader.from_builtin(shading_3d.SMOOTH_COLOR) params = dict(indices=indices) if indices else {} batch = batch_for_shader(shader, 'TRIS', {"pos" : coords, "color": vcols}, **params) batch.draw(shader) @@ -181,13 +178,12 @@ def face_geom(geom, config): def draw_faces_uniform(context, args): geom, config = args - # print(geom.f_faces, config.shade) if config.draw_gl_wireframe: - bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_LINE) + drawing.set_polygonmode_line() if config.draw_gl_polygonoffset: - bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) - bgl.glPolygonOffset(1.0, 1.0) + drawing.enable_polygon_offset_fill() + drawing.set_polygon_offset_amounts() if config.shade == "flat": draw_uniform('TRIS', geom.f_verts, geom.f_faces, config.face4f) @@ -199,7 +195,8 @@ def draw_faces_uniform(context, args): draw_smooth(geom.f_verts, geom.smooth_vnorms, indices=geom.f_faces) if config.draw_gl_wireframe: - bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) + # this is to reset the state of drawing to fill + drawing.set_polygonmode_fill() def edges_geom(geom, config): @@ -231,10 +228,10 @@ def edges_geom(geom, config): def draw_complex(context, args): geom, config = args if config.draw_gl_polygonoffset: - bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) + drawing.disable_polygon_offset_fill() if config.shade != 'normals': - bgl.glEnable(bgl.GL_BLEND) + drawing.enable_blendmode() if config.display_edges: draw_lines_uniform(context, config, geom.e_vertices, geom.e_edges, config.line4f, config.line_width) @@ -243,10 +240,10 @@ def draw_complex(context, args): if config.display_verts: draw_uniform('POINTS', geom.verts, None, config.vcol, config.point_size) if config.shade != 'normals': - bgl.glDisable(bgl.GL_BLEND) + drawing.disable_blendmode() if config.draw_gl_polygonoffset: # or restore to the state found when entering this function. TODO! - bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) + drawing.disable_polygon_offset_fill() class SvSolidViewerNode(SverchCustomTreeNode, bpy.types.Node): diff --git a/nodes/viz/console_node.py b/nodes/viz/console_node.py index bb486b1935..870ada0800 100644 --- a/nodes/viz/console_node.py +++ b/nodes/viz/console_node.py @@ -12,7 +12,6 @@ import itertools import bpy -import bgl import gpu from gpu_extras.batch import batch_for_shader @@ -23,6 +22,7 @@ from sverchok.utils.sv_update_utils import sv_get_local_path from sverchok.utils.sv_font_xml_parser import get_lookup_dict, letters_to_uv from sverchok.utils.sv_nodeview_draw_helper import SvNodeViewDrawMixin, get_console_grid +from sverchok.utils.modules.drawing_abstractions import drawing #from sverchok.utils.decorators_compilation import njit def get_desired_xy(node): @@ -391,8 +391,8 @@ def terminal_text_to_uv(lines): def simple_console_xy(context, args, loc): texture, config = args - act_tex = bgl.Buffer(bgl.GL_INT, 1) - bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture.texture_dict['texture']) + act_tex = drawing.new_buffer_texture() + drawing.bind_texture_2d(texture.texture_dict['texture']) config.shader.bind() # if not config.syntax_mode == "None": @@ -513,13 +513,13 @@ def get_font_texture(self): dsize = data.size data = data.repeat(3).reshape(-1, 3) data = np.concatenate((data, np.ones(dsize)[:,None]),axis=1).flatten() - name = bgl.Buffer(bgl.GL_INT, 1) - bgl.glGenTextures(1, name) + name = drawing.new_buffer_texture() + drawing.generate_textures(name) + self.texture_dict['texture'] = name[0] - self.texture_dict['texture_data'] = data # bgl.Buffer(bgl.GL_FLOAT, data.size, data.tolist()) + self.texture_dict['texture_data'] = data - # return self.texture_dict.get('texture') - + def sv_init(self, context): self.inputs.new("SvStringsSocket", "text") self.id_data.update_gl_scale_info() @@ -543,23 +543,12 @@ def draw_buttons_ext(self, context, layout): for color_name in lexed_colors: row = col.row() row.prop(self, color_name) - def init_texture(self, width, height): - clr = bgl.GL_RGBA texname = self.texture_dict['texture'] data = self.texture_dict['texture_data'] + drawing.init_complex_texture(width, height, texname, data, 'RGBA') - texture = bgl.Buffer(bgl.GL_FLOAT, data.size, data.tolist()) - bgl.glPixelStorei(bgl.GL_UNPACK_ALIGNMENT, 1) - bgl.glEnable(bgl.GL_TEXTURE_2D) - bgl.glBindTexture(bgl.GL_TEXTURE_2D, texname) - bgl.glActiveTexture(bgl.GL_TEXTURE0) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) - bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, clr, width, height, 0, clr, bgl.GL_FLOAT, texture) def set_node_props(self, socket_data): multiline, (chars_x, chars_y) = text_decompose('\n'.join(socket_data), self.last_n_lines) diff --git a/nodes/viz/vd_matrix.py b/nodes/viz/vd_matrix.py index 6a3bb98651..d5ad6085f0 100644 --- a/nodes/viz/vd_matrix.py +++ b/nodes/viz/vd_matrix.py @@ -1,23 +1,12 @@ -# ##### BEGIN GPL LICENSE BLOCK ##### -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software Foundation, -# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -# -# ##### END GPL LICENSE BLOCK ##### - - -import bgl +# This file is part of project Sverchok. It's copyrighted by the contributors +# recorded in the version control history of the file, available from +# its original location https://github.com/nortikin/sverchok/commit/master +# +# SPDX-License-Identifier: GPL3 +# License-Filename: LICENSE + + + import bpy import gpu from gpu_extras.batch import batch_for_shader @@ -29,10 +18,10 @@ from sverchok.utils.sv_batch_primitives import MatrixDraw28 from sverchok.data_structure import node_id, updateNode from sverchok.node_tree import SverchCustomTreeNode +from sverchok.utils.modules.drawing_abstractions import drawing, shading_2d if not bpy.app.background: - shader_name = f'{"2D_" if bpy.app.version < (3, 4) else ""}SMOOTH_COLOR' - smooth_2d_shader = gpu.shader.from_builtin(shader_name) + smooth_2d_shader = gpu.shader.from_builtin(shading_2d.SMOOTH_COLOR) else: smooth_2d_shader = None @@ -77,9 +66,9 @@ def screen_v3d_batch_matrix_overlay(context, args): indices=indices_shifted) # smooth_2d_shader.bind() - bgl.glEnable( bgl.GL_BLEND ) + drawing.enable_blendmode() batch.draw(smooth_2d_shader) - bgl.glDisable( bgl.GL_BLEND ) + drawing.disable_blendmode() def match_color_to_matrix(node): diff --git a/nodes/viz/viewer_2d.py b/nodes/viz/viewer_2d.py index 374051919d..3ab6eb2906 100644 --- a/nodes/viz/viewer_2d.py +++ b/nodes/viz/viewer_2d.py @@ -24,10 +24,10 @@ import bpy from bpy.props import FloatProperty, IntProperty, EnumProperty, BoolProperty, FloatVectorProperty, IntVectorProperty -import bgl import gpu from gpu_extras.batch import batch_for_shader +from sverchok.utils.modules.drawing_abstractions import drawing from sverchok.data_structure import updateNode, node_id from sverchok.node_tree import SverchCustomTreeNode from sverchok.ui import bgl_callback_nodeview as nvBGL @@ -186,24 +186,24 @@ def view_2d_geom(x, y, args): config.p_batch.draw(config.p_shader) if config.draw_edges: - bgl.glLineWidth(config.edge_width) + drawing.set_line_width(config.edge_width) config.e_batch = batch_for_shader(config.e_shader, 'LINES', {"pos": geom.e_vertices, "color": geom.e_vertex_colors}, indices=geom.e_indices) config.e_shader.bind() config.e_shader.uniform_float("x_offset", x) config.e_shader.uniform_float("y_offset", y) config.e_shader.uniform_float("viewProjectionMatrix", matrix) config.e_batch.draw(config.e_shader) - bgl.glLineWidth(1) + drawing.reset_line_width() if config.draw_verts: - bgl.glPointSize(config.point_size) + drawing.set_point_size(config.point_size) config.v_batch = batch_for_shader(config.v_shader, 'POINTS', {"pos": geom.v_vertices, "color": geom.points_color}) config.v_shader.bind() config.v_shader.uniform_float("x_offset", x) config.v_shader.uniform_float("y_offset", y) config.v_shader.uniform_float("viewProjectionMatrix", matrix) config.v_batch.draw(config.v_shader) - bgl.glPointSize(1) + drawing.reset_point_size() def path_from_nums(nums, x, y, num_width, num_height, maxmin, sys_scale): diff --git a/nodes/viz/viewer_draw_curve.py b/nodes/viz/viewer_draw_curve.py index 2985672212..3cf374c434 100644 --- a/nodes/viz/viewer_draw_curve.py +++ b/nodes/viz/viewer_draw_curve.py @@ -10,7 +10,7 @@ import bpy from mathutils import Matrix, Vector from bpy.props import StringProperty, BoolProperty, IntProperty, EnumProperty, FloatVectorProperty, FloatProperty -import bgl + import gpu from gpu_extras.batch import batch_for_shader @@ -22,46 +22,47 @@ from sverchok.utils.sv_operator_mixins import SvGenericNodeLocator from sverchok.ui.bgl_callback_3dview import callback_disable, callback_enable from sverchok.utils.sv_3dview_tools import Sv3DviewAlign +from sverchok.utils.modules.drawing_abstractions import drawing, shading_3d def draw_edges(shader, points, edges, line_width, color, is_smooth=False): if is_smooth: draw_edges_colored(shader, points, edges, line_width, [color for i in range(len(points))]) else: - bgl.glLineWidth(line_width) + drawing.set_line_width(line_width) batch = batch_for_shader(shader, 'LINES', {"pos": points}, indices=edges) shader.bind() shader.uniform_float('color', color) batch.draw(shader) - bgl.glLineWidth(1) + drawing.reset_line_width() def draw_edges_colored(shader, points, edges, line_width, colors): - bgl.glLineWidth(line_width) + drawing.set_line_width(line_width) batch = batch_for_shader(shader, 'LINES', {"pos": points, "color": colors}, indices=edges) shader.bind() batch.draw(shader) - bgl.glLineWidth(1) + drawing.reset_line_width() def draw_points(shader, points, size, color): - bgl.glPointSize(size) + drawing.set_point_size(size) batch = batch_for_shader(shader, 'POINTS', {"pos": points}) shader.bind() shader.uniform_float('color', color) batch.draw(shader) - bgl.glPointSize(1) + drawing.reset_point_size() def draw_points_colored(shader, points, size, colors): - bgl.glPointSize(size) + drawing.set_point_size(size) batch = batch_for_shader(shader, 'POINTS', {"pos": points, "color": colors}) shader.bind() batch.draw(shader) - bgl.glPointSize(1) + drawing.reset_point_size() def draw_curves(context, args): node, draw_inputs, v_shader, e_shader = args is_smooth = node.draw_curvature - bgl.glEnable(bgl.GL_BLEND) + drawing.enable_blendmode() for item in draw_inputs: @@ -87,7 +88,7 @@ def draw_curves(context, args): if node.draw_verts and item.points is not None: draw_points(v_shader, item.points, node.verts_size, node.verts_color) - bgl.glEnable(bgl.GL_BLEND) + drawing.enable_blendmode() class SvBakeCurveOp(bpy.types.Operator, SvGenericNodeLocator): """B A K E CURVES""" @@ -280,14 +281,11 @@ def sv_init(self, context): self.inputs.new('SvStringsSocket', 'Resolution').prop_name = 'resolution' def draw_all(self, draw_inputs): - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}UNIFORM_COLOR' - v_shader = gpu.shader.from_builtin(shader_name) + v_shader = gpu.shader.from_builtin(shading_3d.UNIFORM_COLOR) if self.draw_curvature: - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}SMOOTH_COLOR' - e_shader = gpu.shader.from_builtin(shader_name) + e_shader = gpu.shader.from_builtin(shading_3d.SMOOTH_COLOR) else: - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}UNIFORM_COLOR' - e_shader = gpu.shader.from_builtin(shader_name) + e_shader = gpu.shader.from_builtin(shading_3d.UNIFORM_COLOR) draw_data = { 'tree_name': self.id_data.name[:], diff --git a/nodes/viz/viewer_draw_mk4.py b/nodes/viz/viewer_draw_mk4.py index c24f937ea7..0e43a172ab 100644 --- a/nodes/viz/viewer_draw_mk4.py +++ b/nodes/viz/viewer_draw_mk4.py @@ -13,7 +13,7 @@ from mathutils.noise import random, seed_set import bpy from bpy.props import StringProperty, FloatProperty, IntProperty, EnumProperty, BoolProperty, FloatVectorProperty -import bgl + import gpu from gpu_extras.batch import batch_for_shader @@ -23,6 +23,7 @@ from sverchok.ui.bgl_callback_3dview import callback_disable, callback_enable from sverchok.utils.sv_batch_primitives import MatrixDraw28 from sverchok.utils.sv_shader_sources import dashed_vertex_shader, dashed_fragment_shader +from sverchok.utils.modules.drawing_abstractions import drawing, shading_3d from sverchok.utils.geom import multiply_vectors_deep from sverchok.utils.modules.polygon_utils import pols_normals from sverchok.utils.modules.vertex_utils import np_vertex_normals @@ -40,27 +41,63 @@ uniform mat4 viewProjectionMatrix; in vec3 position; - out vec3 pos; void main() { - pos = position; gl_Position = viewProjectionMatrix * vec4(position, 1.0f); } ''' +default_geometry_shader = ''' + uniform mat4 viewProjectionMatrix; + + out VS_OUT + { + vec3 FaceNormal; + } vs_out; + + + layout(triangles) in; + layout(triangle_strip, max_vertices = 3) out; + + void main() + { + vec3 ab = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz; + vec3 ac = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz; + vec3 normal3 = normalize(cross(ab, ac)); + vec4 normal4 = vec4(normal3, 1.0); + vs_out.FaceNormal = normal3; + vec4 rescale = vec4(0.00003, 0.00003, 0.00003, 0.0); + vec4 offset = vec4(normal4 * rescale); + + for (int i = 0; i < gl_in.length(); i++) + { + gl_Position = gl_in[i].gl_Position + offset; + EmitVertex(); + } + + EndPrimitive(); + } + +''' + default_fragment_shader = ''' - uniform float brightness; - in vec3 pos; - out vec4 FragColor; + in VS_OUT + { + vec3 FaceNormal; + } fs_in; + + out vec4 gl_FragColor; void main() { - FragColor = vec4(pos * brightness, 1.0); + gl_FragColor = vec4(fs_in.FaceNormal.xyz, 0.7); } ''' + + def ensure_triangles(coords, indices, handle_concave_quads): """ this fully tesselates the incoming topology into tris, @@ -125,21 +162,24 @@ def view_3d_geom(context, args): geom, config = args - bgl.glEnable(bgl.GL_BLEND) + drawing.enable_blendmode() if config.draw_polys: if config.draw_gl_wireframe: - bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_LINE) + drawing.set_polygonmode_line() if config.draw_gl_polygonoffset: - bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) - bgl.glPolygonOffset(1.0, 1.0) + drawing.enable_polygon_offset_fill() + drawing.set_polygon_offset_amounts() if config.shade_mode == 'fragment': p_batch = batch_for_shader(config.p_shader, 'TRIS', {"position": geom.p_vertices}, indices=geom.p_indices) config.p_shader.bind() matrix = context.region_data.perspective_matrix config.p_shader.uniform_float("viewProjectionMatrix", matrix) - config.p_shader.uniform_float("brightness", 0.5) + #if hasattr(config, "brightness"): + # config.p_shader.uniform_float("brightness", config.brightness) + #else: + # config.p_shader.uniform_float("brightness", 0.5) else: if config.uniform_pols: p_batch = batch_for_shader(config.p_shader, 'TRIS', {"pos": geom.p_vertices}, indices=geom.p_indices) @@ -152,13 +192,14 @@ def view_3d_geom(context, args): p_batch.draw(config.p_shader) if config.draw_gl_polygonoffset: - bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) + drawing.disable_polygon_offset_fill() if config.draw_gl_wireframe: - bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) + # this is to reset the state of drawing to fill + drawing.set_polygonmode_fill() if config.draw_edges: - bgl.glLineWidth(config.line_width) + drawing.set_line_width(config.line_width) if config.draw_dashed: shader = config.dashed_shader @@ -182,11 +223,11 @@ def view_3d_geom(context, args): config.e_shader.bind() e_batch.draw(config.e_shader) - bgl.glLineWidth(1) + drawing.reset_line_width() if config.draw_verts: if geom.v_vertices and (len(geom.v_vertices[0])==3): - bgl.glPointSize(config.point_size) + drawing.set_point_size(config.point_size) if config.uniform_verts: v_batch = batch_for_shader(config.v_shader, 'POINTS', {"pos": geom.v_vertices}) config.v_shader.bind() @@ -196,9 +237,9 @@ def view_3d_geom(context, args): config.v_shader.bind() v_batch.draw(config.v_shader) - bgl.glPointSize(1) + drawing.reset_point_size() - bgl.glDisable(bgl.GL_BLEND) + drawing.disable_blendmode() def splitted_polygons_geom(polygon_indices, original_idx, v_path, cols, idx_offset): @@ -455,34 +496,28 @@ def generate_mesh_geom(config, vecs_in): if config.draw_verts: if config.uniform_verts: - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}UNIFORM_COLOR' - config.v_shader = gpu.shader.from_builtin(shader_name) + config.v_shader = gpu.shader.from_builtin(shading_3d.UNIFORM_COLOR) else: - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}SMOOTH_COLOR' - config.v_shader = gpu.shader.from_builtin(shader_name) + config.v_shader = gpu.shader.from_builtin(shading_3d.SMOOTH_COLOR) geom.v_vertices, geom.points_color = v_vertices, points_color if config.draw_edges: if config.edges_use_vertex_color and e_vertices: e_vertex_colors = points_color if config.uniform_edges: - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}UNIFORM_COLOR' - config.e_shader = gpu.shader.from_builtin(shader_name) + config.e_shader = gpu.shader.from_builtin(shading_3d.UNIFORM_COLOR) else: - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}SMOOTH_COLOR' - config.e_shader = gpu.shader.from_builtin(shader_name) + config.e_shader = gpu.shader.from_builtin(shading_3d.SMOOTH_COLOR) geom.e_vertices, geom.e_vertex_colors, geom.e_indices = e_vertices, e_vertex_colors, e_indices if config.draw_polys and config.shade_mode != 'fragment': if config.uniform_pols: - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}UNIFORM_COLOR' - config.p_shader = gpu.shader.from_builtin(shader_name) + config.p_shader = gpu.shader.from_builtin(shading_3d.UNIFORM_COLOR) else: if config.polygon_use_vertex_color and config.shade_mode not in ['facet', 'smooth']: p_vertex_colors = points_color - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}SMOOTH_COLOR' - config.p_shader = gpu.shader.from_builtin(shader_name) + config.p_shader = gpu.shader.from_builtin(shading_3d.SMOOTH_COLOR) geom.p_vertices, geom.p_vertex_colors, geom.p_indices = p_vertices, p_vertex_colors, p_indices elif config.shade_mode == 'fragment' and config.draw_polys: @@ -500,7 +535,7 @@ def generate_mesh_geom(config, vecs_in): config.draw_fragment_function = ND.get('draw_fragment') config.p_shader = gpu.types.GPUShader(config.node.custom_vertex_shader, config.node.custom_fragment_shader) else: - config.p_shader = gpu.types.GPUShader(default_vertex_shader, default_fragment_shader) + config.p_shader = gpu.types.GPUShader(default_vertex_shader, default_fragment_shader, geocode=default_geometry_shader) geom.p_vertices, geom.p_vertex_colors, geom.p_indices = p_vertices, p_vertex_colors, p_indices return geom @@ -555,6 +590,8 @@ class SvViewerDrawMk4(SverchCustomTreeNode, bpy.types.Node): name="Draw gl wireframe", default=False, update=updateNode) + brightness: FloatProperty(min=0.0, max=1.0, default=0.8) + vector_light: FloatVectorProperty( name='vector light', subtype='DIRECTION', min=0, max=1, size=3, default=(0.2, 0.6, 0.4), update=updateNode) @@ -652,7 +689,6 @@ def populate_node_with_custom_shader_from_text(self): except Exception as err: print(err) - # reset custom shader self.custom_vertex_shader = '' self.custom_fragment_shader = '' @@ -818,10 +854,9 @@ def create_config(self): config.u_dash_size = self.u_dash_size config.u_gap_size = self.u_gap_size config.u_resolution = self.u_resolution[:] + # config.brightness = self.brightness # this is only to test the passthrough of the geometry shader. config.node = self - - return config @@ -956,6 +991,5 @@ def show_viewport(self, is_show: bool): callback_disable(node_id(self)) - classes = [SvViewerDrawMk4,] register, unregister = bpy.utils.register_classes_factory(classes) diff --git a/nodes/viz/viewer_draw_surface.py b/nodes/viz/viewer_draw_surface.py index 12ae0f6fba..e462753b08 100644 --- a/nodes/viz/viewer_draw_surface.py +++ b/nodes/viz/viewer_draw_surface.py @@ -10,7 +10,6 @@ import bpy from mathutils import Matrix, Vector from bpy.props import StringProperty, BoolProperty, IntProperty, EnumProperty, FloatVectorProperty -import bgl import gpu from gpu_extras.batch import batch_for_shader @@ -22,23 +21,24 @@ from sverchok.utils.sv_operator_mixins import SvGenericNodeLocator from sverchok.ui.bgl_callback_3dview import callback_disable, callback_enable from sverchok.utils.sv_3dview_tools import Sv3DviewAlign +from sverchok.utils.modules.drawing_abstractions import drawing, shading_3d def draw_edges(shader, points, edges, line_width, color): - bgl.glLineWidth(line_width) + drawing.set_line_width(line_width) batch = batch_for_shader(shader, 'LINES', {"pos": points}, indices=edges) shader.bind() shader.uniform_float('color', color) batch.draw(shader) - bgl.glLineWidth(1) + drawing.reset_line_width() def draw_points(shader, points, size, color): - bgl.glPointSize(size) + drawing.set_point_size(size) batch = batch_for_shader(shader, 'POINTS', {"pos": points}) shader.bind() shader.uniform_float('color', color) batch.draw(shader) - bgl.glPointSize(1) + drawing.reset_point_size() def draw_polygons(shader, points, tris, vertex_colors): batch = batch_for_shader(shader, 'TRIS', {"pos": points, 'color': vertex_colors}, indices=tris) @@ -48,7 +48,7 @@ def draw_polygons(shader, points, tris, vertex_colors): def draw_surfaces(context, args): node, draw_inputs, v_shader, e_shader, p_shader = args - bgl.glEnable(bgl.GL_BLEND) + drawing.enable_blendmode() for item in draw_inputs: @@ -73,7 +73,7 @@ def draw_surfaces(context, args): if node.draw_verts: draw_points(v_shader, item.points_list, node.verts_size, node.verts_color) - bgl.glEnable(bgl.GL_BLEND) + drawing.disable_blendmode() class SvBakeSurfaceOp(bpy.types.Operator, SvGenericNodeLocator): """B A K E SURFACES""" @@ -304,12 +304,9 @@ def draw_buttons_ext(self, context, layout): self.draw_buttons(context, layout) def draw_all(self, draw_inputs): - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}UNIFORM_COLOR' - v_shader = gpu.shader.from_builtin(shader_name) - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}UNIFORM_COLOR' - e_shader = gpu.shader.from_builtin(shader_name) - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}SMOOTH_COLOR' - p_shader = gpu.shader.from_builtin(shader_name) + v_shader = gpu.shader.from_builtin(shading_3d.UNIFORM_COLOR) + e_shader = gpu.shader.from_builtin(shading_3d.UNIFORM_COLOR) + p_shader = gpu.shader.from_builtin(shading_3d.SMOOTH_COLOR) draw_data = { 'tree_name': self.id_data.name[:], diff --git a/nodes/viz/viewer_texture.py b/nodes/viz/viewer_texture.py index d71a1192ce..52aacc9ce8 100644 --- a/nodes/viz/viewer_texture.py +++ b/nodes/viz/viewer_texture.py @@ -14,7 +14,6 @@ import bpy import gpu -import bgl from bpy.props import ( FloatProperty, EnumProperty, StringProperty, BoolProperty, IntProperty @@ -25,6 +24,7 @@ from sverchok.node_tree import SverchCustomTreeNode from sverchok.ui import bgl_callback_nodeview as nvBGL2 from sverchok.ui import sv_image as svIMG +from sverchok.utils.modules.drawing_abstractions import drawing # shared stuff between implementations from sverchok.utils.sv_texture_utils import generate_batch_shader @@ -198,8 +198,7 @@ def calculate_total_size(self): def get_buffer(self): data = self.inputs['Float'].sv_get(deepcopy=False) self.total_size = self.calculate_total_size() - - texture = bgl.Buffer(bgl.GL_FLOAT, self.total_size, np.resize(data, self.total_size).tolist()) + texture = drawing.new_buffer_texture_sized(self.total_size, np.resize(data, self.total_size).tolist()) return texture def draw_buttons(self, context, layout): @@ -264,8 +263,8 @@ def sv_init(self, context): def delete_texture(self): n_id = node_id(self) if n_id in self.texture: - names = bgl.Buffer(bgl.GL_INT, 1, [self.texture[n_id]]) - bgl.glDeleteTextures(1, names) + names = drawing.get_buffer([self.texture[n_id]]) + drawing.delete_texture(names) def process(self): if not self.inputs['Float'].is_linked: @@ -295,12 +294,10 @@ def process(self): if self.activate: texture = self.get_buffer() width, height = self.texture_width_height - # x, y = self.xy_offset gl_color_constant = gl_color_dict.get(self.color_mode) - - name = bgl.Buffer(bgl.GL_INT, 1) - bgl.glGenTextures(1, name) + name = drawing.new_buffer_texture() + drawing.generate_textures(name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_constant) diff --git a/nodes/viz/viewer_texture_lite.py b/nodes/viz/viewer_texture_lite.py index 86be31c996..67125b05ff 100644 --- a/nodes/viz/viewer_texture_lite.py +++ b/nodes/viz/viewer_texture_lite.py @@ -8,7 +8,6 @@ import os import numpy as np -import bgl import gpu import bpy from bpy.props import EnumProperty, StringProperty, IntProperty, PointerProperty, FloatProperty @@ -21,6 +20,7 @@ from sverchok.utils.sv_texture_utils import generate_batch_shader from sverchok.utils.sv_texture_utils import simple_screen, init_texture, get_drawing_location from sverchok.utils.sv_texture_utils import gl_color_list, gl_color_dict, factor_buffer_dict +from sverchok.utils.modules.drawing_abstractions import drawing out_modes = [ @@ -94,8 +94,8 @@ def sv_init(self, context): def delete_texture(self): n_id = node_id(self) if n_id in self.texture: - names = bgl.Buffer(bgl.GL_INT, 1, [self.texture[n_id]]) - bgl.glDeleteTextures(1, names) + names = drawing.get_buffer([self.texture[n_id]]) + drawing.delete_texture(names) def process(self): @@ -120,10 +120,10 @@ def process(self): # x, y = self.xy_offset width, height, colm = self.width_custom_tex, self.height_custom_tex, self.color_mode total_size = width * height * factor_buffer_dict.get(colm) - texture = bgl.Buffer(bgl.GL_FLOAT, total_size, np.resize(self.inputs[0].sv_get(), total_size).tolist()) + texture = drawing.new_buffer_texture_sized(total_size, np.resize(self.inputs[0].sv_get(), total_size).tolist()) - name = bgl.Buffer(bgl.GL_INT, 1) - bgl.glGenTextures(1, name) + name = drawing.new_buffer_texture() + drawing.generate_textures(name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_constant) diff --git a/nodes/viz/viewer_waveform_output.py b/nodes/viz/viewer_waveform_output.py index 53bd3e9c93..225e8de988 100644 --- a/nodes/viz/viewer_waveform_output.py +++ b/nodes/viz/viewer_waveform_output.py @@ -12,8 +12,6 @@ import inspect import bpy -import blf -import bgl import gpu from gpu_extras.batch import batch_for_shader diff --git a/ui/bgl_callback_3dview.py b/ui/bgl_callback_3dview.py index 299033aa20..3015a54f68 100644 --- a/ui/bgl_callback_3dview.py +++ b/ui/bgl_callback_3dview.py @@ -17,7 +17,7 @@ # ##### END GPL LICENSE BLOCK ##### import bpy -import bgl +from sverchok.utils.modules.drawing_abstractions import drawing SpaceView3D = bpy.types.SpaceView3D @@ -62,9 +62,9 @@ def callback_disable_all(): def restore_opengl_defaults(): - bgl.glLineWidth(1) - bgl.glDisable(bgl.GL_BLEND) - bgl.glDisable(bgl.GL_DEPTH_TEST) + drawing.reset_line_width() + drawing.disable_blendmode() + drawing.disable_depth_test() # glIsEnabled with argument # GL_POLYGON_OFFSET_FILL, @@ -80,7 +80,7 @@ def draw_callback_px(n_id, data): context = bpy.context drawing_func = data.get('custom_function') # must accept 'context' first args = data.get('args', (None,)) # args does not need to be a tuple. - bgl.glEnable(bgl.GL_DEPTH_TEST) + drawing.enable_depth_test() drawing_func(context, args) restore_opengl_defaults() diff --git a/ui/bgl_callback_nodeview.py b/ui/bgl_callback_nodeview.py index f7f4940808..a09f6a2c68 100644 --- a/ui/bgl_callback_nodeview.py +++ b/ui/bgl_callback_nodeview.py @@ -24,11 +24,11 @@ import bpy import blf -import bgl from bpy.types import SpaceNodeEditor from sverchok.utils.sv_stethoscope_helper import draw_text_data, draw_graphical_data from sverchok.utils.sv_logging import sv_logger +from sverchok.utils.modules.drawing_abstractions import drawing callback_dict = {} point_dict = {} @@ -98,9 +98,8 @@ def callback_disable_filtered(pattern): def restore_opengl_defaults(): - bgl.glLineWidth(1) - bgl.glDisable(bgl.GL_BLEND) - # bgl.glColor4f(0.0, 0.0, 0.0, 1.0) # doesn't exist anymore .. + drawing.set_line_width(1) + drawing.disable_blendmode() def get_xy_from_data(data): @@ -189,12 +188,10 @@ def draw_callback_px(n_id, data): ''' x, y = get_xy_from_data(data) - # bgl.glEnable(bgl.GL_DEPTH_TEST) drawing_func = data.get('custom_function') args = data.get('args', (None,)) drawing_func(bpy.context, args, (x, y)) restore_opengl_defaults() - # bgl.glDisable(bgl.GL_DEPTH_TEST) def _draw_text_handler(tree_id, node_id, text: str, color=(1, 1, 1, 1), scale=1.0, align='RIGHT', @@ -235,7 +232,7 @@ def _draw_text_handler(tree_id, node_id, text: str, color=(1, 1, 1, 1), scale=1. font_id = 0 dpi = 72 - blf.size(font_id, text_height, dpi) + drawing.blf_size(font_id, text_height, dpi) blf.color(font_id, *color) for line in text.split('\n'): @@ -245,7 +242,7 @@ def _draw_text_handler(tree_id, node_id, text: str, color=(1, 1, 1, 1), scale=1. def _get_text_location(node, align='RIGHT') -> tuple[int, int]: - """Find location for a text nearby give node""" + """Find location for a text nearby given node""" (x, y) = node.absolute_location gap = 10 diff --git a/utils/modules/drawing_abstractions.py b/utils/modules/drawing_abstractions.py new file mode 100644 index 0000000000..717adfca42 --- /dev/null +++ b/utils/modules/drawing_abstractions.py @@ -0,0 +1,136 @@ +# This file is part of project Sverchok. It's copyrighted by the contributors +# recorded in the version control history of the file, available from +# its original location https://github.com/nortikin/sverchok/commit/master +# +# SPDX-License-Identifier: GPL3 +# License-Filename: LICENSE + + +# from sverchok.utils.modules.drawing_abstractions import drawing + +import bpy +import gpu +import blf +from typing import NamedTuple + + +class shading_3d(NamedTuple): + if bpy.app.version <= (3, 4): + UNIFORM_COLOR = "3D_UNIFORM_COLOR" + SMOOTH_COLOR = "3D_SMOOTH_COLOR" + else: + UNIFORM_COLOR = "UNIFORM_COLOR" + SMOOTH_COLOR = "SMOOTH_COLOR" + +class shading_2d(NamedTuple): + if bpy.app.version <= (3, 4): + UNIFORM_COLOR = "2D_UNIFORM_COLOR" + SMOOTH_COLOR = "2D_SMOOTH_COLOR" + else: + UNIFORM_COLOR = "UNIFORM_COLOR" + SMOOTH_COLOR = "SMOOTH_COLOR" + + +""" +These classes encapsulate any and all sets of bgl instructions that Sverchok used prior to +the deprecation of the bgl module. It's not clear yet if the gpu module will achieve parity, +some Nodes might need considerable revision - here we attempt to make at least some viz nodes +draw in a useful way in B3.5 and up. Certain nodes that use elaborate buffer and texture +configurations will need additional attention and might even not reach parity for some time, +or indeed at all. Replacement techniques will be investigated. + +""" + +if bpy.app.version >= (3, 5, 0): + + def placeholder_function(*params): + return + + class Drawing: + + set_polygonmode_line = placeholder_function + + def set_polygonmode_fill(self): + gpu.state.face_culling_set("BACK") + gpu.state.front_facing_set(False) + + set_line_width = gpu.state.line_width_set + reset_line_width = lambda self: gpu.state.line_width_set(1) + set_point_size = gpu.state.point_size_set + reset_point_size = lambda self: gpu.state.point_size_set(1) + enable_polygon_offset_fill = lambda self: gpu.state.face_culling_set("BACK") + disable_polygon_offset_fill = placeholder_function + set_polygon_offset_amounts = placeholder_function + + enable_blendmode = lambda self: gpu.state.blend_set("ALPHA") + disable_blendmode = lambda self: gpu.state.blend_set("NONE") + enable_depth_test = lambda self: gpu.state.depth_test_set("LESS_EQUAL") + disable_depth_test = lambda self: gpu.state.depth_test_set("NONE") + + new_buffer_texture = placeholder_function + get_buffer = placeholder_function + new_buffer_texture_sized = lambda self, size, data: gpu.types.Buffer("FLOAT", size, data) + bind_texture_2d = placeholder_function + init_complex_texture = placeholder_function + generate_textures = placeholder_function + delete_texture = placeholder_function + + blf_size = lambda self, font_id, height, dpi: blf.size(font_id, height) + + +else: + + import bgl + + class Drawing: + + blf_size = blf.size + + set_polygonmode_line = lambda self: bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_LINE) + set_polygonmode_fill = lambda self: bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) + set_line_width = bgl.glLineWidth + reset_line_width = lambda self: bgl.glLineWidth(1) + set_point_size = bgl.glPointSize + reset_point_size = lambda self: bgl.glPointSize(1) + enable_polygon_offset_fill = lambda self: bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) + disable_polygon_offset_fill = lambda self: bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) + set_polygon_offset_amounts = lambda self: bgl.glPolygonOffset(1.0, 1.0) + + enable_blendmode = lambda self: bgl.glEnable(bgl.GL_BLEND) + disable_blendmode = lambda self: bgl.glDisable(bgl.GL_BLEND) + enable_depth_test = lambda self: bgl.glEnable(bgl.GL_DEPTH_TEST) + disable_depth_test = lambda self: bgl.glDisable(bgl.GL_DEPTH_TEST) + disable_texture_2d = lambda self: bgl.glDisable(bgl.GL_TEXTURE_2D) + + new_buffer_texture = lambda self: bgl.Buffer(bgl.GL_INT, 1) + get_buffer = lambda self, indexed_buffer: bgl.Buffer(bgl.GL_INT, 1, indexed_buffer) + new_buffer_texture_sized = lambda self, size, data: bgl.Buffer(bgl.GL_FLOAT, size, data) + bind_texture_2d = lambda self, texture: bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture) + delete_texture = lambda self, texture: bgl.glDeleteTextures(1, texture) + + def init_image_from_texture(self, width, height, texname, texture, format): + + if format in {bgl.GL_RGBA, bgl.GL_RGB, bgl.GL_RED}: + ... + else: + format = {'BW': bgl.GL_RED, 'RGB': bgl.GL_RGB, 'RGBA': bgl.GL_RGBA}.get(format) + + bgl.glPixelStorei(bgl.GL_UNPACK_ALIGNMENT, 1) + bgl.glEnable(bgl.GL_TEXTURE_2D) + bgl.glBindTexture(bgl.GL_TEXTURE_2D, texname) + bgl.glActiveTexture(bgl.GL_TEXTURE0) + bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) + bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) + bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) + bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) + bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, format, width, height, 0, format, bgl.GL_FLOAT, texture) + + def init_complex_texture(self, width, height, texname, data, format): + texture = self.new_buffer_texture_sized(bgl.GL_FLOAT, data.size, data.tolist()) + self.init_image_from_texture(width, height, texname, texture, format) + + + generate_textures = lambda self, name: bgl.glGenTextures(1, name) # returns an indexable item + + +drawing = Drawing() diff --git a/utils/nodes_mixins/console_mixin.py b/utils/nodes_mixins/console_mixin.py index 8a15bc2413..1d803d3e31 100644 --- a/utils/nodes_mixins/console_mixin.py +++ b/utils/nodes_mixins/console_mixin.py @@ -9,11 +9,11 @@ import bpy import numpy as np -import bgl, gpu +import gpu from gpu_extras.batch import batch_for_shader from sverchok.utils.sv_nodeview_draw_helper import get_console_grid, get_xy_for_bgl_drawing - +from sverchok.utils.modules.drawing_abstractions import drawing from sverchok.nodes.viz.console_node import ( simple_console_xy, terminal_text_to_uv, @@ -122,26 +122,17 @@ class LexMixin(): def init_texture(self, width, height): texname = self.texture_dict['texture'] data = self.texture_dict['texture_data'] - clr = bgl.GL_RGBA if not 'texture_buffer' in self.texture_dict: #print('initializing texture longform') - texture = bgl.Buffer(bgl.GL_FLOAT, data.size, data.tolist()) + texture = drawing.new_buffer_texture_sized(data.size, data.tolist()) self.texture_dict['texture_buffer'] = texture else: #print("reusing") texture = self.texture_dict['texture_buffer'] - bgl.glPixelStorei(bgl.GL_UNPACK_ALIGNMENT, 1) - bgl.glEnable(bgl.GL_TEXTURE_2D) - - bgl.glBindTexture(bgl.GL_TEXTURE_2D, texname) - bgl.glActiveTexture(bgl.GL_TEXTURE0) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) - bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, clr, width, height, 0, clr, bgl.GL_FLOAT, texture) + drawing.init_image_from_texture(self, width, height, texname, texture, 'RGBA') + def get_font_texture(self): if not self.texture_dict: @@ -153,10 +144,11 @@ def get_font_texture(self): dsize = data.size data = data.repeat(3).reshape(-1, 3) data = np.concatenate((data, np.ones(dsize)[:,None]),axis=1).flatten() - name = bgl.Buffer(bgl.GL_INT, 1) - bgl.glGenTextures(1, name) + name = drawing.new_buffer_texture() + drawing.generate_textures(name) + self.texture_dict['texture'] = name[0] - self.texture_dict['texture_data'] = data # bgl.Buffer(bgl.GL_FLOAT, data.size, data.tolist()) + self.texture_dict['texture_data'] = data def get_lexed_colors(self): return [(lex_name, getattr(self, lex_name)[:]) for lex_name in lexed_colors] diff --git a/utils/nodeview_time_graph_drawing.py b/utils/nodeview_time_graph_drawing.py index ff8307e3c9..e43747fa70 100644 --- a/utils/nodeview_time_graph_drawing.py +++ b/utils/nodeview_time_graph_drawing.py @@ -6,7 +6,7 @@ # License-Filename: LICENSE -import bgl +# import bgl import blf import bpy from mathutils import Vector @@ -16,7 +16,7 @@ import sverchok from sverchok.ui import bgl_callback_nodeview as nvBGL2 from sverchok.utils.modules.shader_utils import ShaderLib2D - +from sverchok.utils.modules.drawing_abstractions import drawing # https://github.com/nortikin/sverchok/commit/c0ef777acef561a5e9cd308ec05c1382b9006de8 @@ -110,7 +110,7 @@ def get_xy_for_bgl_drawing(node): _x, _y = _x, _y + (text_height - 9) return _x * location_theta, _y * location_theta - blf.size(font_id, int(text_height), 72) + drawing.blf_size(font_id, int(text_height), 72) blf.color(font_id, r, g, b, 1.0) for idx, node_data in data_tree.items(): node = node_tree.nodes.get(node_data['name']) @@ -151,7 +151,7 @@ def draw_overlay(*data): text_height = 10 line_height = 10 + 3 - blf.size(font_id, int(text_height), 72) + drawing.blf_size(font_id, int(text_height), 72) blf.color(font_id, r, g, b, 1.0) if not node_tree.sv_show_time_graph: diff --git a/utils/sv_batch_primitives.py b/utils/sv_batch_primitives.py index 109523373d..0ba9a7ca76 100644 --- a/utils/sv_batch_primitives.py +++ b/utils/sv_batch_primitives.py @@ -10,6 +10,8 @@ from gpu_extras.batch import batch_for_shader from mathutils import Matrix, Vector from sverchok.utils.sv_logging import sv_logger +from sverchok.utils.modules.drawing_abstractions import shading_3d + if bpy.app.background: print("Will not initialize shaders in the background mode") @@ -19,10 +21,8 @@ def draw_matrix(self, *args, **kwargs): sv_logger.info("draw_matrix: do nothing in background mode") else: - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}UNIFORM_COLOR' - uniform_shader = gpu.shader.from_builtin(shader_name) - shader_name = f'{"3D_" if bpy.app.version < (3, 4) else ""}SMOOTH_COLOR' - smooth_shader = gpu.shader.from_builtin(shader_name) + uniform_shader = gpu.shader.from_builtin(shading_3d.UNIFORM_COLOR) + smooth_shader = gpu.shader.from_builtin(shading_3d.SMOOTH_COLOR) class MatrixDraw28(object): diff --git a/utils/sv_idx_viewer28_draw.py b/utils/sv_idx_viewer28_draw.py index 14266dee17..e57532d2fb 100644 --- a/utils/sv_idx_viewer28_draw.py +++ b/utils/sv_idx_viewer28_draw.py @@ -11,7 +11,6 @@ import bpy import blf -import bgl import gpu from gpu_extras.batch import batch_for_shader @@ -20,6 +19,8 @@ from mathutils.bvhtree import BVHTree from sverchok.data_structure import Vector_generate +from sverchok.utils.modules.drawing_abstractions import drawing + SpaceView3D = bpy.types.SpaceView3D callback_dict = {} @@ -85,7 +86,7 @@ def draw_indices_2D(context, args): font_id = 0 text_height = int(13.0 * scale) - blf.size(font_id, text_height, 72) # should check prefs.dpi + drawing.blf_size(font_id, text_height, 72) # should check prefs.dpi region_mid_width = region.width / 2.0 region_mid_height = region.height / 2.0 @@ -249,7 +250,7 @@ def draw_indices_2D_wbg(context, args): font_id = 0 text_height = int(13.0 * scale) - blf.size(font_id, text_height, 72) # should check prefs.dpi + drawing.blf_size(font_id, text_height, 72) # should check prefs.dpi region_mid_width = region.width / 2.0 region_mid_height = region.height / 2.0 diff --git a/utils/sv_stethoscope_helper.py b/utils/sv_stethoscope_helper.py index 5dfcc8bc3a..a695ac2106 100644 --- a/utils/sv_stethoscope_helper.py +++ b/utils/sv_stethoscope_helper.py @@ -9,6 +9,7 @@ import blf import bpy +from sverchok.utils.modules.drawing_abstractions import drawing def get_sane_xy(data): return_value = (120, 120) @@ -37,7 +38,7 @@ def draw_text_data(data): text_height = 15 * scale line_height = 14 * scale - blf.size(font_id, int(text_height), 72) + drawing.blf_size(font_id, int(text_height), 72) blf.color(font_id, r, g, b, 1.0) ypos = y @@ -58,11 +59,11 @@ def draw_graphical_data(data): if not lines: return - blf.size(font_id, int(text_height), 72) + drawing.blf_size(font_id, int(text_height), 72) def draw_text(color, xpos, ypos, line): r, g, b = color - blf.color(font_id, r, g, b, 1.0) # bgl.glColor3f(*color) + blf.color(font_id, r, g, b, 1.0) blf.position(0, xpos, ypos, 0) blf.draw(font_id, line) return blf.dimensions(font_id, line) diff --git a/utils/sv_texture_utils.py b/utils/sv_texture_utils.py index b31006179e..12bb5377ae 100644 --- a/utils/sv_texture_utils.py +++ b/utils/sv_texture_utils.py @@ -5,10 +5,9 @@ # SPDX-License-Identifier: GPL3 # License-Filename: LICENSE -import bgl import gpu from gpu_extras.batch import batch_for_shader - +from sverchok.utils.modules.drawing_abstractions import drawing tx_vertex_shader = ''' uniform mat4 viewProjectionMatrix; @@ -49,23 +48,7 @@ ''' def init_texture(width, height, texname, texture, clr): - # function to init the texture - bgl.glPixelStorei(bgl.GL_UNPACK_ALIGNMENT, 1) - - bgl.glEnable(bgl.GL_TEXTURE_2D) - bgl.glBindTexture(bgl.GL_TEXTURE_2D, texname) - bgl.glActiveTexture(bgl.GL_TEXTURE0) - - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) - bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) - - bgl.glTexImage2D( - bgl.GL_TEXTURE_2D, - 0, clr, width, height, - 0, clr, bgl.GL_FLOAT, texture - ) + drawing.init_image_from_texture(width, height, texname, texture, clr) def simple_screen(context, args, xy): @@ -79,10 +62,9 @@ def draw_texture(x=0, y=0, w=30, h=10, texname=texname, c=cMod): # function to draw a texture matrix = gpu.matrix.get_projection_matrix() - bgl.glDisable(bgl.GL_DEPTH_TEST) - - act_tex = bgl.Buffer(bgl.GL_INT, 1) - bgl.glBindTexture(bgl.GL_TEXTURE_2D, texname) + drawing.disable_depth_test() + act_tex = drawing.new_buffer_texture() + drawing.bind_texture_2d(texname) shader.bind() shader.uniform_int("image", act_tex) @@ -93,8 +75,8 @@ def draw_texture(x=0, y=0, w=30, h=10, texname=texname, c=cMod): batch.draw(shader) # restoring settings - bgl.glBindTexture(bgl.GL_TEXTURE_2D, act_tex[0]) - bgl.glDisable(bgl.GL_TEXTURE_2D) + drawing.bind_texture_2d(act_tex[0]) + drawing.disable_texture_2d() draw_texture(x=x, y=y, w=width, h=height, texname=texname, c=cMod)