def update_heatmap(self, glcontext, heatmap): glActiveTexture(GL_TEXTURE0 + 1) glBindTexture(GL_TEXTURE_2D, self.hm_tex_id) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, heatmap.shape[1], heatmap.shape[0], GL_RGBA, GL_FLOAT, heatmap)
def update_heatmap(self, glcontext, hm_data): hm_w, hm_h = self.engine.heatmap_size glActiveTexture(GL_TEXTURE0 + 1) glBindTexture(GL_TEXTURE_2D, self.hm_tex_id) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, hm_w, hm_h, GL_RED, GL_FLOAT, hm_data)
def render_single_texture(self, tex): glBindVertexArray(self.vao_id) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, get_gl_texture_id(tex)) self.default_shader.use() self.default_shader.set_uniform_1i('image_tex', 0) glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, None) return True
def do_render(self, filter, in_tex): # Black borders. glClearColor(0.0, 0.0, 0.0, 0.0) glClear(GL_COLOR_BUFFER_BIT) glBindVertexArray(self.vao_id) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, in_tex.tex_id) self.shader.use() self.shader.set_uniform_1f('u_scale_x', self.scale_x) self.shader.set_uniform_1f('u_scale_y', self.scale_y) glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, None)
def update_aggregate_heatmap(self, glcontext): # Since we're in a different thread, check if this is even ready yet. if self.heatmap_sum is None: return if self.ahm: agg_heatmap = self.heatmap_sum / np.max(self.heatmap_sum) else: agg_heatmap = np.zeros_like(self.heatmap_sum) hm_w, hm_h = self.engine.heatmap_size glActiveTexture(GL_TEXTURE0 + 3) glBindTexture(GL_TEXTURE_2D, self.agg_hm_tex_id) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, hm_w, hm_h, GL_RED, GL_FLOAT, agg_heatmap)
def setup_scene(self, image_tex, bind_hm=True, bind_bg=True): glBindVertexArray(self.vao_id) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, image_tex) glActiveTexture(GL_TEXTURE0 + 1) glBindTexture(GL_TEXTURE_2D, self.hm_tex_id if bind_hm else 0) glActiveTexture(GL_TEXTURE0 + 2) glBindTexture(GL_TEXTURE_2D, get_gl_texture_id(self.bg_buf) if bind_bg else 0) glActiveTexture(GL_TEXTURE0 + 3) glBindTexture(GL_TEXTURE_2D, self.agg_hm_tex_id) self.hm_shader.use() self.hm_shader.set_uniform_1i('image_tex', 0) self.hm_shader.set_uniform_1i('hm_tex', 1) self.hm_shader.set_uniform_1i('bg_tex', 2) self.hm_shader.set_uniform_1i('agg_hm_tex', 3)
def init_gl(self, glcontext): #TODO deinit at some point assert not self.glcontext vert_stage = GstGL.GLSLStage.new_default_vertex(glcontext) frag_stage = GstGL.GLSLStage.new_with_string( glcontext, GL_FRAGMENT_SHADER, GstGL.GLSLVersion.NONE, GstGL.GLSLProfile.COMPATIBILITY | GstGL.GLSLProfile.ES, FRAGMENT_SHADER_SRC) self.hm_shader = GstGL.GLShader.new(glcontext) self.hm_shader.compile_attach_stage(vert_stage) self.hm_shader.compile_attach_stage(frag_stage) self.hm_shader.link() self.default_shader = GstGL.GLShader.new_default(glcontext) a_position = self.default_shader.get_attribute_location('a_position') a_texcoord = self.default_shader.get_attribute_location('a_texcoord') self.vao_id = glGenVertexArrays(1) glBindVertexArray(self.vao_id) self.positions_buffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.positions_buffer) glBufferData(GL_ARRAY_BUFFER, ArrayDatatype.arrayByteCount(POSITIONS), POSITIONS, GL_STATIC_DRAW) self.texcoords_buffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.texcoords_buffer) glBufferData(GL_ARRAY_BUFFER, ArrayDatatype.arrayByteCount(TEXCOORDS), TEXCOORDS, GL_STATIC_DRAW) self.vbo_indices_buffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vbo_indices_buffer) glBufferData(GL_ARRAY_BUFFER, ArrayDatatype.arrayByteCount(INDICES), INDICES, GL_STATIC_DRAW) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.vbo_indices_buffer) glBindBuffer(GL_ARRAY_BUFFER, self.positions_buffer) glVertexAttribPointer.baseFunction(a_position, 2, GL_FLOAT, GL_FALSE, 0, None) glBindBuffer(GL_ARRAY_BUFFER, self.texcoords_buffer) glVertexAttribPointer.baseFunction(a_texcoord, 2, GL_FLOAT, GL_FALSE, 0, None) glEnableVertexAttribArray(a_position) glEnableVertexAttribArray(a_texcoord) glBindVertexArray(0) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) glBindBuffer(GL_ARRAY_BUFFER, 0) hm_w, hm_h = self.get_heatmap_texture_size() texture_ids = glGenTextures(1) self.hm_tex_id = texture_ids glActiveTexture(GL_TEXTURE0 + 1) glBindTexture(GL_TEXTURE_2D, self.hm_tex_id) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, hm_w, hm_h, 0, GL_RGBA, GL_FLOAT, None) self.glcontext = glcontext
def on_draw(self, sink, context, sample): assert context == self.glcontext self.draws += 1 assert context == self.glcontext frame_texture = _get_gl_texture_id(sample.get_buffer()) overlay_buffer = self.get_front_buffer() overlay_texture = overlay_buffer.texture_id if overlay_buffer else 0 glDisable(GL_BLEND) glBindVertexArray(self.vao) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, frame_texture) self.shader.use() self.shader.set_uniform_1i('frame', 0) glUniformMatrix4fv(self.u_transformation, 1, GL_FALSE, self.matrix) glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, None) if overlay_texture: glBindTexture(GL_TEXTURE_2D, overlay_texture) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glBlendEquation(GL_FUNC_ADD) glUniformMatrix4fv(self.u_transformation, 1, GL_FALSE, IDENTITY_MATRIX) glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, None) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, 0) glDisable(GL_BLEND) context.clear_shader() glBindVertexArray(0) if overlay_buffer: overlay_buffer.set_sync_point() self.emit('drawn') if not self.fps_start: self.fps_start = time.monotonic() elapsed = time.monotonic() - self.fps_start if self.print_fps and elapsed > self.print_fps: incoming_fps = self.incoming_frames / elapsed draw_fps = self.draws / elapsed incoming_overlay_fps = self.incoming_overlays / elapsed render_fps = self.rendered_overlays / elapsed print( 'glsvgoverlaysink: in frames {} ({:.2f} fps) svg {} ({:.2f} fps), rendered {} ({:.2f} fps), draw {} ({:.2f} fps)' .format(self.incoming_frames, self.incoming_frames / elapsed, self.incoming_overlays, self.incoming_overlays / elapsed, self.rendered_overlays, self.rendered_overlays / elapsed, self.draws, self.draws / elapsed)) self.incoming_frames = 0 self.incoming_overlays = 0 self.rendered_overlays = 0 self.draws = 0 self.fps_start = time.monotonic() return True