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): super().__init__(src_fbuffer, src_default) self.normal_prog = Program(src_normal.vert, src_normal.frag) self.phong_prog = Program(src_phong.vert, src_phong.frag) self.lookat_matrix = np.identity(4) self.material = BasicMaterial(self.fill_color) # Camera position self.camera_pos = np.zeros(3) # Blinn-Phong Parameters self.ambient = np.array([0.2]*3) self.diffuse = np.array([0.6]*3) self.specular = np.array([0.8]*3) self.shininess = 8 # Lights self.MAX_LIGHTS_PER_CATEGORY = 8 self.ambient_light_color = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.directional_light_dir = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.directional_light_color = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.directional_light_specular = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.point_light_color = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.point_light_pos = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.point_light_specular = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.const_falloff = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 1, np.float32) self.linear_falloff = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 1, np.float32) self.quadratic_falloff = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 1, np.float32) self.curr_linear_falloff, self.curr_quadratic_falloff, self.curr_constant_falloff = 0.0, 0.0, 0.0 self.light_specular = np.array([0.0]*3)
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)
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 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): app.Canvas.__init__(self, size=(512, 512), keys='interactive') self.image = Program(image_vertex, image_fragment, 4) self.image['position'] = (-1, -1), (-1, +1), (+1, -1), (+1, +1) self.image['texcoord'] = (0, 0), (0, +1), (+1, 0), (+1, +1) self.image['vmin'] = +0.0 self.image['vmax'] = +1.0 self.image['cmap'] = 0 # Colormap index to use self.image['colormaps'] = colormaps self.image['n_colormaps'] = colormaps.shape[0] self.image['image'] = idxs.astype('float32') self.image['image'].interpolation = 'linear' set_viewport(0, 0, *self.physical_size) self.lines = Program(lines_vertex, lines_fragment) self.lines["position"] = np.zeros((4 + 4 + 514 + 514, 2), np.float32) color = np.zeros((4 + 4 + 514 + 514, 4), np.float32) color[1:1 + 2, 3] = 0.25 color[5:5 + 2, 3] = 0.25 color[9:9 + 512, 3] = 0.5 color[523:523 + 512, 3] = 0.5 self.lines["color"] = color set_state(clear_color='white', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) self.show()
def __init__(self): app.Canvas.__init__(self, keys='interactive') # This size is used for comparison with agg (via matplotlib) self.size = 512, 512 + 2 * 32 self.title = "Markers demo [press space to change marker]" self.vbo = VertexBuffer(data) self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = ortho(0, self.size[0], 0, self.size[1], -1, 1) self.programs = [ Program(markers.vert, markers.frag + markers.tailed_arrow), Program(markers.vert, markers.frag + markers.disc), Program(markers.vert, markers.frag + markers.diamond), Program(markers.vert, markers.frag + markers.square), Program(markers.vert, markers.frag + markers.cross), Program(markers.vert, markers.frag + markers.arrow), Program(markers.vert, markers.frag + markers.vbar), Program(markers.vert, markers.frag + markers.hbar), Program(markers.vert, markers.frag + markers.clobber), Program(markers.vert, markers.frag + markers.ring) ] for program in self.programs: program.bind(self.vbo) program["u_antialias"] = u_antialias, program["u_size"] = 1 program["u_model"] = self.model program["u_view"] = self.view program["u_projection"] = self.projection self.index = 0 self.program = self.programs[self.index]
def __init__(self, channels=10, timepoints=10000, srate=1017.25): super(TSV_TEST_GLOO, self).__init__() self.n_channels = channels self.n_timepoints = timepoints self.srate = srate self.magrin = 10 self.ticksize = 10 self.height = 0.0 self.width = 0.0 self._is_init = False self.is_on_draw = False self._init_data() # Build program & data # ---------------------------------------- self.data_pgr = Program(vertex, fragment, count=timepoints) #self.data_pgr'color'] = [ (1,0,0,1), (0,1,0,1), (0,0,1,1), (1,1,0,1) ] #self.data_pgr['position'] = [ (-1,-1), (-1,+1), (+1,-1), (+1,+1) ] #self.program['scale'] = 1.0 self.xgrid_pgr = Program(grid_vertex, grid_fragment) self.xgrid_pgr['position'] = self.init_xgrid() self.xgrid_pgr['color'] = np.array([0.0, 0.0, 0.0, 1.0], dtype=np.float32) self.ygrid_pgr = Program(grid_vertex, grid_fragment) self.ygrid_pgr['position'] = self.init_ygrid() self.ygrid_pgr['color'] = np.array([0.50, 0.50, 0.50, 1.0], dtype=np.float32)
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 __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 __init__(self): super().__init__(src_fbuffer, src_default) self.style = Style3D() self.normal_prog = Program(src_normal.vert, src_normal.frag) self.phong_prog = Program(src_phong.vert, src_phong.frag) self.lookat_matrix = np.identity(4) # Camera position self.camera_pos = np.zeros(3) # Lights self.MAX_LIGHTS_PER_CATEGORY = 8 self.ambient_light_color = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.directional_light_dir = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.directional_light_color = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.directional_light_specular = GlslList( self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.point_light_color = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.point_light_pos = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.point_light_specular = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 3, np.float32) self.const_falloff = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 1, np.float32) self.linear_falloff = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 1, np.float32) self.quadratic_falloff = GlslList(self.MAX_LIGHTS_PER_CATEGORY, 1, np.float32) self.curr_linear_falloff, self.curr_quadratic_falloff, self.curr_constant_falloff = 0.0, 0.0, 0.0 self.light_specular = np.array([0.0] * 3)
def __init__(self): self.image = Program(image_vertex, image_fragment, 4) self.image['position'] = (-1, -1), (-1, +1), (+1, -1), (+1, +1) self.image['texcoord'] = (0, 0), (0, +1), (+1, 0), (+1, +1) self.image['vmin'] = +0.0 self.image['vmax'] = +1.0 self.image['cmap'] = 0 # Colormap index to use self.image['colormaps'] = colormaps self.image['n_colormaps'] = colormaps.shape[0] self.image['image'] = I.astype('float32') self.image['image'].interpolation = 'linear' self.lines = Program(lines_vertex, lines_fragment) self.lines["position"] = np.zeros((4 + 4 + 514 + 514, 2), np.float32) color = np.zeros((4 + 4 + 514 + 514, 4), np.float32) color[1:1 + 2, 3] = 0.25 color[5:5 + 2, 3] = 0.25 color[9:9 + 512, 3] = 0.5 color[523:523 + 512, 3] = 0.5 self.lines["color"] = color app.Canvas.__init__(self, show=True, size=(512, 512), keys='interactive')
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 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 __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: 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) # 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 set_shaders(self, vertex_shader=None, fragment_shader=None): # vs = vertex_shader if vertex_shader else self._vertex_shader fs = fragment_shader if fragment_shader else self._fragment_shader self._program = Program(vs, fs, count=4) # self._data = np.zeros(4, dtype=[('a_position', np.float32, 2), ('a_texcoord', np.float32, 2)]) # self._data['a_texcoord'] = np.array([[0., 1.], [1., 1.], [0., 0.], [1., 0.]]) # self._program['u_model'] = np.eye(4, dtype=np.float32) self._program['u_view'] = np.eye(4, dtype=np.float32) # self._coordinate = [0, 0] self._origin = [0, 0] # self._program['texture'] = np.zeros((self._height, self._width), dtype='uint8') # self.apply_magnification()
def on_initialize(self, event): # Build program & data self.program = Program(vertex, fragment, count=4) self.program['color'] = [(1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1), (1, 1, 0, 1)] self.program['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
def test_context_sharing(): """Test context sharing""" with Canvas() as c1: vert = "uniform vec4 pos;\nvoid main (void) {gl_Position = pos;}" frag = "uniform vec4 pos;\nvoid main (void) {gl_FragColor = pos;}" program = Program(vert, frag) program['pos'] = [1, 2, 3, 4] program._glir.flush() def check(): # Do something to program and see if it worked program['pos'] = [1, 2, 3, 4] # Do command program._glir.flush() # Execute that command check_error() # Check while c1 is active check() # Check while c2 is active (with different context) with Canvas() as c2: # pyglet always shares if 'pyglet' not in c2.app.backend_name.lower(): assert_raises(RuntimeError, check) # Tests unable to create canvas on glut if c1.app.backend_name.lower() in ('glut', ): assert_raises(RuntimeError, Canvas, context=c1.context) return # Check while c2 is active (with *same* context) with Canvas(context=c1.context) as c2: assert c1.context is c2.context # Same context object check()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.timer = app.Timer('auto', connect=self.on_timer, start=True) with open('vertex.glsl') as f: self.vshader = f.read() with open('fragment.glsl') as f: self.fshader = f.read() self.program = Program(self.vshader, self.fshader) v, i, iOutline = create_cube() self.vertices = VertexBuffer(v) self.indices = IndexBuffer(i) self.outlineIndices = IndexBuffer(iOutline) self.program.bind(self.vertices) self.theta, self.phi = 0, 0 self.program['model'] = np.eye(4) self.program['view'] = translate([0, 0, -5]) self.program['texture'] = checkerboard() gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), polygon_offset=(1, 1), blend_func=('src_alpha', 'one_minus_src_alpha'), line_width=5, depth_test=True)
def make_eye(self, texture, eye): '''make_eye (eye_texture, eye) Arguments: - eye_texture: The texture (bound to a framebuffer), that represents the view of the eye - eye: 'left' or 'right', the eye being rendered Todo: - Use vertex buffer instead of manually binding ''' assert isinstance(texture, Texture2D), "texture not a texture 2D instance!" assert eye in ['left', 'right' ], eye + " is not a valid eye (Should be left or right)" program = Program(self._vert_shader, self._frag_shader) i_buffer = self._i_buffers[eye + '_indices'] _buffer = self._v_buffers[eye + '_buffer'] Logger.log('Loading {} eye distortion mesh pos'.format(eye)) program['pos'] = _buffer['pos'] Logger.log('Loading {} eye distortion mesh red_xy'.format(eye)) program['red_xy'] = _buffer['red_xy'] Logger.log('Loading {} eye distortion mesh green_xy'.format(eye)) program['green_xy'] = _buffer['green_xy'] Logger.log('Loading {} eye distortion mesh blue_xy'.format(eye)) program['blue_xy'] = _buffer['blue_xy'] program['vignette'] = _buffer['vignette'] program['texture'] = texture return program, IndexBuffer(i_buffer)
def __init__(self): app.Canvas.__init__(self, size=(512, 512), title='Colored cube', keys='interactive') # Build cube data V, I, _ = create_cube() vertices = VertexBuffer(V) self.indices = IndexBuffer(I) # Build program self.program = Program(vertex, fragment) self.program.bind(vertices) # Build view, model, projection & normal view = translate((0, 0, -5)) model = np.eye(4, dtype=np.float32) self.program['model'] = model self.program['view'] = view self.phi, self.theta = 0, 0 gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True) self.activate_zoom() self.timer = app.Timer('auto', self.on_timer, start=True) self.show()
def __init__(self): app.Canvas.__init__(self, title='Rain [Move mouse]', size=(512, 512), keys='interactive') # Build data # -------------------------------------- n = 500 self.data = np.zeros(n, [('a_position', np.float32, 2), ('a_fg_color', np.float32, 4), ('a_size', np.float32, 1)]) self.index = 0 self.program = Program(vertex, fragment) self.vdata = VertexBuffer(self.data) self.program.bind(self.vdata) self.program['u_antialias'] = 1.00 self.program['u_linewidth'] = 1.00 self.program['u_model'] = np.eye(4, dtype=np.float32) self.program['u_view'] = np.eye(4, dtype=np.float32) self.activate_zoom() gloo.set_clear_color('white') gloo.set_state(blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) self.timer = app.Timer('auto', self.on_timer, start=True) self.show()
def __init__(self, *args, **kwargs): '''Drawable(*args, **kwargs) -> Drawable Everything is tracked internally, different drawables will handle things differently Inherit from this for all drawables. Inheriting: You must define a make_mesh, make_shaders, and draw method. If you do not make shaders, you will get a default If you do not make a mesh, you'll get a square that takes up the screen ''' self.model = np.eye(4) self.view = np.eye(4) self.projection = np.eye(4) self.mesh = self.make_mesh() self.vert_shader, self.frag_shader = self.make_shaders() self.program = Program(self.vert_shader, self.frag_shader) self.program.bind(VertexBuffer(self.mesh)) if hasattr(self, make_texture): self.texture = self.make_texture() assert isinstance(self.texture, Texture2D), "Texture passed is not a texture!" self.program['texture'] = self.texture cube["texture"].interpolation = 'linear'
def on_initialize(self, event): # Build cube data V, I, O = create_cube() vertices = VertexBuffer(V) self.faces = IndexBuffer(I) self.outline = IndexBuffer(O) # Build program # -------------------------------------- self.program = Program(vertex, fragment) self.program.bind(vertices) # Build view, model, projection & normal # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) translate(view, 0, 0, -5) self.program['u_model'] = model self.program['u_view'] = view self.phi, self.theta = 0, 0 # OpenGL initalization # -------------------------------------- gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True, polygon_offset=(1, 1), line_width=0.75, blend_func=('src_alpha', 'one_minus_src_alpha')) self.timer.start()
def __init__(self): app.Canvas.__init__(self, size=(512, 512), title='Rotating cube', keys='interactive') self.timer = app.Timer('auto', self.on_timer) # Build cube data V, I, O = create_cube() vertices = VertexBuffer(V) self.faces = IndexBuffer(I) self.outline = IndexBuffer(O) # Build program # -------------------------------------- self.program = Program(vertex, fragment) self.program.bind(vertices) # Build view, model, projection & normal # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) translate(view, 0, 0, -5) self.program['u_model'] = model self.program['u_view'] = view self.phi, self.theta = 0, 0 # OpenGL initalization # -------------------------------------- gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True, polygon_offset=(1, 1), line_width=0.75, blend_func=('src_alpha', 'one_minus_src_alpha')) self.timer.start()
def __init__(self): app.Canvas.__init__(self, size=(512, 512), title='Textured cube', keys='interactive') self.timer = app.Timer('auto', self.on_timer) # Build cube data V, I, _ = create_cube() vertices = VertexBuffer(V) self.indices = IndexBuffer(I) # Build program self.program = Program(vertex, fragment) self.program.bind(vertices) # Build view, model, projection & normal view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) translate(view, 0, 0, -5) self.program['model'] = model self.program['view'] = view self.program['texture'] = checkerboard() self.phi, self.theta = 0, 0 # OpenGL initalization gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True) self.timer.start()
def test_context_sharing(): """Test context sharing""" with Canvas() as c1: vert = VertexShader("uniform vec4 pos;" "void main (void) {gl_Position = pos;}") frag = FragmentShader("uniform vec4 pos;" "void main (void) {gl_FragColor = pos;}") program = Program(vert, frag) program['pos'] = [1, 2, 3, 4] program.activate() # should print def check(): program.activate() check_error() with Canvas() as c2: # pyglet always shares if 'pyglet' not in c2.app.backend_name.lower(): assert_raises(RuntimeError, check) if c1.app.backend_name.lower() in ('glut',): assert_raises(RuntimeError, Canvas, context=c1.context) else: with Canvas(context=c1.context) as c2: assert c1.context is c2.context # Same context object check()
def test_context_sharing(): """Test context sharing""" with Canvas() as c1: vert = "attribute vec4 pos;\nvoid main (void) {gl_Position = pos;}" frag = "void main (void) {gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}" program = Program(vert, frag) program['pos'] = [(1, 2, 3, 1), (4, 5, 6, 1)] program.draw('points') def check(): # Do something to program and see if it worked program['pos'] = [(1, 2, 3, 1), (4, 5, 6, 1)] # Do command program.draw('points') check_error() # Check while c1 is active check() # Check while c2 is active (with different context) with Canvas() as c2: # pyglet always shares if 'pyglet' not in c2.app.backend_name.lower(): assert_raises(Exception, check) # Check while c2 is active (with *same* context) with Canvas(shared=c1.context) as c2: assert c1.context.shared is c2.context.shared # same object check()
def on_initialize(self, event): # Build cube data V, F, O = create_cube() vertices = VertexBuffer(V) self.faces = IndexBuffer(F) self.outline = IndexBuffer(O) # Build view, model, projection & normal # -------------------------------------- self.view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) translate(self.view, 0, 0, -5) normal = np.array(np.matrix(np.dot(self.view, model)).I.T) # Build program # -------------------------------------- self.program = Program(vertex, fragment) self.program.bind(vertices) self.program["u_light_position"] = 2, 2, 2 self.program["u_light_intensity"] = 1, 1, 1 self.program["u_model"] = model self.program["u_view"] = self.view self.program["u_normal"] = normal self.phi, self.theta = 0, 0 # OpenGL initalization # -------------------------------------- gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True, polygon_offset=(1, 1), blend_func=('src_alpha', 'one_minus_src_alpha'), line_width=0.75) self.timer.start()