def modal(self, context, event): if not context.area or not context.region: return {'PASS_THROUGH'} namespace['data_timer'][0] = math.sin(namespace['timer'].time_duration) namespace['data_timer'][1] = math.sin( namespace['timer'].time_duration) * 2 bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, namespace['vbo_point'][0]) # Inserts data from the timer into the Y and Z coordinates of the 3rd vertex, starting at index 7, its Y coordinate. index = 7 float_byte_count = 4 insertion_size = float_byte_count * 2 # 2 floats are being edited right now: Y position and Z position. bgl.glBufferSubData(bgl.GL_ARRAY_BUFFER, index * float_byte_count, insertion_size, namespace['data_timer']) if event.type == 'ESC' and event.value == 'PRESS': context.area.tag_redraw() context.window_manager.event_timer_remove(namespace['timer']) draw_handler_remove() return {'FINISHED'} namespace[ 'projection_matrix'][:] = context.region_data.perspective_matrix context.area.tag_redraw() return {'RUNNING_MODAL'}
def get_tri_co(self, index): bgl.glBindVertexArray(self.vao[0]) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tris[0]) bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 36, 36, self.tri_co) bgl.glBindVertexArray(0) return self.tri_co
def get_loosevert_co(self, index): bgl.glBindVertexArray(self.vao[0]) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_verts[0]) bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 12, 12, self.vert_co) bgl.glBindVertexArray(0) return self.vert_co
def get_edge_co(self, index): bgl.glBindVertexArray(self.vao[0]) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edges[0]) bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 24, 24, self.edge_co) bgl.glBindVertexArray(0) return self.edge_co
def draw_callback_px(self, context): kha_SystemImpl.frame() bgl.glUseProgram(0) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0) bgl.glBindBuffer(bgl.GL_ELEMENT_ARRAY_BUFFER, 0) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_ALWAYS)
def vertexAttribPointer(self, vbo, varName, size, gltype, normalized=bgl.GL_FALSE, stride=0, buf=buf_zero, enable=True): assert varName in self.shaderVars, 'Variable %s not found' % varName v = self.shaderVars[varName] q, l, t = v['qualifier'], v['location'], v['type'] if l == -1: if not v['reported']: print('COULD NOT FIND %s' % (varName)) v['reported'] = True return if DEBUG_PRINT: print( 'assign (enable=%s) vertattrib pointer: %s (%s,%d,%s) = %d (%dx%s,normalized=%s,stride=%d)' % (str(enable), varName, q, l, t, vbo, size, self.gltype_names[gltype], str(normalized), stride)) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, vbo) bgl.glVertexAttribPointer(l, size, gltype, normalized, stride, buf) if self.checkErrors: self.drawing.glCheckError('vertexAttribPointer %s' % varName) if enable: bgl.glEnableVertexAttribArray(l) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
def bind(self, co, buf_id, offset): bgl.glUniform1f(self.unif_offset, float(offset)) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, co[0]) bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf) bgl.glEnableVertexAttribArray(self.attr_pos) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, buf_id[0]) bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf) bgl.glEnableVertexAttribArray(self.attr_primitive_id)
def get_bounds_co(self, index): bgl.glBindVertexArray(self.vao[0]) check_error("glBindVertexArray(self.vao[0])") bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_bounds_co[0]) check_error("glBindBuffer") bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 12, 12, self.bounds_co) check_error("glGetBufferSubData") bgl.glBindVertexArray(0) check_error("glBindVertexArray(0)") return self.bounds_co
def draw_callback_view(verts, faces): VERT = 0 FACES = 1 buffers = bgl.Buffer(bgl.GL_INT, [2]) bgl.glGenBuffers(2, buffers) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, buffers[VERT]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 4 * 3 * len(verts), verts, bgl.GL_STATIC_DRAW) bgl.glVertexPointer(3, bgl.GL_FLOAT, 0, verts) bgl.glColor3f(1.0, 0, 0) bgl.glEnableClientState(bgl.GL_VERTEX_ARRAY) bgl.glDrawArrays(bgl.GL_POINTS, 0, len(verts)) bgl.glDisableClientState(bgl.GL_VERTEX_ARRAY)
def prepare_viewport(self, ctx, dg): width, height = ctx.region.width, ctx.region.height self.dim = (width, height) buf = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetIntegerv(bgl.GL_DRAW_FRAMEBUFFER_BINDING, buf) fb = buf[0] self.genfb(width, height) bgl.glGenVertexArrays(1, buf) self.vao = buf[0] bgl.glBindVertexArray(self.vao) quad = bgl.Buffer(bgl.GL_FLOAT, 2 * 4, [0, 0, width, 0, width, height, 0, height]) uv = bgl.Buffer(bgl.GL_FLOAT, 2 * 4, [0, 0, 1, 0, 1, 1, 0, 1]) self.bind_display_space_shader(dg.scene) bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, buf) self.unbind_display_space_shader() self.quad_in = bgl.glGetAttribLocation(buf[0], "pos") self.uv_in = bgl.glGetAttribLocation(buf[0], "texCoord") bgl.glEnableVertexAttribArray(self.quad_in) bgl.glEnableVertexAttribArray(self.uv_in) self.vtx_buf = bgl.Buffer(bgl.GL_INT, 2) bgl.glGenBuffers(2, self.vtx_buf) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vtx_buf[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 2 * 4 * 4, quad, bgl.GL_STATIC_DRAW) bgl.glVertexAttribPointer(self.quad_in, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vtx_buf[1]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 2 * 4 * 4, uv, bgl.GL_STATIC_DRAW) bgl.glVertexAttribPointer(self.uv_in, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glDisableVertexAttribArray(self.quad_in) bgl.glDisableVertexAttribArray(self.uv_in) bgl.glBindFramebuffer(bgl.GL_FRAMEBUFFER, fb)
def vertexAttribPointer(self, vbo, varName, size, gltype, normalized=bgl.GL_FALSE, stride=0, buf=buf_zero, enable=True): assert varName in self.shaderVars, 'Variable %s not found' % varName v = self.shaderVars[varName] q,l,t = v['qualifier'],v['location'],v['type'] if l == -1: if not v['reported']: print('COULD NOT FIND %s' % (varName)) v['reported'] = True return if DEBUG_PRINT: print('assign (enable=%s) vertattrib pointer: %s (%s,%d,%s) = %d (%dx%s,normalized=%s,stride=%d)' % (str(enable), varName, q, l, t, vbo, size, self.gltype_names[gltype], str(normalized),stride)) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, vbo) bgl.glVertexAttribPointer(l, size, gltype, normalized, stride, buf) if self.checkErrors: self.drawing.glCheckError('vertexAttribPointer %s' % varName) if enable: bgl.glEnableVertexAttribArray(l) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
def draw(self, opts): if self.count == 0: return if self.gltype == bgl.GL_LINES: if opts.get('line width', 1.0) <= 0: return elif self.gltype == bgl.GL_POINTS: if opts.get('point size', 1.0) <= 0: return nosel = opts.get('no selection', False) mx, my, mz = opts.get('mirror x', False), opts.get( 'mirror y', False), opts.get('mirror z', False) focus = opts.get('focus mult', 1.0) bmeshShader.assign('focus_mult', focus) bmeshShader.assign('use_selection', 0.0 if nosel else 1.0) bmeshShader.assign('cull_backfaces', 1.0 if opts.get('cull backfaces', False) else 0.0) bmeshShader.assign('alpha_backface', opts.get('alpha backface', 0.5)) bmeshShader.assign('normal_offset', opts.get('normal offset', 0.0)) bmeshShader.assign('constrain_offset', 1.0 if opts.get('constrain offset', True) else 0.0) bmeshShader.vertexAttribPointer( self.vbo_pos, 'vert_pos', 3, bgl.GL_FLOAT, buf=buf_zero) self._check_error('draw: vertex attrib array pos') bmeshShader.vertexAttribPointer( self.vbo_norm, 'vert_norm', 3, bgl.GL_FLOAT, buf=buf_zero) self._check_error('draw: vertex attrib array norm') bmeshShader.vertexAttribPointer( self.vbo_sel, 'selected', 1, bgl.GL_FLOAT, buf=buf_zero) self._check_error('draw: vertex attrib array sel') bgl.glBindBuffer(bgl.GL_ELEMENT_ARRAY_BUFFER, self.vbo_idx) self._check_error('draw: element array buffer idx') glSetOptions(self.options_prefix, opts) self._draw(1, 1, 1) if mx or my or mz: glSetOptions('%s mirror' % self.options_prefix, opts) if mx: self._draw(-1, 1, 1) if my: self._draw(1, -1, 1) if mz: self._draw(1, 1, -1) if mx and my: self._draw(-1, -1, 1) if mx and mz: self._draw(-1, 1, -1) if my and mz: self._draw(1, -1, -1) if mx and my and mz: self._draw(-1, -1, -1) bmeshShader.disableVertexAttribArray('vert_pos') bmeshShader.disableVertexAttribArray('vert_norm') bmeshShader.disableVertexAttribArray('selected') bgl.glBindBuffer(bgl.GL_ELEMENT_ARRAY_BUFFER, 0) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
def __init__(self, dimensions): # Generate dummy float image buffer self.dimensions = dimensions width, height = dimensions pixels = [1, 0.2, 0.1, 1.0] * width * height pixels = bgl.Buffer(bgl.GL_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) # 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)
def __init__(self, dimensions): # Generate dummy float image buffer 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) # 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) # 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)
def _init_opengl(self, engine, scene): # Create texture self.texture = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, self.texture) self.texture_id = self.texture[0] # Bind shader that converts from scene linear to display space, # use the scene's color management settings. engine.bind_display_space_shader(scene) 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 width = self._width * self._pixel_size height = self._height * self._pixel_size position = [ self._offset_x, self._offset_y, self._offset_x + width, self._offset_y, self._offset_x + width, self._offset_y + height, self._offset_x, self._offset_y + 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, NULL) bgl.glBindVertexArray(NULL) engine.unbind_display_space_shader()
def __init__(self, dimensions): 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) 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.glBindTexture(bgl.GL_TEXTURE_2D, 0) shader_program = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program) 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) 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)
def init_opengl(self, engine, scene): self.texture = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, self.texture) self.generate_texture() engine.bind_display_space_shader(scene) shader_program = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program) 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) position = [ 0.0, 0.0, self.width, 0.0, self.width, self.height, 0.0, self.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) engine.unbind_display_space_shader()
def _draw_texture(texture_id, x, y, width, height): # INITIALIZATION # Getting shader program shader_program = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program) # Generate vertex array vertex_array = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenVertexArrays(1, vertex_array) texturecoord_location = bgl.glGetAttribLocation( shader_program[0], "texCoord") position_location = bgl.glGetAttribLocation(shader_program[0], "pos") # Generate geometry buffers for drawing textured quad position = [x, y, x + width, y, x + width, y + height, x, y + 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) vertex_buffer = bgl.Buffer(bgl.GL_INT, 2) bgl.glGenBuffers(2, vertex_buffer) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, vertex_buffer[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, vertex_buffer[1]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0) # DRAWING bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture_id) bgl.glBindVertexArray(vertex_array[0]) bgl.glEnableVertexAttribArray(texturecoord_location) bgl.glEnableVertexAttribArray(position_location) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, vertex_buffer[0]) bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, vertex_buffer[1]) bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0) bgl.glDrawArrays(bgl.GL_TRIANGLE_FAN, 0, 4) bgl.glBindVertexArray(0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # DELETING bgl.glDeleteBuffers(2, vertex_buffer) bgl.glDeleteVertexArrays(1, vertex_array)
def buffer(self, pos, norm, sel, idx): sizeOfFloat, sizeOfInt = 4, 4 self.count = 0 count = len(pos) counts = list(map(len, [pos, norm, sel])) goodcounts = all(c == count for c in counts) assert goodcounts, ('All arrays must contain ' 'the same number of elements %s' % str(counts)) if count == 0: return try: buf_pos = bgl.Buffer(bgl.GL_FLOAT, [count, 3], pos) buf_norm = bgl.Buffer(bgl.GL_FLOAT, [count, 3], norm) buf_sel = bgl.Buffer(bgl.GL_FLOAT, count, sel) if idx: # WHY NO GL_UNSIGNED_INT????? buf_idx = bgl.Buffer(bgl.GL_INT, count, idx) if self.DEBUG_PRINT: print('buf_pos = ' + shorten_floats(str(buf_pos))) print('buf_norm = ' + shorten_floats(str(buf_norm))) except Exception as e: print( 'ERROR (buffer): caught exception while ' 'buffering to Buffer ' + str(e)) raise e try: bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_pos) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, count * 3 * sizeOfFloat, buf_pos, bgl.GL_STATIC_DRAW) self._check_error('buffer: vbo_pos') bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_norm) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, count * 3 * sizeOfFloat, buf_norm, bgl.GL_STATIC_DRAW) self._check_error('buffer: vbo_norm') bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_sel) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, count * 1 * sizeOfFloat, buf_sel, bgl.GL_STATIC_DRAW) self._check_error('buffer: vbo_sel') if idx: bgl.glBindBuffer(bgl.GL_ELEMENT_ARRAY_BUFFER, self.vbo_idx) bgl.glBufferData(bgl.GL_ELEMENT_ARRAY_BUFFER, count * sizeOfInt, buf_idx, bgl.GL_STATIC_DRAW) self._check_error('buffer: vbo_idx') except Exception as e: print( 'ERROR (buffer): caught exception while ' 'buffering from Buffer ' + str(e)) raise e finally: bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0) bgl.glBindBuffer(bgl.GL_ELEMENT_ARRAY_BUFFER, 0) del buf_pos, buf_norm, buf_sel if idx: del buf_idx if idx: self.count = len(idx) self.render_indices = True else: self.count = len(pos) self.render_indices = False
def Draw(self, index_offset): self.first_index = index_offset bgl.glUseProgram(self.shader.program) bgl.glBindVertexArray(self.vao[0]) bgl.glUniformMatrix4fv(self.unif_MV, 1, bgl.GL_TRUE, self.MV) bgl.glUniformMatrix4fv(self.unif_MVP, 1, bgl.GL_TRUE, self.MVP) if self.draw_tris: bgl.glUniform1f(self.unif_offset, float(index_offset)) # bgl has no glUniform1ui :\ bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tris[0]) bgl.glEnableVertexAttribArray(self.attr_pos) bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tri_indices[0]) bgl.glEnableVertexAttribArray(self.attr_primitive_id) bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glDrawArrays(bgl.GL_TRIANGLES, 0, self.num_tris * 3) index_offset += self.num_tris bgl.glDepthRange(-0.00005, 0.99995) if self.draw_edges: bgl.glUniform1f(self.unif_offset, float(index_offset)) #TODO: use glUniform1ui bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edges[0]) bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_pos) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edge_indices[0]) bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_primitive_id) bgl.glDrawArrays(bgl.GL_LINES, 0, self.num_edges * 2) index_offset += self.num_edges if self.draw_verts: bgl.glUniform1f(self.unif_offset, float(index_offset)) #TODO: use glUniform1ui bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_verts[0]) bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_pos) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_looseverts_indices[0]) bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_primitive_id) bgl.glDrawArrays(bgl.GL_POINTS, 0, self.num_verts) bgl.glDepthRange(0.0, 1.0)
def _restore_shader_state(cls): bgl.glUseProgram(cls.cur_program[0]) bgl.glBindVertexArray(cls.cur_vao[0]) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, cls.cur_vbo[0]) bgl.glBindBuffer(bgl.GL_ELEMENT_ARRAY_BUFFER, cls.cur_ebo[0])
def draw_view(): ###################### ######## BIND ######## ###################### first_time = not bgl.glIsVertexArray(namespace['vao'][0]) if first_time: namespace['uniform_set'] = False # Unlike VBOs, a VAO has to be generated and deleted from within the draw callback in which it will be bound. bgl.glGenVertexArrays(1, namespace['vao']) bgl.glBindVertexArray(namespace['vao'][0]) float_byte_count = 4 # Attribute: "point", 3D float vector bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, namespace['vbo_point'][0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, len(namespace['data_point']) * float_byte_count, namespace['data_point'], bgl.GL_DYNAMIC_DRAW) bgl.glVertexAttribPointer(0, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glEnableVertexAttribArray(0) # Attribute: "color", 4D float vector bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, namespace['vbo_color'][0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, len(namespace['data_color']) * float_byte_count, namespace['data_color'], bgl.GL_DYNAMIC_DRAW) bgl.glVertexAttribPointer(1, 4, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glEnableVertexAttribArray(1) bgl.glBindVertexArray(0) ###################### ######## DRAW ######## ###################### bgl.glEnable(bgl.GL_BLEND) bgl.glUseProgram(namespace['shader_program']) if not namespace['uniform_set']: bgl.glUniformMatrix4fv( namespace['perspective_uniform_location'], 1, bgl. GL_TRUE, # Matrices in Blender are row-major while matrices in OpenGL are column-major, so Blender's perspective matrix has to be transposed for OpenGL. namespace['projection_matrix']) # In this case I only want to update the uniform once, even though namespace['projection_matrix'] is being updated constantly. namespace['uniform_set'] = True bgl.glBindVertexArray(namespace['vao'][0]) bgl.glDrawArrays(bgl.GL_TRIANGLES, 0, 3) bgl.glUseProgram(0) bgl.glBindVertexArray(0) bgl.glDisable(bgl.GL_BLEND)
def try_initialize(self): if self.initialized: return self.initialized = True resx, resy = self.resx, self.resy width, height = self.dimensions self.pixels = bgl.Buffer(bgl.GL_FLOAT, resx * resy * 3, self.pixels_np) # 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_RGB16F, resx, resy, 0, bgl.GL_RGB, bgl.GL_FLOAT, self.pixels) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # 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)
def __init__(self, dimensions): global urhoImage # Generate dummy float image buffer self.dimensions = dimensions width, height = dimensions print("NEW CUSTOMDRAWDATA with resolution %s:%s" % (width, height)) blank = Image.new('RGBA', (width, height), (100, 100, 100, 255)) blank.alpha_composite(urhoImage, dest=(int(width / 2 - urhoImage.width / 2), int(height / 2 - urhoImage.height / 2))) #self.pixels = [255,0,0,255] * width * height self.pixels = list(blank.tobytes()) self.pixels = bgl.Buffer(bgl.GL_BYTE, width * height * 4, self.pixels) # Generate texture self.updateTextureOnDraw = False 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_RGBA, width, height, 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, self.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) # 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, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.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)
def __init__(self, viewport_dimensions, image_dimensions, pixels): self.log = logging.getLogger('blospray') self.log.info('CustomDrawData.__init__(viewport_dimensions=%s, image_dimensions=%s, fbpixels=%s) [%s]' % \ (viewport_dimensions, image_dimensions, pixels.shape, self)) viewport_width, viewport_height = self.viewport_dimensions = viewport_dimensions image_width, image_height = self.image_dimensions = image_dimensions assert pixels is not None pixels = bgl.Buffer(bgl.GL_FLOAT, image_width * image_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, image_width, image_height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # 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, viewport_width, 0.0, viewport_width, viewport_height, 0.0, viewport_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)
def __init__(self, obj): self._NULL = VoidBufValue(0) self.MVP = bgl.Buffer(bgl.GL_FLOAT, (4, 4)) # self.obj = obj self.first_index = 0 self.vbo_segs = None self.vbo_segs_co = None self.vbo_origin = None self.vbo_origin_co = None self.num_segs = 0 self.num_origins = 0 self._draw_segs = False self._draw_origins = False self.is_mesh = False self.snap_mode = 0 self.vao = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenVertexArrays(1, self.vao) bgl.glBindVertexArray(self.vao[0]) _arrays = _Object_Arrays(obj) if _arrays.segs_co: self.vbo_segs_co = bgl.Buffer(bgl.GL_INT, 1) self.num_segs = len(_arrays.segs_co) bgl.glGenBuffers(1, self.vbo_segs_co) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_segs_co[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_segs * 24, _arrays.segs_co, bgl.GL_STATIC_DRAW) segs_indices = np.repeat(np.arange(self.num_segs, dtype='f4'), 2) self.vbo_segs = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_segs) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_segs[0]) bgl.glBufferData( bgl.GL_ARRAY_BUFFER, self.num_segs * 8, np_array_as_bgl_Buffer(segs_indices), bgl.GL_STATIC_DRAW ) del segs_indices self._draw_segs = True # objects origin if _arrays.origin_co: self.vbo_origin_co = bgl.Buffer(bgl.GL_INT, 1) self.num_origins = len(_arrays.origin_co) bgl.glGenBuffers(1, self.vbo_origin_co) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_origin_co[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_origins * 12, _arrays.origin_co, bgl.GL_STATIC_DRAW) orig_indices = np.arange(self.num_origins, dtype='f4') self.vbo_origin = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_origin) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_origin[0]) bgl.glBufferData( bgl.GL_ARRAY_BUFFER, self.num_origins * 4, np_array_as_bgl_Buffer(orig_indices), bgl.GL_STATIC_DRAW ) del orig_indices self.is_mesh = _arrays.is_mesh self._draw_origins = True del _arrays bgl.glBindVertexArray(0)
def __init__(self, scene, dimensions, perspective, blocksize): print('redraw!') # Generate dummy float image buffer self.dimensions = dimensions self.perspective = perspective width, height = dimensions resx, resy = scene.res if blocksize != 0: resx //= blocksize resy //= blocksize pixels = np.empty(resx * resy * 3, np.float32) scene._fast_export_image(pixels, blocksize) self.pixels = bgl.Buffer(bgl.GL_FLOAT, resx * resy * 3, 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_RGB16F, resx, resy, 0, bgl.GL_RGB, bgl.GL_FLOAT, self.pixels) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # 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)
def __init__(self, obj, draw_tris, draw_edges, draw_verts): GPU_Indices_Mesh.init_opengl() self.obj = obj self.draw_tris = draw_tris self.draw_edges = draw_edges self.draw_verts = draw_verts self.vbo = None self.vbo_tris = None self.vbo_edges = None self.vbo_verts = None ## Create VAO ## self.vao = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenVertexArrays(1, self.vao) bgl.glBindVertexArray(self.vao[0]) ## Init Array ## mesh_arrays = _Mesh_Arrays(obj, draw_tris, draw_edges, draw_verts) ## Create VBO for vertices ## if mesh_arrays.verts_co is None: self.draw_tris = False self.draw_edges = False self.draw_verts = False return if False: # Blender 2.8 self.vbo_len = len(mesh_arrays.verts_co) self.vbo = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo[0]) verts_co = bgl.Buffer(bgl.GL_FLOAT, mesh_arrays.verts_co.shape, mesh_arrays.verts_co) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.vbo_len * 12, verts_co, bgl.GL_STATIC_DRAW) ## Create VBO for Tris ## if mesh_arrays.tri_verts is not None: self.tri_verts = mesh_arrays.tri_verts self.num_tris = len(self.tri_verts) np_tris_co = mesh_arrays.verts_co[mesh_arrays.tri_verts] np_tris_co = bgl.Buffer(bgl.GL_FLOAT, np_tris_co.shape, np_tris_co) self.vbo_tris = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_tris) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tris[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_tris * 36, np_tris_co, bgl.GL_STATIC_DRAW) del np_tris_co tri_indices = np.repeat(np.arange(self.num_tris, dtype = 'f4'), 3) tri_indices = bgl.Buffer(bgl.GL_FLOAT, tri_indices.shape, tri_indices) self.vbo_tri_indices = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_tri_indices) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tri_indices[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_tris * 12, tri_indices, bgl.GL_STATIC_DRAW) del tri_indices else: self.num_tris = 0 self.draw_tris = False ## Create VBO for Edges ## if mesh_arrays.edge_verts is not None: self.edge_verts = mesh_arrays.edge_verts self.num_edges = len(self.edge_verts) np_edges_co = mesh_arrays.verts_co[mesh_arrays.edge_verts] np_edges_co = bgl.Buffer(bgl.GL_FLOAT, np_edges_co.shape, np_edges_co) self.vbo_edges = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_edges) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edges[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_edges * 24, np_edges_co, bgl.GL_STATIC_DRAW) del np_edges_co edge_indices = np.repeat(np.arange(self.num_edges, dtype = 'f4'), 2) edge_indices = bgl.Buffer(bgl.GL_FLOAT, edge_indices.shape, edge_indices) self.vbo_edge_indices = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_edge_indices) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edge_indices[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_edges * 8, edge_indices, bgl.GL_STATIC_DRAW) del edge_indices else: self.num_edges = 0 self.draw_edges = False ## Create EBO for Loose Verts ## if mesh_arrays.looseverts is not None: self.looseverts = mesh_arrays.looseverts self.num_verts = len(mesh_arrays.looseverts) np_lverts_co = mesh_arrays.verts_co[mesh_arrays.looseverts] np_lverts_co = bgl.Buffer(bgl.GL_FLOAT, np_lverts_co.shape, np_lverts_co) self.vbo_verts = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_verts) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_verts[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_verts * 12, np_lverts_co, bgl.GL_STATIC_DRAW) del np_lverts_co looseverts_indices = np.arange(self.num_verts, dtype = 'f4') looseverts_indices = bgl.Buffer(bgl.GL_FLOAT, looseverts_indices.shape, looseverts_indices) self.vbo_looseverts_indices = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_looseverts_indices) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_looseverts_indices[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_verts * 4, looseverts_indices, bgl.GL_STATIC_DRAW) del looseverts_indices else: self.num_verts = 0 self.draw_verts = False del mesh_arrays bgl.glBindVertexArray(0)
def render(self, draw_data): io = self.io shader = self._bl_shader display_width, display_height = io.display_size fb_width = int(display_width * io.display_fb_scale[0]) fb_height = int(display_height * io.display_fb_scale[1]) if fb_width == 0 or fb_height == 0: return draw_data.scale_clip_rects(*io.display_fb_scale) # backup GL state ( last_program, last_texture, last_active_texture, last_array_buffer, last_element_array_buffer, last_vertex_array, last_blend_src, last_blend_dst, last_blend_equation_rgb, last_blend_equation_alpha, last_viewport, last_scissor_box, ) = self._backup_integers( gl.GL_CURRENT_PROGRAM, 1, gl.GL_TEXTURE_BINDING_2D, 1, gl.GL_ACTIVE_TEXTURE, 1, gl.GL_ARRAY_BUFFER_BINDING, 1, gl.GL_ELEMENT_ARRAY_BUFFER_BINDING, 1, gl.GL_VERTEX_ARRAY_BINDING, 1, gl.GL_BLEND_SRC, 1, gl.GL_BLEND_DST, 1, gl.GL_BLEND_EQUATION_RGB, 1, gl.GL_BLEND_EQUATION_ALPHA, 1, gl.GL_VIEWPORT, 4, gl.GL_SCISSOR_BOX, 4, ) last_enable_blend = gl.glIsEnabled(gl.GL_BLEND) last_enable_cull_face = gl.glIsEnabled(gl.GL_CULL_FACE) last_enable_depth_test = gl.glIsEnabled(gl.GL_DEPTH_TEST) last_enable_scissor_test = gl.glIsEnabled(gl.GL_SCISSOR_TEST) gl.glEnable(gl.GL_BLEND) gl.glBlendEquation(gl.GL_FUNC_ADD) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDisable(gl.GL_CULL_FACE) gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_SCISSOR_TEST) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glViewport(0, 0, int(fb_width), int(fb_height)) ortho_projection = (2.0 / display_width, 0.0, 0.0, 0.0, 0.0, 2.0 / -display_height, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0) shader.bind() shader.uniform_float("ProjMtx", ortho_projection) shader.uniform_int("Texture", 0) for commands in draw_data.commands_lists: size = commands.idx_buffer_size * imgui.INDEX_SIZE // 4 address = commands.idx_buffer_data ptr = C.cast(address, C.POINTER(C.c_int)) idx_buffer_np = np.ctypeslib.as_array(ptr, shape=(size, )) size = commands.vtx_buffer_size * imgui.VERTEX_SIZE // 4 address = commands.vtx_buffer_data ptr = C.cast(address, C.POINTER(C.c_float)) vtx_buffer_np = np.ctypeslib.as_array(ptr, shape=(size, )) vtx_buffer_shaped = vtx_buffer_np.reshape(-1, imgui.VERTEX_SIZE // 4) idx_buffer_offset = 0 for command in commands.commands: x, y, z, w = command.clip_rect gl.glScissor(int(x), int(fb_height - w), int(z - x), int(w - y)) vertices = vtx_buffer_shaped[:, :2] uvs = vtx_buffer_shaped[:, 2:4] colors = vtx_buffer_shaped.view(np.uint8)[:, 4 * 4:] colors = colors.astype('f') / 255.0 indices = idx_buffer_np[idx_buffer_offset:idx_buffer_offset + command.elem_count] gl.glBindTexture(gl.GL_TEXTURE_2D, command.texture_id) batch = batch_for_shader(shader, 'TRIS', { "Position": vertices, "UV": uvs, "Color": colors, }, indices=indices) batch.draw(shader) idx_buffer_offset += command.elem_count # restore modified GL state gl.glUseProgram(last_program) gl.glActiveTexture(last_active_texture) gl.glBindTexture(gl.GL_TEXTURE_2D, last_texture) gl.glBindVertexArray(last_vertex_array) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, last_array_buffer) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer) gl.glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha) gl.glBlendFunc(last_blend_src, last_blend_dst) if last_enable_blend: gl.glEnable(gl.GL_BLEND) else: gl.glDisable(gl.GL_BLEND) if last_enable_cull_face: gl.glEnable(gl.GL_CULL_FACE) else: gl.glDisable(gl.GL_CULL_FACE) if last_enable_depth_test: gl.glEnable(gl.GL_DEPTH_TEST) else: gl.glDisable(gl.GL_DEPTH_TEST) if last_enable_scissor_test: gl.glEnable(gl.GL_SCISSOR_TEST) else: gl.glDisable(gl.GL_SCISSOR_TEST) gl.glViewport(last_viewport[0], last_viewport[1], last_viewport[2], last_viewport[3]) gl.glScissor(last_scissor_box[0], last_scissor_box[1], last_scissor_box[2], last_scissor_box[3])
def __init__(self, img): print('CustomDrawData.__init__()') self.img = img width, height = self.dimensions = self.img.size imgbytes = img.transpose(Image.FLIP_TOP_BOTTOM).tobytes() nimg = numpy.frombuffer(imgbytes, dtype=numpy.uint8) nimg = nimg.astype(numpy.float32) / 255.0 pixels = bgl.Buffer(bgl.GL_FLOAT, width * height * 4, nimg) # 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_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # 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)
def __init__(self, obj, draw_tris, draw_edges, draw_verts): GPU_Indices_Mesh.init_opengl() self._NULL = gl_buffer_void_as_long(0) self.MVP = bgl.Buffer(bgl.GL_FLOAT, (4, 4)) self.obj = obj self.draw_tris = draw_tris self.draw_edges = draw_edges self.draw_verts = draw_verts self.vbo = None self.vbo_tris = None self.vbo_edges = None self.vbo_verts = None ## Create VAO ## self.vao = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenVertexArrays(1, self.vao) bgl.glBindVertexArray(self.vao[0]) ## Init Array ## mesh_arrays = _Mesh_Arrays(obj, draw_tris, draw_edges, draw_verts) ## Create VBO for vertices ## if mesh_arrays.verts_co is None: self.draw_tris = False self.draw_edges = False self.draw_verts = False return if False: # Blender 2.8 self.vbo_len = len(mesh_arrays.verts_co) self.vbo = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo[0]) verts_co = bgl.Buffer(bgl.GL_FLOAT, mesh_arrays.verts_co.shape, mesh_arrays.verts_co) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.vbo_len * 12, verts_co, bgl.GL_STATIC_DRAW) ## Create VBO for Tris ## if mesh_arrays.tri_verts is not None: self.tri_verts = mesh_arrays.tri_verts self.num_tris = len(self.tri_verts) np_tris_co = mesh_arrays.verts_co[mesh_arrays.tri_verts] np_tris_co = bgl.Buffer(bgl.GL_FLOAT, np_tris_co.shape, np_tris_co) self.vbo_tris = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_tris) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tris[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_tris * 36, np_tris_co, bgl.GL_STATIC_DRAW) del np_tris_co tri_indices = np.repeat(np.arange(self.num_tris, dtype='f4'), 3) tri_indices = bgl.Buffer(bgl.GL_FLOAT, tri_indices.shape, tri_indices) self.vbo_tri_indices = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_tri_indices) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tri_indices[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_tris * 12, tri_indices, bgl.GL_STATIC_DRAW) del tri_indices else: self.num_tris = 0 self.draw_tris = False ## Create VBO for Edges ## if mesh_arrays.edge_verts is not None: self.edge_verts = mesh_arrays.edge_verts self.num_edges = len(self.edge_verts) np_edges_co = mesh_arrays.verts_co[mesh_arrays.edge_verts] np_edges_co = bgl.Buffer(bgl.GL_FLOAT, np_edges_co.shape, np_edges_co) self.vbo_edges = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_edges) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edges[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_edges * 24, np_edges_co, bgl.GL_STATIC_DRAW) del np_edges_co edge_indices = np.repeat(np.arange(self.num_edges, dtype='f4'), 2) edge_indices = bgl.Buffer(bgl.GL_FLOAT, edge_indices.shape, edge_indices) self.vbo_edge_indices = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_edge_indices) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edge_indices[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_edges * 8, edge_indices, bgl.GL_STATIC_DRAW) del edge_indices else: self.num_edges = 0 self.draw_edges = False ## Create EBO for Loose Verts ## if mesh_arrays.looseverts is not None: self.looseverts = mesh_arrays.looseverts self.num_verts = len(mesh_arrays.looseverts) np_lverts_co = mesh_arrays.verts_co[mesh_arrays.looseverts] np_lverts_co = bgl.Buffer(bgl.GL_FLOAT, np_lverts_co.shape, np_lverts_co) self.vbo_verts = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_verts) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_verts[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_verts * 12, np_lverts_co, bgl.GL_STATIC_DRAW) del np_lverts_co looseverts_indices = np.arange(self.num_verts, dtype='f4') looseverts_indices = bgl.Buffer(bgl.GL_FLOAT, looseverts_indices.shape, looseverts_indices) self.vbo_looseverts_indices = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, self.vbo_looseverts_indices) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_looseverts_indices[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_verts * 4, looseverts_indices, bgl.GL_STATIC_DRAW) del looseverts_indices else: self.num_verts = 0 self.draw_verts = False del mesh_arrays bgl.glBindVertexArray(0)
def __init__(self, dimensions, perspective, region3d): self.dimensions = dimensions self.perspective = perspective width = bpy.context.scene.tina_resolution_x height = bpy.context.scene.tina_resolution_y pixels = worker.render_main(width, height, region3d) # Generate dummy image buffer pixels = bgl.Buffer(bgl.GL_INT, width * height, 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_UNSIGNED_INT_8_8_8_8_REV, 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) bgl.glUseProgram(self.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( self.program, "texCoord") position_location = bgl.glGetAttribLocation(self.program, "pos") tex0_location = bgl.glGetAttribLocation(self.program, "tex0") bgl.glUniform1i(tex0_location, 0) bgl.glEnableVertexAttribArray(texturecoord_location) bgl.glEnableVertexAttribArray(position_location) # Generate geometry buffers for drawing textured quad width, height = dimensions 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)