def on_initialize(self, event): # Note: read as bytes, then decode; py2.6 compat with open(op.join(this_dir, 'vertex_vispy.glsl'), 'rb') as fid: vert = fid.read().decode('ASCII') with open(op.join(this_dir, 'fragment_seed.glsl'), 'rb') as f: frag_seed = f.read().decode('ASCII') with open(op.join(this_dir, 'fragment_flood.glsl'), 'rb') as f: frag_flood = f.read().decode('ASCII') with open(op.join(this_dir, 'fragment_display.glsl'), 'rb') as f: frag_display = f.read().decode('ASCII') self.programs = [ Program(vert, frag_seed), Program(vert, frag_flood), Program(vert, frag_display) ] # Initialize variables # using two FBs slightly faster than switching on one self.fbo_to = [FrameBuffer(), FrameBuffer()] self._setup_textures('shape1.tga') vtype = np.dtype([('position', 'f4', 2), ('texcoord', 'f4', 2)]) vertices = np.zeros(4, dtype=vtype) vertices['position'] = [[-1., -1.], [-1., 1.], [1., -1.], [1., 1.]] vertices['texcoord'] = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]] vertices = VertexBuffer(vertices) for program in self.programs: program.bind(vertices)
def __init__(self): self.use_shaders = True app.Canvas.__init__(self, size=(512, 512), keys='interactive') # Note: read as bytes, then decode; py2.6 compat with open(op.join(this_dir, 'vertex_vispy.glsl'), 'rb') as fid: vert = fid.read().decode('ASCII') with open(op.join(this_dir, 'fragment_seed.glsl'), 'rb') as f: frag_seed = f.read().decode('ASCII') with open(op.join(this_dir, 'fragment_flood.glsl'), 'rb') as f: frag_flood = f.read().decode('ASCII') with open(op.join(this_dir, 'fragment_display.glsl'), 'rb') as f: frag_display = f.read().decode('ASCII') self.programs = [ Program(vert, frag_seed), Program(vert, frag_flood), Program(vert, frag_display) ] # Initialize variables # using two FBs slightly faster than switching on one self.fbo_to = [FrameBuffer(), FrameBuffer()] self._setup_textures('shape1.tga') vtype = np.dtype([('position', 'f4', 2), ('texcoord', 'f4', 2)]) vertices = np.zeros(4, dtype=vtype) vertices['position'] = [[-1., -1.], [-1., 1.], [1., -1.], [1., 1.]] vertices['texcoord'] = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]] vertices = VertexBuffer(vertices) for program in self.programs: program.bind(vertices) self._timer = app.Timer('auto', self.update, start=True) self.show()
def test_use_framebuffer(): """Test drawing to a framebuffer""" shape = (100, 300) # for some reason Windows wants a tall window... data = np.random.rand(*shape).astype(np.float32) use_shape = shape + (3, ) with Canvas(size=shape[::-1]) as c: orig_tex = Texture2D(data) fbo_tex = Texture2D(use_shape, format='rgb') rbo = RenderBuffer(shape, 'color') fbo = FrameBuffer(color=fbo_tex) c._glir.set_verbose(True) assert_equal(c.size, shape[::-1]) set_viewport((0, 0) + c.size) with fbo: draw_texture(orig_tex) draw_texture(fbo_tex) out_tex = _screenshot()[::-1, :, 0].astype(np.float32) assert_equal(out_tex.shape, c.size[::-1]) assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.) fbo.color_buffer = rbo fbo.depth_buffer = RenderBuffer(shape) fbo.stencil_buffer = None print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer)) clear(color='black') with fbo: clear(color='black') draw_texture(orig_tex) out_rbo = _screenshot()[:, :, 0].astype(np.float32) assert_allclose(data * 255., out_tex, atol=1) assert_allclose(data * 255., out_rbo, atol=1)
def test_use_framebuffer(): """Test drawing to a framebuffer""" shape = (100, 300) # for some reason Windows wants a tall window... data = np.random.rand(*shape).astype(np.float32) use_shape = shape + (3,) with Canvas(size=shape[::-1]) as c: orig_tex = Texture2D(data) fbo_tex = Texture2D(use_shape, format='rgb') rbo = RenderBuffer(shape, 'color') fbo = FrameBuffer(color=fbo_tex) c.context.glir.set_verbose(True) assert_equal(c.size, shape[::-1]) set_viewport((0, 0) + c.size) with fbo: draw_texture(orig_tex) draw_texture(fbo_tex) out_tex = _screenshot()[::-1, :, 0].astype(np.float32) assert_equal(out_tex.shape, c.size[::-1]) assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.) fbo.color_buffer = rbo fbo.depth_buffer = RenderBuffer(shape) fbo.stencil_buffer = None print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer)) clear(color='black') with fbo: clear(color='black') draw_texture(orig_tex) out_rbo = _screenshot()[:, :, 0].astype(np.float32) assert_allclose(data * 255., out_tex, atol=1) assert_allclose(data * 255., out_rbo, atol=1)
def test_use_framebuffer(): """Test drawing to a framebuffer""" shape = (100, 100) data = np.random.rand(*shape).astype(np.float32) orig_tex = Texture2D(data) use_shape = shape + (3,) fbo_tex = Texture2D(shape=use_shape, dtype=np.ubyte, format='rgb') rbo = ColorBuffer(shape=shape) fbo = FrameBuffer(color=fbo_tex) with Canvas(size=(100, 100)) as c: set_viewport((0, 0) + c.size) with fbo: draw_texture(orig_tex) draw_texture(fbo_tex) out_tex = _screenshot()[::-1, :, 0].astype(np.float32) assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.) fbo.color_buffer = rbo fbo.depth_buffer = DepthBuffer(shape) fbo.stencil_buffer = None print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer)) clear(color='black') with fbo: clear(color='black') draw_texture(orig_tex) out_rbo = _screenshot()[:, :, 0].astype(np.float32) assert_allclose(data * 255., out_tex, atol=1) assert_allclose(data * 255., out_rbo, atol=1)
def test_renderbuffer(): # Set with no args assert_raises(ValueError, RenderBuffer) # Set shape only R = RenderBuffer((10, 20)) assert R.shape == (10, 20) assert R.format is None # Set both shape and format gloo.context.get_current_glir_queue().clear() R = RenderBuffer((10, 20), 'color') assert R.shape == (10, 20) assert R.format is 'color' # glir_cmds = R._glir.clear() assert len(glir_cmds) == 2 assert glir_cmds[0][0] == 'CREATE' assert glir_cmds[1][0] == 'SIZE' # Orther formats assert RenderBuffer((10, 20), 'depth').format == 'depth' assert RenderBuffer((10, 20), 'stencil').format == 'stencil' # Test reset size and format R.resize((9, 9), 'depth') assert R.shape == (9, 9) assert R.format == 'depth' R.resize((8, 8), 'stencil') assert R.shape == (8, 8) assert R.format == 'stencil' # Wrong formats assert_raises(ValueError, R.resize, (9, 9), 'no_format') assert_raises(ValueError, R.resize, (9, 9), []) # Resizable R = RenderBuffer((10, 20), 'color', False) assert_raises(RuntimeError, R.resize, (9, 9), 'color') # Attaching sets the format F = FrameBuffer() # R = RenderBuffer((9, 9)) F.color_buffer = R assert F.color_buffer is R assert R.format == 'color' # F.depth_buffer = RenderBuffer((9, 9)) assert F.depth_buffer.format == 'depth' # F.stencil_buffer = RenderBuffer((9, 9)) assert F.stencil_buffer.format == 'stencil'
def test_renderbuffer(): # Set with no args assert_raises(ValueError, RenderBuffer) # Set shape only R = RenderBuffer((10, 20)) assert R.shape == (10, 20) assert R.format is None # Set both shape and format gloo.get_a_context().glir.clear() R = RenderBuffer((10, 20), 'color') assert R.shape == (10, 20) assert R.format is 'color' # glir_cmds = R._context.glir.clear() assert len(glir_cmds) == 2 assert glir_cmds[0][0] == 'CREATE' assert glir_cmds[1][0] == 'SIZE' # Orther formats assert RenderBuffer((10, 20), 'depth').format == 'depth' assert RenderBuffer((10, 20), 'stencil').format == 'stencil' # Test reset size and format R.resize((9, 9), 'depth') assert R.shape == (9, 9) assert R.format == 'depth' R.resize((8, 8), 'stencil') assert R.shape == (8, 8) assert R.format == 'stencil' # Wrong formats assert_raises(ValueError, R.resize, (9, 9), 'no_format') assert_raises(ValueError, R.resize, (9, 9), []) # Resizable R = RenderBuffer((10, 20), 'color', False) assert_raises(RuntimeError, R.resize, (9, 9), 'color') # Attaching sets the format F = FrameBuffer() # R = RenderBuffer((9, 9)) F.color_buffer = R assert F.color_buffer is R assert R.format == 'color' # F.depth_buffer = RenderBuffer((9, 9)) assert F.depth_buffer.format == 'depth' # F.stencil_buffer = RenderBuffer((9, 9)) assert F.stencil_buffer.format == 'stencil'
def on_initialize(self, event): # Build cube data # -------------------------------------- texcoord = [(0, 0), (0, 1), (1, 0), (1, 1)] vertices = [(-2, -1, 0), (-2, +1, 0), (+2, -1, 0), (+2, +1, 0)] vertices = VertexBuffer(vertices) camera_pitch = 0.0 # Degrees self.rotate = [camera_pitch, 0, 0] self.translate = [0, 0, -3] # Build program # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) scale(model, 1, 1, 1) self.phi = 0 self.cube = Program(cube_vertex, cube_fragment) self.cube['position'] = vertices # 4640 x 2256 imtex = cv2.imread(os.path.join(img_path, 'stage.jpg')) self.cube['texcoord'] = texcoord self.cube["texture"] = np.uint8( np.clip(imtex + np.random.randint(-60, 20, size=imtex.shape), 0, 255)) + 5 self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view color = Texture2D((640, 640, 3), interpolation='linear') self.framebuffer = FrameBuffer(color, RenderBuffer((640, 640))) self.quad = Program(quad_vertex, quad_fragment) self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.quad['texture'] = color self.objects = [self.cube] # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer('auto', connect=self.on_timer, start=True) self.pub_timer = app.Timer(0.1, connect=self.send_ros_img, start=True) self._set_projection(self.size)
def on_initialize(self, event): self.rho = 0.0 # Build cube data # -------------------------------------- self.checker = Program(cube_vertex, cube_fragment) self.checker['texture'] = checkerboard() self.checker['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.checker['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] # sheet, indices = make_sheet((960, 1080)) # sheet_buffer = VertexBuffer(sheet) left_eye = Texture2D((960, 1080, 3), interpolation='linear') self.left_eye_buffer = FrameBuffer(left_eye, RenderBuffer((960, 1080))) # Build program # -------------------------------------- self.view = np.eye(4, dtype=np.float32) self.program = Program(vertex, fragment) distortion_buffer = VertexBuffer(make_distortion()) self.program.bind(distortion_buffer) self.program['rotation'] = self.view self.program['texture'] = left_eye # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer('auto', connect=self.on_timer, start=True) self._set_projection(self.size)
def initialize_renderer(): """Initialize the OpenGL renderer. For an OpenGL based renderer this sets up the viewport and creates the shader programs. """ global fbuffer global fbuffer_prog global default_prog fbuffer = FrameBuffer() vertices = np.array( [[-1.0, -1.0], [+1.0, -1.0], [-1.0, +1.0], [+1.0, +1.0]], np.float32) texcoords = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]], dtype=np.float32) fbuf_vertices = VertexBuffer(data=vertices) fbuf_texcoords = VertexBuffer(data=texcoords) fbuffer_prog = Program(src_fbuffer.vert, src_fbuffer.frag) fbuffer_prog['texcoord'] = fbuf_texcoords fbuffer_prog['position'] = fbuf_vertices default_prog = Program(src_default.vert, src_default.frag) reset_view()
def __init__(self): app.Canvas.__init__(self, title='Grayscott Reaction-Diffusion', size=(512, 512), keys='interactive') self.scale = 4 self.comp_size = (256, 256) comp_w, comp_h = self.comp_size dt = 1.0 dd = 1.5 species = { # name : [r_u, r_v, f, k] 'Bacteria 1': [0.16, 0.08, 0.035, 0.065], 'Bacteria 2': [0.14, 0.06, 0.035, 0.065], 'Coral': [0.16, 0.08, 0.060, 0.062], 'Fingerprint': [0.19, 0.05, 0.060, 0.062], 'Spirals': [0.10, 0.10, 0.018, 0.050], 'Spirals Dense': [0.12, 0.08, 0.020, 0.050], 'Spirals Fast': [0.10, 0.16, 0.020, 0.050], 'Unstable': [0.16, 0.08, 0.020, 0.055], 'Worms 1': [0.16, 0.08, 0.050, 0.065], 'Worms 2': [0.16, 0.08, 0.054, 0.063], 'Zebrafish': [0.16, 0.08, 0.035, 0.060] } P = np.zeros((comp_h, comp_w, 4), dtype=np.float32) P[:, :] = species['Unstable'] UV = np.zeros((comp_h, comp_w, 4), dtype=np.float32) UV[:, :, 0] = 1.0 r = 32 UV[comp_h / 2 - r:comp_h / 2 + r, comp_w / 2 - r:comp_w / 2 + r, 0] = 0.50 UV[comp_h / 2 - r:comp_h / 2 + r, comp_w / 2 - r:comp_w / 2 + r, 1] = 0.25 UV += np.random.uniform(0.0, 0.01, (comp_h, comp_w, 4)) UV[:, :, 2] = UV[:, :, 0] UV[:, :, 3] = UV[:, :, 1] self.pingpong = 1 self.compute = Program(compute_vertex, compute_fragment, 4) self.compute["params"] = P self.compute["texture"] = UV self.compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.compute['dt'] = dt self.compute['dx'] = 1.0 / comp_w self.compute['dy'] = 1.0 / comp_h self.compute['dd'] = dd self.compute['pingpong'] = self.pingpong self.render = Program(render_vertex, render_fragment, 4) self.render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.render["texture"] = self.compute["texture"] self.render['pingpong'] = self.pingpong self.fbo = FrameBuffer(self.compute["texture"], RenderBuffer(self.comp_size)) set_state(depth_test=False, clear_color='black') self._timer = app.Timer('auto', connect=self.update, start=True)
def initialize_renderer(self): self.fbuffer = FrameBuffer() vertices = np.array( [[-1.0, -1.0], [+1.0, -1.0], [-1.0, +1.0], [+1.0, +1.0]], np.float32) texcoords = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]], dtype=np.float32) self.fbuf_vertices = VertexBuffer(data=vertices) self.fbuf_texcoords = VertexBuffer(data=texcoords) self.fbuffer_prog['texcoord'] = self.fbuf_texcoords self.fbuffer_prog['position'] = self.fbuf_vertices self.vertex_buffer = VertexBuffer() self.index_buffer = IndexBuffer()
def on_initialize(self, event): self.scale = 4 self.comp_size = (256, 256) comp_w, comp_h = self.comp_size dt = 1.0 dd = 1.5 species = { # name : [r_u, r_v, f, k] 'Bacteria 1': [0.16, 0.08, 0.035, 0.065], 'Bacteria 2': [0.14, 0.06, 0.035, 0.065], 'Coral': [0.16, 0.08, 0.060, 0.062], 'Fingerprint': [0.19, 0.05, 0.060, 0.062], 'Spirals': [0.10, 0.10, 0.018, 0.050], 'Spirals Dense': [0.12, 0.08, 0.020, 0.050], 'Spirals Fast': [0.10, 0.16, 0.020, 0.050], 'Unstable': [0.16, 0.08, 0.020, 0.055], 'Worms 1': [0.16, 0.08, 0.050, 0.065], 'Worms 2': [0.16, 0.08, 0.054, 0.063], 'Zebrafish': [0.16, 0.08, 0.035, 0.060] } P = np.zeros((comp_h, comp_w, 4), dtype=np.float32) P[:, :] = species['Unstable'] UV = np.zeros((comp_h, comp_w, 4), dtype=np.float32) UV[:, :, 0] = 1.0 r = 32 UV[comp_h / 2 - r:comp_h / 2 + r, comp_w / 2 - r:comp_w / 2 + r, 0] = 0.50 UV[comp_h / 2 - r:comp_h / 2 + r, comp_w / 2 - r:comp_w / 2 + r, 1] = 0.25 UV += np.random.uniform(0.0, 0.01, (comp_h, comp_w, 4)) UV[:, :, 2] = UV[:, :, 0] UV[:, :, 3] = UV[:, :, 1] self.pingpong = 1 self.compute = Program(compute_vertex, compute_fragment, 4) self.compute["params"] = P self.compute["texture"] = UV self.compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.compute['dt'] = dt self.compute['dx'] = 1.0 / comp_w self.compute['dy'] = 1.0 / comp_h self.compute['dd'] = dd self.compute['pingpong'] = self.pingpong self.render = Program(render_vertex, render_fragment, 4) self.render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.render["texture"] = self.compute["texture"] self.render['pingpong'] = self.pingpong self.fbo = FrameBuffer(self.compute["texture"], DepthBuffer(self.comp_size)) set_state(depth_test=False, clear_color='black')
def __init__(self): app.Canvas.__init__(self, title="Conway game of life", size=(512, 512), keys='interactive') # Build programs # -------------- self.comp_size = self.size size = self.comp_size + (4, ) Z = np.zeros(size, dtype=np.float32) Z[...] = np.random.randint(0, 2, size) Z[:256, :256, :] = 0 gun = """ ........................O........... ......................O.O........... ............OO......OO............OO ...........O...O....OO............OO OO........O.....O...OO.............. OO........O...O.OO....O.O........... ..........O.....O.......O........... ...........O...O.................... ............OO......................""" x, y = 0, 0 for i in range(len(gun)): if gun[i] == '\n': y += 1 x = 0 elif gun[i] == 'O': Z[y, x] = 1 x += 1 self.pingpong = 1 self.compute = Program(compute_vertex, compute_fragment, 4) self.compute["texture"] = Z self.compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.compute['dx'] = 1.0 / size[1] self.compute['dy'] = 1.0 / size[0] self.compute['pingpong'] = self.pingpong self.render = Program(render_vertex, render_fragment, 4) self.render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.render["texture"] = self.compute["texture"] self.render['pingpong'] = self.pingpong self.fbo = FrameBuffer(self.compute["texture"], RenderBuffer(self.comp_size)) set_state(depth_test=False, clear_color='black') self._timer = app.Timer('auto', connect=self.update, start=True) self.show()
def test_use_framebuffer(): """Test drawing to a framebuffer""" shape = (100, 300) # for some reason Windows wants a tall window... data = np.random.rand(*shape).astype(np.float32) use_shape = shape + (3, ) with Canvas(size=shape[::-1]) as c: c.app.process_events() c.set_current() if c.app.backend_name.lower() == 'pyqt5': # PyQt5 on OSX for some reason sets this to 1024x768... c.size = shape[::-1] c.app.process_events() orig_tex = Texture2D(data) fbo_tex = Texture2D(use_shape, format='rgb') rbo = RenderBuffer(shape, 'color') fbo = FrameBuffer(color=fbo_tex) c.context.glir.set_verbose(True) assert c.size == shape[::-1] c.set_current() set_viewport((0, 0) + c.size) with fbo: draw_texture(orig_tex) draw_texture(fbo_tex) out_tex = _screenshot()[::-1, :, 0].astype(np.float32) assert out_tex.shape == c.size[::-1] assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.) fbo.color_buffer = rbo fbo.depth_buffer = RenderBuffer(shape) fbo.stencil_buffer = None print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer)) clear(color='black') with fbo: clear(color='black') draw_texture(orig_tex) out_rbo = _screenshot()[:, :, 0].astype(np.float32) assert_allclose(data * 255., out_tex, atol=1) assert_allclose(data * 255., out_rbo, atol=1)
def test_use_framebuffer(): """Test drawing to a framebuffer""" shape = (100, 300) # for some reason Windows wants a tall window... data = np.random.rand(*shape).astype(np.float32) use_shape = shape + (3,) with Canvas(size=shape[::-1]) as c: c.app.process_events() c.set_current() if c.app.backend_name.lower() == 'pyqt5': # PyQt5 on OSX for some reason sets this to 1024x768... c.size = shape[::-1] c.app.process_events() orig_tex = Texture2D(data) fbo_tex = Texture2D(use_shape, format='rgb') rbo = RenderBuffer(shape, 'color') fbo = FrameBuffer(color=fbo_tex) c.context.glir.set_verbose(True) assert c.size == shape[::-1] c.set_current() set_viewport((0, 0) + c.size) with fbo: draw_texture(orig_tex) draw_texture(fbo_tex) out_tex = _screenshot()[::-1, :, 0].astype(np.float32) assert out_tex.shape == c.size[::-1] assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.) fbo.color_buffer = rbo fbo.depth_buffer = RenderBuffer(shape) fbo.stencil_buffer = None print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer)) clear(color='black') with fbo: clear(color='black') draw_texture(orig_tex) out_rbo = _screenshot()[:, :, 0].astype(np.float32) assert_allclose(data * 255., out_tex, atol=1) assert_allclose(data * 255., out_rbo, atol=1)
def initialize_renderer(self): self.fbuffer = FrameBuffer() vertices = np.array( [[-1.0, -1.0], [+1.0, -1.0], [-1.0, +1.0], [+1.0, +1.0]], np.float32) texcoords = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]], dtype=np.float32) self.fbuf_vertices = VertexBuffer(data=vertices) self.fbuf_texcoords = VertexBuffer(data=texcoords) self.fbuffer_prog = Program(src_fbuffer.vert, src_fbuffer.frag) self.fbuffer_prog['texcoord'] = self.fbuf_texcoords self.fbuffer_prog['position'] = self.fbuf_vertices self.vertex_buffer = VertexBuffer() self.index_buffer = IndexBuffer() self.default_prog = Program(src_default.vert, src_default.frag) self.reset_view()
def on_initialize(self, event): # Build cube data # -------------------------------------- vertices, indices, _ = create_cube() vertices = VertexBuffer(vertices) self.indices = IndexBuffer(indices) # Build program # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) translate(view, 0, 0, -7) self.phi, self.theta = 60, 20 rotate(model, self.theta, 0, 0, 1) rotate(model, self.phi, 0, 1, 0) self.cube = Program(cube_vertex, cube_fragment) self.cube.bind(vertices) self.cube["texture"] = checkerboard() self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view depth = DepthBuffer((512, 512)) color = Texture2D(shape=(512, 512, 3), interpolation='linear', dtype=np.dtype(np.float32)) self.framebuffer = FrameBuffer(color=color, depth=depth) self.quad = Program(quad_vertex, quad_fragment, count=4) self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.quad['texture'] = color # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer('auto', connect=self.on_timer, start=True) self._set_projection(self.size)
def on_initialize(self, event): # Build cube data # -------------------------------------- texcoord = [(0, 0), (0, 1), (1, 0), (1, 1)] vertices = [(-2, -1, 0), (-2, +1, 0), (+2, -1, 0), (+2, +1, 0)] vertices = VertexBuffer(vertices) camera_pitch = 0.0 # Degrees self.rotate = [camera_pitch, 0, 0] self.translate = [0, 0, -3] # Build program # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) scale(model, 1, 1, 1) self.phi = 0 self.cube = Program(cube_vertex, cube_fragment) self.cube['position'] = vertices # 4640 x 2256 imtex = cv2.imread(os.path.join(img_path, 'stage.jpg')) self.cube['texcoord'] = texcoord self.cube["texture"] = np.uint8(np.clip(imtex + np.random.randint(-60, 20, size=imtex.shape), 0, 255)) + 5 self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view color = Texture2D((640, 640, 3), interpolation='linear') self.framebuffer = FrameBuffer(color, RenderBuffer((640, 640))) self.quad = Program(quad_vertex, quad_fragment) self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.quad['texture'] = color self.objects = [self.cube] # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer('auto', connect=self.on_timer, start=True) self.pub_timer = app.Timer(0.1, connect=self.send_ros_img, start=True) self._set_projection(self.size)
def on_initialize(self, event): # Build programs # -------------- self.comp_size = (512, 512) size = self.comp_size + (4, ) Z = np.zeros(size, dtype=np.float32) Z[...] = np.random.randint(0, 2, size) Z[:256, :256, :] = 0 gun = """ ........................O........... ......................O.O........... ............OO......OO............OO ...........O...O....OO............OO OO........O.....O...OO.............. OO........O...O.OO....O.O........... ..........O.....O.......O........... ...........O...O.................... ............OO......................""" x, y = 0, 0 for i in range(len(gun)): if gun[i] == '\n': y += 1 x = 0 elif gun[i] == 'O': Z[y, x] = 1 x += 1 self.pingpong = 1 self.compute = Program(compute_vertex, compute_fragment, 4) self.compute["texture"] = Z self.compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.compute['dx'] = 1.0 / size[1] self.compute['dy'] = 1.0 / size[0] self.compute['pingpong'] = self.pingpong self.render = Program(render_vertex, render_fragment, 4) self.render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.render["texture"] = self.compute["texture"] self.render['pingpong'] = self.pingpong self.fbo = FrameBuffer(self.compute["texture"], DepthBuffer(self.comp_size)) set_state(depth_test=False, clear_color='black')
def on_initialize(self, event): # Build cube data # -------------------------------------- vertices, indices, _ = cube() vertices = VertexBuffer(vertices) self.indices = IndexBuffer(indices) # Build program # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) translate(view, 0, 0, -7) self.phi, self.theta = 60, 20 rotate(model, self.theta, 0, 0, 1) rotate(model, self.phi, 0, 1, 0) self.cube = Program(cube_vertex, cube_fragment) self.cube.bind(vertices) self.cube["texture"] = checkerboard() self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view depth = DepthBuffer((512, 512)) color = Texture2D(shape=(512, 512, 3), dtype=np.dtype(np.float32)) self.framebuffer = FrameBuffer(color=color, depth=depth) self.quad = Program(quad_vertex, quad_fragment, count=4) self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.quad['texture'] = color self.quad["texture"].interpolation = 'linear' # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer(1.0 / 60) self.timer.connect(self.on_timer) self.timer.start() self._set_projection(self.size)
def __init__(self): app.Canvas.__init__(self, title='Framebuffer post-processing', keys='interactive', size=(512, 512)) # Build cube data # -------------------------------------- vertices, indices, _ = create_cube() vertices = VertexBuffer(vertices) self.indices = IndexBuffer(indices) # Build program # -------------------------------------- view = translate((0, 0, -7)) self.phi, self.theta = 60, 20 model = rotate(self.theta, (0, 0, 1)).dot(rotate(self.phi, (0, 1, 0))) self.cube = Program(cube_vertex, cube_fragment) self.cube.bind(vertices) self.cube["texture"] = checkerboard() self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view color = Texture2D((512, 512, 3), interpolation='linear') self.framebuffer = FrameBuffer(color, RenderBuffer((512, 512))) self.quad = Program(quad_vertex, quad_fragment, count=4) self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.quad['texture'] = color # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer('auto', connect=self.on_timer, start=True) self._set_projection(self.physical_size) self.show()
class Canvas(app.Canvas): def __init__(self): app.Canvas.__init__(self, title='Framebuffer post-processing', keys='interactive', size=(512, 512)) def on_initialize(self, event): # Build cube data # -------------------------------------- vertices, indices, _ = create_cube() vertices = VertexBuffer(vertices) self.indices = IndexBuffer(indices) # Build program # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) translate(view, 0, 0, -7) self.phi, self.theta = 60, 20 rotate(model, self.theta, 0, 0, 1) rotate(model, self.phi, 0, 1, 0) self.cube = Program(cube_vertex, cube_fragment) self.cube.bind(vertices) self.cube["texture"] = checkerboard() self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view depth = DepthBuffer((512, 512)) color = Texture2D(shape=(512, 512, 3), interpolation='linear', dtype=np.dtype(np.float32)) self.framebuffer = FrameBuffer(color=color, depth=depth) self.quad = Program(quad_vertex, quad_fragment, count=4) self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.quad['texture'] = color # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer('auto', connect=self.on_timer, start=True) self._set_projection(self.size) def on_draw(self, event): self.framebuffer.activate() set_viewport(0, 0, 512, 512) clear(color=True, depth=True) set_state(depth_test=True) self.cube.draw('triangles', self.indices) self.framebuffer.deactivate() set_viewport(0, 0, *self.size) clear(color=True) set_state(depth_test=False) self.quad.draw('triangle_strip') def on_resize(self, event): self._set_projection(event.size) def _set_projection(self, size): width, height = size set_viewport(0, 0, width, height) projection = perspective(30.0, width / float(height), 2.0, 10.0) self.cube['projection'] = projection def on_timer(self, event): self.theta += .5 self.phi += .5 model = np.eye(4, dtype=np.float32) rotate(model, self.theta, 0, 0, 1) rotate(model, self.phi, 0, 1, 0) self.cube['model'] = model self.update()
class OpenGLRenderer(ABC): def __init__(self, src_fbuffer, src_default): self.default_prog = None self.fbuffer_prog = None self.fbuffer = None self.fbuffer_tex_front = None self.fbuffer_tex_back = None self.vertex_buffer = None self.index_buffer = None # Renderer Globals: STYLE/MATERIAL PROPERTIES # self.style = Style() # Renderer Globals: Curves self.stroke_weight = 1 self.stroke_cap = ROUND self.stroke_join = MITER # Renderer Globals # VIEW MATRICES, ETC # self.viewport = None self.texture_viewport = None self.transform_matrix = np.identity(4) self.projection_matrix = np.identity(4) # Renderer Globals: RENDERING self.draw_queue = [] # Shaders self.fbuffer_prog = Program(src_fbuffer.vert, src_fbuffer.frag) self.default_prog = Program(src_default.vert, src_default.frag) def initialize_renderer(self): self.fbuffer = FrameBuffer() vertices = np.array( [[-1.0, -1.0], [+1.0, -1.0], [-1.0, +1.0], [+1.0, +1.0]], np.float32) texcoords = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]], dtype=np.float32) self.fbuf_vertices = VertexBuffer(data=vertices) self.fbuf_texcoords = VertexBuffer(data=texcoords) self.fbuffer_prog['texcoord'] = self.fbuf_texcoords self.fbuffer_prog['position'] = self.fbuf_vertices self.vertex_buffer = VertexBuffer() self.index_buffer = IndexBuffer() def render_default(self, draw_type, draw_queue): # 1. Get the maximum number of vertices persent in the shapes # in the draw queue. # if len(draw_queue) == 0: return num_vertices = 0 for vertices, _, _ in draw_queue: num_vertices = num_vertices + len(vertices) # 2. Create empty buffers based on the number of vertices. # data = np.zeros(num_vertices, dtype=[('position', np.float32, 3), ('color', np.float32, 4)]) # 3. Loop through all the shapes in the geometry queue adding # it's information to the buffer. # sidx = 0 draw_indices = [] for vertices, idx, color in draw_queue: num_shape_verts = len(vertices) data['position'][sidx:(sidx + num_shape_verts), ] = np.array(vertices) color_array = np.array([color] * num_shape_verts) data['color'][sidx:sidx + num_shape_verts, :] = color_array draw_indices.append(sidx + idx) sidx += num_shape_verts self.vertex_buffer.set_data(data) self.index_buffer.set_data(np.hstack(draw_indices)) # 4. Bind the buffer to the shader. # self.default_prog.bind(self.vertex_buffer) # 5. Draw the shape using the proper shape type and get rid of # the buffers. # self.default_prog.draw(draw_type, indices=self.index_buffer) def cleanup(self): """Run the clean-up routine for the renderer. This method is called when all drawing has been completed and the program is about to exit. """ self.default_prog.delete() self.fbuffer_prog.delete() self.fbuffer.delete() def _transform_vertices(self, vertices, local_matrix, global_matrix): return np.dot(np.dot(vertices, local_matrix.T), global_matrix.T)[:, :3]
class Renderer2D: def __init__(self): self.default_prog = None self.fbuffer_prog = None self.texture_prog = None self.line_prog = None self.fbuffer = None self.fbuffer_tex_front = None self.fbuffer_tex_back = None self.vertex_buffer = None self.index_buffer = None ## Renderer Globals: USEFUL CONSTANTS self.COLOR_WHITE = (1, 1, 1, 1) self.COLOR_BLACK = (0, 0, 0, 1) self.COLOR_DEFAULT_BG = (0.8, 0.8, 0.8, 1.0) ## Renderer Globals: STYLE/MATERIAL PROPERTIES ## self.background_color = self.COLOR_DEFAULT_BG self.fill_color = self.COLOR_WHITE self.fill_enabled = True self.stroke_color = self.COLOR_BLACK self.stroke_enabled = True self.tint_color = self.COLOR_BLACK self.tint_enabled = False ## Renderer Globals: Curves self.stroke_weight = 1 self.stroke_cap = 2 self.stroke_join = 0 ## Renderer Globals ## VIEW MATRICES, ETC ## self.viewport = None self.texture_viewport = None self.transform_matrix = np.identity(4) self.modelview_matrix = np.identity(4) self.projection_matrix = np.identity(4) ## Renderer Globals: RENDERING self.draw_queue = [] def initialize_renderer(self): self.fbuffer = FrameBuffer() vertices = np.array( [[-1.0, -1.0], [+1.0, -1.0], [-1.0, +1.0], [+1.0, +1.0]], np.float32) texcoords = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]], dtype=np.float32) self.fbuf_vertices = VertexBuffer(data=vertices) self.fbuf_texcoords = VertexBuffer(data=texcoords) self.fbuffer_prog = Program(src_fbuffer.vert, src_fbuffer.frag) self.fbuffer_prog['texcoord'] = self.fbuf_texcoords self.fbuffer_prog['position'] = self.fbuf_vertices self.vertex_buffer = VertexBuffer() self.index_buffer = IndexBuffer() self.default_prog = Program(src_default.vert, src_default.frag) self.texture_prog = Program(src_texture.vert, src_texture.frag) self.texture_prog['texcoord'] = self.fbuf_texcoords self.reset_view() def reset_view(self): self.viewport = ( 0, 0, int(builtins.width * builtins.pixel_x_density), int(builtins.height * builtins.pixel_y_density), ) self.texture_viewport = ( 0, 0, builtins.width, builtins.height, ) gloo.set_viewport(*self.viewport) cz = (builtins.height / 2) / math.tan(math.radians(30)) self.projection_matrix = matrix.perspective_matrix( math.radians(60), builtins.width / builtins.height, 0.1 * cz, 10 * cz) self.modelview_matrix = matrix.translation_matrix(-builtins.width / 2, \ builtins.height / 2, \ -cz) self.modelview_matrix = self.modelview_matrix.dot( matrix.scale_transform(1, -1, 1)) self.transform_matrix = np.identity(4) self.default_prog['modelview'] = self.modelview_matrix.T.flatten() self.default_prog['projection'] = self.projection_matrix.T.flatten() self.texture_prog['modelview'] = self.modelview_matrix.T.flatten() self.texture_prog['projection'] = self.projection_matrix.T.flatten() self.line_prog = Program(src_line.vert, src_line.frag) self.line_prog['modelview'] = self.modelview_matrix.T.flatten() self.line_prog['projection'] = self.projection_matrix.T.flatten() self.line_prog["height"] = builtins.height self.fbuffer_tex_front = Texture2D( (builtins.height, builtins.width, 3)) self.fbuffer_tex_back = Texture2D((builtins.height, builtins.width, 3)) for buf in [self.fbuffer_tex_front, self.fbuffer_tex_back]: self.fbuffer.color_buffer = buf with self.fbuffer: self.clear() def clear(self, color=True, depth=True): """Clear the renderer background.""" gloo.set_state(clear_color=self.background_color) gloo.clear(color=color, depth=depth) def _comm_toggles(self, state=True): gloo.set_state(blend=state) gloo.set_state(depth_test=state) if state: gloo.set_state(blend_func=('src_alpha', 'one_minus_src_alpha')) gloo.set_state(depth_func='lequal') @contextmanager def draw_loop(self): """The main draw loop context manager. """ self.transform_matrix = np.identity(4) self.default_prog['modelview'] = self.modelview_matrix.T.flatten() self.default_prog['projection'] = self.projection_matrix.T.flatten() self.fbuffer.color_buffer = self.fbuffer_tex_back with self.fbuffer: gloo.set_viewport(*self.texture_viewport) self._comm_toggles() self.fbuffer_prog['texture'] = self.fbuffer_tex_front self.fbuffer_prog.draw('triangle_strip') yield self.flush_geometry() self.transform_matrix = np.identity(4) gloo.set_viewport(*self.viewport) self._comm_toggles(False) self.clear() self.fbuffer_prog['texture'] = self.fbuffer_tex_back self.fbuffer_prog.draw('triangle_strip') self.fbuffer_tex_front, self.fbuffer_tex_back = self.fbuffer_tex_back, self.fbuffer_tex_front def _transform_vertices(self, vertices, local_matrix, global_matrix): return np.dot(np.dot(vertices, local_matrix.T), global_matrix.T)[:, :3] def _add_to_draw_queue_simple(self, stype, vertices, idx, fill, stroke, stroke_weight, stroke_cap, stroke_join): """Adds shape of stype to draw queue """ if stype == 'lines': self.draw_queue.append( (stype, (vertices, idx, stroke, stroke_weight, stroke_cap, stroke_join))) else: self.draw_queue.append((stype, (vertices, idx, fill))) def render(self, shape): fill = shape.fill.normalized if shape.fill else None stroke = shape.stroke.normalized if shape.stroke else None stroke_weight = shape.stroke_weight stroke_cap = shape.stroke_cap stroke_join = shape.stroke_join obj_list = get_render_primitives(shape) for obj in obj_list: stype, vertices, idx = obj # Transform vertices vertices = self._transform_vertices( np.hstack([vertices, np.ones((len(vertices), 1))]), shape._matrix, self.transform_matrix) # Add to draw queue self._add_to_draw_queue_simple(stype, vertices, idx, fill, stroke, stroke_weight, stroke_cap, stroke_join) def flush_geometry(self): """Flush all the shape geometry from the draw queue to the GPU. """ current_queue = [] for index, shape in enumerate(self.draw_queue): current_shape = self.draw_queue[index][0] current_queue.append(self.draw_queue[index][1]) if current_shape == "lines": self.render_line(current_queue) else: self.render_default(current_shape, current_queue) current_queue = [] self.draw_queue = [] def render_default(self, draw_type, draw_queue): # 1. Get the maximum number of vertices persent in the shapes # in the draw queue. # if len(draw_queue) == 0: return num_vertices = 0 for vertices, _, _ in draw_queue: num_vertices = num_vertices + len(vertices) # 2. Create empty buffers based on the number of vertices. # data = np.zeros(num_vertices, dtype=[('position', np.float32, 3), ('color', np.float32, 4)]) # 3. Loop through all the shapes in the geometry queue adding # it's information to the buffer. # sidx = 0 draw_indices = [] for vertices, idx, color in draw_queue: num_shape_verts = len(vertices) data['position'][sidx:(sidx + num_shape_verts), ] = vertices color_array = np.array([color] * num_shape_verts) data['color'][sidx:sidx + num_shape_verts, :] = color_array draw_indices.append(sidx + idx) sidx += num_shape_verts self.vertex_buffer.set_data(data) self.index_buffer.set_data(np.hstack(draw_indices)) # 4. Bind the buffer to the shader. # self.default_prog.bind(self.vertex_buffer) # 5. Draw the shape using the proper shape type and get rid of # the buffers. # self.default_prog.draw(draw_type, indices=self.index_buffer) def render_line(self, queue): ''' This rendering algorithm works by tesselating the line into multiple triangles. Reference: https://blog.mapbox.com/drawing-antialiased-lines-with-opengl-8766f34192dc ''' if len(queue) == 0: return pos = [] posPrev = [] posCurr = [] posNext = [] markers = [] side = [] linewidth = [] join_type = [] cap_type = [] color = [] for line in queue: if len(line[1]) == 0: continue for segment in line[1]: for i in range( len(segment) - 1): # the data is sent to renderer in line segments for j in [0, 0, 1, 0, 1, 1]: # all the vertices of triangles if i + j - 1 >= 0: posPrev.append(line[0][segment[i + j - 1]]) else: posPrev.append(line[0][segment[i + j]]) if i + j + 1 < len(segment): posNext.append(line[0][segment[i + j + 1]]) else: posNext.append(line[0][segment[i + j]]) posCurr.append(line[0][segment[i + j]]) markers.extend( [1.0, -1.0, -1.0, -1.0, 1.0, -1.0]) # Is the vertex up/below the line segment side.extend([1.0, 1.0, -1.0, 1.0, -1.0, -1.0]) # Left or right side of the segment pos.extend([line[0][segment[i]]] * 6) # Left vertex of each segment linewidth.extend([line[3]] * 6) join_type.extend([line[5]] * 6) cap_type.extend([line[4]] * 6) color.extend([line[2]] * 6) if len(pos) == 0: return posPrev = np.array(posPrev, np.float32) posCurr = np.array(posCurr, np.float32) posNext = np.array(posNext, np.float32) markers = np.array(markers, np.float32) side = np.array(side, np.float32) pos = np.array(pos, np.float32) linewidth = np.array(linewidth, np.float32) join_type = np.array(join_type, np.float32) cap_type = np.array(cap_type, np.float32) color = np.array(color, np.float32) self.line_prog['pos'] = gloo.VertexBuffer(pos) self.line_prog['posPrev'] = gloo.VertexBuffer(posPrev) self.line_prog['posCurr'] = gloo.VertexBuffer(posCurr) self.line_prog['posNext'] = gloo.VertexBuffer(posNext) self.line_prog['marker'] = gloo.VertexBuffer(markers) self.line_prog['side'] = gloo.VertexBuffer(side) self.line_prog['linewidth'] = gloo.VertexBuffer(linewidth) self.line_prog['join_type'] = gloo.VertexBuffer(join_type) self.line_prog['cap_type'] = gloo.VertexBuffer(cap_type) self.line_prog["color"] = gloo.VertexBuffer(color) self.line_prog.draw('triangles') def render_image(self, image, location, size): """Render the image. :param image: image to be rendered :type image: builtins.Image :param location: top-left corner of the image :type location: tuple | list | builtins.Vector :param size: target size of the image to draw. :type size: tuple | list | builtins.Vector """ self.flush_geometry() self.texture_prog[ 'fill_color'] = self.tint_color if self.tint_enabled else self.COLOR_WHITE self.texture_prog['transform'] = self.transform_matrix.T.flatten() x, y = location sx, sy = size imx, imy = image.size data = np.zeros(4, dtype=[('position', np.float32, 2), ('texcoord', np.float32, 2)]) data['texcoord'] = np.array( [[0.0, 1.0], [1.0, 1.0], [0.0, 0.0], [1.0, 0.0]], dtype=np.float32) data['position'] = np.array( [[x, y + sy], [x + sx, y + sy], [x, y], [x + sx, y]], dtype=np.float32) self.texture_prog['texture'] = image._texture self.texture_prog.bind(VertexBuffer(data)) self.texture_prog.draw('triangle_strip') def cleanup(self): """Run the clean-up routine for the renderer. This method is called when all drawing has been completed and the program is about to exit. """ self.default_prog.delete() self.fbuffer_prog.delete() self.line_prog.delete() self.fbuffer.delete()
def __init__(self, canvas, background_color=(0,0,0,0), text_color=(1,1,1)): # State.register_button(position, text) self.canvas = canvas self.text = None self.tcolor = (text_color[0],text_color[1],text_color[2], 1) self.bcolor = background_color self.timer = 0 self.fade_timer = 0 self.projection = np.eye(4) self.view = np.eye(4) self.model = np.eye(4) height, width = 5.0, 15.0 # Meters orientation_vector = (1, 1, 0) unit_orientation_angle = np.array(orientation_vector) / np.linalg.norm(orientation_vector) scale_factor = 0.4 lowest_button = -5.2 midset = 0.2 position = -2 scale(self.model, scale_factor) #yrotate(self.model, -90) # rotate(self.model, 30, *unit_orientation_angle) offset = (position * ((height + midset) * scale_factor)) translate(self.model, -4.4, lowest_button + offset, -10) self.size = (int(height*100), int(width*100)) # Add texture coordinates # Rectangle of height height self.vertices = np.array([ [-width / 2, -height / 2, 0], [ width / 2, -height / 2, 0], [ width / 2, height / 2, 0], [-width / 2, height / 2, 0], ], dtype=np.float32) self.tex_coords = np.array([ [0, 0], [1, 0], [1, 1], [0, 1], ], dtype=np.float32) self.indices = IndexBuffer([ 0, 1, 2, 2, 3, 0, ]) self.program = Program(self.toast_vertex_shader, self.toast_fragment_shader) self.program['vertex_position'] = self.vertices self.program['default_texcoord'] = self.tex_coords self.program['view'] = self.view self.program['model'] = self.model self.program['projection'] = self.projection self.program['background_color'] = (0,0,0,0) # no background yet self.program['text_color'] = (0,0,0,0) # no text yet # self.texture = Texture2D(shape=(1000, 1000) + (3,)) # self.text_buffer = FrameBuffer(self.texture, RenderBuffer((1000, 1000))) self.texture = Texture2D(shape=self.size + (3,)) self.text_buffer = FrameBuffer(self.texture, RenderBuffer(self.size)) self.program['texture'] = self.texture #self.program['text_color'] = self.tcolor # set the tcolor #self.program['background_color'] = self.bcolor # set the tcolor self.first = False # do not draw text until needed self.make_text('Default Text') # Create Empty text object
class Canvas(app.Canvas): def __init__(self): app.Canvas.__init__(self, title='Framebuffer post-processing', keys='interactive', size=(640, 640)) rospy.init_node('simulated_arm_view') self.img_pub = Image_Publisher('/sim/arm_camera/image_rect', encoding='8UC1') self.listener = tf.TransformListener() def on_initialize(self, event): # Build cube data # -------------------------------------- texcoord = [(0, 0), (0, 1), (1, 0), (1, 1)] vertices = [(-1, -1, 0), (-1, +1, 0), (+1, -1, 0), (+1, +1, 0)] vertices = VertexBuffer(vertices) #self.listener.waitForTransform("/robot", "/wrist_joint", rospy.Time(), rospy.Duration(4)) """while not rospy.is_shutdown(): try: now = rospy.Time.now() self.listener.waitForTransform("/wrist_joint", "/robot", rospy.Time(), rospy.Duration(4)) (trans,rot) = listener.lookupTransform("/wrist_joint", "/robot", rospy.Time(), rospy.Duration(4)) """ #pos, rot = self.listener.lookupTransform("/wrist_joint", "/robot", rospy.Time(0)) #print list(pos) camera_pitch = 0.0 # Degrees self.rotate = [camera_pitch, 0, 0] #self.translate = list(pos) self.translate = [0, 0, -5] # Build program # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) scale(model, 10, 10, 10) self.phi = 0 self.cube = Program(cube_vertex, cube_fragment) self.cube['position'] = vertices # 4640 x 2256 imtex = cv2.imread(os.path.join(img_path, 'rubixFront.jpg')) self.cube['texcoord'] = texcoord self.cube["texture"] = np.uint8(np.clip(imtex + np.random.randint(-60, 20, size=imtex.shape), 0, 255)) + 5 self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view color = Texture2D((640, 640, 3), interpolation='linear') self.framebuffer = FrameBuffer(color, RenderBuffer((640, 640))) self.quad = Program(quad_vertex, quad_fragment) self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.quad['texture'] = color self.objects = [self.cube] # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer('auto', connect=self.on_timer, start=True) self.pub_timer = app.Timer(0.1, connect=self.send_ros_img, start=True) self._set_projection(self.size) def send_ros_img(self, event): # Read frame buffer, get rid of alpha channel img = self.framebuffer.read()[:, :, 2] self.img_pub.publish(img) def on_draw(self, event): # Image rect with self.framebuffer: set_clear_color('gray') clear(color=True, depth=True) set_state(depth_test=True) self.cube.draw('triangle_strip') set_clear_color('pink') clear(color=True) set_state(depth_test=True) self.quad.draw('triangle_strip') def on_resize(self, event): self._set_projection(event.size) def _set_projection(self, size): width, height = self.size set_viewport(0, 0, width, height) projection = perspective(100.0, width / float(height), 1.0, 20.0) self.cube['projection'] = projection def on_timer(self, event): model = np.eye(4, dtype=np.float32) #scale(model, 1, 1, 1) self.cube['model'] = model self.view = np.eye(4) xrotate(self.view, self.rotate[0]) yrotate(self.view, self.rotate[1]) zrotate(self.view, self.rotate[2]) translate(self.view, *self.translate) self.listener.waitForTransform("/robot", "/wrist_joint", rospy.Time(), rospy.Duration(4)) pos, rot = self.listener.lookupTransform("/robot", "/wrist_joint", rospy.Time(0)) print list(pos) self.translate[0] = -list(pos)[0] * 10 self.translate[1] = -list(pos)[1] * 10 self.translate[2] = -5 self.cube['view'] = self.view self.update() def on_key_press(self, event): """Controls - a(A) - move left d(D) - move right w(W) - move up s(S) - move down""" if(event.text.lower() == 'p'): print(self.view) elif(event.text.lower() == 'd'): self.translate[0] += -0.1 elif(event.text.lower() == 'a'): self.translate[0] += 0.1 elif(event.text.lower() == 'w'): self.translate[1] += -0.1 elif(event.text.lower() == 's'): self.translate[1] += 0.1 elif(event.text.lower() == 'q'): self.rotate[2] += -1 elif(event.text.lower() == 'e'): self.rotate[2] += 1 elif(event.text.lower() == 'z'): self.translate[2] += -0.1 elif(event.text.lower() == 'x'): self.translate[2] += 0.1
def __init__(self, text, canvas, position=1, color=(0.1, 0.0, 0.7)): ''' Give this the - text to be written - main app.canvas - position (1-9, or which button position this should occupy) ''' # State Controller State.register_button(position, text) self.position = position self.canvas = canvas self.projection = np.eye(4) self.view = np.eye(4) self.model = np.eye(4) height, width = 5.0, 15.0 # Meters orientation_vector = (1, 1, 0) unit_orientation_angle = np.array(orientation_vector) / np.linalg.norm( orientation_vector) scale_factor = 0.2 lowest_button = -5.2 midset = 0.2 scale(self.model, scale_factor) yrotate(self.model, -60) # rotate(self.model, 30, *unit_orientation_angle) offset = (position * ((height + midset) * scale_factor)) translate(self.model, -7.4, lowest_button + offset, -10) pixel_to_length = 10 self.size = map(lambda o: pixel_to_length * o, [width, height]) # Add texture coordinates # Rectangle of height height self.vertices = np.array([ [-width / 2, -height / 2, 0], [width / 2, -height / 2, 0], [width / 2, height / 2, 0], [-width / 2, height / 2, 0], ], dtype=np.float32) self.tex_coords = np.array([ [0, 0], [1, 0], [1, 1], [0, 1], ], dtype=np.float32) self.indices = IndexBuffer([ 0, 1, 2, 2, 3, 0, ]) self.program = Program(self.button_vertex_shader, self.button_fragment_shader) self.program['vertex_position'] = self.vertices self.program['default_texcoord'] = self.tex_coords self.program['view'] = self.view self.program['model'] = self.model self.program['projection'] = self.projection self.program['background_color'] = color self.program['highlighted'] = 0 # self.texture = Texture2D(shape=(1000, 1000) + (3,)) # self.text_buffer = FrameBuffer(self.texture, RenderBuffer((1000, 1000))) self.texture = Texture2D(shape=(500, 1500) + (3, )) self.text_buffer = FrameBuffer(self.texture, RenderBuffer((500, 1500))) self.program['texture'] = self.texture self.text = text self.make_text(self.text) self.first = True
class Canvas(app.Canvas): def __init__(self): app.Canvas.__init__(self, title='Framebuffer post-processing', keys='interactive', size=(512, 512)) def on_initialize(self, event): # Build cube data # -------------------------------------- vertices, indices, _ = create_cube() vertices = VertexBuffer(vertices) self.indices = IndexBuffer(indices) # Build program # -------------------------------------- view = translate((0,0,-7)) self.phi, self.theta = np.radians(60), np.radians(20) model = rotate(self.theta, (0, 0, 1)) * rotate(self.phi, (0, 1, 0)) self.cube = Program(cube_vertex, cube_fragment) self.cube.bind(vertices) self.cube["texture"] = checkerboard() self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view depth = DepthBuffer((512, 512)) color = Texture2D(shape=(512, 512, 3), dtype=np.dtype(np.float32)) self.framebuffer = FrameBuffer(color=color, depth=depth) self.quad = Program(quad_vertex, quad_fragment, count=4) self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.quad['texture'] = color self.quad["texture"].interpolation = 'linear' # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer('auto', connect=self.on_timer, start=True) self._set_projection(self.size) def on_draw(self, event): self.framebuffer.activate() set_viewport(0, 0, 512, 512) clear(color=True, depth=True) set_state(depth_test=True) self.cube.draw('triangles', self.indices) self.framebuffer.deactivate() set_viewport(0, 0, *self.size) clear(color=True) set_state(depth_test=False) self.quad.draw('triangle_strip') def on_resize(self, event): self._set_projection(event.size) def _set_projection(self, size): width, height = size set_viewport(0, 0, width, height) projection = perspective(30.0, width / float(height), 2.0, 10.0) self.cube['projection'] = projection def on_timer(self, event): self.theta += .02 self.phi += .02 model = rotate(self.theta, (0, 0, 1)) * rotate(self.phi, (0, 1, 0)) self.cube['model'] = model self.update()
def test_framebuffer(): # Test init with no args F = FrameBuffer() glir_cmds = F._glir.clear() assert len(glir_cmds) == 1 glir_cmds[0][0] == 'CREATE' # Activate / deactivate F.activate() glir_cmd = F._glir.clear()[-1] assert glir_cmd[0] == 'FRAMEBUFFER' assert glir_cmd[2] is True # F.deactivate() glir_cmd = F._glir.clear()[-1] assert glir_cmd[0] == 'FRAMEBUFFER' assert glir_cmd[2] is False # with F: pass glir_cmds = F._glir.clear() assert len(glir_cmds) == 2 assert glir_cmds[0][0] == 'FRAMEBUFFER' assert glir_cmds[1][0] == 'FRAMEBUFFER' assert glir_cmds[0][2] is True and glir_cmds[1][2] is False # Init with args R = RenderBuffer((3, 3)) F = FrameBuffer(R) assert F.color_buffer is R # R2 = RenderBuffer((3, 3)) F.color_buffer = R2 assert F.color_buffer is R2 # Wrong buffers F = FrameBuffer() assert_raises(TypeError, FrameBuffer.color_buffer.fset, F, 'FOO') assert_raises(TypeError, FrameBuffer.color_buffer.fset, F, []) assert_raises(TypeError, FrameBuffer.depth_buffer.fset, F, 'FOO') assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, F, 'FOO') color_buffer = RenderBuffer((9, 9), 'color') assert_raises(ValueError, FrameBuffer.depth_buffer.fset, F, color_buffer) # But None is allowed! F.color_buffer = None # Shape R1 = RenderBuffer((3, 3)) R2 = RenderBuffer((3, 3)) R3 = RenderBuffer((3, 3)) F = FrameBuffer(R1, R2, R3) assert F.shape == R1.shape assert R1.format == 'color' assert R2.format == 'depth' assert R3.format == 'stencil' # Resize F.resize((10, 10)) assert F.shape == (10, 10) assert F.shape == R1.shape assert F.shape == R2.shape assert F.shape == R3.shape assert R1.format == 'color' assert R2.format == 'depth' assert R3.format == 'stencil' # Shape from any buffer F.color_buffer = None assert F.shape == (10, 10) F.depth_buffer = None assert F.shape == (10, 10) F.stencil_buffer = None assert_raises(RuntimeError, FrameBuffer.shape.fget, F) # Also with Texture luminance T = gloo.Texture2D((20, 30)) R = RenderBuffer(T.shape) assert T.format == 'luminance' F = FrameBuffer(T, R) assert F.shape == T.shape[:2] assert F.shape == R.shape assert T.format == 'luminance' assert R.format == 'depth' # Resize F.resize((10, 10)) assert F.shape == (10, 10) assert T.shape == (10, 10, 1) assert F.shape == R.shape assert T.format == 'luminance' assert R.format == 'depth' # Also with Texture RGB T = gloo.Texture2D((20, 30, 3)) R = RenderBuffer(T.shape) assert T.format == 'rgb' F = FrameBuffer(T, R) assert F.shape == T.shape[:2] assert F.shape == R.shape assert T.format == 'rgb' assert R.format == 'depth' # Resize F.resize((10, 10)) assert F.shape == (10, 10) assert T.shape == (10, 10, 3) assert F.shape == R.shape assert T.format == 'rgb' assert R.format == 'depth' # Wrong shape in resize assert_raises(ValueError, F.resize, (9, 9, 1)) assert_raises(ValueError, F.resize, (9, )) assert_raises(ValueError, F.resize, 'FOO')
def __init__(self): self.projection = np.eye(4) self.view = np.eye(4) self.model = np.eye(4) height, width = 3.0, 6.0 scale_factor = 0.2 x_offset = -8 y_offset = 2 pixel_to_length = 10 color = (1.0, 1.0, 1.0) scale(self.model, scale_factor) yrotate(self.model, -60) translate(self.model, x_offset, y_offset, -10) size = (int(height * 100), int(width * 100)) self.vertices = np.array([ [-width / 2, -height / 2, 0], [width / 2, -height / 2, 0], [width / 2, height / 2, 0], [-width / 2, height / 2, 0], ], dtype=np.float32) self.tex_coords = np.array([ [0, 0], [1, 0], [1, 1], [0, 1], ], dtype=np.float32) self.indices = IndexBuffer([ 0, 1, 2, 2, 3, 0, ]) self.program = Program(self.battery_vertex_shader, self.battery_fragment_shader) self.texture = Texture2D(shape=size + (3, )) self.text_buffer = FrameBuffer(self.texture, RenderBuffer(size)) images = [] images.append( Image.open( os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_low_color.png'))) images.append( Image.open( os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_used_color.png'))) images.append( Image.open( os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_full_color.png'))) self.level_texture = {} # texture for each level for x in range(0, len(images)): default_image = images[x] default_image = default_image.rotate(-90) default_image = default_image.resize(size) default_image = default_image.transpose(Image.FLIP_TOP_BOTTOM) default_image_array = np.asarray(default_image) self.level_texture[x + 1] = default_image_array #default_image_array = imageio.imread(os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_full_color.png')) # self.default_tex = Texture2D(data=default_image_array) # self.default_tex = Texture2D(shape=size + (3,)) # self.default_tex.set_data(self.level_texture[3]) self.program['vertex_position'] = self.vertices self.program['default_texcoord'] = self.tex_coords self.program['view'] = self.view self.program['model'] = self.model self.program['projection'] = self.projection self.program['hide'] = 0 # self.tex_program = Program(self.tex_vert_shader, self.battery_fragment_shader) # self.tex_program['vertex_position'] = self.vertices # self.tex_program['default_texcoord'] = self.tex_coords # self.tex_program['hide'] = 0 # self.tex_program['texture'] = self.default_tex self.flag = True # flag to update the texture self.level = 3 # level of the battery 1 - 3 full_middle_split = 75 # split between levels 2 and 3 middle_low_split = 25 # split between levels 1 and 2 fault_tolerance = 5 self.full_lower = full_middle_split - fault_tolerance # lower limit for going from 3 to 2 self.middle_upper = full_middle_split + fault_tolerance # upper limit for going from 2 to 3 self.middle_lower = middle_low_split - fault_tolerance # lower limit for going from 2 to 1 self.low_upper = middle_low_split + fault_tolerance # upper limit for going from 1 to 2
def test_framebuffer(): # Test init with no args gloo.get_a_context().glir.clear() F = FrameBuffer() glir_cmds = F._context.glir.clear() assert len(glir_cmds) == 1 glir_cmds[0][0] == 'CREATE' # Activate / deactivate F.activate() glir_cmd = F._context.glir.clear()[-1] assert glir_cmd[0] == 'FRAMEBUFFER' assert glir_cmd[2] is True # F.deactivate() glir_cmd = F._context.glir.clear()[-1] assert glir_cmd[0] == 'FRAMEBUFFER' assert glir_cmd[2] is False # with F: pass glir_cmds = F._context.glir.clear() assert len(glir_cmds) == 2 assert glir_cmds[0][0] == 'FRAMEBUFFER' assert glir_cmds[1][0] == 'FRAMEBUFFER' assert glir_cmds[0][2] is True and glir_cmds[1][2] is False # Init with args R = RenderBuffer((3, 3)) F = FrameBuffer(R) assert F.color_buffer is R # R2 = RenderBuffer((3, 3)) F.color_buffer = R2 assert F.color_buffer is R2 # Wrong buffers F = FrameBuffer() assert_raises(TypeError, FrameBuffer.color_buffer.fset, F, 'FOO') assert_raises(TypeError, FrameBuffer.color_buffer.fset, F, []) assert_raises(TypeError, FrameBuffer.depth_buffer.fset, F, 'FOO') assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, F, 'FOO') color_buffer = RenderBuffer((9, 9), 'color') assert_raises(ValueError, FrameBuffer.depth_buffer.fset, F, color_buffer) # But None is allowed! F.color_buffer = None # Shape R1 = RenderBuffer((3, 3)) R2 = RenderBuffer((3, 3)) R3 = RenderBuffer((3, 3)) F = FrameBuffer(R1, R2, R3) assert F.shape == R1.shape assert R1.format == 'color' assert R2.format == 'depth' assert R3.format == 'stencil' # Resize F.resize((10, 10)) assert F.shape == (10, 10) assert F.shape == R1.shape assert F.shape == R2.shape assert F.shape == R3.shape assert R1.format == 'color' assert R2.format == 'depth' assert R3.format == 'stencil' # Shape from any buffer F.color_buffer = None assert F.shape == (10, 10) F.depth_buffer = None assert F.shape == (10, 10) F.stencil_buffer = None assert_raises(RuntimeError, FrameBuffer.shape.fget, F) # Also with Texture luminance T = gloo.Texture2D((20, 30)) R = RenderBuffer(T.shape) assert T.format == 'luminance' F = FrameBuffer(T, R) assert F.shape == T.shape[:2] assert F.shape == R.shape assert T.format == 'luminance' assert R.format == 'depth' # Resize F.resize((10, 10)) assert F.shape == (10, 10) assert T.shape == (10, 10, 1) assert F.shape == R.shape assert T.format == 'luminance' assert R.format == 'depth' # Also with Texture RGB T = gloo.Texture2D((20, 30, 3)) R = RenderBuffer(T.shape) assert T.format == 'rgb' F = FrameBuffer(T, R) assert F.shape == T.shape[:2] assert F.shape == R.shape assert T.format == 'rgb' assert R.format == 'depth' # Resize F.resize((10, 10)) assert F.shape == (10, 10) assert T.shape == (10, 10, 3) assert F.shape == R.shape assert T.format == 'rgb' assert R.format == 'depth' # Wrong shape in resize assert_raises(ValueError, F. resize, (9, 9, 1)) assert_raises(ValueError, F. resize, (9,)) assert_raises(ValueError, F. resize, 'FOO')
class Renderer3D: def __init__(self): self.default_prog = None self.fbuffer = None self.fbuffer_tex_front = None self.fbuffer_tex_back = None self.vertex_buffer = None self.index_buffer = None ## Renderer Globals: USEFUL CONSTANTS self.COLOR_WHITE = (1, 1, 1, 1) self.COLOR_BLACK = (0, 0, 0, 1) self.COLOR_DEFAULT_BG = (0.8, 0.8, 0.8, 1.0) ## Renderer Globals: STYLE/MATERIAL PROPERTIES ## self.background_color = self.COLOR_DEFAULT_BG self.fill_color = self.COLOR_WHITE self.fill_enabled = True self.stroke_color = self.COLOR_BLACK self.stroke_enabled = True self.tint_color = self.COLOR_BLACK self.tint_enabled = False ## Renderer Globals: Curves self.stroke_weight = 1 self.stroke_cap = 2 self.stroke_join = 0 ## Renderer Globals ## VIEW MATRICES, ETC ## self.viewport = None self.texture_viewport = None self.transform_matrix = np.identity(4) self.projection_matrix = np.identity(4) self.lookat_matrix = np.identity(4) ## Renderer Globals: RENDERING self.draw_queue = [] def initialize_renderer(self): self.fbuffer = FrameBuffer() vertices = np.array( [[-1.0, -1.0], [+1.0, -1.0], [-1.0, +1.0], [+1.0, +1.0]], np.float32) texcoords = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]], dtype=np.float32) self.fbuf_vertices = VertexBuffer(data=vertices) self.fbuf_texcoords = VertexBuffer(data=texcoords) self.fbuffer_prog = Program(src_fbuffer.vert, src_fbuffer.frag) self.fbuffer_prog['texcoord'] = self.fbuf_texcoords self.fbuffer_prog['position'] = self.fbuf_vertices self.vertex_buffer = VertexBuffer() self.index_buffer = IndexBuffer() self.default_prog = Program(src_default.vert, src_default.frag) self.reset_view() def reset_view(self): self.viewport = ( 0, 0, int(builtins.width * builtins.pixel_x_density), int(builtins.height * builtins.pixel_y_density), ) self.texture_viewport = ( 0, 0, builtins.width, builtins.height, ) gloo.set_viewport(*self.viewport) cz = (builtins.height / 2) / math.tan(math.radians(30)) self.projection_matrix = matrix.perspective_matrix( math.radians(60), builtins.width / builtins.height, 0.1 * cz, 10 * cz) self.transform_matrix = np.identity(4) self.default_prog['projection'] = self.projection_matrix.T.flatten() self.default_prog['perspective_matrix'] = self.lookat_matrix.T.flatten( ) self.fbuffer_tex_front = Texture2D( (builtins.height, builtins.width, 3)) self.fbuffer_tex_back = Texture2D((builtins.height, builtins.width, 3)) for buf in [self.fbuffer_tex_front, self.fbuffer_tex_back]: self.fbuffer.color_buffer = buf with self.fbuffer: self.clear() self.fbuffer.depth_buffer = gloo.RenderBuffer( (builtins.height, builtins.width)) def clear(self, color=True, depth=True): """Clear the renderer background.""" gloo.set_state(clear_color=self.background_color) gloo.clear(color=color, depth=depth) def _comm_toggles(self, state=True): gloo.set_state(blend=state) gloo.set_state(depth_test=state) if state: gloo.set_state(blend_func=('src_alpha', 'one_minus_src_alpha')) gloo.set_state(depth_func='lequal') @contextmanager def draw_loop(self): """The main draw loop context manager. """ self.transform_matrix = np.identity(4) self.default_prog['projection'] = self.projection_matrix.T.flatten() self.default_prog['perspective_matrix'] = self.lookat_matrix.T.flatten( ) self.fbuffer.color_buffer = self.fbuffer_tex_back with self.fbuffer: gloo.set_viewport(*self.texture_viewport) self._comm_toggles() self.fbuffer_prog['texture'] = self.fbuffer_tex_front self.fbuffer_prog.draw('triangle_strip') yield self.flush_geometry() self.transform_matrix = np.identity(4) gloo.set_viewport(*self.viewport) self._comm_toggles(False) self.clear() self.fbuffer_prog['texture'] = self.fbuffer_tex_back self.fbuffer_prog.draw('triangle_strip') self.fbuffer_tex_front, self.fbuffer_tex_back = self.fbuffer_tex_back, self.fbuffer_tex_front def _transform_vertices(self, vertices, local_matrix, global_matrix): return np.dot(np.dot(vertices, local_matrix.T), global_matrix.T)[:, :3] def render(self, shape): if isinstance(shape, Geometry): n = len(shape.vertices) tverts = self._transform_vertices( np.hstack([shape.vertices, np.ones((n, 1))]), shape.matrix, self.transform_matrix) edges = shape.edges faces = shape.faces self.add_to_draw_queue('poly', tverts, edges, faces, self.fill_color, self.stroke_color) elif isinstance(shape, PShape): vertices = shape._draw_vertices n, _ = vertices.shape tverts = self._transform_vertices( np.hstack([vertices, np.zeros((n, 1)), np.ones((n, 1))]), shape._matrix, self.transform_matrix) fill = shape.fill.normalized if shape.fill else None stroke = shape.stroke.normalized if shape.stroke else None edges = shape._draw_edges faces = shape._draw_faces if edges is None: print(vertices) print("whale") exit() if 'open' in shape.attribs: overtices = shape._draw_outline_vertices no, _ = overtices.shape toverts = self._transform_vertices( np.hstack([overtices, np.zeros((no, 1)), np.ones((no, 1))]), shape._matrix, self.transform_matrix) self.add_to_draw_queue('poly', tverts, edges, faces, fill, None) self.add_to_draw_queue('path', toverts, edges[:-1], None, None, stroke) else: self.add_to_draw_queue(shape.kind, tverts, edges, faces, fill, stroke) def add_to_draw_queue(self, stype, vertices, edges, faces, fill=None, stroke=None): """Add the given vertex data to the draw queue. :param stype: type of shape to be added. Should be one of {'poly', 'path', 'point'} :type stype: str :param vertices: (N, 3) array containing the vertices to be drawn. :type vertices: np.ndarray :param edges: (N, 2) array containing edges as tuples of indices into the vertex array. This can be None when not appropriate (eg. for points) :type edges: None | np.ndarray :param faces: (N, 3) array containing faces as tuples of indices into the vertex array. For 'point' and 'path' shapes, this can be None :type faces: np.ndarray :param fill: Fill color of the shape as a normalized RGBA tuple. When set to `None` the shape doesn't get a fill (default: None) :type fill: None | tuple :param stroke: Stroke color of the shape as a normalized RGBA tuple. When set to `None` the shape doesn't get stroke (default: None) :type stroke: None | tuple """ fill_shape = self.fill_enabled and not (fill is None) stroke_shape = self.stroke_enabled and not (stroke is None) if fill_shape and stype not in ['point', 'path']: idx = np.array(faces, dtype=np.uint32).ravel() self.draw_queue.append(["triangles", (vertices, idx, fill)]) if stroke_shape: if stype == 'point': idx = np.arange(0, len(vertices), dtype=np.uint32) self.draw_queue.append(["points", (vertices, idx, stroke)]) else: idx = np.array(edges, dtype=np.uint32).ravel() self.draw_queue.append(["lines", (vertices, idx, stroke)]) def flush_geometry(self): """Flush all the shape geometry from the draw queue to the GPU. """ current_queue = [] for index, shape in enumerate(self.draw_queue): current_shape, current_obj = self.draw_queue[index][ 0], self.draw_queue[index][1] # If current_shape is lines, bring it to the front by epsilon # to resolve z-fighting if current_shape == 'lines': # line_transform is used whenever we render lines to break ties in depth # We transform the points to camera space, move them by Z_EPSILON, and them move them back to world space line_transform = inv(self.lookat_matrix).dot( translation_matrix(0, 0, Z_EPSILON).dot(self.lookat_matrix)) vertices = current_obj[0] current_obj = (np.hstack( [vertices, np.ones( (vertices.shape[0], 1))]).dot(line_transform.T)[:, :3], current_obj[1], current_obj[2]) current_queue.append(current_obj) if index < len(self.draw_queue) - 1: if self.draw_queue[index][0] == self.draw_queue[index + 1][0]: continue self.render_default(current_shape, current_queue) current_queue = [] self.draw_queue = [] def render_default(self, draw_type, draw_queue): # 1. Get the maximum number of vertices persent in the shapes # in the draw queue. # if len(draw_queue) == 0: return num_vertices = 0 for vertices, _, _ in draw_queue: num_vertices = num_vertices + len(vertices) # 2. Create empty buffers based on the number of vertices. # data = np.zeros(num_vertices, dtype=[('position', np.float32, 3), ('color', np.float32, 4)]) # 3. Loop through all the shapes in the geometry queue adding # it's information to the buffer. # sidx = 0 draw_indices = [] for vertices, idx, color in draw_queue: num_shape_verts = len(vertices) data['position'][sidx:(sidx + num_shape_verts), ] = np.array(vertices) color_array = np.array([color] * num_shape_verts) data['color'][sidx:sidx + num_shape_verts, :] = color_array draw_indices.append(sidx + idx) sidx += num_shape_verts self.vertex_buffer.set_data(data) self.index_buffer.set_data(np.hstack(draw_indices)) # 4. Bind the buffer to the shader. # self.default_prog.bind(self.vertex_buffer) # 5. Draw the shape using the proper shape type and get rid of # the buffers. # self.default_prog.draw(draw_type, indices=self.index_buffer) def cleanup(self): """Run the clean-up routine for the renderer. This method is called when all drawing has been completed and the program is about to exit. """ self.default_prog.delete() self.fbuffer_prog.delete() self.fbuffer.delete()
def on_initialize(self, event): # Build cube data # -------------------------------------- texcoord = [(0, 0), (0, 1), (1, 0), (1, 1)] vertices = [(-1, -1, 0), (-1, +1, 0), (+1, -1, 0), (+1, +1, 0)] vertices = VertexBuffer(vertices) #self.listener.waitForTransform("/robot", "/wrist_joint", rospy.Time(), rospy.Duration(4)) """while not rospy.is_shutdown(): try: now = rospy.Time.now() self.listener.waitForTransform("/wrist_joint", "/robot", rospy.Time(), rospy.Duration(4)) (trans,rot) = listener.lookupTransform("/wrist_joint", "/robot", rospy.Time(), rospy.Duration(4)) """ #pos, rot = self.listener.lookupTransform("/wrist_joint", "/robot", rospy.Time(0)) #print list(pos) camera_pitch = 0.0 # Degrees self.rotate = [camera_pitch, 0, 0] #self.translate = list(pos) self.translate = [0, 0, -5] # Build program # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) scale(model, 10, 10, 10) self.phi = 0 self.cube = Program(cube_vertex, cube_fragment) self.cube['position'] = vertices # 4640 x 2256 imtex = cv2.imread(os.path.join(img_path, 'rubixFront.jpg')) self.cube['texcoord'] = texcoord self.cube["texture"] = np.uint8(np.clip(imtex + np.random.randint(-60, 20, size=imtex.shape), 0, 255)) + 5 self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view color = Texture2D((640, 640, 3), interpolation='linear') self.framebuffer = FrameBuffer(color, RenderBuffer((640, 640))) self.quad = Program(quad_vertex, quad_fragment) self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.quad['texture'] = color self.objects = [self.cube] # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer('auto', connect=self.on_timer, start=True) self.pub_timer = app.Timer(0.1, connect=self.send_ros_img, start=True) self._set_projection(self.size)