def __init__(self): app.Canvas.__init__(self, keys='interactive') self.size = 800, 800 self.title = "D'oh ! A big donut" self.program = gloo.Program(vert, frag) self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.translate = 5 translate(self.view, 0, 0, -self.translate) self.program.bind(gloo.VertexBuffer(data)) self.program['u_linewidth'] = u_linewidth self.program['u_antialias'] = u_antialias self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_size'] = 5 / self.translate self.theta = 0 self.phi = 0 self.clock = 0 self.stop_rotation = False gloo.set_state('translucent', clear_color='white') self.program['u_clock'] = 0.0 self._timer = app.Timer('auto', connect=self.on_timer, start=True)
def draw_distortion(self, *Contexts): '''Distorter.draw(list_of_drawables) Draw the drawables to the right and left-eye render buffers, then apply the distortion and display these buffers to the screen TODO: How can we globally handle view? Should we even bother trying to find the 'right' way to do this? ''' gloo.set_clear_color('black') gloo.set_state(depth_test=True) with self.left_eye: gloo.clear(color=True, depth=True) for context in Contexts: context.translate(0, self.IPD / 2, 0) context.set_projection(self.L_projection) context.draw() with self.right_eye: gloo.clear(color=True, depth=True) for context in Contexts: context.translate(0, -self.IPD / 2, 0) context.set_projection(self.R_projection) context.draw() gloo.clear(color=True, depth=True) self.left_eye_program.draw('triangles', self.left_eye_indices) self.right_eye_program.draw('triangles', self.right_eye_indices)
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, outline = create_cube() vertices = VertexBuffer(V) self.faces = IndexBuffer(I) self.outline = IndexBuffer(outline) # 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['u_model'] = model self.program['u_view'] = view self.phi, self.theta = 0, 0 self.activate_zoom() # OpenGL initialization # -------------------------------------- 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() self.show()
def initializeGL(self): """ docstring """ self._mutex.acquire() # save mouse cursor position for smooth rotation self.lastPos = QtCore.QPoint() gloo.set_state(clear_color=(0.2734375, 0.5078125, 0.703125, 1.00), depth_test=True, polygon_offset=(1, 1), blend_func=('src_alpha', 'one_minus_src_alpha'), line_width=0.75) # create camera and light self._camera = Camera() self._lights = self._controller.getLightCollection() self._objects = [] self._makePlane([0,0,0], 200, 200) # examples : should be removed or used for empty scenes # self._makeCube((0,1.1,0)) # self._makeSphere((0,3,0)) self._loadObjects() self._chosenAlgo.init(self._objects, self._camera, self._controller.getLightCollection(), self._options) self._cameraRotation = AutoRotateCamera(self._camera,1) self._mutex.release()
def __init__(self, svbrdf, mesh, camera, size): super().__init__(size, 0, 1000, camera, show=True) gloo.set_state(depth_test=True) gloo.set_viewport(0, 0, *self.size) self.lights = [ Light((20, 30, 100), 2000), Light((20, 30, -100), 2000), Light((0, 100, 10), 2000), ] vertex_positions = mesh.expand_face_vertices() vertex_normals = mesh.expand_face_normals() vertex_tangents, vertex_bitangents = mesh.expand_tangents() vertex_uvs = mesh.expand_face_uvs() material = SVBRDFMaterial(svbrdf) self.renderables = [Renderable(material, { 'a_position': vertex_positions, 'a_normal': vertex_normals, 'a_tangent': vertex_tangents, 'a_bitangent': vertex_bitangents, 'a_uv': vertex_uvs, }, len(self.lights))]
def on_draw(self, event): while os.path.exists(self.filename()): self.frame += 1 if self.frame > self.anim_frames: vispy.app.quit() print("{:5d}/{}".format(self.frame, self.anim_frames)) self.fbo.activate() gloo.set_state(depth_test=False, clear_color='black') gloo.clear(color=True) w, h = self.fbsize gloo.set_viewport(0, 0, w, h) self.render['aspect'] = w / h self.render['width'] = w self.render['frame'] = self.frame self.render.draw('triangle_strip') self.context.finish() arr = self.fbo.read().copy() arr[:, :, 3] = 255 img = Image.fromarray(arr) img.save(self.filename()) self.frame += 1 self.update()
def __init__(self, *args, **kwargs): self.exit = kwargs.pop('exit', False) self.inputs_path = kwargs.pop('inputs_path', None) super(StillCanvas, self).__init__(*args, **kwargs) gloo.set_state(clear_color='black', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) self.inputs = None if self.inputs_path is not None: self.input_manager = FileStoredInput(self.inputs_path) self.time = self.input_manager.stored_time definition = Definitions[self.input_manager.stored_definition_name] else: self.input_manager = RandomInput() self.time = float(random.randint(0, 345212312)) definition = Definitions[random.choice(Definitions.keys())] self.fractal = FractalProgram(definition, mask=False) self.apply_zoom() self._timer = app.Timer(1.0 / 5, connect=self.update, start=True) if self.exit: app.Timer(1, connect=self.write_and_exit, start=True, iterations=1) # self.update(None) # self.update(None) self.show()
def __init__(self, atoms, radius=6.): app.Canvas.__init__(self, title='Molecular viewer', keys='interactive', size=(1200, 800)) self.ps = self.pixel_scale self.zoom = .5/numpy.amax(numpy.linalg.norm(atoms['zyx'], axis=1)) self.program = gloo.Program(vertex, fragment) self.view = scale(self.zoom*numpy.ones(3)) self.model = numpy.eye(4, dtype=numpy.float32) self.projection = numpy.eye(4, dtype=numpy.float32) data = numpy.zeros(atoms.size, [('a_position', numpy.float32, 3), ('a_color', numpy.float32, 4), ('a_radius', numpy.float32, 1)]) data['a_position'] = atoms['zyx'] data['a_color'] = 1,0,0,1 data['a_color'][atoms['Z']==16] = 1,1,0,1 data['a_color'][atoms['Z']==74] = 0,.5,1,1 data['a_radius'] = atoms['Z']**(1/3)*radius/self.zoom self.program.bind(gloo.VertexBuffer(data)) self.program['u_zoom'] = self.zoom self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_light_position'] = 0., 0., 2. self.program['u_light_spec_position'] = -5., 5., -5. self.apply_zoom() self.program['u_model'] = self.model self.program['u_view'] = self.view gloo.set_state(depth_test=True, clear_color='white') self.show()
def __init__(self, **kwargs): # Initialize the canvas for real app.Canvas.__init__(self, keys='interactive', **kwargs) self.size = 512, 512 self.position = 50, 50 self.vbo = gloo.VertexBuffer(data) self.index = gloo.IndexBuffer(edges) self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.program = gloo.Program(vert, frag) self.program.bind(self.vbo) self.program['u_size'] = 1 self.program['u_antialias'] = u_antialias self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_projection'] = self.projection self.program_e = gloo.Program(vs, fs) self.program_e.bind(self.vbo) set_state(clear_color='white', depth_test=False, blend=True, blend_func=('src_alpha', 'one_minus_src_alpha'))
def __init__(self): app.Canvas.__init__(self, keys='interactive') self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) # Sets the view to an appropriate position over the terrain self.default_view = np.array([[0.8, 0.2, -0.48, 0], [-0.5, 0.3, -0.78, 0], [-0.01, 0.9, -0.3, 0], [-4.5, -21.5, -7.4, 1]], dtype=np.float32) self.view = self.default_view self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.translate = [0, 0, 0] self.rotate = [0, 0, 0] self.program['u_height'] = height self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['a_position'] = gloo.VertexBuffer(triangles) self.activate_zoom() gloo.set_state(clear_color='black', depth_test=True) self.show()
def on_draw(self, event): gloo.clear() # Filled cube gloo.set_state(blend=False, depth_test=True, polygon_offset_fill=True) self.program.draw('triangles', self.filled_buf)
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, **kwargs): app.Canvas.__init__(self, **kwargs) self.geometry = 0, 0, 400, 400 mesh = create_sphere(10, 10, radius=2.) vertices = mesh.get_vertices() tris = mesh.get_faces() data=vertices[:, 2] self.filled_buf = gloo.IndexBuffer(tris) self.program = ModularProgram(VERT_CODE, FRAG_CODE) colormap = get_colormap('autumn') self.color = Function(colormap.glsl_map) self.program.frag['color'] = self.color('floor(v_value*10.+0.5)/10.') # Set attributes self.program['a_position'] = gloo.VertexBuffer(vertices) self.program['a_value'] = gloo.VertexBuffer(normalize(data, data.min(), data.max())) # Handle transformations self.init_transforms() gloo.set_clear_color((1, 1, 1, 1)) gloo.set_state(depth_test=True) self._timer = app.Timer('auto', connect=self.update_transforms) self._timer.start()
def __init__(self,controller, **kwargs): app.Canvas.__init__(self, **kwargs) self._controller = controller self.geometry = 0, 0, 400, 400 self.program = gloo.Program("shaders/raytracingalgo.vertexshader", "shaders/raytracingalgo.fragmentshader") self.program['a_position'] = [(-1., -1.), (-1., +1.),(+1., -1.), (+1., +1.)] self.program['sphere_position_0'] = (.75, .1, 1.) self.program['sphere_radius_0'] = .6 self.program['sphere_color_0'] = (1., 1., 1.) self.program['sphere_position_1'] = (-.75, .1, 2.25) self.program['sphere_radius_1'] = .6 self.program['sphere_color_1'] = (1., 1., 1.) self.program['plane_position'] = (0., -.5, 0.) self.program['plane_normal'] = (0., 1., 0.) self.program['light_intensity'] = 1. self.program['light_specular'] = (1., 50.) self.program['light_position'] = (5., 5., -10.) self.program['light_color'] = (1., 1., 1.) self.program['ambient'] = .05 self.program['O'] = (0., 0., -1.) gloo.set_clear_color((1, 1, 1, 1)) gloo.set_state(depth_test=True) #self.program.draw('triangle_strip') #self.active = True self._timer = app.Timer('auto', connect=self.timedUpdate, start=True) self._fps = 1 self._timerfps = 24 self._softFPS = []
def __init__(self, *args, **kwargs): self.fake_inputs = kwargs.pop('fake_inputs', False) self.draw_bones = kwargs.pop('draw_bones', False) self.kiosk_interval = kwargs.pop('kiosk_interval', 0) self.start_definition = kwargs.pop('start_definition', 0) self.start_input = kwargs.pop('start_input', 0) self.show_mask = kwargs.pop('mask', False) super(MainCanvas, self).__init__(*args, **kwargs) gloo.set_state(clear_color='black', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) self.skeleton_bones = SkeletonBonesProgram() self.mask = MaskProgram() self.inputs = None self.input_manager = None self._starttime = time.time() self.definition_position = self.start_definition self.input_position = self.start_input self.rotate() if self.kiosk_interval > 0: self.kiosk_timer = app.Timer(self.kiosk_interval, connect=self.rotate, start=True) self._timer = app.Timer('auto', connect=self.update, 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 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()
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): 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'] = I.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) self.size = 800, 600 # fovy, zfar params self.fovy = 45.0 self.zfar = 10.0 width, height = self.size self.aspect = width / float(height) self.program = gloo.Program(vert, frag) self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) translate(self.view, 0, 0, -5.0) self.program['u_model'] = self.model self.program['u_view'] = self.view self.theta = 0 self.phi = 0 self.visible = True self._timer = app.Timer(1.0 / 60) self._timer.connect(self.on_timer) self._timer.start() # --------------------------------- gloo.set_clear_color((1, 1, 1, 1)) gloo.set_state('opaque') gloo.set_polygon_offset(1, 1)
def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(800, 600)) self.vertices, self.filled, self.outline = cube() self.filled_buf = gloo.IndexBuffer(self.filled) self.outline_buf = gloo.IndexBuffer(self.outline) self.program = gloo.Program(vert, frag) self.program.bind(gloo.VertexBuffer(self.vertices)) self.view = translate((0, 0, -5)) self.model = np.eye(4, dtype=np.float32) gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1]) self.projection = perspective(45.0, self.size[0] / float(self.size[1]), 2.0, 10.0) self.program['u_projection'] = self.projection self.program['u_model'] = self.model self.program['u_view'] = self.view self.theta = 0 self.phi = 0 gloo.set_clear_color('white') gloo.set_state('opaque') gloo.set_polygon_offset(1, 1) self._timer = app.Timer('auto', connect=self.on_timer, start=True) self.show()
def __init__(self): app.Canvas.__init__(self, keys='interactive') self.size = 800, 600 self.vertices, self.filled, self.outline = cube() self.filled_buf = gloo.IndexBuffer(self.filled) self.outline_buf = gloo.IndexBuffer(self.outline) self.program = gloo.Program(vert, frag) self.program.bind(gloo.VertexBuffer(self.vertices)) self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) translate(self.view, 0, 0, -5) self.program['u_model'] = self.model self.program['u_view'] = self.view self.theta = 0 self.phi = 0 gloo.set_clear_color('white') gloo.set_state('opaque') gloo.set_polygon_offset(1, 1) self._timer = app.Timer('auto', connect=self.on_timer, start=True)
def __init__(self): app.Canvas.__init__(self, position=(50, 50), keys='interactive') self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) self.program['a_position'] = gloo.VertexBuffer(v_position) self.program['a_color'] = gloo.VertexBuffer(v_color) self.program['a_size'] = gloo.VertexBuffer(v_size) self.program['u_pan'] = (0., 0.) self.program['u_scale'] = (1., 1.) self.program['u_vec1'] = (1., 0., 0., 0.) self.program['u_vec2'] = (0., 1., 0., 0.) # Circulant matrix. circ = np.diagflat(np.ones(ndim-1), 1) circ[-1, 0] = -1 if ndim % 2 == 0 else 1 self.logcirc = logm(circ) # We will solve the equation dX/dt = log(circ) * X in real time # to compute the matrix exponential expm(t*log(circ)). self.mat = np.eye(ndim) self.dt = .001 gloo.set_state(clear_color=(1, 1, 1, 1), blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) self._timer = app.Timer('auto', connect=self.on_timer, start=True)
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 draw(self, projection_size): width, height = projection_size px_scale = 2.0/width, 2.0/height if len(self.text) == 0: return if self._vertices is None: self._vertices, _ = _text_to_vbo(self._text, self._font, self._anchors[0], self._anchors[1], self._font._lowres_size) idx = (np.array([0, 1, 2, 0, 2, 3], np.uint32) + np.arange(0, 4*len(self._text), 4, dtype=np.uint32)[:, np.newaxis]) self._ib = IndexBuffer(idx.ravel()) ps = (self._font_size / 72.) * 92. self._program['u_npix'] = ps self._program['u_font_atlas_shape'] = self._font._atlas.shape[:2] self._program['u_kernel'] = self._font._kernel self._program['u_scale'] = ps * px_scale[0], ps * px_scale[1] self._program['u_rotation'] = self._rotation self._program['u_pos'] = self._pos self._program['u_color'] = self._color.rgba self._program['u_font_atlas'] = self._font._atlas self._program.bind(self._vertices) set_state(blend=True, depth_test=False, blend_func=BLEND_FUNC) with self._draw_context(): self._program.draw('triangles', self._ib)
def __init__(self): app.Canvas.__init__(self, keys='interactive') # Time self._t = time.time() self._pos = 0.0, 0.0 self._button = None # Create program self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) # Create vertex buffers self.vbo_position = gloo.VertexBuffer(particles['position'].copy()) self.vbo_color = gloo.VertexBuffer(particles['color'].copy()) self.vbo_size = gloo.VertexBuffer(particles['size'].copy()) # Bind vertex buffers self.program['color'] = self.vbo_color self.program['size'] = self.vbo_size self.program['position'] = self.vbo_position gloo.set_state(clear_color=(0, 0, 0, 1), blend=True, blend_func=('src_alpha', 'one')) self._timer = app.Timer('auto', connect=self.update, start=True)
def __init__(self, time_acceleration=1.0, show_window=True, physics_dt=(1 / 30.)): app.Canvas.__init__(self, keys='interactive', size=(800, 800)) rospy.on_shutdown(self.end) # How much sim time should pass for each real world second self.dt_per_second = time_acceleration # How much physics time should pass for each physics iteration self.physics_dt = physics_dt print 'Time per second', self.dt_per_second, 'gaptime:', self.physics_dt / self.dt_per_second self.size = (800, 800) self.translate = np.array([0.0, 0.0, 0.0]) self.rotate = np.array([0.0, 0.0, 0.0]) gloo.set_state(depth_test=True, blend=True, preset='translucent') self._timer = app.Timer('auto', connect=self.on_timer, start=True) self.physics_timer = app.Timer(self.physics_dt / self.dt_per_second, connect=self.step_physics, start=True) self.clock = 0.0 self.view = np.eye(4) # Do any visualization? # TODO: Can we do any rendering at all like this? if show_window: self.show() self.keypress_pub = rospy.Publisher('sim/keypress', String, queue_size=10)
def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(800, 800)) self.title = "Atom [zoom with mouse scroll" self.translate = 6.5 self.program = gloo.Program(vert, frag) self.view = translate((0, 0, -self.translate)) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.apply_zoom() self.program.bind(gloo.VertexBuffer(data)) self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_size'] = 5 / self.translate self.theta = 0 self.phi = 0 self.clock = 0 self.stop_rotation = False gloo.set_state('translucent', depth_test=False) self.program['u_clock'] = 0.0 self._timer = app.Timer('auto', connect=self.on_timer, start=True) self.show()
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 __init__(self): app.Canvas.__init__(self, title='Spacy', keys='interactive', size=(800, 600)) self.program = gloo.Program(vertex, fragment) self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.activate_zoom() self.timer = app.Timer('auto', connect=self.update, start=True) # Set uniforms (some are set later) self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_pixel_scale'] = self.pixel_scale # Set attributes self.program['a_position'] = np.zeros((N, 3), np.float32) self.program['a_offset'] = np.zeros((N, 1), np.float32) # Init self._timeout = 0 self._active_block = 0 for i in range(NBLOCKS): self._generate_stars() self._timeout = time.time() + SPEED gloo.set_state(clear_color='black', depth_test=False, blend=True, blend_equation='func_add', blend_func=('src_alpha', 'one_minus_src_alpha')) self.show()
def draw(self, transforms): gloo.set_state(cull_face='front_and_back') # Set the two transforms required by the vertex shader: self.program.vert['visual_to_doc'] = transforms.visual_to_document self.program.vert['doc_to_render'] = ( transforms.framebuffer_to_render * transforms.document_to_framebuffer) # Set the scale factor between document and framebuffer coordinate # systems. This assumes a simple linear / isotropic scale; more complex # transforms will yield strange results! fbs = np.linalg.norm(transforms.document_to_framebuffer.map([1, 0]) - transforms.document_to_framebuffer.map([0, 0])) self.program.frag['doc_fb_scale'] = fbs self.program.frag['line_width'] = (self.weight + 1) * fbs # Finally, draw the triangles. self.program.draw('triangle_strip')
def draw(self,x,y,width,height): #gloo.set_clear_color('white') self.location = [x,y] self.size = [width,height] self.apply_resize([x,y,width,height]) gloo.set_state(blend=False, depth_test=True, polygon_offset_fill=True) self.program['u_color'] = 1, 1, 1, 1 self.program.draw('triangles', self.filled_buf) # Outline gloo.set_state(blend=True, depth_test=True, polygon_offset_fill=False) gloo.set_depth_mask(False) self.program['u_color'] = 0, 0, 0, 1 self.program.draw('lines', self.outline_buf) gloo.set_depth_mask(True)
def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(800, 600)) ps = self.pixel_scale # Create vertices n = 1000000 data = np.zeros(n, [('a_position', np.float32, 3), ('a_bg_color', np.float32, 4), ('a_fg_color', np.float32, 4), ('a_size', np.float32, 1)]) data['a_position'] = 0.45 * np.random.randn(n, 3) data['a_bg_color'] = np.random.uniform(0.85, 1.00, (n, 4)) data['a_fg_color'] = 0, 0, 0, 1 data['a_size'] = np.random.uniform(5*ps, 10*ps, n) u_linewidth = 1.0 u_antialias = 1.0 self.translate = 5 self.program = gloo.Program(vert, frag) self.view = translate((0, 0, -self.translate)) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.apply_zoom() self.program.bind(gloo.VertexBuffer(data)) self.program['u_linewidth'] = u_linewidth self.program['u_antialias'] = u_antialias self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_size'] = 5 / self.translate self.n = n self.data = data self.theta = 0 self.phi = 0 gloo.set_state('translucent', clear_color='white') self.timer = app.Timer('auto', connect=self.on_timer, start=True) self.show()
def on_initialize(self, event): # 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) 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)
def on_initialize(self, event): # 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.phi, self.theta = 0, 0 gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True) self.timer.start()
def __init__(self): self.itime = 0 app.Canvas.__init__(self, title='Molecular viewer', keys='interactive', size=(1200, 800)) self.ps = self.pixel_scale self.translate = 40 self.program = gloo.Program(vertex, fragment) self.view = translate((0, 0, -self.translate)) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.apply_zoom() self.cosmos = planet.Cosmos(n=500) gloo.set_state(depth_test=True, clear_color='black') self._timer = app.Timer('auto', connect=self.on_timer, start=True) self.show()
def __init__(self): app.Canvas.__init__(self, title='Use your wheel to zoom!', keys='interactive') self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) self.program['a_position'] = y.reshape(-1, 1) self.program['a_color'] = color self.program['a_index'] = index self.program['u_scale'] = (1., 1.) self.program['u_size'] = (nrows, ncols) self.program['u_n'] = n gloo.set_viewport(0, 0, *self.physical_size) self._timer = app.Timer('auto', connect=self.on_timer, start=True) gloo.set_state(clear_color='black', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha'))
def __init__(self, core_controller: CoreController): app.Canvas.__init__(self, title='Use your wheel to zoom!', keys='interactive', app='PySide2') self.core_controller = core_controller self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) self.program['a_position'] = y.reshape(-1, 1) self.program['a_color'] = color self.program['a_index'] = index self.program['u_scale'] = (1., 1.) self.program['u_size'] = (nrows, ncols) self.program['u_n'] = n gloo.set_viewport(0, 0, *self.physical_size) gloo.set_state(clear_color='black', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha'))
def __init__(self): # setup initial width, height app.Canvas.__init__(self, keys='interactive', size=(800, 600)) # create a new shader program self.program = gloo.Program(VERT_SHADER, FRAG_SHADER, count=len(galaxy)) # load the star texture self.texture = gloo.Texture2D(load_galaxy_star_image(), interpolation='linear') self.program['u_texture'] = self.texture # construct the model, view and projection matrices self.view = transforms.translate((0, 0, -5)) self.program['u_view'] = self.view self.model = np.eye(4, dtype=np.float32) self.program['u_model'] = self.model self.program['u_colormap'] = colors w, h = self.size self.projection = perspective(45.0, w / float(h), 1.0, 1000.0) self.program['u_projection'] = self.projection # start the galaxy to some decent point in the future galaxy.update(100000) data = self.__create_galaxy_vertex_data() # setup the VBO once the galaxy vertex data has been setup # bind the VBO for the first time self.data_vbo = gloo.VertexBuffer(data) self.program.bind(self.data_vbo) # setup blending gloo.set_state(clear_color=(0.0, 0.0, 0.03, 1.0), depth_test=False, blend=True, blend_func=('src_alpha', 'one')) self._timer = app.Timer('auto', connect=self.update, start=True)
def __init__(self): app.Canvas.__init__(self, keys='interactive') ps = self.pixel_scale # Create vertices n = 10000 v_position = 0.25 * np.random.randn(n, 2).astype(np.float32) v_color = np.random.uniform(0, 1, (n, 3)).astype(np.float32) v_size = np.random.uniform(2*ps, 12*ps, (n, 1)).astype(np.float32) self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) # Set uniform and attribute self.program['a_color'] = gloo.VertexBuffer(v_color) self.program['a_position'] = gloo.VertexBuffer(v_position) self.program['a_size'] = gloo.VertexBuffer(v_size) gloo.set_state(clear_color='white', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) self.show()
def __init__(self): app.Canvas.__init__(self, size=(512, 512), title='Lighted cube', keys='interactive') self.timer = app.Timer('auto', self.on_timer) # Build cube data V, F, outline = create_cube() vertices = VertexBuffer(V) self.faces = IndexBuffer(F) self.outline = IndexBuffer(outline) # Build view, model, projection & normal # -------------------------------------- self.view = translate((0, 0, -5)) model = np.eye(4, dtype=np.float32) 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 self.activate_zoom() # OpenGL initialization # -------------------------------------- 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() self.show()
def __init__(self): app.Canvas.__init__(self, title='Use your wheel to zoom!', show=False, keys='interactive') #Signal self.program = gloo.Program(SIGNAL_VERT_SHADER, SIGNAL_FRAG_SHADER) self.program['a_position'] = y.reshape(-1, 1) #self.program['a_color'] = color_signals self.program['a_th_spikes'] = th_color_spikes self.program['see_spikes'] = 1.0 self.program['a_index'] = index self.program['u_scale'] = (1., 1.) self.program['u_size'] = (nrows, ncols) self.program['u_n'] = nb_samples #Box self.program_box = gloo.Program(vert=BOX_VERT_SHADER, frag=BOX_FRAG_SHADER) self.program_box['a_box_index'] = box_index self.program_box['a_box_number'] = box_number self.program_box['a_corner_position'] = corner_positions self.program_box['u_size'] = (nrows, ncols) self.program_box['u_scale'] = (1., 1.) #Threshold self.program_th = gloo.Program(vert=THRESHOLD_VERT_SHADER, frag=THRESHOLD_FRAG_SHADER) self.program_th['a_threshold_index'] = th_index self.program_th['a_threshold_position'] = th_position_std self.program_th['display'] = True self.program_th['u_size'] = (nrows, ncols) self.program_th['u_scale'] = (1., 1.) gloo.set_viewport(0, 0, *self.physical_size) self._timer = app.Timer('auto', connect=self.on_timer, start=True) gloo.set_state(clear_color='black', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) self.show()
def __init__ ( self ): # config=dict(samples=8) turns on 4x MSAA (anti-aliasing) app.Canvas.__init__( self, keys='interactive', size=(800,600), config=dict(samples=4)) self.V, self.F = teapot() self.teapot = gloo.Program( VSHADER, FSHADER ) # initialize a buffer of all the vertices self.teapot.bind(gloo.VertexBuffer(self.V)) # each element in F is an array of 3 indices corresponding to the # locations within the vertex buffer that represent the face's 3 vertices. # when we draw the object, openGL will handle this association process automatically. self.FF = gloo.IndexBuffer(self.F) self.normals = gloo.Program( VSHADER_NORM, FSHADER_NORM ) self.normals['a_position'] = np.array([*zip(self.V['a_position'], self.V['a_position'] + self.V['a_normal'])])\ .reshape(( 2 * len(self.V['a_normal']), 3 )) self.normals['u_color'] = (1,0,0) # u_view is the location of the camera. self.teapot['u_view'] = self.normals['u_view'] = translate((0,0,-10)) # u_model is how the world is represented relative to the camera. # in this case, we intend on having the teapot spin around. self.teapot['u_model'] = self.normals['u_model'] = np.eye( 4, dtype=np.float32 ) self.teapot['u_base_color'] = (0, 1, 0) self.teapot['u_reflection'] = 1. self.teapot['u_ambient'] = .11 self.teapot['u_light_color'] = (1, 1, 1) self.teapot['u_light_intensity'] = 1. self.teapot['u_light_specular'] = (1, 50) self.teapot['u_light_position'] = (10, 10, 20) self.teapot['u_O'] = (0, 0, 10) self.theta, self.phi = 0, 0 self.set_viewport() self.timer = app.Timer( 'auto', self.on_timer, start=True ) gloo.set_state(depth_test=True) self.show()
def __init__(self, sensor=None, i=0, yaw=False, title='Rotating Cube'): app.Canvas.__init__(self, size=(640, 640), title=title, keys='interactive') self.timer = app.Timer('auto', self.on_timer) self.sensor = sensor self.i = i self.yaw = yaw # 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 = translate((0, 0, -5)) model = np.eye(4, dtype=np.float32) self.program['u_model'] = model self.program['u_view'] = view self.theta, self.psi, self.phi = 0, 0, 0 self.activate_zoom() # OpenGL initialization # -------------------------------------- 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() self.show()
def on_draw(self, event): """ canvas update callback """ # Clear depth and color buffers gloo.clear(color=C0) # Filled cube gloo.set_state(depth_test=True, blend=True, depth_mask=False) #gloo.set_depth_mask(False) #gl.glEnable(gl.GL_DEPTH_TEST) #gl.glDepthMask(gl.GL_FALSE) #gl.glEnable(gl.GL_BLEND) # self.quads['u_pass'] = 0.0 self.framebuffer.color_buffer = self.accum self.framebuffer.activate() gloo.clear(color=(0, 0, 0, 0)) #gloo.set_blend_func('one', 'one') #gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE) gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ONE, gl.GL_ONE, gl.GL_ONE) self.quads.draw('triangles', self.indices) self.framebuffer.deactivate() # self.quads['u_pass'] = 1.0 self.framebuffer.color_buffer = self.reveal self.framebuffer.activate() gloo.clear(color=(1, 1, 1, 1)) #gloo.set_blend_func('zero', 'one_minus_src_color') #gl.glBlendFunc(gl.GL_ZERO, gl.GL_ONE_MINUS_SRC_COLOR) gl.glBlendFuncSeparate(gl.GL_ZERO, gl.GL_ONE_MINUS_SRC_COLOR, gl.GL_ZERO, gl.GL_ONE_MINUS_SRC_COLOR) self.quads.draw('triangles', self.indices) self.framebuffer.deactivate() # Filled cube #gloo.set_blend_func('src_alpha', 'one_minus_src_alpha') gl.glBlendFuncSeparate(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA, gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # gloo.set_state('translucent', blend=True, depth_test=False) self.post.draw('triangle_strip', self.indices)
def __init__(self, cfg, type='rgbd', clip_near=0.01, clip_far=10.0): size = util.read_cfg_tuple(cfg, 'renderer', 'resolution', default=(640, 480)) app.Canvas.__init__(self, config=dict(samples=8), show=False, size=size) self.shape = (size[1], size[0]) self.clip_near = clip_near self.clip_far = clip_far self.yz_flip = np.eye(4, dtype=np.float32) self.yz_flip[1, 1], self.yz_flip[2, 2] = -1, -1 self.cam = util.read_cfg_cam(cfg, 'renderer', 'intrinsics', default=np.identity(3)) # Projection matrix self.mat_proj = self.build_projection(self.cam, 0, 0, size[0], size[1], clip_near, clip_far) # Set up shader programs self.type = type if self.type == 'rgbd' or self.type == 'rgbc' or self.type == 'corr': self.program = gloo.Program(_vertex_code, _fragment_code_rgb) elif self.type == 'normals': self.program = gloo.Program(_vertex_code, _fragment_code_normals) gloo.set_state(preset='translucent') gloo.set_clear_color((0.0, 0.0, 0.0, 0.0)) gloo.set_viewport(0, 0, *self.size) # Smooth gl.glEnable(gl.GL_LINE_SMOOTH) gl.glEnable(gl.GL_POLYGON_SMOOTH) gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) gl.glHint(gl.GL_POLYGON_SMOOTH_HINT, gl.GL_NICEST) # Multisampling gl.glEnable(gl.GL_MULTISAMPLE)
def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(800, 600)) ps = self.pixel_scale self.title = "A very fake galaxy [mouse scroll to zoom]" data = np.zeros(n, [('a_position', np.float32, 3), ('a_size', np.float32, 1), ('a_dist', np.float32, 1)]) for i in range(3): P, S, D = make_arm(p, i * 2 * np.pi / 3) data['a_dist'][(i + 0) * p:(i + 1) * p] = D data['a_position'][(i + 0) * p:(i + 1) * p] = P data['a_size'][(i + 0) * p:(i + 1) * p] = S * ps self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.theta, self.phi = 0, 0 self.translate = 5 self.view = translate((0, 0, -self.translate)) self.program.bind(gloo.VertexBuffer(data)) self.program['u_colormap'] = gloo.Texture2D(cmap) self.program['u_size'] = 5. / self.translate self.program['u_model'] = self.model self.program['u_view'] = self.view self.apply_zoom() gloo.set_state(depth_test=False, blend=True, blend_func=('src_alpha', 'one'), clear_color='black') # Start the timer upon initialization. self.timer = app.Timer('auto', connect=self.on_timer) self.timer.start() self.show()
def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(800, 800)) self.translate = 20 # Z Start Location self.program = gloo.Program(vert, frag) self.view = translate((0, 0, -self.translate)) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) # t1 = np.array(X.shape[0]) # for ind, val in enumerate(X): # t1[ind] = Text('Text in root scene (24 pt)', parent=c.scene, color='red') # t1[ind].font_size = 24 # t1[ind] = pos = val, Y[ind], Z[ind] self.font_size = self.physical_size[1] / 24 self.text_pos = self.physical_size[0] / 2, 5 * self.physical_size[ 1] / 6 self.text = visuals.TextVisual(' ', bold=True) self.text.color = 'white' self.apply_zoom() self.program.bind(gloo.VertexBuffer(data)) self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_size'] = 50 / (self.translate) self.theta = 0 self.phi = 0 self.frame = 0 self.stop_rotation = False gloo.set_state('translucent', depth_test=False) self.program['u_frame'] = 0.0 xyzs[:, 3] = spikes[int(self.program['u_frame'][0])] self.program['a_xyzs'] = xyzs self._timer = app.Timer('auto', connect=self.on_timer, start=True) self.show()
def __init__(self, pdbdata, mode='cpk'): #Startup app.Canvas.__init__(self, keys='interactive', size=(W, H)) #Loading shaders self.program = gloo.Program(vertex, fragment) #Analyze pdb file self.parser = PDBParser(QUIET=True, PERMISSIVE=True) self.structure = self.parser.get_structure('model', pdbdata) #Mode selection if mode not in Canvas.visualization_modes: raise Exception('Not recognized visualization mode %s' % mode) self.mode = mode #Get the data of our atoms self.atom_information() #Camera settings self.translate = max(abs(np.concatenate(self.coordinates))) + 40 self.translate = max(-1, self.translate) self.view = translate((0, 0, -self.translate), dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.program['u_projection'] = self.projection self.quaternion = Quaternion() #Load data depending on the mode self.apply_zoom() self.load_data() #self.lines = visuals.LinePlotVisual(self.chain_coords[1], width= 5.0, marker_size= 0.0, color=self.chain_colors[1]) gloo.set_state(depth_test=True, clear_color='white') self.show()
def __init__(self, **kwargs): app.Canvas.__init__(self, **kwargs) self.geometry = 0, 0, 400, 400 self.program = gloo.Program(VERT_CODE, FRAG_CODE) # Set attributes self.program['a_position'] = gloo.VertexBuffer(positions) self.program['a_texcoord'] = gloo.VertexBuffer(texcoords) self.program['u_texture'] = gloo.Texture2D(load_crate()) # Handle transformations self.init_transforms() gloo.set_clear_color((1, 1, 1, 1)) gloo.set_state(depth_test=True) self._timer = app.Timer('auto', connect=self.update_transforms) self._timer.start()
def __init__(self): app.Canvas.__init__(self, title='Molecular viewer', keys='interactive') self.size = 1200, 800 self.program = gloo.Program(vertex, fragment) self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.translate = 40 translate(self.view, 0, 0, -self.translate) fname = load_data_file('molecular_viewer/micelle.npz') self.load_molecule(fname) self.load_data() self.theta = 0 self.phi = 0 gloo.set_state(depth_test=True, clear_color='black') self._timer = app.Timer('auto', connect=self.on_timer, start=True)
def __init__(self): app.Canvas.__init__(self, title='Use your wheel to zoom!', keys='interactive', app="PySide") self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) self.program['a_position'] = y.reshape(-1, 1) self.program['a_color'] = color self.program['a_index'] = index self.program['u_scale'] = (1., 1.) self.program['u_size'] = (nrows, ncols) self.program['u_n'] = n gloo.set_viewport(0, 0, *self.physical_size) #TODO change positional timer argument back to 'auto' and multiple handle sample pulling logic self._timer = app.Timer(1.0/250.0, connect=self.on_timer, start=True) gloo.set_state(clear_color='black', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) self.show()
def on_draw(self, event): gloo.clear() if self.visible: self.program.bind(gloo.VertexBuffer(self.data1)) self.program.draw('points') self.program.bind(self.vertices_buff) gloo.set_state(blend=False, depth_test=True, polygon_offset_fill=True) self.program['u_color'] = 1, 1, 1, 1 self.program.draw('triangles', self.filled_buf) gloo.set_state(blend=True, depth_test=True, polygon_offset_fill=False) gloo.set_depth_mask(False) self.program['u_color'] = 0, 0, 0, 1 self.program.draw('lines', self.outline_buf) gloo.set_depth_mask(True)
def __init__(self, prime_numbers): app.Canvas.__init__(self, keys='interactive') self.prime_numbers = prime_numbers # Create vertices v_position, v_color, v_size = self.get_vertices() self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) # Set uniform and attribute self.program['a_color'] = gloo.VertexBuffer(v_color) self.program['a_position'] = gloo.VertexBuffer(v_position) self.program['a_size'] = gloo.VertexBuffer(v_size) gloo.set_state(clear_color='white', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) img = self.render() io.write_png('render.png', img) self.show()
def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(800, 600)) # Create program self._program = gloo.Program(VERT_SHADER, FRAG_SHADER) self._program.bind(gloo.VertexBuffer(data)) self._program['s_texture'] = gloo.Texture2D(im1) # Create first explosion self._new_explosion() # Enable blending gloo.set_state(blend=True, clear_color='black', blend_func=('src_alpha', 'one')) gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1]) self._timer = app.Timer('auto', connect=self.update, start=True) self.show()
def on_initialize(self, event): self.nodes = np.zeros(self.M + self.N, [('position', 'f4', 3), ('color', 'f4', 4), ('size', 'f4', 1)]) self.agents = self.nodes[:self.N] self.goods = self.nodes[self.N:] self.agents['position'][:] = np.random.uniform(-0.25, +0.25, (self.N, 3)) self.agents['size'] = 30 self.agents['color'][:] = 0, 0, 1, 1 self.goods['size'] = 20 self.goods['color'][:] = 0, 1, 0, 1 self.goods['position'][:] = np.random.uniform(-0.25, +0.25, (self.M, 3)) #Text self.width = self.size[0] self.height = self.size[1] self.font_size = 12. self.text = [ visuals.TextVisual(str(x), bold=True, color='white') for x in range(self.N) ] self.tr_sys = visuals.transforms.TransformSystem(self) self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) # Set uniform and attribute self.vbo_position = gloo.VertexBuffer(self.nodes['position'].copy()) self.vbo_color = gloo.VertexBuffer(self.nodes['color'].copy()) self.vbo_size = gloo.VertexBuffer(self.nodes['size'].copy()) self.program['color'] = self.vbo_color self.program['size'] = self.vbo_size self.program['position'] = self.vbo_position gloo.set_state(clear_color='white', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha'))
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 = 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 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.size)
def __init__(self, title='VisPy canvas', size=(800, 600), position=None, show=False, autoswap=True, app=None, create_native=True, vsync=False, resizable=True, decorate=True, fullscreen=False, config=None, shared=None, keys=None, parent=None, dpi=None, always_on_top=False, px_scale=1, bgcolor='black'): super(Canvas, self).__init__(title=title, size=size, position=position, show=show, autoswap=autoswap, app=app, create_native=create_native, vsync=vsync, resizable=resizable, decorate=decorate, fullscreen=fullscreen, config=config, shared=shared, keys=keys, parent=parent, dpi=dpi, always_on_top=always_on_top, px_scale=px_scale, bgcolor=bgcolor) gloo.set_state(depth_test=True)
def __init__(self,points,colors,sizes,title="Clarity Visualization",size=(1440,900),axis=True): SceneCanvas.__init__(self, title=title, keys='interactive', size=size) # Create vertices n, _ = points.shape data = np.zeros(n, [('a_position', np.float32, 3), ('a_bg_color', np.float32, 4), ('a_fg_color', np.float32, 4), ('a_size', np.float32, 1)]) data['a_position'] = points data['a_bg_color'] = colors # RGBA data['a_fg_color'] = 255, 255, 255, 1 # outline color data['a_size'] = sizes # length n u_linewidth = 0 u_antialias = 0 self.translate = np.max(points[:,2]) self.scale = self.translate self.program = gloo.Program(vert, frag) self.view = translate((0,0, -self.translate)) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.apply_zoom() self.program.bind(gloo.VertexBuffer(data)) self.program['u_linewidth'] = u_linewidth self.program['u_antialias'] = u_antialias self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_size'] = self.scale / self.translate # smallest is 1 self.theta = 0 self.phi = 0 gloo.set_state('translucent', clear_color='black') self.timer = app.Timer('auto', connect=self.on_timer, start=True) self.show()
def create_mesh(self, x, y, u, v): tri = scipy.spatial.Delaunay(np.column_stack([x, y])) edges = tri.simplices.astype(np.uint32) uv = [] for c in [u, v]: if c is not None: c = c.astype('f4') c = .5 + .5 * c / np.abs(c).max() uv.append(c) data = np.column_stack([x.astype('f4'), y.astype('f4')] + uv).view(dtype=[ ('position', 'f4', 2), ('texcoord', 'f4', 2 if v is not None else 1), ]) self.vbo = gloo.VertexBuffer(data) self.index = gloo.IndexBuffer(edges) gloo.set_state(blend=True, clear_color='white', blend_func=('src_alpha', 'one_minus_src_alpha'))