def on_draw(dt): # window.clear() global rgb extent_shape = (int(shape[0] * ssaa), int(shape[1] * ssaa)) # Frame buffer object color_buf = np.zeros((extent_shape[0], extent_shape[1], 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((extent_shape[0], extent_shape[1]), np.float32).view(gloo.DepthTexture) fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf) fbo.activate() # OpenGL setup gl.glEnable(gl.GL_DEPTH_TEST) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, extent_shape[1], extent_shape[0]) gl.glDisable(gl.GL_CULL_FACE) program.draw(gl.GL_TRIANGLES, I) rgb = np.zeros((extent_shape[0], extent_shape[1], 4), dtype=np.float32) gl.glReadPixels(0, 0, extent_shape[1], extent_shape[0], gl.GL_RGBA, gl.GL_FLOAT, rgb) rgb.shape = extent_shape[0], extent_shape[1], 4 rgb = rgb[::-1, :] rgb = np.round(rgb[:, :, :3] * 255).astype( np.uint8) # Convert to [0, 255] import cv2 rgb = cv2.resize(rgb, shape, interpolation=cv2.INTER_AREA) fbo.deactivate()
def draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj): program = gloo.Program(_depth_vertex_code, _depth_fragment_code) program.bind(vertex_buffer) program['u_mv'] = _compute_model_view(mat_model, mat_view) program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj) # Frame buffer object color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture) fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf) fbo.activate() gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled gl.glClearColor(0.0, 0.0, 0.0, 0.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, shape[1], shape[0]) # Rendering program.draw(gl.GL_TRIANGLES, index_buffer) # Retrieve the contents of the FBO texture depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32) gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth) depth.shape = shape[0], shape[1], 4 depth = depth[::-1, :] depth = depth[:, :, 0] # Depth is saved in the first channel fbo.deactivate() return depth
def get_fbo(self, size): if self._fbo is not None: h, w, _ = self._fbo.color[0].shape if size == (w, h): return self._fbo w, h = size texture = np.zeros((h, w, 4), dtype=np.uint8).view(gloo.Texture2D) self._fbo = gloo.FrameBuffer(color=[texture]) return self._fbo
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 fbo(self, size): if self._fbo is not None: h, w, _ = self._fbo.color[0].shape if size == (w, h): return self._fbo w, h = size log.debug("Stage %s creating fbo %s" % (repr(self), repr((w, h, 4)))) texture = np.zeros((h, w, 4), dtype=np.uint8).view(gloo.Texture2D) self._fbo = gloo.FrameBuffer(color=[texture]) return self._fbo
def draw_color(shape, vertex_buffers, index_buffers, mat_models, mat_views, mat_projs, ambient_weight, light_color, bg_color): assert (len(vertex_buffers) == len(index_buffers)) assert (len(vertex_buffers) == len(mat_models)) assert (len(vertex_buffers) == len(mat_views)) assert (len(vertex_buffers) == len(mat_projs)) program = gloo.Program(_color_vertex_code, _color_fragment_code) # Frame buffer object color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture) fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf) fbo.activate() # OpenGL setup gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled gl.glClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3]) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, shape[1], shape[0]) for i in xrange(len(vertex_buffers)): vertex_buffer = vertex_buffers[i] index_buffer = index_buffers[i] mat_model = mat_models[i] mat_view = mat_views[i] mat_proj = mat_projs[i] program.bind(vertex_buffer) program['u_light_eye_pos'] = [0, 0, 0] program['light_color'] = [ light_color[0], light_color[1], light_color[2] ] program['u_light_ambient_w'] = ambient_weight program['u_mv'] = _compute_model_view(mat_model, mat_view) program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj) # Rendering program.draw(gl.GL_TRIANGLES, index_buffer) # Retrieve the contents of the FBO texture rgb = np.zeros((shape[0], shape[1], 4), dtype=np.float32) gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, rgb) rgb.shape = shape[0], shape[1], 4 rgb = rgb[::-1, :] rgb = np.round(rgb[:, :, :3] * 255).astype(np.uint8) # Convert to [0, 255] fbo.deactivate() return rgb
def __init__(self, size, n, w=1, autoread=False, short=False, channels=4, wrapping=gl.GL_REPEAT, interpolation=gl.GL_LINEAR): """Circular collection of FrameBuffer size: 2d dimensions in pixels n: number of framebuffers (time dimension): n=0 is a dummy n=1 is just an FBO n=2 ping-pongs (one history buffer available) n>2 increases available history (to n-1) w: number of colorbuffers per framebuffer (default 1) autoread: if True, replace cpu_state when deactivating. if False, replace when cpu property is requested since activating short: use int8 texture (otherwise float32) channels: number of color channels wrapping: gl.GL_CLAMP_TO_EDGE, GL_REPEAT, or GL_MIRRORED_REPEAT interpolation: gl.GL_NEAREST or GL_LINEAR An NBuffer has size[0]*size[1]*n*w*channels total pixels. """ self.size = size # self.dtype = np.uint8 if short else np.float32 self.dtype = torch.uint8 if short else torch.float32 def gen_tex(): ttype = gloo.Texture2D if short else gloo.TextureFloat2D # tex = np.zeros((*size[::-1], channels), self.dtype).view(ttype) tex = torch.zeros((*size[::-1], channels), dtype=self.dtype).numpy().view(ttype) tex.interpolation = interpolation tex.wrapping = wrapping return tex self._state = [ gloo.FrameBuffer(color=[gen_tex() for i in range(w)]) for j in range(n) ] self.head = 0 self.n = n self.cpu_state = None self.autoread = autoread self.readback_buffer = 0
def draw_label(shape, vertex_buffers, index_buffers, mat_models, mat_views, mat_projs, labels): assert (len(vertex_buffers) == len(index_buffers)) assert (len(vertex_buffers) == len(mat_models)) assert (len(vertex_buffers) == len(mat_views)) assert (len(vertex_buffers) == len(mat_projs)) assert (labels is not None) assert (len(vertex_buffers) == len(labels)) program = gloo.Program(_label_vertex_code, _label_fragment_code) # Frame buffer object color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture) fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf) fbo.activate() # OpenGL setup gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled gl.glClearColor(0.0, 0.0, 0.0, 0.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, shape[1], shape[0]) for i in range(len(vertex_buffers)): vertex_buffer = vertex_buffers[i] index_buffer = index_buffers[i] mat_model = mat_models[i] mat_view = mat_views[i] mat_proj = mat_projs[i] label = labels[i] program.bind(vertex_buffer) program['u_mv'] = _compute_model_view(mat_model, mat_view) # program['u_nm'] = compute_normal_matrix(model, view) program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj) program['label'] = label # Rendering program.draw(gl.GL_TRIANGLES, index_buffer) # Retrieve the contents of the FBO texture label_map = np.zeros((shape[0], shape[1], 4), dtype=np.float32) gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, label_map) label_map.shape = shape[0], shape[1], 4 label_map = label_map[::-1, :] label_map = label_map[:, :, 0] fbo.deactivate() return label_map
def __init__(self, width, height, mode='rgb+depth', shading='phong', bg_color=(0.0, 0.0, 0.0, 0.0)): """Constructor. :param width: Width of the rendered image. :param height: Height of the rendered image. :param mode: Rendering mode ('rgb+depth', 'rgb', 'depth'). :param shading: Type of shading ('flat', 'phong'). :param bg_color: Color of the background (R, G, B, A). """ super(RendererPython, self).__init__(width, height) self.mode = mode self.shading = shading self.bg_color = bg_color # Indicators whether to render RGB and/or depth image. self.render_rgb = self.mode in ['rgb', 'rgb+depth'] self.render_depth = self.mode in ['depth', 'rgb+depth'] # Structures to store object models and related info. self.models = {} self.model_bbox_corners = {} self.model_textures = {} # Rendered images. self.rgb = None self.depth = None # Window for rendering. self.window = app.Window(visible=False) # Per-object vertex and index buffer. self.vertex_buffers = {} self.index_buffers = {} # Per-object OpenGL programs for rendering of RGB and depth images. self.rgb_programs = {} self.depth_programs = {} # The frame buffer object. rgb_buf = np.zeros((self.height, self.width, 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((self.height, self.width), np.float32).view(gloo.DepthTexture) self.fbo = gloo.FrameBuffer(color=rgb_buf, depth=depth_buf) # Activate the created frame buffer object. self.fbo.activate()
def on_draw(dt): nonlocal depth color_buf = np.zeros((imgsize, imgsize, 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((imgsize, imgsize), np.float32).view(gloo.DepthTexture) fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf) fbo.activate() window.clear() # Fill cube gl.glDisable(gl.GL_BLEND) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_POLYGON_OFFSET_FILL) gl.glClearColor(0.0, 0.0, 0.0, 0.0) # Rotate cube model = np.eye(4, dtype=np.float32) # model = R_ @ model glm.rotate(model, R[0], 0, 0, 1) glm.rotate(model, R[1], 0, 1, 0) glm.rotate(model, R[2], 1, 0, 0) # Translate cube glm.translate(model, *t) cube['u_model'] = model # cube['u_normal'] = np.array(np.matrix(np.dot(view, model)).I.T) cube.draw(gl.GL_TRIANGLES, I) # depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32) # gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth) # depth.shape = shape[0], shape[1], 4 # depth = depth[::-1, :] # depth = depth[:, :, 0] # Depth is saved in the first channel # Export screenshot gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_FLOAT, depthbuffer) # png.from_array(np.floor((depthbuffer - 0) / depthbuffer.max() * 255).astype(np.uint8), # 'RGB').save('./images' + # f'depth{time.time()}.png') fbo.deactivate() fbo.delete() depth = depthbuffer.reshape((128, 128, 3))[::-1, :, 0]
def draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj): assert type(mat_view) is list, 'Requires list of arrays.' program = gloo.Program(_depth_vertex_code, _depth_fragment_code) program.bind(vertex_buffer) # Frame buffer object color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture) fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf) fbo.activate() # OpenGL setup gl.glEnable(gl.GL_DEPTH_TEST) gl.glClearColor(0.0, 0.0, 0.0, 0.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, shape[1], shape[0]) for i in range(len(mat_view)): program['u_mv'] = _compute_model_view(mat_model, mat_view[i]) program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view[i], mat_proj) # Keep the back-face culling disabled because of objects which do not have # well-defined surface (e.g. the lamp from the dataset of Hinterstoisser) gl.glDisable(gl.GL_CULL_FACE) # gl.glEnable(gl.GL_CULL_FACE) # gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled # Rendering program.draw(gl.GL_TRIANGLES, index_buffer) # Retrieve the contents of the FBO texture depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32) gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth) depth.shape = shape[0], shape[1], 4 depth = depth[::-1, :] depth = depth[:, :, 0] # Depth is saved in the first channel fbo.deactivate() return depth
def draw_label(shape, vertex_buffer, index_buffer, texture, mat_model, mat_view, mat_proj, ambient_weight, bg_color, shading, inst_ids): assert type(mat_view) is list, 'Requires list of arrays.' program = gloo.Program(_label_vertex_code, _label_fragment_code) program.bind(vertex_buffer) # FBO color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture) fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf) fbo.activate() # OpenGL setup gl.glEnable(gl.GL_DEPTH_TEST) gl.glClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3]) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, shape[1], shape[0]) print('inst ids = ' + str(inst_ids)) for i in range(len(mat_view)): program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view[i], mat_proj) program['inst_id'] = inst_ids[ i] / 255. # input instance-id is mapped to range [0,1] gl.glDisable(gl.GL_CULL_FACE) program.draw(gl.GL_TRIANGLES, index_buffer) label = np.zeros((shape[0], shape[1], 4), dtype=np.float32) gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, label) label.shape = shape[0], shape[1], 4 label = label[::-1, :] label = np.round(label[:, :, 0] * 255).astype( np.uint8) # Label is saved in the first channel fbo.deactivate() return label
def __init__(self, model, im_size, texture=None, bg_color=(0.0, 0.0, 0.0, 0.0), ambient_weight=0.5, shading='flat', mode='rgb+depth'): # Set texture / color of vertices texture_uv = np.zeros((model['pts'].shape[0], 2), np.float32) colors = np.ones((model['pts'].shape[0], 3), np.float32) * 0.5 # Set the vertex data vertices_type = [('a_position', np.float32, 3), ('a_normal', np.float32, 3), ('a_color', np.float32, colors.shape[1]), ('a_texcoord', np.float32, 2)] vertices = np.array( list(zip(model['pts'], model['normals'], colors, texture_uv)), vertices_type) # Create buffers self.vertex_buffer = vertices.view(gloo.VertexBuffer) self.index_buffer = model['faces'].flatten().astype(np.uint32).view( gloo.IndexBuffer) self.window = app.Window(visible=False) self.program = gloo.Program(_normal_vertex_code, _normal_fragment_code) self.program.bind(self.vertex_buffer) scale = max(im_size) shape = (scale, scale) color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture) fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf) fbo.activate() gl.glEnable(gl.GL_DEPTH_TEST) gl.glClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3]) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, shape[1], shape[0]) gl.glDisable(gl.GL_CULL_FACE)
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_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)
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)
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) accumulation = np.zeros((window.height, window.width, 4), np.float32).view(gloo.TextureFloat2D) revealage = np.zeros((window.height, window.width), np.ubyte).view(gloo.Texture2D) framebuffer = gloo.FrameBuffer(color=[accumulation]) quads = gloo.Program(vert_quads, frag_quads, count=12) quads["position"] = [(-1, -1, -1), (-1, +1, -1), (+1, -1, -1), (+1, +1, -1), (-1, -1, 0), (-1, +1, 0), (+1, -1, 0), (+1, +1, 0), (-1, -1, +1), (-1, +1, +1), (+1, -1, +1), (+1, +1, +1)] quads["position"] *= 10 quads["color"] = C1, C1, C1, C1, C2, C2, C2, C2, C3, C3, C3, C3 indices = np.zeros((3, 6), dtype=np.uint32) indices[0] = 0 + np.array([0, 1, 2, 1, 2, 3]) indices[1] = 4 + np.array([0, 1, 2, 1, 2, 3]) indices[2] = 8 + np.array([0, 1, 2, 1, 2, 3]) indices = indices.view(gloo.IndexBuffer) # Post composition
def draw_color( shape, vertex_buffer, index_buffer, texture, mat_model, mat_view, mat_proj, ambient_weight, bg_color, shading, ): # Set shader for the selected shading if shading == "flat": color_fragment_code = _color_fragment_flat_code else: # 'phong' color_fragment_code = _color_fragment_phong_code program = gloo.Program(_color_vertex_code, color_fragment_code) program.bind(vertex_buffer) program["u_light_eye_pos"] = [0, 0, 0] # Camera origin program["u_light_ambient_w"] = ambient_weight program["u_mv"] = _compute_model_view(mat_model, mat_view) program["u_nm"] = _compute_normal_matrix(mat_model, mat_view) program["u_mvp"] = _compute_model_view_proj(mat_model, mat_view, mat_proj) if texture is not None: program["u_use_texture"] = int(True) program["u_texture"] = texture else: program["u_use_texture"] = int(False) program["u_texture"] = np.zeros((1, 1, 4), np.float32) # Frame buffer object color_buf = np.zeros((shape[0], shape[1], 4), np.float32).view(gloo.TextureFloat2D) depth_buf = np.zeros((shape[0], shape[1]), np.float32).view(gloo.DepthTexture) fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf) fbo.activate() # OpenGL setup gl.glEnable(gl.GL_DEPTH_TEST) gl.glClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3]) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, shape[1], shape[0]) # gl.glEnable(gl.GL_BLEND) # gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) # gl.glHint(gl.GL_POLYGON_SMOOTH_HINT, gl.GL_NICEST) # gl.glDisable(gl.GL_LINE_SMOOTH) # gl.glDisable(gl.GL_POLYGON_SMOOTH) # gl.glEnable(gl.GL_MULTISAMPLE) # Keep the back-face culling disabled because of objects which do not have # well-defined surface (e.g. the lamp from the dataset of Hinterstoisser) gl.glDisable(gl.GL_CULL_FACE) # gl.glEnable(gl.GL_CULL_FACE) # gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled # Rendering program.draw(gl.GL_TRIANGLES, index_buffer) # Retrieve the contents of the FBO texture rgb = np.zeros((shape[0], shape[1], 4), dtype=np.float32) gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, rgb) rgb.shape = shape[0], shape[1], 4 rgb = rgb[::-1, :] rgb = np.round(rgb[:, :, :3] * 255).astype(np.uint8) # Convert to [0, 255] fbo.deactivate() return rgb
def __init__(self, width, height, depth, interpolation=gl.GL_NEAREST): self.texture = np.zeros((height, width, depth), np.float32).view(gloo.TextureFloat2D) self.texture.interpolation = interpolation self.framebuffer = gloo.FrameBuffer(color=self.texture) self.clear()
offset = offsets["8 rooks"] width, height, zoom = 32, 16, 20 p0, p1, p2 = (26, 3), (10, 13), (4, 6) window = app.Window(width=width * zoom, height=height * zoom, color=(0, 0, 0, 1)) scene = gloo.Program(vertex, scene_fragment, count=3) V = np.array([p0, p1, p2]) scene['position'] = 2 * V / (width, height) - 1 tex1 = np.zeros((height, width, 4), np.float32).view(gloo.Texture2D) ssaa = gloo.Program(vertex, ssaa_fragment, count=4) framebuffer_1 = gloo.FrameBuffer(color=tex1) ssaa['position'] = (-1, +1), (+1, +1), (-1, -1), (+1, -1) ssaa['texture'] = tex1 ssaa['offset'] = 0, 0 tex2 = np.zeros((height, width, 4), np.float32).view(gloo.Texture2D) final = gloo.Program(vertex, final_fragment, count=4) framebuffer_2 = gloo.FrameBuffer(color=tex2) final['position'] = (-1, +1), (+1, +1), (-1, -1), (+1, -1) final['texture'] = tex2 final['offset'] = 0, 0 offset_index = 0 framebuffer = np.zeros((window.height, window.width * 3), dtype=np.uint8)
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) @window.event def on_resize(width, height): cube['u_projection'] = glm.perspective(45.0, width / float(height), 2.0,
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]) 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)
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:
{ vec4 color = texture2D(texture, v_texcoord); gl_FragColor = color / vec4(20,20,20,1); } """ window = app.Window(width=512, height=512) @window.event def on_draw(dt): window.clear() framebuffer.activate() quad_1.draw(gl.GL_TRIANGLE_STRIP) # The texture can be read as np.ndarray. # out_texture = framebuffer.color[0].get() framebuffer.deactivate() quad_2.draw(gl.GL_TRIANGLE_STRIP) texture = np.zeros((window.height, window.width, 4), np.float32).view(gloo.TextureFloat2D) framebuffer = gloo.FrameBuffer(color=[texture]) quad_1 = gloo.Program(vertex_1, fragment_1, count=4) quad_1["position"] = (-1, -1), (-1, +1), (+1, -1), (+1, +1) quad_2 = gloo.Program(vertex_2, fragment_2, count=4) quad_2["position"] = (-1, -1), (-1, +1), (+1, -1), (+1, +1) quad_2["texture"] = texture app.run()
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)
gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ONE, gl.GL_ZERO, gl.GL_ONE_MINUS_SRC_ALPHA) teapot.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_process.draw(gl.GL_TRIANGLE_STRIP) accumulation = np.zeros((window.height, window.width, 4), np.float32).view(gloo.TextureFloat2D) revealage = np.zeros((window.height, window.width), np.float32).view(gloo.TextureFloat2D) framebuffer = gloo.FrameBuffer(color=[accumulation, revealage]) vertices, indices = primitives.teapot() vertices["position"] *= 10 teapot = gloo.Program(teapot_vert, teapot_frag) teapot.bind(vertices) teapot['texture'] = data.checkerboard() # Post composition post_process = gloo.Program(post_process_vert, post_process_frag) post_process['tex_accumulation'] = accumulation post_process['tex_revealage'] = revealage post_process['position'] = [(-1, -1), (-1, 1), (1, -1), (1, 1)] trackball = Trackball(Position("position"), znear=0.1, zfar=100.0, distance=50) teapot['transform'] = trackball
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():