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_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): app.Canvas.__init__(self, size=(512, 512), title='Colored cube', keys='interactive') # Build cube data V, I, _ = create_cube() vertices = VertexBuffer(V) self.indices = IndexBuffer(I) # Build program self.program = Program(vertex, fragment) self.program.bind(vertices) # Build view, model, projection & normal view = translate((0, 0, -5)) model = np.eye(4, dtype=np.float32) self.program['model'] = model self.program['view'] = view self.phi, self.theta = 0, 0 gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True) self.activate_zoom() self.timer = app.Timer('auto', self.on_timer, start=True) self.show()
def __init__(self): app.Canvas.__init__(self, 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, *args, **kwargs): super().__init__(*args, **kwargs) self.timer = app.Timer('auto', connect=self.on_timer, start=True) with open('vertex.glsl') as f: self.vshader = f.read() with open('fragment.glsl') as f: self.fshader = f.read() self.program = Program(self.vshader, self.fshader) v, i, iOutline = create_cube() self.vertices = VertexBuffer(v) self.indices = IndexBuffer(i) self.outlineIndices = IndexBuffer(iOutline) self.program.bind(self.vertices) self.theta, self.phi = 0, 0 self.program['model'] = np.eye(4) self.program['view'] = translate([0, 0, -5]) self.program['texture'] = checkerboard() gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), polygon_offset=(1, 1), blend_func=('src_alpha', 'one_minus_src_alpha'), line_width=5, depth_test=True)
def __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, view=None, transform=None, **kwargs): self.view = view # Add a 3D cube to show us the unit cube. The 1.001 factor is to make # sure that the grid lines are not 'hidden' by volume renderings on the # front side due to numerical precision. vertices, filled_indices, outline_indices = create_cube() self.axis = scene.visuals.Mesh(vertices['position'], outline_indices, parent=self.view.scene, color=kwargs['axis_color'], mode='lines') self.axis.transform = transform self.xax = Axis(pos=[[-1.0, 0], [1.0, 0]], tick_direction=(0, -1), parent=self.view.scene, axis_label='X', anchors=['center', 'middle'], **kwargs) self.yax = Axis(pos=[[0, -1.0], [0, 1.0]], tick_direction=(-1, 0), parent=self.view.scene, axis_label='Y', anchors=['center', 'middle'], **kwargs) self.zax = Axis(pos=[[0, -1.0], [0, 1.0]], tick_direction=(-1, 0), parent=self.view.scene, axis_label='Z', anchors=['center', 'middle'], **kwargs) self.xtr = STTransform() self.xtr = self.xtr.as_matrix() self.xtr.rotate(45, (1, 0, 0)) self.xtr.translate((0, -1., -1.)) self.ytr = STTransform() self.ytr = self.ytr.as_matrix() self.ytr.rotate(-45, (0, 1, 0)) self.ytr.translate((-1, 0, -1.)) self.ztr = STTransform() self.ztr = self.ztr.as_matrix() self.ztr.rotate(45, (0, 1, 0)) self.ztr.rotate(90, (1, 0, 0)) self.ztr.translate((-1, -1, 0.)) self.xax.transform = ChainTransform(transform, self.xtr) self.yax.transform = ChainTransform(transform, self.ytr) self.zax.transform = ChainTransform(transform, self.ztr)
def __init__(self, parent=None): super(VispyWidget, self).__init__(parent=parent) # Prepare Vispy canvas. We set the depth_size to 24 to avoid issues # with isosurfaces on MacOS X self.canvas = scene.SceneCanvas(keys='interactive', show=False, config={'depth_size': 24}) # Set up a viewbox self.view = self.canvas.central_widget.add_view() self.view.parent = self.canvas.scene # Set whether we are emulating a 3D texture. This needs to be enabled # as a workaround on Windows otherwise VisPy crashes. self.emulate_texture = (sys.platform == 'win32' and sys.version_info[0] < 3) self.scene_transform = scene.STTransform() self.limit_transforms = {} # Add a 3D cube to show us the unit cube. The 1.001 factor is to make # sure that the grid lines are not 'hidden' by volume renderings on the # front side due to numerical precision. vertices, filled_indices, outline_indices = create_cube() self.axis = scene.visuals.Mesh(vertices['position'], outline_indices, color=(1, 1, 1), mode='lines') self.axis.transform = self.scene_transform self.view.add(self.axis) # Create a turntable camera. For now, this is the only camerate type # we support, but if we support more in future, we should implement # that here # Remove the fov=60 here to solve the mismatch of selection problem # self.view.camera = scene.cameras.TurntableCamera(parent=self.view.scene, distance=2) self.view.camera = scene.cameras.TurntableCamera( parent=self.view.scene, distance=2.0) # Add the native canvas widget to this widget layout = QtGui.QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.addWidget(self.canvas.native) self.setLayout(layout) # We need to call render here otherwise we'll later encounter an OpenGL # program validation error. self.canvas.render() # Set up callbacks add_callback(self, 'visible_axes', nonpartial(self._toggle_axes))
def __init__(self): vertices, indices, _ = create_cube() vertices = VertexBuffer(vertices) self.indices = IndexBuffer(indices) self.program = Program(self.cube_vertex, self.cube_fragment) self.model = np.eye(4) self.view = np.eye(4) self.program.bind(vertices) self.program['texture'] = utils.checkerboard() self.program['texture'].interpolation = 'linear' self.program['model'] = self.model self.program['view'] = self.view
def test_mesh_color(): # Create visual vertices, filled_indices, outline_indices = create_cube() axis = scene.visuals.Mesh(vertices['position'], outline_indices, color='black', mode='lines') # Change color (regression test for a bug that caused this to reset # the vertex data to None) axis.color = (0.1, 0.3, 0.7, 0.9) new_vertices = axis.mesh_data.get_vertices() np.testing.assert_allclose(axis.color.rgba, (0.1, 0.3, 0.7, 0.9)) np.testing.assert_allclose(vertices['position'], new_vertices)
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, 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 __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 make_mesh(self): '''mesh() Generates a default mesh (a cube) treat it as though it is a property (i.e. not a function) ex: >>> x = Target((100, 100, 100)) >>> mesh = Target.mesh ''' cube = Program(cube_vertex, cube_fragment) cube.bind(vertices) self.program['model'] = model self.program['view'] = view self.program['projection'] = projection if self.mesh is None: vertices, indices, _ = create_cube() # self.mesh = else: return self.mesh
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 test_mesh_bounds(): # Create 3D visual vertices, filled_indices, outline_indices = create_cube() axis = scene.visuals.Mesh(vertices['position'], outline_indices, color='black', mode='lines') # Test bounds for all 3 axes for i in range(3): np.testing.assert_allclose(axis.bounds(i), (-1.0, 1.0)) # Create 2D visual using projection of cube axis = scene.visuals.Mesh(vertices['position'][:, :2], outline_indices, color='black', mode='lines') # Test bounds for first 2 axes for i in range(2): np.testing.assert_allclose(axis.bounds(i), (-1.0, 1.0)) # Test bounds for 3rd axis np.testing.assert_allclose(axis.bounds(2), (0.0, 0.0))
import sys from vispy import scene from vispy.geometry import create_cube from vispy.scene.visuals import Cube canvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True, bgcolor=(0.5, 0.5, 0.5)) view = canvas.central_widget.add_view() vertices, _, _ = create_cube() RGB_f = vertices['color'] RGB_f[..., 3] *= 0.5 cube1 = Cube(vertex_colors=RGB_f, parent=view.scene, size=0.5) axis = scene.visuals.XYZAxis(parent=view.scene) view.camera = 'turntable' view.camera.fov = 45 if __name__ == '__main__' and sys.flags.interactive == 0: canvas.app.run()
def test_cube(): """Test cube function""" vertices, filled, outline = create_cube() assert_array_equal(np.arange(len(vertices)), np.unique(filled)) assert_array_equal(np.arange(len(vertices)), np.unique(outline))