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 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 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 run(self): glfw.set_key_callback(self.window, self.key_callback) glfw.set_cursor_pos_callback(self.window, self.mouse_cursor_pos_callback) glfw.set_mouse_button_callback(self.window, self.mouse_button_callback) glfw.set_scroll_callback(self.window, self.mouse_scroll_callback) glfw.set_window_size_callback(self.window, self.window_size_callback) while not glfw.window_should_close( self.window) and not self.quit_requested: self.app.on_draw() glfw.poll_events() self.impl.process_inputs() self.app.menu() gl.glClearColor(0., 0., 0.2, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) self.app.on_draw() imgui.render() self.impl.render(imgui.get_draw_data()) glfw.swap_buffers(self.window) self.impl.shutdown() glfw.terminate()
def __exit__(self, type, value, traceback): # Done with "original" rendering self._framebuffers[0].deactivate() # Actual filtering starts here gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glDisable(gl.GL_DEPTH_TEST) # Apply all filters using ping-pong framebuffers index = 0 for i in range(len(self._programs)-1): program = self._programs[i] if i == 0: # special case for first rendering program['filtered'] = self._framebuffers[0].color[0] else: program['filtered'] = self._framebuffers[1+index].color[0] index = (index + 1) % 2 # ping-pong self._framebuffers[index+1].activate() self._programs[i].draw(gl.GL_TRIANGLE_STRIP) self._framebuffers[index+1].deactivate() # Final rendering (no transformation) at original viewport size program = self._programs[-1] program['filtered'] = self._framebuffers[index+1].color[0] gl.glViewport( *self._viewport ) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glDisable(gl.GL_DEPTH_TEST) program.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 render_texture(self, texture): self.before_render(texture) #print("Rendering %s to texture" % repr(self)) fbo_size = None if self._force_size is not None: fbo_size = self._force_size elif texture is not None: h, w, _ = texture.shape fbo_size = w, h elif self.preferred_size is not None: fbo_size = self.preferred_size else: assert False, "A preferred size must be set if there is no input texture / no size forced" fbo = self.fbo(fbo_size) fbo.activate() gl.glViewport(0, 0, fbo_size[0], fbo_size[1]) gl.glClearColor(0.0, 0.0, 0.0, 0.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT) self._transform_flipy = True self.render(texture, fbo_size) fbo.deactivate() self.after_render() return fbo.color[0]
def activate(self, clear=False): gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0) gl.glViewport(0, 0, self.width, self.height) if clear: # self._win.clear() gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glEnable(gl.GL_DEPTH_TEST) yield self
def on_draw(dt): global theta, dtheta gl.glClear(gl.GL_COLOR_BUFFER_BIT) program.draw(gl.GL_TRIANGLES, I) theta += dtheta model = np.eye(4, dtype=np.float32) glm.rotate(model, theta, 0, 0, 1) program['model'] = model
def on_draw(dt): global theta, dtheta gl.glClear(gl.GL_COLOR_BUFFER_BIT) C.draw() theta += dtheta model = np.eye(4, dtype=np.float32) glm.rotate(model, theta, 0, 0, 1) C['model'] = model
def clear(self, color=None): """ Clear the whole window """ if color is not None: gl.glClearColor(*color) gl.glClear(self._clearflags) gl.glClearColor(*self.color) else: gl.glClear(self._clearflags)
def clear(self,color=None): """ Clear the whole window """ if color is not None: gl.glClearColor(*color) gl.glClear(self._clearflags) gl.glClearColor(*self.color) else: gl.glClear(self._clearflags)
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 paintGL(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT) if self._program is not None: # Send the updated transformation matrices to the shader self._program[OBJECT_MATRIX_NAME] = self._object_to_world self._program[VIEW_MATRIX_NAME] = self._world_to_view self._program[PROJECTION_MATRIX_NAME] = self._view_to_projection self._program[UNIFORM_VERTEX_MAXES] = self._maxes self._program[UNIFORM_VERTEX_MINS] = self._mins self._program.draw(gl.GL_TRIANGLES, self._I)
def render(self, size, render_func): fbo = self.get_fbo(size) fbo.activate() gl.glViewport(0, 0, size[0], size[1]) gl.glClearColor(0.0, 0.0, 0.0, 0.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT) render_func() fbo.deactivate() return fbo.color[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 activate(self, clear=False): self._framebuffer.activate() gl.glViewport(0, 0, self.width, self.height) if clear: # color_attachment_flags = [] # for i in range(len(self._color_attachments)): # color_attachment_flags.append(gl.GL_COLOR_ATTACHMENT0 + i) # gl.glDrawBuffers(np.array(color_attachment_flags, dtype=np.uint32)) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glEnable(gl.GL_DEPTH_TEST) yield self self._framebuffer.deactivate()
def paintGL(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT) self.program.draw(gl.GL_TRIANGLES, self.I) self._current_frame += 1 if self.callback and (self._current_frame % self.cb_freg) == 0 and self._current_frame > 0: self.callback(self.get_image()) if self._current_frame >= self._frame_count: self.rendered_image = self.get_image() self.close()
def draw(self): # consume all segments present at start of drawing with self.target: gl.glViewport(0, 0, *self.target.size) gl.glClear(gl.GL_COLOR_BUFFER_BIT) n = self.segments.qsize() for _ in range(n): p, c, s = self.segments.get() self.data['a_position'][:] = p self.data['a_color'][:] = c self.data['a_size'][:] = s # gl.glClearColor(0,0,0,0.001) # gl.glEnable(gl.GL_BLEND) # gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) self.program.draw(gl.GL_POINTS)
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 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): global pingpong pingpong = 1 - pingpong compute["pingpong"] = pingpong render["pingpong"] = pingpong gl.glDisable(gl.GL_BLEND) framebuffer.activate() gl.glViewport(0, 0, cwidth, cheight) compute.draw(gl.GL_TRIANGLE_STRIP) framebuffer.deactivate() gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glViewport(0, 0, window.width, window.height) render.draw(gl.GL_TRIANGLE_STRIP)
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 CreateObstacles(dest, width, height): dest.activate() gl.glViewport(0, 0, width, height) gl.glClearColor(0, 0, 0, 0) gl.glClear(gl.GL_COLOR_BUFFER_BIT) T = np.ones((height,width,3), np.float32).view(gloo.Texture2D) T[+1:-1,+1:-1] = 0.0 T[...,0] += disc(shape = (GridHeight,GridWidth), center = (GridHeight/2,GridWidth/2), radius = 32) T[...,2] += -2*disc(shape = (GridHeight,GridWidth), center = (GridHeight/2,GridWidth/2), radius = 32) prog_fill["Sampler"] = T prog_fill.draw(gl.GL_TRIANGLE_STRIP) dest.deactivate()
def CreateObstacles(dest, width, height): dest.activate() gl.glViewport(0, 0, width, height) gl.glClearColor(0, 0, 0, 0) gl.glClear(gl.GL_COLOR_BUFFER_BIT) T = np.ones((height, width, 3), np.float32).view(gloo.Texture2D) T[+1:-1, +1:-1] = 0.0 T[..., 0] += disc(shape=(GridHeight, GridWidth), center=(GridHeight / 2, GridWidth / 2), radius=32) T[..., 2] += -2 * disc(shape=(GridHeight, GridWidth), center=(GridHeight / 2, GridWidth / 2), radius=32) prog_fill["Sampler"] = T prog_fill.draw(gl.GL_TRIANGLE_STRIP) dest.deactivate()
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 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 _draw_rgb(self, obj_id, mat_model, mat_view, mat_proj): """Renders an RGB image. :param obj_id: ID of the object model to render. :param mat_model: 4x4 ndarray with the model matrix. :param mat_view: 4x4 ndarray with the view matrix. :param mat_proj: 4x4 ndarray with the projection matrix. :return: HxWx3 ndarray with the rendered RGB image. """ # Update the OpenGL program. program = self.rgb_programs[obj_id] program['u_light_eye_pos'] = [0, 0, 0] # Camera origin. program['u_light_ambient_w'] = self.light_ambient_weight program['u_mv'] = _calc_model_view(mat_model, mat_view) program['u_nm'] = _calc_normal_matrix(mat_model, mat_view) program['u_mvp'] = _calc_model_view_proj(mat_model, mat_view, mat_proj) # OpenGL setup. gl.glEnable(gl.GL_DEPTH_TEST) gl.glClearColor(self.bg_color[0], self.bg_color[1], self.bg_color[2], self.bg_color[3]) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, self.width, self.height) # Keep the back-face culling disabled because of objects which do not have # well-defined surface (e.g. the lamp from the lm dataset). gl.glDisable(gl.GL_CULL_FACE) # Rendering. program.draw(gl.GL_TRIANGLES, self.index_buffers[obj_id]) # Get the content of the FBO texture. rgb = np.zeros((self.height, self.width, 4), dtype=np.float32) gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl.GL_FLOAT, rgb) rgb.shape = (self.height, self.width, 4) rgb = rgb[::-1, :] rgb = np.round(rgb[:, :, :3] * 255).astype( np.uint8) # Convert to [0, 255]. return rgb
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 draw_color(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj, ambient_weight, bg_color): program = gloo.Program(_color_vertex_code, _color_fragment_code) program.bind(vertex_buffer) program['u_light_eye_pos'] = [0, 0, 0] program['u_light_ambient_w'] = ambient_weight 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) # 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]) # 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 process(dt): # Poll for and process events # -> Add toolkit specific code here to process events # -> Must always exit for window in __windows__: # Make window active window.activate() # Clear window using window clear flags gl.glClear(window._clearflags) # Dispatch the main draw event window.dispatch_event('on_draw', dt) # Dispatch the idle event window.dispatch_event('on_idle', dt) # Swap buffers window.swap() return len(__windows__)
def process(dt): """ Process events for all windows. Non blocking. """ # Poll for and process events # -> Add toolkit specific code here to process events # -> Must return (non bloking) for window in __windows__: # Make window active window.activate() # Clear window using window clear flags gl.glClear(window._clearflags) # Dispatch the main draw event window.dispatch_event("on_draw", dt) # Dispatch the idle event window.dispatch_event("on_idle", dt) # Swap buffers window.swap() return len(__windows__)
def render(self, scene, cull_face=True): self.fbo.activate() gl.glEnable(gl.GL_PROGRAM_POINT_SIZE) gl.glEnable(gl.GL_DEPTH_TEST) gl.glShadeModel(gl.GL_FLAT) if cull_face: gl.glEnable(gl.GL_CULL_FACE) gl.glCullFace(gl.GL_BACK) else: gl.glDisable(gl.GL_CULL_FACE) gl.glClearColor(*self.clear_color) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, self.viewport_size[0], self.viewport_size[1]) if scene.draw_points: scene.program.draw(gl.GL_POINTS) else: assert scene.index_buffer is not None scene.program.draw(gl.GL_TRIANGLES, scene.index_buffer) if self.out_buffer_location == 'torch': frame = cpy_texture_to_tensor(self.color_buf_cuda, self.out_buf).clone() elif self.out_buffer_location == 'opengl': frame = self.out_buf else: gl.glReadPixels(0, 0, self.viewport_size[0], self.viewport_size[1], gl.GL_RGB, gl.GL_FLOAT, self.out_buf) frame = self.out_buf.copy() self.fbo.deactivate() return frame
def _draw_depth(self, obj_id, mat_model, mat_view, mat_proj): """Renders a depth image. :param obj_id: ID of the object model to render. :param mat_model: 4x4 ndarray with the model matrix. :param mat_view: 4x4 ndarray with the view matrix. :param mat_proj: 4x4 ndarray with the projection matrix. :return: HxW ndarray with the rendered depth image. """ # Update the OpenGL program. program = self.depth_programs[obj_id] program['u_mv'] = _calc_model_view(mat_model, mat_view) program['u_mvp'] = _calc_model_view_proj(mat_model, mat_view, mat_proj) # 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, self.width, self.height) # Keep the back-face culling disabled because of objects which do not have # well-defined surface (e.g. the lamp from the lm dataset). gl.glDisable(gl.GL_CULL_FACE) # Rendering. program.draw(gl.GL_TRIANGLES, self.index_buffers[obj_id]) # Get the content of the FBO texture. depth = np.zeros((self.height, self.width, 4), dtype=np.float32) gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl.GL_FLOAT, depth) depth.shape = (self.height, self.width, 4) depth = depth[::-1, :] depth = depth[:, :, 0] # Depth is saved in the first channel return depth
def clear(self): self.activate() gl.glClearColor(0, 0, 0, 0) gl.glClear(gl.GL_COLOR_BUFFER_BIT) self.deactivate()
def ClearSurface(surface, v): surface.activate() gl.glClearColor(v, v, v, v) gl.glClear(gl.GL_COLOR_BUFFER_BIT) surface.deactivate()
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 clear(self): """ Clear the whole window """ gl.glClearColor(*self.color) gl.glClear(self._clearflags)
def on_draw(dt): gl.glClear(gl.GL_COLOR_BUFFER_BIT) C.draw()
def clear(self): """ Clear the whole window """ gl.glClear(self._clearflags)
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 on_draw(dt): gl.glClear(gl.GL_COLOR_BUFFER_BIT) program.draw(gl.GL_TRIANGLE_STRIP)
def on_draw(dt): data["time"] += dt quad["time"] = data["time"] # window.clear() gl.glViewport(0, 0, window.width, window.height) # framebuffer.activate() # gl.glViewport(0, 0, width, height) # quad["time"] = 0.2 # quad.draw(gl.GL_TRIANGLE_STRIP) # quad2["time"] = 0.1 # quad2["color"] = 0,0,1,1 # quad2.draw(gl.GL_TRIANGLE_STRIP) # gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0, gl.GL_FRONT) # values = gl.glReadPixels(0, 0, width, height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) # values = np.fromstring(values, np.ubyte).reshape((height, width, 4)) # red = gl.glReadPixels(0, 0, width, height, gl.GL_RED, gl.GL_UNSIGNED_BYTE) # #red = np.fromstring(red, np.ubyte).reshape((480, 640)) # #red = np.transpose(red) # red = np.fromstring(red, np.ubyte).reshape((height, width)) # #red = np.transpose(red) # print(values.shape) # print(np.sum(values[..., 0])) # print(np.sum(values[..., 3])) # print(np.sum(red)) # print(np.min(red)) # print(np.max(red)) # print(red[320,240]) # img = values[..., [2, 1, 0, 3]] # #img = red # cv2.imwrite("img.png", img) # cv2.imshow("img", img) # cv2.waitKey(50) # framebuffer.deactivate() # framebuffer.color[0][:] = 0 # framebuffer.color[1][:] = 0 framebuffer.activate() gl.glViewport(0, 0, width, height) # gl.glDrawBuffer(gl.GL_COLOR_ATTACHMENT0 | gl.GL_COLOR_ATTACHMENT1) gl.glDrawBuffers( np.array([gl.GL_COLOR_ATTACHMENT0, gl.GL_COLOR_ATTACHMENT1], dtype=np.uint32)) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glClear(gl.GL_DEPTH_BUFFER_BIT) global phi, theta # Filled cube gl.glDisable(gl.GL_BLEND) # gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_POLYGON_OFFSET_FILL) cube['u_color'] = np.array([1, 1, 1, 1]) cube.draw(gl.GL_TRIANGLES, faces) # Outlined cube # gl.glDisable(gl.GL_POLYGON_OFFSET_FILL) # gl.glEnable(gl.GL_BLEND) # gl.glDepthMask(gl.GL_FALSE) # cube['u_color'] = np.array([0, 0, 0, 1]) # cube.draw(gl.GL_LINES, outline) # gl.glDepthMask(gl.GL_TRUE) gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0) # gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1) pixels = gl.glReadPixels(0, 0, width, height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT0) # pixels = gl.glReadPixels(0, 0, width, height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) framebuffer.deactivate() gl.glDisable(gl.GL_BLEND) gl.glDisable(gl.GL_DEPTH_TEST) # pixels = 100 * np.ones((height, width, 4), np.ubyte) # pixels[:, :, 3] = 1 # pixels[:, :, 2] = 0 gl.glDrawPixels(width, height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, pixels) # gl.glDisable(gl.GL_DEPTH_TEST) # # quad["time"] = 0.2 # quad.draw(gl.GL_TRIANGLE_STRIP) # quad2["time"] = 0.1 # quad2["color"] = 0, 0, 1, 1 # quad2.draw(gl.GL_TRIANGLE_STRIP) # gl.glEnable(gl.GL_DEPTH_TEST) # Make cube rotate theta += 0.5 # degrees 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['u_model'] = model