From 3c978a73d1ffc287263b371a6a7e9b38cb169f45 Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Sun, 2 May 2021 16:30:23 -0300 Subject: [PATCH] Docs: PyAPI: Replace use of the bgl module in the RenderEngine example The bgl module will be discontinued. This example is seen at: https://docs.blender.org/api/current/bpy.types.RenderEngine.html uses it. Differential Revision: https://developer.blender.org/D11262 --- .../examples/bpy.types.RenderEngine.py | 72 ++++--------------- 1 file changed, 13 insertions(+), 59 deletions(-) diff --git a/doc/python_api/examples/bpy.types.RenderEngine.py b/doc/python_api/examples/bpy.types.RenderEngine.py index 45910194244..0b8795340ad 100644 --- a/doc/python_api/examples/bpy.types.RenderEngine.py +++ b/doc/python_api/examples/bpy.types.RenderEngine.py @@ -4,7 +4,9 @@ Simple Render Engine """ import bpy -import bgl +import array +import gpu +from gpu_extras.presets import draw_texture_2d class CustomRenderEngine(bpy.types.RenderEngine): @@ -100,8 +102,7 @@ class CustomRenderEngine(bpy.types.RenderEngine): dimensions = region.width, region.height # Bind shader that converts from scene linear to display space, - bgl.glEnable(bgl.GL_BLEND) - bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE_MINUS_SRC_ALPHA) + gpu.state.blend_set('ALPHA_PREMULT') self.bind_display_space_shader(scene) if not self.draw_data or self.draw_data.dimensions != dimensions: @@ -110,7 +111,7 @@ class CustomRenderEngine(bpy.types.RenderEngine): self.draw_data.draw() self.unbind_display_space_shader() - bgl.glDisable(bgl.GL_BLEND) + gpu.state.blend_set('NONE') class CustomDrawData: @@ -119,68 +120,21 @@ class CustomDrawData: self.dimensions = dimensions width, height = dimensions - pixels = [0.1, 0.2, 0.1, 1.0] * width * height - pixels = bgl.Buffer(bgl.GL_FLOAT, width * height * 4, pixels) + pixels = width * height * array.array('f', [0.1, 0.2, 0.1, 1.0]) + pixels = gpu.types.Buffer('FLOAT', width * height * 4, pixels) # Generate texture - self.texture = bgl.Buffer(bgl.GL_INT, 1) - bgl.glGenTextures(1, self.texture) - bgl.glActiveTexture(bgl.GL_TEXTURE0) - bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) - bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels) - bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) - bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) - bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) + self.texture = gpu.types.GPUTexture((width, height), format='RGBA16F', data=pixels) - # Bind shader that converts from scene linear to display space, - # use the scene's color management settings. - shader_program = bgl.Buffer(bgl.GL_INT, 1) - bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program) - - # Generate vertex array - self.vertex_array = bgl.Buffer(bgl.GL_INT, 1) - bgl.glGenVertexArrays(1, self.vertex_array) - bgl.glBindVertexArray(self.vertex_array[0]) - - texturecoord_location = bgl.glGetAttribLocation(shader_program[0], "texCoord") - position_location = bgl.glGetAttribLocation(shader_program[0], "pos") - - bgl.glEnableVertexAttribArray(texturecoord_location) - bgl.glEnableVertexAttribArray(position_location) - - # Generate geometry buffers for drawing textured quad - position = [0.0, 0.0, width, 0.0, width, height, 0.0, height] - position = bgl.Buffer(bgl.GL_FLOAT, len(position), position) - texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0] - texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord) - - self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2) - - bgl.glGenBuffers(2, self.vertex_buffer) - bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0]) - bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW) - bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) - - bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1]) - bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW) - bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) - - bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0) - bgl.glBindVertexArray(0) + # Note: This is just a didactic example. + # In this case it would be more convenient to fill the texture with: + # self.texture.clear('FLOAT', value=[0.1, 0.2, 0.1, 1.0]) def __del__(self): - bgl.glDeleteBuffers(2, self.vertex_buffer) - bgl.glDeleteVertexArrays(1, self.vertex_array) - bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) - bgl.glDeleteTextures(1, self.texture) + del self.texture def draw(self): - bgl.glActiveTexture(bgl.GL_TEXTURE0) - bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) - bgl.glBindVertexArray(self.vertex_array[0]) - bgl.glDrawArrays(bgl.GL_TRIANGLE_FAN, 0, 4) - bgl.glBindVertexArray(0) - bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) + draw_texture_2d(self.texture, (0, 0), self.texture.width, self.texture.height) # RenderEngines also need to tell UI Panels that they are compatible with.