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 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 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 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 initgl(self): if not self.pycuda_initialized: self.setup_gl(self.width, self.height) self.pycuda_initialized = True """Initalize gl states when the frame is created""" gl.glViewport(0, 0, self.width, self.height) gl.glClearColor(0.0, 0.0, 0.0, 0.0) self.dt_history = [1000 / 60] self.t0 = time.time() self.t_last = self.t0 self.nframes = 0
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 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 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_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 initializeGL(self): gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_BLEND) # gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glFrontFace(gl.GL_CCW) gl.glClearColor(*self._clear_color) gl.glDisable(gl.GL_CULL_FACE) self._init_camera() self._init_default_shader() if self.cc: self.cc.active_material_changed.connect( self._active_material_changed) # Start an update timer to refresh rendering self._timer = QTimer() self._timer.setInterval(int(1000 / self._frame_rate)) self._timer.timeout.connect(self.update) self._timer.start() self.init_done.emit()
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 initializeGL(self): gl.glEnable(gl.GL_DEPTH_TEST) gl.glShadeModel(gl.GL_FLAT) gl.glDisable(gl.GL_BLEND) gl.glFrontFace(gl.GL_CCW) gl.glClearColor(*self._clear_color) gl.glDisable(gl.GL_CULL_FACE) # Setup object self.V, self.I = object_vertices.get_2d_plane() self.program.bind(self.V) # 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 # Start an update timer to refresh rendering self._timer = QTimer() self._timer.setInterval(int(1000 / self._frame_rate)) self._timer.timeout.connect(self.update) self._timer.start()
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 _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 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 color(self, color): self._color = color gl.glClearColor(*self._color)
def clear(self): """ Clear the whole window """ gl.glClearColor(*self.color) gl.glClear(self._clearflags)
def ClearSurface(surface, v): surface.activate() gl.glClearColor(v, v, v, v) gl.glClear(gl.GL_COLOR_BUFFER_BIT) surface.deactivate()
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 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_init(self): """ Window initialization """ gl.glClearColor(*self._color)
@window.event def on_mouse_scroll(x, y, dx, dy): scale = C["scale"][0] C["scale"] = min(max(0.01, scale + .01 * dy * scale), 100) font = Font("Vera.ttf") C = gp.GlyphCollection(dtypes=[('translate', np.float32, 2)], translate='shared') for anchor_x in ['left', 'center', 'right']: for anchor_y in ['bottom', 'center', 'top']: C.append("Hello", font, anchor_x=anchor_x, anchor_y=anchor_y, color=(0,0,0,.5)) theta,dtheta = 0,0 C['u_kernel'] = gp.data.get("spatial-filters.npy") C['atlas_data'] = font.atlas C['atlas_data'].interpolation = gl.GL_LINEAR C['atlas_shape'] = font.atlas.shape[1],font.atlas.shape[0] C['scale'] = 1.0 C['model'] = np.eye(4, dtype=np.float32) gl.glClearColor(1.0, 1.0, 1.0, 1.0) gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gp.run()
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_init(self): """ Window initialization """ gl.glClearColor(*self.color)
def clear(self): self.activate() gl.glClearColor(0, 0, 0, 0) gl.glClear(gl.GL_COLOR_BUFFER_BIT) self.deactivate()
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)