def __init__(self): app.Canvas.__init__(self, title='Hello OpenGL', keys='interactive') self.program = gloo.Program(vertex, fragment) self.vertices = gloo.VertexBuffer( np.array([[.5, .5], [.5, -.5], [-.5, -.5], [-.5, .5]]).astype(np.float32)) self.indices = gloo.IndexBuffer([0, 1, 3, 1, 2, 3]) self.texCoord = [(1, 1), (1, 0), (0, 0), (0, 1)] #flip vertically is not there in vispy yet, we can use np.flip self.texture1 = Texture2D( data=np.flip(io.imread('../Assets/container.jpg'), 0)) self.texture2 = Texture2D( data=np.flip(io.imread('../Assets/smiley.jpg'), 0)) self.program['a_position'] = self.vertices self.program['aTexCoord'] = self.texCoord self.program['texture1'] = self.texture1 self.program['texture2'] = self.texture2 self.timer = app.Timer('auto', self.on_timer, start=True) self.show()
def __init__(self): app.Canvas.__init__(self, title='Hello OpenGL', keys='interactive') self.startTime = time() self.program = gloo.Program(vertex, fragment) self.vertices = gloo.VertexBuffer(np.array([ [.5, .5], [.5, -.5], [-.5, -.5], [-.5, .5]]).astype(np.float32)) self.indices = gloo.IndexBuffer([0, 1, 3, 1, 2, 3]) self.texCoord = [(1, 1), (1, 0), (0, 0), (0, 1)] self.texture1 = Texture2D(data=io.imread('../Assets/container.jpg', flipVertically=True)) self.texture2 = Texture2D(data=io.imread('../Assets/smiley.jpg', flipVertically=True)) #trans stores matrix in glm format #_trans stores matrix in numpy format self.trans = None self._trans = None self.program['a_position'] = self.vertices self.program['aTexCoord'] = self.texCoord self.program['texture1'] = self.texture1 self.program['texture2'] = self.texture2 self.timer = app.Timer('auto', self.on_timer, start=True) self.show()
def __init__(self,size): app.Canvas.__init__(self, title='Hello OpenGL', keys='interactive', size=size) builtins.width, builtins.height = size self.startTime = time() self.program = gloo.Program(vertex, fragment) self.vertices = gloo.VertexBuffer(np.array([ [.5, .5], [.5, -.5], [-.5, -.5], [-.5, .5]]).astype(np.float32)) self.indices = gloo.IndexBuffer([0, 1, 3, 1, 2, 3]) self.texCoord = [(1, 1), (1, 0), (0, 0), (0, 1)] self.texture1 = Texture2D(data=io.imread('../Assets/container.jpg', flipVertically=True)) self.texture2 = Texture2D(data=io.imread('../Assets/smiley.jpg', flipVertically=True)) self.model = None self.view = None self.projection = None self.program['a_position'] = self.vertices self.program['aTexCoord'] = self.texCoord self.program['texture1'] = self.texture1 self.program['texture2'] = self.texture2 self.timer = app.Timer('auto', self.on_timer, start=True) self.show()
def __init__(self, size=(800, 480), title="Euclidean honeycombs and their duals", glsl_code="./glsl/euclidean_honeycombs.frag"): app.Canvas.__init__(self, keys="interactive", size=size, title=title) with open(glsl_code, "r") as f: code = f.read() self.program = gloo.Program(vertex, fragment % code) self.program["position"] = [(-1, -1), (-1, 1), (1, 1), (-1, -1), (1, 1), (1, -1)] self.program["iTime"] = 0. self.program["iResolution"] = (self.physical_size[0], self.physical_size[1], 0) self.program["iMouse"] = (0, 0, 0, 0) self.T = [1, 1, 1, 0] self.latticeType = 0 self.dual = False self.snub = False self.program["T"] = self.T self.program["latticeType"] = self.latticeType self.program["snub"] = self.snub self.program["dual"] = self.dual img = imread("./glsl/rusty_metal.jpg") tex = gloo.Texture2D(img) tex.interpolation = "linear" tex.wrapping = "repeat" self.program["iChannel0"] = tex self.timer = app.Timer('auto', connect=self.on_timer, start=False)
def _images_thumbnails(self): from vispy.io import imsave, imread from skimage.transform import resize import numpy as np gallery_dir = op.join(IMAGES_DIR, 'gallery') thumbs_dir = op.join(IMAGES_DIR, 'thumbs') carousel_dir = op.join(IMAGES_DIR, 'carousel') for fname in os.listdir(gallery_dir): filename1 = op.join(gallery_dir, fname) filename2 = op.join(thumbs_dir, fname) filename3 = op.join(carousel_dir, fname) # im = imread(filename1) newx = 200 newy = int(newx * im.shape[0] / im.shape[1]) im = (resize(im, (newy, newx), 2) * 255).astype(np.uint8) imsave(filename2, im) newy = 160 # This should match the carousel size! newx = int(newy * im.shape[1] / im.shape[0]) im = (resize(im, (newy, newx), 1) * 255).astype(np.uint8) imsave(filename3, im) print('Created thumbnail and carousel %s' % fname)
def _images_thumbnails(self): from vispy.io import imsave, imread # TODO: Switch to using PIL for resizing from skimage.transform import resize import numpy as np gallery_dir = op.join(IMAGES_DIR, 'gallery') thumbs_dir = op.join(IMAGES_DIR, 'thumbs') carousel_dir = op.join(IMAGES_DIR, 'carousel') for fname in os.listdir(gallery_dir): filename1 = op.join(gallery_dir, fname) filename2 = op.join(thumbs_dir, fname) filename3 = op.join(carousel_dir, fname) # im = imread(filename1) newx = 200 newy = int(newx * im.shape[0] / im.shape[1]) im = (resize(im, (newy, newx), 2) * 255).astype(np.uint8) imsave(filename2, im) newy = 160 # This should match the carousel size! newx = int(newy * im.shape[1] / im.shape[0]) im = (resize(im, (newy, newx), 1) * 255).astype(np.uint8) imsave(filename3, im) print('Created thumbnail and carousel %s' % fname)
def __init__(self): app.Canvas.__init__(self, title='Hello OpenGL', keys='interactive') # geometry shaders require full OpenGL namespace provided by PyOpenGL self.program = gloo.Program(vertex, fragment) self.vertices = gloo.VertexBuffer( np.array([[.5, .5], [.5, -.5], [-.5, -.5], [-.5, .5]]).astype(np.float32)) self.indices = gloo.IndexBuffer([0, 1, 3, 1, 2, 3]) # when tex coords are greater than (0, 1) then repeat, clamped to edge are used self.texCoord = [(1 * 4, 1 * 4), (1 * 4, 0), (0, 0), (0, 1 * 4)] # self.texture = Texture2D(data=io.imread('smiley.jpg'), # interpolation='linear', # wrapping='repeat') self.texture = Texture2D(data=io.imread('../Assets/smiley.jpg'), interpolation='nearest', wrapping='repeat') self.program['a_position'] = self.vertices self.program['aTexCoord'] = self.texCoord self.program['ourTexture'] = self.texture self.program['ourTexture'] = self.texture self.timer = app.Timer('auto', self.on_timer, start=True) self.show()
def on_timer(self, event): global mReadImages, mReadVideo, cap global frameArr, mFrames if self.texture: #Change on each frame 3-8-2019 if mReadImages: print("self.texture?, frame", self.frame) path = mRootPath + str(self.frame % 8) + ".png" print(path) #im = io.read_png(path) #mRootPath + str(self.frame%8)+".png") im = io.imread(path) #mRootPath + str(self.frame%8)+".png") #imread requires PIL or imageio #directly write video frames from opencv numpy arrays? self.program['u_texture1'] = gloo.Texture2D(im) if mReadVideo: #read video try: if (self.cap): #cv2.waitKey(35) print("mReadVideo?") #cap.set(cv2.CAP_PROP_POS_FRAMES, 0) #ret, frame = self.cap.read(0) #ret, frame = self.cap.read(self.frame) ret, frame = cap.read() #self.frame) #global if ret: cv2.imshow("cap", frame) if ret: self.program['u_texture1'] = gloo.Texture2D(frame) else: print("ret=?, frame=?", ret, frame) self.program['u_texture1'] = gloo.Texture2D( frameArr[self.frame % mFrames]) except (e): print(e) #print("slef.save=", self.save) #sleep(5) if self.save: #True: #self.save==1: print("IN slef.save=", self.save) i = self.render() #without OK? io.write_png(str(self.frame) + ".png", i) #no such: io.write_jpg(str(self.frame)+".jpg", i); #self.mSlowDown = 1; #if not self.mSlowDown: self.mSlowDown = 1 self.program['iTime'] = event.elapsed * self.mSlowDown self.program['iGlobalTime'] = event.elapsed self.program['iDate'] = get_idate() # used in some shadertoy exs #self.update() self.frame += 1 #24-11-2018 print(self.frame) self.frameTime.append((self.frame, event.elapsed)) if (self.frame % frame_divider == 0): ln = len(self.frameTime) fr1, t1 = self.frameTime[ln - 1] fr2, t2 = self.frameTime[ln - 2] fps = (fr1 - fr2) / (t1 - t2) #print({:04.2f} fps, end=", "); print(" %0.2f" % fps, end=", ") sys.stdout.flush() self.update()
def test_read_write_image(): """Test reading and writing of images""" fname = op.join(temp_dir, 'out.png') im1 = load_crate() imsave(fname, im1, format='png') with warnings.catch_warnings(record=True): # PIL unclosed file im2 = imread(fname) assert_allclose(im1, im2)
def _setup_textures(self, fname): data = imread(load_data_file('jfa/' + fname))[::-1].copy() self.texture_size = data.shape self.orig_tex = Texture2D(data, format='luminance', wrapping='repeat', interpolation='nearest') self.comp_texs = [] data = np.zeros(self.texture_size + (4,), np.float32) for _ in range(2): tex = Texture2D(data, format='rgba', wrapping='clamp_to_edge', interpolation='nearest') self.comp_texs.append(tex) self.fbo_to[0].color_buffer = self.comp_texs[0] self.fbo_to[1].color_buffer = self.comp_texs[1] for program in self.programs: program['texw'], program['texh'] = self.texture_size
def set_image(img_path): # Create the image img_data = io.imread(img_path) # Set up a viewbox to display the image with interactive pan/zoom img_height = len(img_data) img_width = len(img_data[0]) center_pos_x = (CANVAS_SIZE_X - img_width) / 2 center_pos_y = (CANVAS_SIZE_Y - img_height) / 2 # img_data = np.random.normal(size=(2, 3, 3), loc=128, # scale=50).astype(np.ubyte) # print(img_data) return img_data, center_pos_x, center_pos_y
def _images_thumbnails(self): from vispy.io import imsave, imread from skimage.transform import resize import numpy as np gallery_dir = os.path.join(IMAGES_DIR, 'gallery') thumbs_dir = os.path.join(IMAGES_DIR, 'thumbs') for fname in os.listdir(gallery_dir): filename1 = os.path.join(gallery_dir, fname) filename2 = os.path.join(thumbs_dir, fname) # im = imread(filename1) newx = 200 newy = int( newx * im.shape[0] / im.shape[1]) im = (resize(im, (newy, newx), 2)*255).astype(np.uint8) imsave(filename2, im) print('Created thumbnail %s' % fname)
def __init__(self): app.Canvas.__init__(self, title='Hello OpenGL', keys='interactive') # geometry shaders require full OpenGL namespace provided by PyOpenGL self.program = gloo.Program(vertex, fragment) self.vertices = gloo.VertexBuffer( np.array([[-0.5, -0.5], [0.5, -0.5], [0.0, 0.5]]).astype(np.float32)) self.indices = gloo.IndexBuffer([0, 1, 2]) self.texCoord = [(.0, .0), (1.0, 0.0), (0.5, 1.0)] self.program['a_position'] = self.vertices self.program['aTexCoord'] = self.texCoord self.program['ourTexture'] = io.imread('../Assets/wall.jpg') self.timer = app.Timer('auto', self.on_timer, start=True) self.show()
def init(self, objects, camera, lights, options): self._objects = objects self._positions = gloo.VertexBuffer(self._concatPositions()) self._indices = gloo.IndexBuffer(numpy.array(self._concatIndices())) self._normals = gloo.VertexBuffer(self._concatNormals()) self._camera = camera self._lights = lights self._options = options if not self._options: self._options = {"anti-aliasing-int" : "4", "anti-aliasing-float" : "4.0", "spreading" : "700.0", "bias" : "0.05"} vertex_str, fragment_str = self._loadShaders() vertex_texture, fragment_texture = self._loadShaders(texture=True) self._projection = self._createProjectionMatrix() self._programs = [] for obj in self._objects: if obj.getTexture(): newProg = gloo.Program(vertex_texture, fragment_texture) newProg['u_texture'] = gloo.Texture2D(imread(obj.getTexture())) newProg['texcoord'] = obj.getTexBuffer() else: newProg = gloo.Program(vertex_str, fragment_str) if obj.getColor(): newProg['u_color'] = obj.getColorAlpha() else: newProg['u_color'] = DEFAULT_COLOR newProg['position'] = obj.getVertexBuffer() newProg['u_projection'] = self._projection self._programs.append(newProg) self._lightObjects = self._createLightObjects() self._lightPrograms = self._createLightPrograms() self.active = True self._fps = 1
import numpy as np from vispy import app, io import threading import time import random import vispy import rendering import keyboard im1 = np.flipud(np.fliplr(io.imread('images/Red_Apple.jpg'))) im2 = np.flipud(np.fliplr(io.imread('images/Blue_Apple.jpg'))) FPS = 10 renderer = rendering.getRenderer() # init the renderer # create two modules (using a test image) d1 = rendering.Drawable(verticies=[[-1, -1], [0, -1], [-1, 0], [0, 0]], tex_data=im1) # d2/d3 alternate between apple textures d2 = rendering.Drawable([[-.2, -.2], [1, -.2], [-.2, 1], [1, 1]], tex_data=im1) d3 = rendering.Drawable([[-.2, -.2], [1, -.2], [-.2, 1], [1, 1]], tex_data=im2, start=False) # keyboard listner, prints keypresses/releases def key_stuff(event, direction): if (direction == 'down' and not event.text == ''): print event.text if (event.key == vispy.keys.ESCAPE): exit()
from vispy import scene from vispy import app from vispy import io import numpy as np canvas = scene.SceneCanvas(keys='interactive') canvas.size = 600, 600 canvas.show() # Set up a viewbox to display the image with interactive pan/zoom view = canvas.central_widget.add_view() view.camera.invert_y = False # Load lena img_data = io.imread('lena.png') image = scene.visuals.Image(img_data, parent=view.scene) # Set the view bounds to show the entire image with some padding view.camera.rect = (-10, -10, image.size[0]+20, image.size[1]+20) import sys if __name__ == '__main__' and sys.flags.interactive == 0: app.run()
def __init__(self, size): app.Canvas.__init__(self, title='Hello OpenGL', keys='interactive', size=size) # vispy wrapper of glfw dont have the wrapper of this function yet, I am opening a PR for this # by the time we can use this to capture and hide the mouse self._app.native.glfwSetInputMode(self.native._id, self._app.native.GLFW_CURSOR, self._app.native.GLFW_CURSOR_DISABLED) builtins.width, builtins.height = size self.startTime = time() self.first_mouse = True self.cubePositions = [ glm.vec3( 0.0, 0.0, 0.0), glm.vec3( 2.0, 5.0, -15.0), glm.vec3(-1.5, -2.2, -2.5), glm.vec3(-3.8, -2.0, -12.3), glm.vec3( 2.4, -0.4, -3.5), glm.vec3(-1.7, 3.0, -7.5), glm.vec3( 1.3, -2.0, -2.5), glm.vec3( 1.5, 2.0, -2.5), glm.vec3( 1.5, 0.2, -1.5), glm.vec3(-1.3, 1.0, -1.5)] self.program = gloo.Program(vertex, fragment) self.vertices = np.array([[-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [0.5, 0.5, -0.5], [0.5, 0.5, -0.5], [-0.5, 0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [0.5, -0.5, 0.5], [0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, 0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [0.5, 0.5, -0.5], [0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, -0.5, 0.5], [-0.5, -0.5, 0.5], [-0.5, -0.5, -0.5], [-0.5, 0.5, -0.5], [0.5, 0.5, -0.5], [0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, 0.5, -0.5] ]).astype(np.float32) self.texCoord = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 0.0], [0.0, 1.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 0.0], [0.0, 1.0] ]).astype(np.float32) self.texture1 = Texture2D(data=io.imread('../Assets/container.jpg', flipVertically=True)) self.texture2 = Texture2D(data=io.imread('../Assets/smiley.jpg', flipVertically=True)) self.model = None self.projection = None self.view = None self.direction = None self.cameraPos = glm.vec3(0, 0, 3) self.cameraFront = glm.vec3(0, 0, -1) self.cameraUp = glm.vec3(0, 1, 0) self.cameraSpeed = 5 #to get key pressed behaviour self.bool_w = False self.bool_a = False self.bool_s = False self.bool_d = False #delta time self.delta_time = 0 self.last_frame = 0 #mouse variables self.last_x = None self.last_y = None self.yaw = -90 self.pitch = 0 self.fov = 45 self.program['a_position'] = self.vertices self.program['aTexCoord'] = self.texCoord self.program['texture1'] = self.texture1 self.program['texture2'] = self.texture2 self.timer = app.Timer('auto', self.on_timer, start=True) gloo.set_state(depth_test=True) self.show()
from vispy.io import imread, load_data_file, read_mesh from vispy.scene.visuals import Mesh from vispy.scene import transforms from vispy.visuals.filters import TextureFilter parser = argparse.ArgumentParser() parser.add_argument('--shading', default='smooth', choices=['none', 'flat', 'smooth'], help="shading mode") args, _ = parser.parse_known_args() mesh_path = load_data_file('spot/spot.obj.gz') texture_path = load_data_file('spot/spot.png') vertices, faces, normals, texcoords = read_mesh(mesh_path) texture = np.flipud(imread(texture_path)) canvas = scene.SceneCanvas(keys='interactive', bgcolor='white', size=(800, 600)) view = canvas.central_widget.add_view() view.camera = 'arcball' # Adapt the depth to the scale of the mesh to avoid rendering artefacts. view.camera.depth_value = 10 * (vertices.max() - vertices.min()) shading = None if args.shading == 'none' else args.shading mesh = Mesh(vertices, faces, shading=shading, color='white') mesh.transform = transforms.MatrixTransform() mesh.transform.rotate(90, (1, 0, 0)) mesh.transform.rotate(135, (0, 0, 1))
def __init__(self,size): app.Canvas.__init__(self, title='Hello OpenGL', keys='interactive', size=size) builtins.width, builtins.height = size self.startTime = time() self.cubePositions = [ glm.vec3( 0.0, 0.0, 0.0), glm.vec3( 2.0, 5.0, -15.0), glm.vec3(-1.5, -2.2, -2.5), glm.vec3(-3.8, -2.0, -12.3), glm.vec3( 2.4, -0.4, -3.5), glm.vec3(-1.7, 3.0, -7.5), glm.vec3( 1.3, -2.0, -2.5), glm.vec3( 1.5, 2.0, -2.5), glm.vec3( 1.5, 0.2, -1.5), glm.vec3(-1.3, 1.0, -1.5)] self.program = gloo.Program(vertex, fragment) self.vertices = np.array([[-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [0.5, 0.5, -0.5], [0.5, 0.5, -0.5], [-0.5, 0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [0.5, -0.5, 0.5], [0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, 0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [0.5, 0.5, -0.5], [0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, -0.5, 0.5], [-0.5, -0.5, 0.5], [-0.5, -0.5, -0.5], [-0.5, 0.5, -0.5], [0.5, 0.5, -0.5], [0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, 0.5, -0.5] ]).astype(np.float32) self.texCoord = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 0.0], [0.0, 1.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 0.0], [0.0, 1.0] ]).astype(np.float32) self.texture1 = Texture2D(data=io.imread('../Assets/container.jpg', flipVertically=True)) self.texture2 = Texture2D(data=io.imread('../Assets/smiley.jpg', flipVertically=True)) self.model = None self.projection = None self.view = glm.lookAt(glm.vec3(0, 0, 3), glm.vec3(0, 0, 0), glm.vec3(0, 1, 0)) self.program['a_position'] = self.vertices self.program['aTexCoord'] = self.texCoord self.program['texture1'] = self.texture1 self.program['texture2'] = self.texture2 self.timer = app.Timer('auto', self.on_timer, start=True) gloo.set_state(depth_test=True) self.show()
@jit def fractal(xmin, xmax, ymin, ymax, width, height, maxiter, image): r1 = np.linspace(xmin, xmax, width) r2 = np.linspace(ymin, ymax, height) n3 = np.empty((width, height)) cs = np.empty((width, height)) for i in range(width): for j in range(height): n3[i, j], cs[i, j] = fractal_pixel( r1[i] + 1j * r2[j], maxiter, image[i, j], image) return (r1, r2, n3, cs) img_data = imread('glitch2.png') img_data = np.array(img_data) img = fractal_image(-20.0, 20, -20, 20, maxiter=320, cmap='gnuplot2', image=img_data) canvas = scene.SceneCanvas(keys='interactive', show=True) viewbox = canvas.central_widget.add_view() interpolation = 'nearest' image = scene.visuals.Image(img, cmap='cubehelix', interpolation=interpolation, parent=viewbox.scene, method='subdivide') viewbox.camera = scene.PanZoomCamera(aspect=1) viewbox.camera.flip = (0, 1, 0) viewbox.camera.set_range()
import numpy as np from vispy.io import imread img_data = imread('001.jpg') width = len(img_data) height = len(img_data[0]) line_number = 1000 img_data = np.array(img_data) print(width) print(height) for i in range(line_number): start = np.random.randint(0, width), np.random.randint(0, height) stop = np.random.randint(0, width), np.random.randint(0, height) start, stop = tuple(np.sort([start, stop], 0)) line = zip(np.linspace(start[0], stop[0], np.max(stop - start), endpoint=False), np.linspace(start[1], stop[1], np.max(stop - start), endpoint=False)) # round coordinates line = np.round(list(line)) # remove duplicates temp_line = [tuple(coord) for coord in line] line = [] seen = set() for coord in temp_line: if coord not in seen: seen.add(coord) line.append((int(coord[0]), int(coord[1]))) pixels = np.array([img_data[coord[0]][coord[1]] for coord in line]) # np.random.shuffle(pixels)
def __init__(self, size): app.Canvas.__init__(self, title='Hello OpenGL', keys='interactive', size=size) # vispy wrapper of glfw dont have the wrapper of this function yet, I am opening a PR for this # by the time we can use this self._app.native.glfwSetInputMode( self.native._id, self._app.native.GLFW_CURSOR, self._app.native.GLFW_CURSOR_DISABLED) builtins.width, builtins.height = size # camera instance self.camera = Camera(position=glm.vec3(0, 0, 3), sensitivity=0.2) self.startTime = time() self.first_mouse = True self.lightPos = [0, 0, 0] self.lightColor = glm.vec3(1) self.cubePositions = [ glm.vec3(1.0, -3.0, 0.0), glm.vec3(2.0, 5.0, -15.0), glm.vec3(-1.5, -2.2, -2.5), glm.vec3(-3.8, -2.0, -12.3), glm.vec3(2.4, -0.4, -3.5), glm.vec3(-1.7, 3.0, -7.5), glm.vec3(1.3, -2.0, -2.5), glm.vec3(1.5, 2.0, -2.5), glm.vec3(1.5, 0.2, -1.5), glm.vec3(-1.3, 1.0, -1.5) ] self.program = gloo.Program(vertex, fragment) self.programLightSource = gloo.Program(lightSourceVertex, lightSourceFragment) self.vertices = np.array([[-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [0.5, 0.5, -0.5], [0.5, 0.5, -0.5], [-0.5, 0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [0.5, -0.5, 0.5], [0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, 0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [0.5, 0.5, -0.5], [0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, -0.5, 0.5], [-0.5, -0.5, 0.5], [-0.5, -0.5, -0.5], [-0.5, 0.5, -0.5], [0.5, 0.5, -0.5], [0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, 0.5, -0.5]]).astype(np.float32) self.aNormal = np.array([ [0, 0, -1], [0, 0, -1], [0, 0, -1], [0, 0, -1], [0, 0, -1], [0, 0, -1], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1], [-1, 0, 0], [-1, 0, 0], [-1, 0, 0], [-1, 0, 0], [-1, 0, 0], [-1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, -1, 0], [0, -1, 0], [0, -1, 0], [0, -1, 0], [0, -1, 0], [0, -1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], ]).astype(np.float32) self.texCoord = np.array( [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 0.0], [0.0, 1.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 0.0], [0.0, 1.0]]).astype(np.float32) self.diffuse_map = gloo.Texture2D( data=np.flip(io.imread("../Assets/container2.png"), 0)) self.specular_map = gloo.Texture2D( data=np.flip(io.imread('../Assets/container2_specular.png'), 0)) # Exercises self.emission_map = gloo.Texture2D( data=np.flip(io.imread('../Assets/matrix.jpg'), 0)) self.model = None self.projection = None self.view = None # delta time self.delta_time = 0 self.last_frame = 0 # mouse variables self.last_x = None self.last_y = None self.timer = app.Timer('auto', self.on_timer, start=True) gloo.set_state(depth_test=True) self.show()
import numpy as np from vispy import app, io import threading import time import random import vispy import rendering import keyboard im1 = np.flipud(np.fliplr(io.imread('images/Red_Apple.jpg'))) im2 = np.flipud(np.fliplr(io.imread('images/Blue_Apple.jpg'))) FPS = 10 renderer = rendering.getRenderer() # init the renderer # create two modules (using a test image) d1 = rendering.Drawable(verticies=[[-1,-1],[0,-1],[-1,0],[0,0]], tex_data=im1) # d2/d3 alternate between apple textures d2 = rendering.Drawable([[-.2,-.2],[1,-.2],[-.2,1],[1,1]], tex_data=im1) d3 = rendering.Drawable([[-.2,-.2],[1,-.2],[-.2,1],[1,1]], tex_data=im2, start=False) # keyboard listner, prints keypresses/releases def key_stuff(event, direction): if(direction == 'down' and not event.text == ''): print event.text if(event.key == vispy.keys.ESCAPE): exit() # create keyboard listener and add a callback key = keyboard.KeyboardListener(renderer) key.add_callback(key_stuff)