def set_framebuffer(self, width, height): color = np.zeros((height, width, 4), np.uint8).view(gloo.Texture2D) color.interpolation = gl.GL_LINEAR pick = np.zeros((height, width, 4), np.uint8).view(gloo.Texture2D) pick.interpolation = gl.GL_LINEAR framebuffer = gloo.FrameBuffer(color=[color, pick], depth=gloo.DepthBuffer(width, height)) self.framebuffer = framebuffer self.img_shader["color"] = self.framebuffer.color[0]
def __init__(self, width, height, *args): # We need 3 framebuffers: # 1 is used to store original rendering # 2 & 3 are used for ping-pong rendering # -> gives 2 as input, render in 3 # -> gives 3 as input, render in 2 # -> ... self._framebuffers = [] for i in range(3): depth = gloo.DepthBuffer(width, height) # depth = np.zeros((height,width),np.float32).view(gloo.DepthTexture) color = np.zeros((height,width,3),np.float32).view(gloo.Texture2D) framebuffer = gloo.FrameBuffer(color=color, depth=depth) self._framebuffers.append(framebuffer) # Build filter programs self._build_programs(*args) self._viewport = 0, 0, width, height
def __init__(self, width, height, num_color_attachments=1, color_dtypes=None, has_depth=True): assert num_color_attachments >= 1, "Need at least one color attachment" if color_dtypes is None: color_dtypes = [np.ubyte] * num_color_attachments assert len(color_dtypes) == num_color_attachments self._color_attachments = [] for i in range(num_color_attachments): color = np.zeros((height, width, 4), color_dtypes[i]) if color_dtypes[i] == np.float32: color = color.view(gloo.TextureFloat2D) else: color = color.view(gloo.Texture2D) color.interpolation = gl.GL_LINEAR self._color_attachments.append(color) self._depth_buffer = gloo.DepthBuffer(width, height) self._framebuffer = gloo.FrameBuffer(color=self._color_attachments, depth=self._depth_buffer)
def _init_buffers(self, viewport_size, out_buffer_location): assert out_buffer_location in ['torch', 'opengl', 'numpy'] if out_buffer_location == 'torch': assert _PYCUDA, 'pycuda is not available' try: import pycuda.gl.autoinit # this may fails in headless mode except: raise RuntimeError( 'PyCUDA init failed, cannot use torch buffer') _ = torch.cuda.FloatTensor( 1, 3, 512, 512) # needs init here, otherwise does not work color_np = np.zeros((viewport_size[1], viewport_size[0], 4), np.float32) self.color_buf, self.color_buf_cuda = create_shared_texture( color_np) self.out_buf = torch.zeros((viewport_size[1], viewport_size[0], 4), dtype=torch.float32).cuda() elif out_buffer_location == 'opengl': self.color_buf = np.zeros( (viewport_size[1], viewport_size[0], 4), dtype=np.float32).view(gloo.TextureFloat2D) self.out_buf = self.color_buf elif out_buffer_location == 'numpy': self.color_buf = np.zeros( (viewport_size[1], viewport_size[0], 4), dtype=np.float32).view(gloo.TextureFloat2D) self.out_buf = np.zeros((viewport_size[1], viewport_size[0], 3), dtype=np.float32) self.viewport_size = viewport_size self.out_buffer_location = out_buffer_location self.depth_buf = gloo.DepthBuffer(viewport_size[0], viewport_size[1], gl.GL_DEPTH_COMPONENT32) self.fbo = gloo.FrameBuffer(color=self.color_buf, depth=self.depth_buf)
window_config.double_buffer = True window_config.samples = 0 window_config.api = "ES" window_config.major_version = 3 window_config.minor_version = 1 window_config.profile = "core" window = glumpy.app.Window(width=width, height=height, config=window_config, visible=True) print(window.config) #fb = np.zeros((640, 480), np.float32).view(gloo.TextureFloat2D) color = np.zeros((height, width, 4), np.ubyte).view(gloo.Texture2D) normal_depth = np.zeros((height, width, 4), np.ubyte).view(gloo.Texture2D) depth_buffer = gloo.DepthBuffer(width, height) # color[..., 3] = 255 color.interpolation = gl.GL_LINEAR normal_depth.interpolation = gl.GL_LINEAR #color = np.ones((640, 480,4),np.ubyte).view(gloo.Texture2D) framebuffer = gloo.FrameBuffer(color=[color, normal_depth], depth=depth_buffer) #framebuffer = gloo.RenderBuffer(width=640, height=480) q = False data = {"time": 0} @window.event def on_init(): gl.glEnable(gl.GL_DEPTH_TEST)
program['linewidth'] = 1.0 program['antialias'] = 1.0 program['transform'] = Trackball() program["id"] = np.arange(n, dtype=np.float32) quad = gloo.Program(quad_vertex, quad_fragment, count=4) quad['position'] = [(-1, -1), (-1, 1), (1, -1), (1, 1)] color = np.zeros((window.height, window.width, 4), np.ubyte).view(gloo.Texture2D) color.interpolation = gl.GL_LINEAR pick = np.zeros((window.height, window.width, 4), np.ubyte).view(gloo.Texture2D) pick.interpolation = gl.GL_LINEAR framebuffer = gloo.FrameBuffer(color=[color, pick], depth=gloo.DepthBuffer(window.width, window.height)) quad["color"] = color index = 0 mouse = 0, 0 @window.event def on_draw(dt): gl.glEnable(gl.GL_DEPTH_TEST) framebuffer.activate() window.clear() program.draw(gl.GL_POINTS) if mouse is not None: gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1, gl.GL_FRONT)
UV[:,:,2] = UV[:,:,0] UV[:,:,3] = UV[:,:,1] pingpong = 1 compute = gloo.Program(compute_vertex, compute_fragment, count=4) compute["params"] = P compute["texture"] = UV compute["texture"].interpolation = gl.GL_NEAREST compute["texture"].wrapping = gl.GL_CLAMP_TO_EDGE compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] compute['dt'] = dt compute['dx'] = 1.0 / w compute['dy'] = 1.0 / h compute['dd'] = dd compute['pingpong'] = pingpong render = gloo.Program(render_vertex, render_fragment, count=4) render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] render["texture"] = compute["texture"] render["texture"].interpolation = gl.GL_LINEAR render["texture"].wrapping = gl.GL_CLAMP_TO_EDGE render['pingpong'] = pingpong framebuffer = gloo.FrameBuffer(color=compute["texture"], depth=gloo.DepthBuffer(w, h)) app.run(framerate=0)
ssao= gloo.Program(ssao_vertex, ssao_fragment, count=4) ssao['position']= [(0,0), (0,1), (1,0), (1,1)] ssao['base'] = 1.00 ssao['strength']= 0.20; ssao['falloff'] = 0.000002; ssao['radius'] = 0.01; ssao['normals'] = np.zeros((800,800,4),np.float32).view(gloo.Texture2D) ssao['normals'].interpolation = gl.GL_LINEAR ssao['colors'] = np.zeros((800,800,4),np.float32).view(gloo.Texture2D) ssao['colors'].interpolation = gl.GL_LINEAR ssao['noise'] = np.random.uniform(0,1,(256,256,3)) ssao['noise'].interpolation = gl.GL_LINEAR framebuffer = gloo.FrameBuffer(color= [ssao["colors"], ssao["normals"]], depth=gloo.DepthBuffer(800, 800)) @window.event def on_draw(dt): # First pass to record colors, normals and depth framebuffer.activate() window.clear() protein.draw(gl.GL_POINTS) framebuffer.deactivate() # Actual Screen Space Ambien Occlusion (SSAO) window.clear() ssao.draw(gl.GL_TRIANGLE_STRIP) @window.event def on_init():
program['fg_color'] = 0,0,0,1 program['bg_color'] = np.random.uniform(0.75, 1.00, (n, 4)) program['bg_color'][:,3] = 1 program['linewidth'] = 1.0 program['antialias'] = 1.0 program['transform'] = Trackball() program["id"] = np.arange(n,dtype=np.float32) quad = gloo.Program(quad_vertex, quad_fragment, count=4) quad['position']= [(-1,-1), (-1,1), (1,-1), (1,1)] color = np.zeros((window.height,window.width,4),np.ubyte).view(gloo.Texture2D) color.interpolation = gl.GL_LINEAR pick = np.zeros((window.height,window.width,4),np.ubyte).view(gloo.Texture2D) pick.interpolation = gl.GL_LINEAR framebuffer = gloo.FrameBuffer(color=[color,pick], depth=gloo.DepthBuffer(window.width, window.height)) quad["color"] = color index = 0 mouse = 0,0 @window.event def on_draw(dt): gl.glEnable(gl.GL_DEPTH_TEST) framebuffer.activate() window.clear() program.draw(gl.GL_POINTS) if mouse is not None: gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1, gl.GL_FRONT) r,g,b,a = gl.glReadPixels(mouse[0],mouse[1],1,1, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE)