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 __init__(self, **kwargs): self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) 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 = 3 translate(self.view, 0, 0, -self.translate) self.vbo = gloo.VertexBuffer(data) self.program.set_vars(self.vbo) self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_size'] = u_size self.theta = 0 self.phi = 0 self.index = 0 self.timer = app.Timer(1.0 / 400) self.timer.connect(self.on_timer) self.timer.start() # Initialize the canvas for real app.Canvas.__init__(self, **kwargs)
def on_mouse_wheel(self, event): self.translate += event.delta[1] self.translate = max(2, self.translate) self.view = np.eye(4, dtype=np.float32) translate(self.view, 0, 0, -self.translate) self.program['u_view'] = self.view self.update()
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') self.size = 800, 800 self.title = "Atom [zoom with mouse scroll" 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 = 6.5 translate(self.view, 0, 0, -self.translate) 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)
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 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 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 __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, **kwargs): self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) 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 = 3 translate(self.view, 0,0, -self.translate) self.vbo = gloo.VertexBuffer(data) self.program.set_vars(self.vbo ) self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_size'] = u_size self.theta = 0 self.phi = 0 self.index = 0 self.timer = app.Timer(1.0/400) self.timer.connect(self.on_timer) self.timer.start() # Initialize for real app.Canvas.__init__(self, **kwargs)
def __init__(self): app.Canvas.__init__(self, keys='interactive') self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) # Set uniform and attribute self.program['a_id'] = gloo.VertexBuffer(a_id) self.program['a_position'] = gloo.VertexBuffer(a_position) 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['u_model'] = self.model self.program['u_view'] = self.view self.theta = 0 self.phi = 0 self.context.set_clear_color('white') self.context.set_state('translucent') self.timer = app.Timer('auto', connect=self.on_timer)
def on_initialize(self, event): # 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 = np.eye(4, dtype=np.float32) transforms.translate(self.view, 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 self.projection = perspective(45.0, self.width / float(self.height), 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 self.__setup_blending_mode()
def test_transforms(): """Test basic transforms""" xfm = np.random.randn(4, 4).astype(np.float32) # Do a series of rotations that should end up into the same orientation # again, to ensure the order of computation is all correct # i.e. if rotated would return the transposed matrix this would not work # out (the translation part would be incorrect) new_xfm = xfm.dot(rotate(180, (1, 0, 0)).dot(rotate(-90, (0, 1, 0)))) new_xfm = new_xfm.dot(rotate(90, (0, 0, 1)).dot(rotate(90, (0, 1, 0)))) new_xfm = new_xfm.dot(rotate(90, (1, 0, 0))) assert_allclose(xfm, new_xfm) new_xfm = translate((1, -1, 1)).dot(translate((-1, 1, -1))).dot(xfm) assert_allclose(xfm, new_xfm) new_xfm = scale((1, 2, 3)).dot(scale((1, 1. / 2., 1. / 3.))).dot(xfm) assert_allclose(xfm, new_xfm) # These could be more complex... xfm = ortho(-1, 1, -1, 1, -1, 1) assert_equal(xfm.shape, (4, 4)) xfm = frustum(-1, 1, -1, 1, -1, 1) assert_equal(xfm.shape, (4, 4)) xfm = perspective(1, 1, -1, 1) assert_equal(xfm.shape, (4, 4))
def __init__(self): app.Canvas.__init__(self) self.size = 800, 600 self.vertices, self.filled, self.outline = cube() self.filled_buf = gloo.ElementBuffer(self.filled) self.outline_buf = gloo.ElementBuffer(self.outline) self.program = gloo.Program(vert, frag) self.program.set_vars(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 self._timer = app.Timer(1.0 / 60) self._timer.connect(self.on_timer) self._timer.start()
def test_transforms(): """Test basic transforms""" xfm = np.random.randn(4, 4).astype(np.float32) for rot in [xrotate, yrotate, zrotate]: new_xfm = rot(rot(xfm, 90), -90) assert_allclose(xfm, new_xfm) new_xfm = rotate(rotate(xfm, 90, 1, 0, 0), 90, -1, 0, 0) assert_allclose(xfm, new_xfm) new_xfm = translate(translate(xfm, 1, -1), 1, -1, 1) assert_allclose(xfm, new_xfm) new_xfm = scale(scale(xfm, 1, 2, 3), 1, 1. / 2., 1. / 3.) assert_allclose(xfm, new_xfm) # These could be more complex... xfm = ortho(-1, 1, -1, 1, -1, 1) assert_equal(xfm.shape, (4, 4)) xfm = frustum(-1, 1, -1, 1, -1, 1) assert_equal(xfm.shape, (4, 4)) xfm = perspective(1, 1, -1, 1) assert_equal(xfm.shape, (4, 4))
def __init__(self): app.Canvas.__init__(self) self.size = 800, 800 self.title = "Atom [zoom with mouse scroll" 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 = 6.5 translate(self.view, 0, 0, -self.translate) 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 self.timer = app.Timer(1.0 / 60) self.timer.connect(self.on_timer) self.timer.start()
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()
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, 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 self._timer = app.Timer('auto', connect=self.on_timer, start=True)
def __init__(self): app.Canvas.__init__(self, keys='interactive') self.size = 800, 600 self.title = "A very fake galaxy [mouse scroll to zoom]" self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) 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.theta, self.phi = 0, 0 self.translate = 5 translate(self.view, 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 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()
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) 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 self.timer = app.Timer(1.0 / 60) self.timer.connect(self.on_timer) self.timer.start()
def __init__(self): Canvas.__init__(self, size=[800, 800], close_keys='ESCAPE', show=True, title='Galaxy') self.galaxy = Galaxy(35000) self.galaxy.reset(13000, 4000, 0.0004, 0.90, 0.90, 0.5, 200, 300) program = gloo.Program(vertex, fragment, count=len(self.galaxy)) view = np.eye(4, dtype=np.float32) translate(view, 0, 0, -5) program['u_view'] = view program['u_model'] = np.eye(4, dtype=np.float32) program['u_projection'] = np.eye(4, dtype=np.float32) from PIL import Image from specrend import (SMPTEsystem, spectrum_to_xyz, norm_rgb, xyz_to_rgb, bb_spectrum) image = Image.open("particle.bmp") program['u_texture'] = np.array(image)/255.0 t0 = 1000. t1 = 10000. n = 256 dt = (t1 - t0) / n colors = np.zeros((1, n, 3), dtype=np.float32) for i in range(n): cs = SMPTEsystem temperature = t0 + i*dt x, y, z = spectrum_to_xyz(bb_spectrum, temperature) r, g, b = xyz_to_rgb(cs, x, y, z) colors[0, i] = norm_rgb(r, g, b) program['u_colormap'] = gloo.Texture2D(colors) program['a_size'] = self.galaxy['size'] program['a_type'] = self.galaxy['type'] self.program = program
def __init__(self, configuration_file, output_file, offline): app.Canvas.__init__(self, title='Molecular viewer', keys='interactive') if offline: self.simulation = None self.load_coordinates_from_file(output_file) else: self.simulation = Simulation(configuration_file, output_filename=output_file) self.simulation.run(s_d=2) 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 = 20 translate(self.view, 0, 0, -self.translate) self.load_molecules_from_simulation(self.get_coordinates()) self.load_data() self.theta = 0 self.phi = 0 self._timer = app.Timer('auto', connect=self.on_timer, start=True)
def on_mouse_wheel(self, event): #print(event.delta[1]) oldscale=self.scale if event.delta[1]>0: self.scale /=event.delta[1]+1 else: self.scale *= -event.delta[1]+1 factor=self.scale/oldscale self.event=event self.projection = ortho(-self.scale/2, self.scale/2, -self.scale/2, self.scale/2, -1, 100) #perspective(1.0, width / float(height), 1.0, 10000000.0) self.program['u_projection'] = self.projection self.view = np.eye(4, dtype=np.float32) x,y=self.getEventCoordinates(event) print(factor) if factor<1: x-(x-self.pos[0])/factor y-(y-self.pos[1])/factor else: x=self.pos[0]-(x-self.pos[0])/factor y=self.pos[1]-(y-self.pos[1])/factor self.pos[0]=x self.pos[1]=y translate(self.view, -x, -y, -1) self.program['u_view'] = self.view self.getEventCoordinates(self.event) self.update()
def get_projection(self, viewbox): w, h = viewbox.resolution from vispy.util import transforms projection = np.eye(4) transforms.scale(projection, 2.0/w, 2.0/h) transforms.translate(projection, -1, -1) transforms.scale(projection, 1, -1) # Flip y-axis return projection
def get_projection(self, viewport): w, h = viewport.resolution from vispy.util import transforms projection = np.eye(4) transforms.scale(projection, 2.0 / w, 2.0 / h) transforms.translate(projection, -1, -1) transforms.scale(projection, 1, -1) # Flip y-axis return projection
def __init__(self): '''Map drawable - contains the goddamn map ''' self.projection = np.eye(4) self.view = np.eye(4) self.model = scale(np.eye(4), 0.6) orientation_vector = (0, 1, 0) unit_orientation_angle = np.array(orientation_vector) / np.linalg.norm(orientation_vector) rotate(self.model, -30, *unit_orientation_angle) translate(self.model, -2.2, -2.4, -9) height, width = 5.0, 5.0 # Meters # 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, 1], [1, 1], [1, 0], [0, 0], ], dtype=np.float32) self.indices = IndexBuffer([ 0, 1, 2, 2, 3, 0, ]) ###### TESTING self.position_lla = self.ecef2llh((738575.65, -5498374.10, 3136355.42)) ###### TESTING self.map, self.ranges = self.cache_map(self.position_lla[:2]) self.map, self.ranges = self.get_map(self.position_lla[:2]) self.program = Program(self.frame_vertex_shader, self.frame_frag_shader) default_map_transform = np.eye(4) self.program['vertex_position'] = self.vertices self.program['default_texcoord'] = self.tex_coords self.program['zoom'] = 1 self.program['view'] = self.view self.program['model'] = self.model self.program['projection'] = self.projection self.program['map_transform'] = default_map_transform self.program['map_center'] = self.position_lla[:2] self.program['map_texture'] = self.map self.program['corners'] = self.ranges self.program['user_position'] = self.position_lla[:2] self.program['hide'] = 0
def init_transforms(self): 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.theta = 0 self.phi = 0 translate(self.view, 0, 0, -5)
def update_scene(self): scale_matrix = scale([1, 1, 1]) rotation_matrix = rotate(self.rotation, [0, 1, 0]) translation_matrix = translate([0.4, 0, -4]) model_matrix = scale_matrix @ rotation_matrix @ translation_matrix self.program['model_matrix'] = model_matrix self.program['normal_matrix'] = np.linalg.inv(model_matrix) self.program['view_matrix'] = translate([0, 0, 0]) self.program['projection_matrix'] = perspective(45, 1.66, 1., 100.)
def on_mouse_wheel(self, event): # zoom in/out self.translate += event.delta[1] self.translate = max(2, self.translate) self.view = np.eye(4, dtype=np.float32) translate(self.view, 0, 0, -self.translate) self.program_data['u_view'] = self.view self.program_axis['u_view'] = self.view self.program_plane['u_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 x/X - rotate about x-axis cw/anti-cw y/Y - rotate about y-axis cw/anti-cw z/Z - rotate about z-axis cw/anti-cw space - reset view p(P) - print current view i(I) - zoom in o(O) - zoom out """ self.translate = [0, 0, 0] self.rotate = [0, 0, 0] if (event.text == 'p' or event.text == 'P'): print(self.view) elif (event.text == 'd' or event.text == 'D'): self.translate[0] = 0.3 elif (event.text == 'a' or event.text == 'A'): self.translate[0] = -0.3 elif (event.text == 'w' or event.text == 'W'): self.translate[1] = 0.3 elif (event.text == 's' or event.text == 'S'): self.translate[1] = -0.3 elif (event.text == 'o' or event.text == 'O'): self.translate[2] = 0.3 elif (event.text == 'i' or event.text == 'I'): self.translate[2] = -0.3 elif (event.text == 'x'): self.rotate = [1, 0, 0] elif (event.text == 'X'): self.rotate = [-1, 0, 0] elif (event.text == 'y'): self.rotate = [0, 1, 0] elif (event.text == 'Y'): self.rotate = [0, -1, 0] elif (event.text == 'z'): self.rotate = [0, 0, 1] elif (event.text == 'Z'): self.rotate = [0, 0, -1] elif (event.text == ' '): self.view = self.default_view translate(self.view, -self.translate[0], -self.translate[1], -self.translate[2]) xrotate(self.view, self.rotate[0]) yrotate(self.view, self.rotate[1]) zrotate(self.view, self.rotate[2]) self.program['u_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 x/X - rotate about x-axis cw/anti-cw y/Y - rotate about y-axis cw/anti-cw z/Z - rotate about z-axis cw/anti-cw space - reset view p(P) - print current view i(I) - zoom in o(O) - zoom out """ self.translate = [0, 0, 0] self.rotate = [0, 0, 0] if(event.text == 'p' or event.text == 'P'): print(self.view) elif(event.text == 'd' or event.text == 'D'): self.translate[0] = 0.3 elif(event.text == 'a' or event.text == 'A'): self.translate[0] = -0.3 elif(event.text == 'w' or event.text == 'W'): self.translate[1] = 0.3 elif(event.text == 's' or event.text == 'S'): self.translate[1] = -0.3 elif(event.text == 'o' or event.text == 'O'): self.translate[2] = 0.3 elif(event.text == 'i' or event.text == 'I'): self.translate[2] = -0.3 elif(event.text == 'x'): self.rotate = [1, 0, 0] elif(event.text == 'X'): self.rotate = [-1, 0, 0] elif(event.text == 'y'): self.rotate = [0, 1, 0] elif(event.text == 'Y'): self.rotate = [0, -1, 0] elif(event.text == 'z'): self.rotate = [0, 0, 1] elif(event.text == 'Z'): self.rotate = [0, 0, -1] elif(event.text == ' '): self.view = self.default_view translate(self.view, -self.translate[0], -self.translate[1], -self.translate[2]) xrotate(self.view, self.rotate[0]) yrotate(self.view, self.rotate[1]) zrotate(self.view, self.rotate[2]) self.program['u_view'] = self.view self.update()
def __init__(self): self.view = identity() self.model = identity() self.rotation = identity() self.projection = identity() self.zoom = 40 self.center = (0, 0, 0, 0) translate(self.view, 0, 0, -self.zoom) self.is_fog = True self.fog_near = -1 self.fog_far = 50 self.fog_color = [0, 0, 0]
def update_matrices(self): self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) rotate(self.model, self.theta, 1, 0, 0) rotate(self.model, self.phi, 0, 1, 0) translate(self.view, 0, 0, -self.translate) self.program['u_model'] = self.model self.program['u_view'] = self.view
def generate_image(img_def): c = context.FakeCanvas() img = _load_img(img_def['background']['path']) h, w, _ = img.shape render_fbo = gloo.FrameBuffer(gloo.Texture2D(img), gloo.RenderBuffer( (h, w))) program = gloo.Program(_vert_std, _frag_tex) program['a_pos'] = _unit_square() program['a_tex_coord'] = _unit_square() gloo.set_state(blend=True, blend_func=('one', 'one_minus_src_alpha'), depth_test=True, depth_func='always') with render_fbo: gloo.set_viewport(0, 0, w, h) gloo.set_clear_depth(0) gloo.clear(depth=True, color=False) instances = img_def['instances'] # The unsigned byte depth buffer extraction sets a limit of 255 instances. # Can be extracted as short if necessary. assert (len(instances) <= 255) for i, inst in enumerate(instances): img = _load_img(inst['path']) ih, iw, _ = img.shape x, y, s, r = (inst[k] for k in ['x', 'y', 's', 'r']) program['u_tex'] = gloo.Texture2D(img, interpolation='linear') program['u_mvp'] = \ transforms.translate((-0.5, -0.5, 0)) @ \ transforms.scale((s * iw, s * ih, 1)) @ \ transforms.rotate(r, (0, 0, 1)) @ \ transforms.translate((x, y, -i-1)) @ \ transforms.ortho(0, w, 0, h, 0, 255) program.draw() rgb = render_fbo.read(alpha=False) depth = gl.glReadPixels(0, 0, w, h, gl.GL_DEPTH_COMPONENT, gl.GL_UNSIGNED_BYTE) if not isinstance(depth, np.ndarray): depth = np.frombuffer(depth, np.uint8) depth = np.flip(depth.reshape(h, w), axis=0) masks = np.empty((h, w, len(instances)), np.bool) for i in range(len(instances)): masks[:, :, i] = depth == i + 1 return rgb, depth, masks
def on_key_press(self, event): if event.text == ' ': if self.timer.running: self.timer.stop() else: self.timer.start() if event.text == '.': self.translate -= 1.0 self.view = translate((0, 0, -self.translate)) if event.text == 'e': self.translate += 1.0 self.view = translate((0, 0, -self.translate)) self.update()
def update_matrices(self): self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) rotate(self.model, self.theta, 1, 0, 0) rotate(self.model, self.phi, 0, 1, 0) translate(self.view, 0, 0, -self.translate) self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_normal'] = np.array( np.matrix(np.dot(self.view, self.model)).I.T)
def update_matrices(self): self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) rotate(self.model, self.theta, 1, 0, 0) rotate(self.model, self.phi, 0, 1, 0) translate(self.view, 0, 0, -self.translate) self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_normal'] = np.array(np.matrix(np.dot(self.view, self.model)).I.T)
def render(self, model, view, projection, mode="color", block_rotation=0, element_transform=np.eye(4, dtype=np.float32), uvlock=False): element_rotation = np.eye(4, dtype=np.float32) if self.rotation: rotationdef = self.rotation axis = { "x": [1, 0, 0], "y": [0, 1, 0], "z": [0, 0, 1] }[rotationdef["axis"]] origin = (np.array(rotationdef.get("origin", [8, 8, 8]), dtype=np.float32) - 8.0) / 16.0 * 2.0 element_rotation = np.dot( transforms.translate(-origin), np.dot(transforms.rotate(rotationdef["angle"], axis), transforms.translate(origin))) program = Element.get_program(mode) self.current_program = program # add rotation of block and element to element transformation block_rotation = transforms.rotate(-90 * block_rotation, (0, 1, 0)) element_transform = np.dot(element_rotation, np.dot(element_transform, block_rotation)) complete_model = np.dot(element_transform, model) program["u_model"] = complete_model program["u_view"] = view program["u_projection"] = projection #program["u_normal"] = np.array(np.matrix(np.dot(view, complete_model)).I.T) for i, (texture, uvs) in enumerate(self.faces): if texture is None: continue self.render_face(i, texture, uvs, complete_model, view, projection, element_rotation=element_rotation, element_transform=element_transform, uvlock=uvlock or mode == "uv")
def on_mouse_wheel(self, event): self.translate -= event.delta[1] self.translate = max(2, self.translate) self.view = translate((0, 0, -self.translate)) self.program['u_view'] = self.view self.program['u_size'] = 5 / self.translate self.update()
def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(W, H)) self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) # Set uniform and attribute self.program['a_id'] = gloo.VertexBuffer(a_id) self.program['a_position'] = gloo.VertexBuffer(a_position) self.translate = 5 self.view = translate((0, 0, -self.translate), dtype=np.float32) 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]), 1.0, 1000.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 self.context.set_clear_color('white') self.context.set_state('translucent') self.timer = app.Timer('auto', connect=self.on_timer) self.show()
def __init__(self): 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() 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) self.show()
def on_mouse_wheel(self, event): self.translate -= event.delta[1] self.translate = max(-1, self.translate) self.view = translate((0, 0, -self.translate)) self.program['u_view'] = self.view self.update()