def on_draw(dt): # Clear depth and color buffers gl.glClearColor(*C0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # Opaque objects rendering gl.glDepthMask(gl.GL_TRUE) gl.glDisable(gl.GL_BLEND) # Transparent objects rendering gl.glDepthMask(gl.GL_FALSE) gl.glEnable(gl.GL_BLEND) framebuffer.activate() gl.glClearColor(0, 0, 0, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) window.clear(color=(0, 0, 0, 1)) gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ONE, gl.GL_ZERO, gl.GL_ONE_MINUS_SRC_ALPHA) scene.draw(gl.GL_TRIANGLES, indices) framebuffer.deactivate() # Compositing gl.glBlendFunc(gl.GL_ONE_MINUS_SRC_ALPHA, gl.GL_SRC_ALPHA) compose.draw(gl.GL_TRIANGLE_STRIP)
def on_draw(dt): # Clear depth and color buffers gl.glClearColor(*C0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # Opaque objects rendering gl.glDepthMask(gl.GL_TRUE) gl.glDisable(gl.GL_BLEND) # Transparent objects rendering gl.glDepthMask(gl.GL_FALSE) gl.glEnable(gl.GL_BLEND) framebuffer.activate() gl.glClearColor(0,0,0,1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) window.clear(color=(0,0,0,1)) gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ONE, gl.GL_ZERO, gl.GL_ONE_MINUS_SRC_ALPHA) scene.draw(gl.GL_TRIANGLES, indices) framebuffer.deactivate() # Compositing gl.glBlendFunc(gl.GL_ONE_MINUS_SRC_ALPHA, gl.GL_SRC_ALPHA) compose.draw(gl.GL_TRIANGLE_STRIP)
def init_program(self): # Ensure size is set #print("program param: ", self.program_params) #print("---[") #print(self.fragment) #print("]---") self.program = gloo.Program(self.vertex, self.fragment, count=4, version="450") self.program['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] if self.title == "Map": self.point_history = collections.deque(maxlen=250) self.point_program = gloo.Program(self.dot_vertex, self.dot_fragment, count=self.point_history.maxlen) self.point_program['position'] = np.zeros( (self.point_history.maxlen, 2), dtype=np.float32) - 2.0 self.point_program['age'] = np.zeros(self.point_history.maxlen, dtype=np.float32) # TODO: make those setting parameter gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) self.on_resize(*self.winsize) if self.iMouse: self.program["iMouse"] = self.iMouse
def __init__(clock=None, framerate=None, backend=None): """ Initialize the main loop Parameters ---------- clock : Clock clock to use to run the app (gives the elementary tick) framerate : int frames per second backend : python module Backend module """ global __clock__ options = parser.get_options() if options.debug: log.setLevel(logging.DEBUG) if framerate is None: framerate = options.framerate if framerate > 0: log.info("Running at %d frames/second" % framerate) else: log.info("Running at full speed") if clock is None: __clock__ = _clock.get_default() else: __clock__ = clock __clock__.set_fps_limit(framerate) # OpenGL Initialization gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1) gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1) gl.glEnable(gl.GL_VERTEX_PROGRAM_POINT_SIZE) gl.glEnable(gl.GL_POINT_SPRITE) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # Initialize timers for all windows for window in backend.windows(): window._clock = __clock__ # Start timers for i in range(len(window._timer_stack)): handler, interval = window._timer_stack[i] __clock__.schedule_interval(handler, interval) # Dispatch init event window.dispatch_event('on_init') # Dispatch an initial resize event window.dispatch_event('on_resize', window._width, window._height) return __clock__
def on_draw(dt): global alpha window.clear() gl.glEnable(gl.GL_BLEND) gl.glDisable(gl.GL_DEPTH_TEST) for i in range(len(image_info)): img = image_info[i] if img is not None: modelmat = np.eye(4, dtype=np.float32) if i > 0: alpha = min(1.0, alpha + dt * 0.4) print('current alpha: {}'.format(alpha)) program['alpha'] = alpha # min(0.6, alpha) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # target position. front_center = face_rect_center(img['face_rect']) back_center = face_rect_center(image_info[i - 1]['face_rect']) translate = back_center - front_center # print('translation: {}, front center: {}, back center: {}'.format(translate, front_center, back_center)) translate = mix(np.zeros(2, dtype=np.float32), translate, alpha) T2 = glm.translation(translate[0], translate[1], 0.0) T1 = np.matrix( glm.translation(front_center[0], front_center[1], 0.0)) # target scale. front_sz = face_rect_size(img['face_rect']) back_sz = face_rect_size(image_info[i - 1]['face_rect']) scale = mix(1.0, back_sz / front_sz, alpha) S = np.eye(4, dtype=np.float32) S = glm.scale(S, scale, scale, 1.0) modelmat = T1.I * S * T1 * T2 else: program['alpha'] = 1.0 - alpha program['u_model'] = modelmat program['position'] = img['image_rect'] program['tex'] = img['image'].view(gloo.Texture2D) program['u_lineflag'] = 0.0 program.draw(gl.GL_TRIANGLE_STRIP) program['position'] = img['face_rect'] program['u_lineflag'] = 1.0 program['u_linecolor'] = [1.0, 0.0, 0.0] program.draw(gl.GL_LINE_LOOP)
def init_program(self): self.program = gloo.Program(vertex, fragment, count=4) self.program['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] if self.dorecord: self.program['max_iter'] = 50 self.program['max_march'] = 200 self.program['fast'] = 1 else: self.program['max_iter'] = self.params['max_iter'] self.program['max_march'] = self.params['max_march'] self.program['fast'] = 0 # Render gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def on_draw(dt): window.clear(color=C0) gl.glDepthMask(gl.GL_FALSE) gl.glEnable(gl.GL_BLEND) # Transparent surfaces framebuffer.activate() window.clear(color=(0,0,0,1)) gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ONE, gl.GL_ZERO, gl.GL_ONE_MINUS_SRC_ALPHA) quads.draw(gl.GL_TRIANGLES, indices) framebuffer.deactivate() # Compositing gl.glBlendFunc(gl.GL_ONE_MINUS_SRC_ALPHA, gl.GL_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) post.draw(gl.GL_TRIANGLE_STRIP)
def on_draw(dt): window.clear(color=C0) gl.glDepthMask(gl.GL_FALSE) gl.glEnable(gl.GL_BLEND) # Transparent surfaces framebuffer.activate() window.clear(color=(0, 0, 0, 1)) gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ONE, gl.GL_ZERO, gl.GL_ONE_MINUS_SRC_ALPHA) quads.draw(gl.GL_TRIANGLES, indices) framebuffer.deactivate() # Compositing gl.glBlendFunc(gl.GL_ONE_MINUS_SRC_ALPHA, gl.GL_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) post.draw(gl.GL_TRIANGLE_STRIP)
def on_draw(dt): gl.glViewport(0, 0, GridWidth, GridHeight) gl.glDisable(gl.GL_BLEND) Advect(Velocity.Ping, Velocity.Ping, Obstacles, Velocity.Pong, VelocityDissipation) Velocity.swap() Advect(Velocity.Ping, Temperature.Ping, Obstacles, Temperature.Pong, TemperatureDissipation) Temperature.swap() Advect(Velocity.Ping, Density.Ping, Obstacles, Density.Pong, DensityDissipation) Density.swap() ApplyBuoyancy(Velocity.Ping, Temperature.Ping, Density.Ping, Velocity.Pong) Velocity.swap() ApplyImpulse(Temperature.Ping, ImpulsePosition, ImpulseTemperature) ApplyImpulse(Density.Ping, ImpulsePosition, ImpulseDensity) ComputeDivergence(Velocity.Ping, Obstacles, Divergence) ClearSurface(Pressure.Ping, 0.0) for i in range(NumJacobiIterations): Jacobi(Pressure.Ping, Divergence, Obstacles, Pressure.Pong) Pressure.swap() SubtractGradient(Velocity.Ping, Pressure.Ping, Obstacles, Velocity.Pong) Velocity.swap() gl.glViewport(0, 0, window.width, window.height) gl.glClearColor(0, 0, 0, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) prog_visualize['u_data'] = Density.Ping.texture prog_visualize['u_shape'] = Density.Ping.texture.shape[ 1], Density.Ping.texture.shape[0] prog_visualize['u_kernel'] = data.get("spatial-filters.npy") prog_visualize["Sampler"] = Density.Ping.texture prog_visualize["FillColor"] = 0.95, 0.925, 1.00 prog_visualize["Scale"] = 1.0 / window.width, 1.0 / window.height prog_visualize.draw(gl.GL_TRIANGLE_STRIP)
def render(self, dt): self.window.activate() self.window.clear() if self.iMat is not None: self.iMat = np.eye(4, dtype=np.float32) if not self.gimbal: glm.xrotate(self.iMat, self.horizontal_angle) glm.yrotate(self.iMat, self.vertical_angle) else: self.position = [0., 0., self.params["distance"]] glm.translate(self.iMat, *self.position) if self.gimbal: glm.xrotate(self.iMat, self.params["horizontal_angle"]) glm.yrotate(self.iMat, self.params["vertical_angle"]) self.params["iMat"] = self.iMat for p in self.program_params: self.program[p] = self.params[p] dt = dt / self.fps if self.iTime: self.program["iTime"] = dt try: self.program.draw(gl.GL_TRIANGLE_STRIP) if self.old_program: self.old_program.delete() del self.old_program self.old_program = None print("Loaded new program!") except RuntimeError: if not self.old_program: raise self.old_program.draw(gl.GL_TRIANGLE_STRIP) self.program.delete() del self.program self.program = self.old_program self.old_program = None self.paused = True if self.title == "Map": gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_ONE_MINUS_DST_COLOR, gl.GL_ZERO) self.point_program.draw(gl.GL_POINTS) self.prev_params = copy.deepcopy(self.params)
def on_draw(dt): gl.glViewport(0, 0, GridWidth, GridHeight) gl.glDisable(gl.GL_BLEND) Advect(Velocity.Ping, Velocity.Ping, Obstacles, Velocity.Pong, VelocityDissipation) Velocity.swap() Advect(Velocity.Ping, Temperature.Ping, Obstacles, Temperature.Pong, TemperatureDissipation) Temperature.swap() Advect(Velocity.Ping, Density.Ping, Obstacles, Density.Pong, DensityDissipation) Density.swap() ApplyBuoyancy(Velocity.Ping, Temperature.Ping, Density.Ping, Velocity.Pong) Velocity.swap() ApplyImpulse(Temperature.Ping, ImpulsePosition, ImpulseTemperature) ApplyImpulse(Density.Ping, ImpulsePosition, ImpulseDensity) ComputeDivergence(Velocity.Ping, Obstacles, Divergence) ClearSurface(Pressure.Ping, 0.0) for i in range(NumJacobiIterations): Jacobi(Pressure.Ping, Divergence, Obstacles, Pressure.Pong) Pressure.swap() SubtractGradient(Velocity.Ping, Pressure.Ping, Obstacles, Velocity.Pong) Velocity.swap() gl.glViewport(0,0,window.width,window.height) gl.glClearColor(0, 0, 0, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) prog_visualize['u_data'] = Density.Ping.texture prog_visualize['u_shape'] = Density.Ping.texture.shape[1], Density.Ping.texture.shape[0] prog_visualize['u_kernel'] = data.get("spatial-filters.npy") prog_visualize["Sampler"] = Density.Ping.texture prog_visualize["FillColor"] = 0.95, 0.925, 1.00 prog_visualize["Scale"] = 1.0/window.width, 1.0/window.height prog_visualize.draw(gl.GL_TRIANGLE_STRIP)
def on_draw(dt): window.clear(color=C0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthMask(gl.GL_FALSE) gl.glEnable(gl.GL_BLEND) # Transparent surfaces # Pass 1: accumulation quads["pass"] = 0 framebuffer.color = accumulation framebuffer.activate() gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE) window.clear(color=(0,0,0,0)) quads.draw(gl.GL_TRIANGLES, indices) framebuffer.deactivate() # Pass 2: revealage quads["pass"] = 1 framebuffer.color = revealage framebuffer.activate() gl.glBlendFunc(gl.GL_ZERO, gl.GL_ONE_MINUS_SRC_COLOR) window.clear(color=(1,1,1,1)) quads.draw(gl.GL_TRIANGLES, indices) framebuffer.deactivate() # Compositing gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) post.draw(gl.GL_TRIANGLE_STRIP)
def on_draw(dt): window.clear(color=C0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthMask(gl.GL_FALSE) gl.glEnable(gl.GL_BLEND) # Transparent surfaces # Pass 1: accumulation quads["pass"] = 0 framebuffer.color = accumulation framebuffer.activate() gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE) window.clear(color=(0, 0, 0, 0)) quads.draw(gl.GL_TRIANGLES, indices) framebuffer.deactivate() # Pass 2: revealage quads["pass"] = 1 framebuffer.color = revealage framebuffer.activate() gl.glBlendFunc(gl.GL_ZERO, gl.GL_ONE_MINUS_SRC_COLOR) window.clear(color=(1, 1, 1, 1)) quads.draw(gl.GL_TRIANGLES, indices) framebuffer.deactivate() # Compositing gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) post.draw(gl.GL_TRIANGLE_STRIP)
def on_draw(dt): surface_ref.set_array(Density.ping_array) kernel_function(np.int32(400), np.int32(400), block=(16,16,1), grid=((400+1)//16+1,(400+1)//16+1)) gl.glViewport(0,0,window.width,window.height) gl.glClearColor(0, 0, 0, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) global t t += dt prog_visualize['u_data'] = Density.Ping.texture prog_visualize['t'] = t prog_visualize['u_shape'] = Density.Ping.texture.shape[1], Density.Ping.texture.shape[0] prog_visualize['u_kernel'] = data.get("spatial-filters.npy") prog_visualize["Sampler"] = Density.Ping.texture prog_visualize["FillColor"] = 0.95, 0.925, 1.00 prog_visualize["Scale"] = 1.0/window.width, 1.0/window.height prog_visualize.draw(gl.GL_TRIANGLE_STRIP) Density.swap()
def on_draw(dt): global framebuffer, offset_index offset_name = list(offsets.keys())[offset_index] offset = offsets[offset_name] gl.glViewport(0, 0, width, height) framebuffer_2.activate() window.clear() framebuffer_2.deactivate() for dx, dy in offset: framebuffer_1.activate() window.clear() scene["offset"] = (2 * dx - 1) / width, (2 * dy - 1) / height scene.draw(gl.GL_TRIANGLE_STRIP) # scene.draw(gl.GL_LINE_LOOP) framebuffer_1.deactivate() framebuffer_2.activate() gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE) gl.glBlendColor(0, 0, 0, 1 / len(offset)) ssaa.draw(gl.GL_TRIANGLE_STRIP) gl.glDisable(gl.GL_BLEND) framebuffer_2.deactivate() gl.glViewport(0, 0, window.width, window.height) window.clear() final.draw(gl.GL_TRIANGLE_STRIP) gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer) framebuffer[...] = framebuffer[::-1, :] # filename = "triangle-ssaa-outlined-%s.png" % offset_name filename = "triangle-ssaa-filled-%s.png" % offset_name png.from_array(framebuffer, 'RGB').save(filename) offset_index += 1
phi += 0.5 # degrees model = np.eye(4, dtype=np.float32) glm.rotate(model, theta, 0, 0, 1) glm.rotate(model, phi, 0, 1, 0) transform['model'] = model # Build cube data V, I, O = colorcube() vertices = V.view(gloo.VertexBuffer) faces = I.view(gloo.IndexBuffer) outline = O.view(gloo.IndexBuffer) cube = gloo.Program(vertex, fragment) cube.bind(vertices) transform = PVMProjection(Position("position")) cube['transform'] = transform window.attach(transform) phi, theta = 0, 0 # OpenGL initalization gl.glEnable(gl.GL_DEPTH_TEST) gl.glPolygonOffset(1, 1) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glLineWidth(0.75) # Run app.run()
r, g, b = xyz_to_rgb(SMPTEsystem, x, y, z) r = min((max(r, 0), 1)) g = min((max(g, 0), 1)) b = min((max(b, 0), 1)) colors[i] = norm_rgb(r, g, b) program = gloo.Program(vertex, fragment, count=len(galaxy)) view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) projection = np.eye(4, dtype=np.float32) glm.translate(view, 0, 0, -5) program["u_model"] = model program["u_view"] = view program["u_colormap"] = colors program["u_texture"] = data.get("particle.png") program["u_texture"].interpolation = gl.GL_LINEAR program["a_temperature"] = (galaxy["temperature"] - t0) / (t1 - t0) program["a_brightness"] = galaxy["brightness"] program["a_size"] = galaxy["size"] program["a_type"] = galaxy["type"] gl.glClearColor(0.0, 0.0, 0.03, 1.0) gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) app.run(framerate=60)
def on_init(): gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE)
x, y, z = spectrum_to_xyz(bb_spectrum, temperature) r, g, b = xyz_to_rgb(SMPTEsystem, x, y, z) r = min((max(r, 0), 1)) g = min((max(g, 0), 1)) b = min((max(b, 0), 1)) colors[i] = norm_rgb(r, g, b) program = gloo.Program(vertex, fragment, count=len(galaxy)) view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) projection = np.eye(4, dtype=np.float32) glm.translate(view, 0, 0, -5) program['u_model'] = model program['u_view'] = view program['u_colormap'] = colors program['u_texture'] = data.get("particle.png") program['u_texture'].interpolation = gl.GL_LINEAR program['a_temperature'] = (galaxy['temperature'] - t0) / (t1 - t0) program['a_brightness'] = galaxy['brightness'] program['a_size'] = galaxy['size'] program['a_type'] = galaxy['type'] gl.glClearColor(0.0, 0.0, 0.03, 1.0) gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) app.run(framerate=60)
def on_init(): gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glPolygonOffset(1, 1) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glLineWidth(2.5)
def __init__(clock=None, framerate=None, backend=None): """ Initialize the main loop Parameters ---------- clock : Clock clock to use to run the app (gives the elementary tick) framerate : int frames per second backend : python module Backend module """ global __clock__ options = parser.get_options() if options.debug: log.setLevel(logging.DEBUG) if framerate is None: framerate = options.framerate if framerate > 0: log.info("Running at %d frames/second" % framerate) else: log.info("Running at full speed") if clock is None: __clock__ = _clock.get_default() else: __clock__ = clock __clock__.set_fps_limit(framerate) # OpenGL Initialization for window in backend.windows(): window.activate() gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1) gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1) gl.glEnable(gl.GL_VERTEX_PROGRAM_POINT_SIZE) gl.glEnable(gl.GL_POINT_SPRITE) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # Initialize timers for all windows for window in backend.windows(): window._clock = __clock__ # Start timers for i in range(len(window._timer_stack)): handler, interval = window._timer_stack[i] __clock__.schedule_interval(handler, interval) # Activate window window.activate() # Dispatch init event window.dispatch_event('on_init') # Dispatch an initial resize event window.dispatch_event('on_resize', window._width, window._height) return __clock__
def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def init_program(self): self.program = gloo.Program(vertex, fragment, count=4) self.program['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] # Render gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def on_init(): gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_DST_ALPHA)
phi += 0.5 # degrees model = np.eye(4, dtype=np.float32) glm.rotate(model, theta, 0, 0, 1) glm.rotate(model, phi, 0, 1, 0) cube['model'] = model # Build cube data V, I, O = colorcube() vertices = V.view(gloo.VertexBuffer) faces = I.view(gloo.IndexBuffer) outline = O.view(gloo.IndexBuffer) cube = gloo.Program(vertex, fragment) cube.bind(vertices) transform = PVMProjection(Position3D("position")) cube['transform'] = transform window.attach(transform) phi, theta = 0, 0 # OpenGL initalization gl.glEnable(gl.GL_DEPTH_TEST) gl.glPolygonOffset(1, 1) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glLineWidth(0.75) # Run app.run()
def on_init(): gl.glEnable(gl.GL_DEPTH_TEST) gl.glPolygonOffset(1, 1) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def render(self, draw_data): # perf: local for faster access io = self.io 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 # todo: provide cleaner version of this backup-restore code last_program = gl.glGetIntegerv(gl.GL_CURRENT_PROGRAM) last_texture = gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D) last_active_texture = gl.glGetIntegerv(gl.GL_ACTIVE_TEXTURE) last_array_buffer = gl.glGetIntegerv(gl.GL_ARRAY_BUFFER_BINDING) last_element_array_buffer = gl.glGetIntegerv( gl.GL_ELEMENT_ARRAY_BUFFER_BINDING) last_vertex_array = gl.glGetIntegerv(gl.GL_VERTEX_ARRAY_BINDING) last_blend_src = gl.glGetIntegerv(gl.GL_BLEND_SRC) last_blend_dst = gl.glGetIntegerv(gl.GL_BLEND_DST) last_blend_equation_rgb = gl.glGetIntegerv(gl.GL_BLEND_EQUATION_RGB) last_blend_equation_alpha = gl.glGetIntegerv( gl.GL_BLEND_EQUATION_ALPHA) last_viewport = gl.glGetIntegerv(gl.GL_VIEWPORT) last_scissor_box = gl.glGetIntegerv(gl.GL_SCISSOR_BOX) 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 = (ctypes.c_float * 16)(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) self.prog["ProjMtx"] = ortho_projection for commands in draw_data.commands_lists: idx_buffer_offset = 0 array_type = c_ubyte * commands.vtx_buffer_size * imgui.VERTEX_SIZE data_carray = array_type.from_address(commands.vtx_buffer_data) if not ( imgui.VERTEX_BUFFER_POS_OFFSET == 0 and \ imgui.VERTEX_BUFFER_UV_OFFSET == 8 and \ imgui.VERTEX_BUFFER_COL_OFFSET == 16 ): log.error( "GlumpyRenderer.render(): imgui vertex buffer layout has changed ! notify the developers ..." ) return #TODO: this is a bit convoluted; Imgui delivers uint8 colors, but glumpy wants float32's dtype = [('Position', np.float32, 2), ('UV', np.float32, 2), ('Color', np.uint8, 4)] vao_content = np.frombuffer(data_carray, dtype=dtype) dtype2 = [('Position', np.float32, 2), ('UV', np.float32, 2), ('Color', np.float32, 4)] vao_content_f = np.zeros(vao_content.shape, dtype=dtype2) for i, val in enumerate(vao_content): vao_content_f[i] = vao_content[i] vao_content_f[i]['Color'] /= 255 v_array = vao_content_f.view(gloo.VertexArray) self.prog.bind(v_array) if imgui.INDEX_SIZE == 1: dtype = np.uint8 if imgui.INDEX_SIZE == 2: dtype = np.uint16 if imgui.INDEX_SIZE == 4: dtype = np.uint32 # gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._elements_handle) # # todo: check this (sizes) # gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, commands.idx_buffer_size * imgui.INDEX_SIZE, ctypes.c_void_p(commands.idx_buffer_data), gl.GL_STREAM_DRAW) array_type = c_ubyte * commands.idx_buffer_size * imgui.INDEX_SIZE data_carray = array_type.from_address(commands.idx_buffer_data) idx_content = np.frombuffer(data_carray, dtype=dtype) for command in commands.commands: # TODO: ImGui Images will not work yet, homogenizing texture id # allocation by imgui/glumpy is likely a larger issue # #accessing command.texture_id crashes the prog # #gl.glBindTexture(gl.GL_TEXTURE_2D, command.texture_id ) x, y, z, w = command.clip_rect gl.glScissor(int(x), int(fb_height - w), int(z - x), int(w - y)) idx_array = idx_content[idx_buffer_offset:( idx_buffer_offset + command.elem_count)].view( gloo.IndexBuffer) self.prog.draw(mode=gl.GL_TRIANGLES, indices=idx_array) 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]) log.debug("----------------------end---------------------------------")