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) if self.checkErrors: self.drawing.glCheckError('vertexAttribPointer %s' % varName) 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 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 _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 __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 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_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 __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 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 __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 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, 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, 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, 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)