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 activate_zoom(self): gloo.set_viewport(0, 0, *self.physical_size) projection = perspective(45.0, self.size[0] / float(self.size[1]), 1.0, 10000.0) self.program['projection'] = projection self.update()
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, size=(1600, 900), no_distort=False): '''Distorter object: Applies distortion to Contexts and drawables - size (X, Y): Size of monitor - distortion (Bool): Apply distortion or not? ''' self.size = size self.left_eye_tex = gloo.Texture2D(shape=(4096, 4096) + (3,)) self.right_eye_tex = gloo.Texture2D(shape=(4096, 4096) + (3,)) self.left_eye = gloo.FrameBuffer(self.left_eye_tex, gloo.RenderBuffer(self.size)) self.right_eye = gloo.FrameBuffer(self.right_eye_tex, gloo.RenderBuffer(self.size)) self.left_eye_program, self.left_eye_indices = Mesh.make_eye(self.left_eye_tex, 'left') self.right_eye_program, self.right_eye_indices = Mesh.make_eye(self.right_eye_tex, 'right') self.IPD = 0.0647 # Interpupilary distance in m # Male: 64.7 mm # Female: 62.3 mm self.L_projection = parameters.projection_left.T self.R_projection = parameters.projection_right.T self.no_distort = no_distort if self.no_distort: self.projection = perspective(30.0, 1920 / float(1080), 2.0, 10.0) self.draw = self.draw_no_distortion else: self.draw = self.draw_distortion
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, coordinate, color, radius, W, H): visuals.Visual.__init__(self, vertex, fragment) self.size = W, H #Camera settings self.translate = 120 self.view = translate((0, 0, -self.translate), dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = perspective(45.0, self.size[0] / float(self.size[1]), 1.0, 1000.0) self.shared_program.vert['model'] = self.model self.shared_program.vert['view'] = self.view self.shared_program.vert['projection'] = self.projection self.shared_program.frag['projection'] = self.projection self.shared_program.vert['light_position'] = 0., 0., 2. self.shared_program.frag['light_spec_position'] = -5., 5., -5. self.shared_program.vert['position'] = coordinate self.shared_program.vert['radius'] = radius self.shared_program.frag['radius'] = radius self.shared_program.frag['color'] = color self._draw_mode = 'points'
def __init__(self): app.Canvas.__init__(self, size=(1024, 1024), title='Skybox example', keys='interactive') self.cubeSize = 10 self.pressed = False self.azimuth = pi / 2.0 self.elevation = pi / 2.0 self.distanceMin = 1 self.distanceMax = 50 self.distance = 30 self.sensitivity = 5.0 self.view = getView(self.azimuth, self.elevation, self.distance) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.program = gloo.Program(vertex_shader, fragment_shader, count=24) self.program['a_position'] = faces * self.cubeSize self.program['a_texcoord'] = faces self.program['a_texture'] = gloo.TextureCube(texture, interpolation='linear') self.program['u_model'] = self.model self.program['u_view'] = self.view gloo.set_viewport(0, 0, *self.physical_size) self.projection = perspective(60.0, self.size[0] / float(self.size[1]), 1.0, 100.0) self.program['u_projection'] = self.projection gl.glEnable(gl.GL_DEPTH_TEST) gloo.set_clear_color('black') self.show()
def toggle_projection(self, event=None): """Toggle between perspective and orthonormal projection modes.""" self.perspective = not self.perspective if self.perspective: self.volume_renderer.set_vol_projection(perspective(60, 1., 100, 0)) else: self.volume_renderer.set_vol_projection(ortho(-1, 1, -1, 1, -1000, 1000))
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, size=(1024, 1024), title='Skybox example', keys='interactive') self.cubeSize = 10 self.pressed = False self.azimuth = pi / 2.0 self.elevation = pi / 2.0 self.distanceMin = 1 self.distanceMax = 50 self.distance = 30 self.sensitivity = 5.0 self.view = getView(self.azimuth, self.elevation, self.distance) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) self.program = gloo.Program(vertex_shader, fragment_shader, count=24) self.program['a_position'] = faces*self.cubeSize self.program['a_texcoord'] = faces self.program['a_texture'] = gloo.TextureCube(texture, interpolation='linear') self.program['u_model'] = self.model self.program['u_view'] = self.view gloo.set_viewport(0, 0, *self.physical_size) self.projection = perspective(60.0, self.size[0] / float(self.size[1]), 1.0, 100.0) self.program['u_projection'] = self.projection gl.glEnable(gl.GL_DEPTH_TEST) gloo.set_clear_color('black') self.show()
def apply_zoom(self): """Determine the projection of the canvas""" width, height = self.physical_size gloo.set_viewport(0, 0, width, height) self.projection = perspective(95.0, width / float(height), 1.0, 400.0) self.program['u_projection'] = self.projection
def __init__(self, mesh, position=(0.0, 0.0, 0.0), orientation=None, color=(255., 0., 0., 255.0), faces=None, shader_manager=None): '''Orientation must be a 4x4 rotation matrix''' self.position = np.array(position, dtype=np.float32) if orientation is None: self.orientation = np.eye(4) else: assert orientation.shape == (4, 4), "Orientation must be a 4x4 numpy array" self.orientation = orientation if len(color) == 3: color = color + (255,) self.color = np.array(color, dtype=np.float32) / 255. # Normalize to [0, 1] self.program = gloo.Program(self._vertex_shader, self._fragment_shader) self.program.bind(mesh) self.faces = gloo.IndexBuffer(faces) self.model = np.eye(4, dtype=np.float32) self.model = self.model.dot(translate(self.position)) self.view = np.eye(4, dtype=np.float32) self.projection = perspective(30.0, 800 / float(800), 2.0, 500.0) self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_projection'] = self.projection self.program['u_color'] = self.color self.children = []
def __init__(self, mesh, position=(0.0, 0.0, 0.0), orientation=None, color=(255., 0., 0., 255.0), faces=None, shader_manager=None): '''Orientation must be a 4x4 rotation matrix''' self.position = np.array(position, dtype=np.float32) if orientation is None: self.orientation = np.eye(4) else: assert orientation.shape == ( 4, 4), "Orientation must be a 4x4 numpy array" self.orientation = orientation if len(color) == 3: color = color + (255, ) self.color = np.array(color, dtype=np.float32) / 255. # Normalize to [0, 1] self.program = gloo.Program(self._vertex_shader, self._fragment_shader) self.program.bind(mesh) self.faces = gloo.IndexBuffer(faces) self.model = np.eye(4, dtype=np.float32) self.model = self.model.dot(translate(self.position)) self.view = np.eye(4, dtype=np.float32) self.projection = perspective(30.0, 800 / float(800), 2.0, 500.0) self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_projection'] = self.projection self.program['u_color'] = self.color self.children = []
def __init__(self, size=(1600, 900), no_distort=False): '''Distorter object: Applies distortion to Contexts and drawables - size (X, Y): Size of monitor - distortion (Bool): Apply distortion or not? ''' self.size = size self.left_eye_tex = gloo.Texture2D(shape=(4096, 4096) + (3, )) self.right_eye_tex = gloo.Texture2D(shape=(4096, 4096) + (3, )) self.left_eye = gloo.FrameBuffer(self.left_eye_tex, gloo.RenderBuffer(self.size)) self.right_eye = gloo.FrameBuffer(self.right_eye_tex, gloo.RenderBuffer(self.size)) self.left_eye_program, self.left_eye_indices = Mesh.make_eye( self.left_eye_tex, 'left') self.right_eye_program, self.right_eye_indices = Mesh.make_eye( self.right_eye_tex, 'right') self.IPD = 0.0647 # Interpupilary distance in m # Male: 64.7 mm # Female: 62.3 mm self.L_projection = parameters.projection_left.T self.R_projection = parameters.projection_right.T self.no_distort = no_distort if self.no_distort: self.projection = perspective(30.0, 1920 / float(1080), 2.0, 10.0) self.draw = self.draw_no_distortion else: self.draw = self.draw_distortion
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 self.transparency = 1 gloo.set_clear_color((0, 0, 0), 0.1) gloo.set_state('opaque') gloo.set_polygon_offset(1, 1) #self._timer = app.Timer('auto', connect=self.on_timer, start=True) #Siempre poner un timer asi se pone bien la funcion update!! solo en pyglet, en pyqt5 funciona joya!!! self._monitor = MidiMonitor(connect=self.on_midi) self.show()
def apply_zoom(self): gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1]) self.projection = perspective(self.fov, self.size[0] / float(self.size[1]), 0.01, 10000.0) for k, prog in self.GL_progs.items(): prog['u_projection'] = self.projection
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 apply_resize(self,viewpoint): gloo.set_viewport(*viewpoint) self.projection = perspective(42.74, self.size[0] / float(self.size[1]), 1.0, 1000.0) # self.projection = ortho(left=-1,right=1,bottom=-1,top=1,znear=0.10,zfar=1000) self.program['u_projection'] = self.projection
def on_resize(self, event): # setup the new viewport gloo.set_viewport(0, 0, *event.physical_size) # recompute the projection matrix w, h = event.size self.projection = perspective(45.0, w / float(h), 1.0, 1000.0) self.program['u_projection'] = self.projection
def on_resize(self, event): gloo.set_viewport(0, 0, event.physical_size[0], event.physical_size[1]) self.projection = perspective(45.0, event.size[0] / float(event.size[1]), 1.0, 100.0) self.program['u_projection'] = self.projection
def on_resize(self, event): width, height = event.size self.size = event.size gloo.set_viewport(0, 0, width, height) self.aspect = width / float(height) self.projection = perspective(45.0, width / float(height), 2, 10.0) self.program_quad['u_projection'] = self.projection
def apply_zoom(self): gloo.set_viewport(0, 0, self.canvas.physical_size[0], self.canvas.physical_size[1]) self.projection = transforms.perspective( 90.0, self.canvas.size[0] / float(self.canvas.size[1]), 1.0, 1000.0) self.transform_uniforms['u_projection'] = self.projection
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 on_resize(self, event): width, height = event.size gloo.set_viewport(0, 0, width, height) self.projection = perspective(45.0, width / float(height), 1.0, 1000.0) self.program_data['u_projection'] = self.projection self.program_axis['u_projection'] = self.projection self.program_plane['u_projection'] = self.projection self.update()
def on_resize(self, event): self.resolution = event.physical_size gloo.set_viewport(0, 0, self.resolution[0], self.resolution[1]) self.projection = perspective(45.0, event.size[0] / float(event.size[1]), 1.0, 1000.0) for program in self.programs: program['u_projection'] = self.projection
def on_resize(self, event): self.width, self.height = event.size # setup the new viewport gloo.set_viewport(0, 0, self.width, self.height) # recompute the projection matrix self.projection = perspective(45.0, self.width / float(self.height), 1.0, 1000.0) self.program['u_projection'] = self.projection
def apply_zoom(self): width, height = self.physical_size vp = (0, 0, width, height) gloo.set_viewport(vp) self.projection = perspective(45.0, width / float(height), 1.0, 1000.0) self.program['u_projection'] = self.projection self.text.transforms.configure(canvas=self, viewport=vp)
def recalc_projection(self): self.projection = perspective( 25.0, self.width / float(self.height), 1.0, 50.0+self.zoom) self.fog_near = self.zoom self.fog_far = 20.0 + self.zoom
def on_resize(self, event): width, height = event.size self.size = event.size gloo.set_viewport(0, 0, width, height) self.aspect = width / float(height) self.projection = perspective(self.fovy, width / float(height), 1.0, self.zfar) self.program['u_projection'] = self.projection
def on_resize(self, event): """ We create a callback function called when the window is being resized. Updating the OpenGL viewport lets us ensure that Vispy uses the entire canvas. """ gloo.set_viewport(0, 0, *event.physical_size) ratio = event.physical_size[0] / float(event.physical_size[1]) self.program['u_projection'] = perspective(45.0, ratio, 2.0, 10.0)
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 toggle_projection(self, event=None): """Toggle between perspective and orthonormal projection modes.""" self.perspective = not self.perspective if self.perspective: self.volume_renderer.set_vol_projection(perspective(60, 1., 100, 0)) self.volume_renderer.uniform_changes['projection'] = 'perspective' else: self.volume_renderer.set_vol_projection(ortho(-1, 1, -1, 1, -1000, 1000)) self.volume_renderer.uniform_changes['projection'] = 'orthographic'
def resetProjection(self): # calculate projection gloo.set_viewport(0, 0, *self.physical_size) aspect = self.size[0] / float(self.size[1]) self._projection = perspective(self.fov, aspect, self.nearDistance, self.farDistance) self.program['projection'] = self._projection # calculate projection for the orientation indicator in the lower left orthoScale = 1/500 orthoProjection = glm.ortho( -self.size[0]*orthoScale , self.size[0]*orthoScale, -self.size[1]*orthoScale, self.size[1]*orthoScale) self._oriProjection = glm.translate(orthoProjection,glm.vec3(-self.size[0]*orthoScale+0.28,-self.size[1]*orthoScale+0.28,0))
def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(800, 600)) rospy.init_node('imu_visualizer') # assert_node_alive('torso_arduino') rospy.Subscriber('imu/data_raw', Imu, self.on_imu) # Cleanup when termniating the node rospy.on_shutdown(self.shutdown) 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.yaw = 0 # yaw self.roll = 0 # roll self.pitch = 0 # pitch gloo.set_clear_color('white') # gloo.set_clear_color('black') gloo.set_state('opaque') gloo.set_polygon_offset(1, 1) self.first = True self.yaw0 = None self._timer = app.Timer('auto', connect=self.on_timer, start=True) self.show() app.run() rospy.signal_shutdown("Shutting done.") self.shutdown() print('Node shutdown.')
def on_key_press(self, event): if event.text == 'j': self.th += self.delta elif event.text == 'k': self.th -= self.delta elif event.text == 'h': self.ph -= self.delta elif event.text == 'l': self.ph += self.delta elif event.text == 'n': width, height = self.size self.fov += self.delta/2 self.fov = np.clip(self.fov, 10., 90.) self.proj = perspective(self.fov, np.divide(width, height), 1., 100.) elif event.text == 'p': width, height = self.size self.fov -= self.delta/2 self.fov = np.clip(self.fov, 10., 90.) self.proj = perspective(self.fov, np.divide(width, height), 1., 100.) self.view = lookat(self.th, self.ph) self.update()
def __init__(self, positions, data_path, atoms_number=50, resolution=(800, 600)): self.data_path = data_path self.resolution = resolution app.Canvas.__init__(self, keys='interactive', size=self.resolution) self.delta_pos = positions[:1].mean() self.positions = positions # - self.delta_pos self.atoms_number = atoms_number self.mouse_press_point = 0, 0 self.mouse_press = False self.theta = 0 self.phi = 0 self.delta_theta = 0 self.delta_phi = 0 self.animation_step = 25000 # max 474984 self.translate = 50 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.alpha = np.array((0, 0, 0, 0)).astype(np.float32).reshape(1, 4) self.a_color = [ np.append(np.random.rand(3), 1).astype(np.float32).reshape(1, 4) for _ in range(self.positions.shape[1]) ] self.plot_lines = PlotLines(self) self.plot_points = PlotPoints(self) self.plot_itrium = PlotItrium(self) self.programs = [ self.plot_lines.program, self.plot_points.program, self.plot_itrium.program ] self.context.set_clear_color('white') self.context.set_state('translucent') 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, 600)) dirname = path.join(path.abspath(path.curdir),'data') positions, faces, normals, texcoords = \ read_mesh(load_data_file('cube.obj', directory=dirname)) self.filled_buf = gloo.IndexBuffer(faces) if False: self.program = gloo.Program(VERT_TEX_CODE, FRAG_TEX_CODE) self.program['a_position'] = gloo.VertexBuffer(positions) self.program['a_texcoord'] = gloo.VertexBuffer(texcoords) self.program['u_texture'] = gloo.Texture2D(load_crate()) else: self.program = gloo.Program(VERT_COLOR_CODE, FRAG_COLOR_CODE) self.program['a_position'] = gloo.VertexBuffer(positions) self.program['u_color'] = 1, 0, 0, 1 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('gray') gloo.set_state('opaque') gloo.set_polygon_offset(1, 1) self._timer = app.Timer('auto', connect=self.on_timer, start=True) self.show()
def get_projection(self, viewbox): w, h = viewbox.resolution fov = self._fov aspect = 1.0 fx = fy = 1.0 # todo: hard-coded # Calculate distance to center in order to have correct FoV and fy. if fov == 0: M = transforms.ortho(-0.5*fx, 0.5*fx, -0.5*fy, 0.5*fy, -10000, 10000) self._d = 0 else: d = fy / (2 * math.tan(math.radians(fov)/2)) val = math.sqrt(10000) # math.sqrt(getDepthValue()) znear, zfar = d/val, d*val M = transforms.perspective(fov, aspect, znear, zfar) # Translation and rotation is done by our 'transformation' parameter return M
def __init__(self, mesh, vertexShader, fragShader): app.Canvas.__init__(self, keys='interactive', size=(1600,900)) self.mesh = mesh self.data = mesh.buildBuffer() self.program = gloo.Program(vertexShader, fragShader) self.model = np.eye(4, dtype=np.float32) self.projection = perspective(45.0, self.size[0] / float(self.size[1]), 1.0, 1000.0) gloo.set_viewport(0, 0, self.size[0], self.size[1]) self.camera = camera.Camera(np.array([0,0,-5], dtype = np.float32), np.array([0,0,0], dtype = np.float32), 45.0, self.size) self.view = self.camera.view self.program.bind(gloo.VertexBuffer(mesh.buildBuffer())) self.program['u_model'] = self.model self.program['u_view'] = self.view self.program['u_projection'] = self.projection self.program['u_lightPos'] = np.array([20, 20, -20], dtype = np.float32); self.program['u_lightColor'] = np.array([1, 1, 1], dtype = np.float32); gloo.set_depth_mask(True) gloo.set_blend_func('src_alpha', 'one_minus_src_alpha') gloo.set_blend_equation('func_add') gloo.set_cull_face('back') gloo.set_front_face('cw') gloo.set_state(blend=True, depth_test=True, polygon_offset_fill=True) self.show() self.theta = 0 self.phi = 0
def apply_zoom(self): 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
def __init__(self): app.Canvas.__init__(self, keys="interactive", size=(W, H)) self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) self.l_bb_center = [] vbuffer = np.array( [ [0.6459411621, 0.4028605652, 0.1], [0.6459389496, 0.4028595352, 0.1], [0.6459381104, 0.4028591156, 0.1], [0.6459369659, 0.4028584671, 0.1], [0.6459358978, 0.4028577423, 0.1], [0.6459277344, 0.4028533173, 0.1], [0.6459257507, 0.402852211, 0.1], [0.6459215546, 0.402849884, 0.1], [0.6459153748, 0.402846489, 0.1], [0.6459139252, 0.4028466415, 0.1], [0.6459128571, 0.4028461456, 0.1], [0.6459088135, 0.4028437805, 0.1], [0.6459063721, 0.4028423691, 0.1], [0.6459036255, 0.4028408813, 0.1], [0.6459024811, 0.4028401947, 0.1], [0.6459013367, 0.4028395844, 0.1], [0.6458982086, 0.402837944, 0.1], [0.6458907318, 0.4028339005, 0.1], [0.6458789825, 0.4028277206, 0.1], [0.645867157, 0.4028213882, 0.1], [0.6458557892, 0.402815094, 0.1], [0.6458435822, 0.4028086472, 0.1], ] ).astype(np.float32) arr_min = np.full(vbuffer.shape, vbuffer.min(axis=0)) arr_max = np.full(vbuffer.shape, vbuffer.max(axis=0)) arr_bounded = vbuffer - arr_min arr_bounded = arr_bounded * vbuffer.max(axis=0) * i_scale_factor arr_bounded = arr_bounded.astype(np.float32) print(arr_bounded) self.l_bb_center = (vbuffer.max(axis=0) - vbuffer.min(axis=0)) * i_scale_factor / 2.0 print(self.l_bb_center) # Set uniform and attribute self.program["a_position"] = gloo.VertexBuffer(arr_bounded) self.translate = 5 self.view = translate((-self.l_bb_center[0], -self.l_bb_center[1], -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.projection = ortho(-1, 1, -1, 1, -10, 10) 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 on_resize(self, event): width, height = event.size gl.glViewport(0, 0, width, height) self.projection = perspective( 45.0, width/float(height), 1.0, 1000.0 ) self.program['u_projection'] = self.projection
def on_resize(self, event): width, height = event.size gloo.set_viewport(0, 0, width, height) self.projection = perspective(25.0, width / float(height), 2.0, 100.0) self.program['u_projection'] = self.projection
def on_resize(self, event): gloo.set_viewport(0, 0, event.physical_size[0], event.physical_size[1]) self.projection = perspective(45.0, event.size[0] / float(event.size[1]), 1.0, 1000.0) # self.projection = ortho(-1, 1, -1, 1, -10, 10) self.program["u_projection"] = self.projection
def on_resize(self, event): width, height = event.size gloo.set_viewport(0, 0, width, height) far = SIZE*(NBLOCKS-2) self.projection = perspective(25.0, width / float(height), 1.0, far) self.program['u_projection'] = self.projection
def activate_zoom(self): gloo.set_viewport(0, 0, *self.physical_size) self.projection = perspective(60.0, self.size[0] / float(self.size[1]), 1.0, 100.0) self.program['u_projection'] = self.projection
def _set_projection(self, size): width, height = size set_viewport(0, 0, width, height) projection = perspective(30.0, width / float(height), 2.0, 10.0) self.cube['projection'] = projection
def on_resize(self, event): gloo.set_viewport(0, 0, event.physical_size[0], event.physical_size[1]) self.projection = perspective(45.0, event.size[0] / float(event.size[1]), 2.0, 10.0) self.program['u_projection'] = self.projection
def on_resize(self, event): gloo.set_viewport(0, 0, *event.size) projection = perspective(45.0, event.size[0] / float(event.size[1]), 2.0, 10.0) self.program['projection'] = projection