def test_reset_data_type(self): data = np.zeros((10, 10), dtype=np.uint8) T = Texture2D(data=data) data = np.zeros((10, 11), dtype=np.float32) T.set_data(data) data = np.zeros((12, 10), dtype=np.int32) T.set_data(data) self.assertRaises(ValueError, T.set_data, np.zeros([10, 10, 10, 1]))
def test_set_misshaped_data_2D(self): data = np.zeros((10, 10), dtype=np.uint8) T = Texture2D(data=data) # with self.assertRaises(ValueError): # T.set_data(np.ones((10, 10))) self.assertRaises(ValueError, T.set_data, np.ones((10, ))) self.assertRaises( ValueError, T.set_data, np.ones((5, 5, 5, 1)), )
def __init__(self, svbrdf): super().__init__(_load_shader('default.vert.glsl'), _load_shader('svbrdf.frag.glsl'), has_texture=True) self.alpha = svbrdf.alpha self.diff_map = Texture2D(svbrdf.diffuse_map, interpolation='linear', wrapping='repeat', internalformat='rgb32f') self.spec_map = Texture2D(svbrdf.specular_map, interpolation='linear', wrapping='repeat', internalformat='rgb32f') self.spec_shape_map = Texture2D(svbrdf.spec_shape_map, wrapping='repeat', internalformat='rgb32f') self.normal_map = Texture2D(svbrdf.normal_map, interpolation='linear', wrapping='repeat', internalformat='rgb32f')
def reset_view(): """Reset the view of the renderer.""" global viewport global texture_viewport global transform_matrix global modelview_matrix global projection_matrix global fbuffer_tex_front global fbuffer_tex_back viewport = ( 0, 0, int(builtins.width * builtins.pixel_x_density), int(builtins.height * builtins.pixel_y_density), ) texture_viewport = ( 0, 0, builtins.width, builtins.height, ) gloo.set_viewport(*viewport) cz = (builtins.height / 2) / math.tan(math.radians(30)) projection_matrix = matrix.perspective_matrix( math.radians(60), builtins.width / builtins.height, 0.1 * cz, 10 * cz) modelview_matrix = matrix.translation_matrix(-builtins.width / 2, \ builtins.height / 2, \ -cz) modelview_matrix = modelview_matrix.dot(matrix.scale_transform(1, -1, 1)) transform_matrix = np.identity(4) default_prog['modelview'] = modelview_matrix.T.flatten() default_prog['projection'] = projection_matrix.T.flatten() fbuffer_tex_front = Texture2D((builtins.height, builtins.width, 3)) fbuffer_tex_back = Texture2D((builtins.height, builtins.width, 3))
def _setup_textures(self, fname): data = imread(load_data_file('jfa/' + fname))[::-1].copy() if data.ndim == 3: data = data[:, :, 0] # Travis gets 2, I get three? self.texture_size = data.shape[:2] 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[1:2]: program['texw'], program['texh'] = self.texture_size
def test_use_framebuffer(): """Test drawing to a framebuffer""" shape = (100, 300) # for some reason Windows wants a tall window... data = np.random.rand(*shape).astype(np.float32) use_shape = shape + (3, ) with Canvas(size=shape[::-1]) as c: c.app.process_events() c.set_current() if c.app.backend_name.lower() == 'pyqt5': # PyQt5 on OSX for some reason sets this to 1024x768... c.size = shape[::-1] c.app.process_events() orig_tex = Texture2D(data) fbo_tex = Texture2D(use_shape, format='rgb') rbo = RenderBuffer(shape, 'color') fbo = FrameBuffer(color=fbo_tex) c.context.glir.set_verbose(True) assert c.size == shape[::-1] c.set_current() set_viewport((0, 0) + c.size) with fbo: draw_texture(orig_tex) draw_texture(fbo_tex) out_tex = _screenshot()[::-1, :, 0].astype(np.float32) assert out_tex.shape == c.size[::-1] assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.) assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.) fbo.color_buffer = rbo fbo.depth_buffer = RenderBuffer(shape) fbo.stencil_buffer = None print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer)) clear(color='black') with fbo: clear(color='black') draw_texture(orig_tex) out_rbo = _screenshot()[:, :, 0].astype(np.float32) assert_allclose(data * 255., out_tex, atol=1) assert_allclose(data * 255., out_rbo, atol=1)
def reset_view(self): self.viewport = ( 0, 0, int(builtins.width * builtins.pixel_x_density), int(builtins.height * builtins.pixel_y_density), ) self.texture_viewport = ( 0, 0, builtins.width, builtins.height, ) gloo.set_viewport(*self.viewport) cz = (builtins.height / 2) / math.tan(math.radians(30)) self.projection_matrix = matrix.perspective_matrix( math.radians(60), builtins.width / builtins.height, 0.1 * cz, 10 * cz) self.transform_matrix = np.identity(4) self.default_prog['projection'] = self.projection_matrix.T.flatten() self.default_prog['perspective_matrix'] = self.lookat_matrix.T.flatten( ) self.fbuffer_tex_front = Texture2D( (builtins.height, builtins.width, 3)) self.fbuffer_tex_back = Texture2D((builtins.height, builtins.width, 3)) for buf in [self.fbuffer_tex_front, self.fbuffer_tex_back]: self.fbuffer.color_buffer = buf with self.fbuffer: self.clear() self.fbuffer.depth_buffer = gloo.RenderBuffer( (builtins.height, builtins.width))
def _bake_channel_positions(self): # WARNING: channel_positions must be in [0,1] because we have a # texture. positions = self.channel_positions.astype(np.float32) positions = _normalize(positions, keep_ratio=True) positions = positions.reshape((1, self.n_channels, -1)) # Rescale a bit and recenter. positions = .1 + .8 * positions u_channel_pos = np.dstack((positions, np.zeros( (1, self.n_channels, 1)))) u_channel_pos = (u_channel_pos * 255).astype(np.uint8) # TODO: more efficient to update the data from an existing texture self.program['u_channel_pos'] = Texture2D(u_channel_pos, wrapping='clamp_to_edge')
def on_initialize(self, event): # Build cube data # -------------------------------------- texcoord = [(0, 0), (0, 1), (1, 0), (1, 1)] vertices = [(-2, -1, 0), (-2, +1, 0), (+2, -1, 0), (+2, +1, 0)] vertices = VertexBuffer(vertices) camera_pitch = 0.0 # Degrees self.rotate = [camera_pitch, 0, 0] self.translate = [0, 0, -3] # Build program # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) scale(model, 1, 1, 1) self.phi = 0 self.cube = Program(cube_vertex, cube_fragment) self.cube['position'] = vertices # 4640 x 2256 imtex = cv2.imread(os.path.join(img_path, 'stage.jpg')) self.cube['texcoord'] = texcoord self.cube["texture"] = np.uint8( np.clip(imtex + np.random.randint(-60, 20, size=imtex.shape), 0, 255)) + 5 self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view color = Texture2D((640, 640, 3), interpolation='linear') self.framebuffer = FrameBuffer(color, RenderBuffer((640, 640))) self.quad = Program(quad_vertex, quad_fragment) 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 self.objects = [self.cube] # 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.pub_timer = app.Timer(0.1, connect=self.send_ros_img, start=True) self._set_projection(self.size)
def reset_view(self): self.viewport = ( 0, 0, int(builtins.width * builtins.pixel_x_density), int(builtins.height * builtins.pixel_y_density), ) self.texture_viewport = ( 0, 0, builtins.width, builtins.height, ) gloo.set_viewport(*self.viewport) # pylint: disable=no-member cz = (builtins.height / 2) / math.tan(math.radians(30)) self.projection_matrix = matrix.perspective_matrix( math.radians(60), builtins.width / builtins.height, 0.1 * cz, 10 * cz ) self.transform_matrix = np.identity(4) self._update_shader_transforms() self.fbuffer_tex_front = Texture2D( (builtins.height, builtins.width, 3)) self.fbuffer_tex_back = Texture2D((builtins.height, builtins.width, 3)) self.fbuffer.depth_buffer = gloo.RenderBuffer( (builtins.height, builtins.width)) for buf in [self.fbuffer_tex_front, self.fbuffer_tex_back]: self.fbuffer.color_buffer = buf with self.fbuffer: self.clear()
def updateMesh(self, vertices=None, faces=None, colors=None, texture=None, textureCoordinates=None): # Set if vertices is not None: self._meshData.set_vertices(verts=vertices) if faces is not None: self._meshData.set_faces(faces) if colors is not None: self._meshData.set_vertex_colors(colors) # Update if vertices is not None or faces is not None: self._vertices = VertexBuffer( self._meshData.get_vertices(indexed='faces').astype( np.float32)) if faces is not None and vertices is not None: self._normals = VertexBuffer( self._meshData.get_vertex_normals(indexed='faces').astype( np.float32)) if colors is not None: self._colors = VertexBuffer( self._meshData.get_vertex_colors(indexed='faces').astype( np.float32)) if texture is not None: self._texture = Texture2D(np.array(texture, dtype=np.float32), interpolation='linear', wrapping='mirrored_repeat') if textureCoordinates is not None: textureMesh = MeshData(vertices=textureCoordinates, faces=self._meshData.get_faces()) self._textureCoordinates = VertexBuffer( textureMesh.get_vertices(indexed='faces').astype(np.float32)) # Update GPU - geometry self.shared_program.vert['position'] = self._vertices self.shared_program.vert['normal'] = self._normals # Update GPU - color if self._texture is None: self.shared_program.vert['color'] = self._colors else: self.shared_program.vert[ 'textureCoordinates'] = self._textureCoordinates self.shared_program['u_objectTexture'] = self._texture
def __init__(self): app.Canvas.__init__(self) self.position = 50, 50 self.size = 512, 512 self.program = Program(VS, FS) self.tex = Texture2D(data1) self.program['a_position'] = VertexBuffer(positions) self.program['a_texcoord'] = VertexBuffer(texcoords) self.program['u_tex'] = self.tex self._timer = app.Timer(.01) self._timer.connect(self.on_timer) self._timer.start() self.measure_fps(.05, callback=self.fps_callback)
def test_reset_data_shape(self): shape1 = 10, 10 shape3 = 10, 10, 3 # Init data (explicit shape) data = np.zeros((10, 10, 1), dtype=np.uint8) T = Texture2D(data=data) assert T.shape == (10, 10, 1) assert T.format == 'luminance' # Set data to rgb T.set_data(np.zeros(shape3, np.uint8)) assert T.shape == (10, 10, 3) assert T.format == 'rgb' # Set data to grayscale T.set_data(np.zeros(shape1, np.uint8)) assert T.shape == (10, 10, 1) assert T.format == 'luminance' # Set size to rgb T.resize(shape3) assert T.shape == (10, 10, 3) assert T._format == 'rgb' # Set size to grayscale T.resize(shape1) assert T.shape == (10, 10, 1) assert T._format == 'luminance' # Keep using old format T.resize(shape1, 'alpha') T.resize(shape1) assert T._format == 'alpha' # Use luminance as default T.resize(shape3) T.resize(shape1) assert T._format == 'luminance' # Too large self.assertRaises(ValueError, T.resize, (5, 5, 5, 1)) # Cannot determine format self.assertRaises(ValueError, T.resize, (5, 5, 5)) # Invalid format self.assertRaises(ValueError, T.resize, shape3, 'foo') self.assertRaises(ValueError, T.resize, shape3, 'alpha')
def __init__(self): app.Canvas.__init__(self, title='Framebuffer post-processing', keys='interactive', size=(512, 512)) # Build cube data # -------------------------------------- vertices, indices, _ = create_cube() vertices = VertexBuffer(vertices) self.indices = IndexBuffer(indices) # Build program # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) translate(view, 0, 0, -7) self.phi, self.theta = 60, 20 rotate(model, self.theta, 0, 0, 1) rotate(model, self.phi, 0, 1, 0) self.cube = Program(cube_vertex, cube_fragment) self.cube.bind(vertices) self.cube["texture"] = checkerboard() self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view color = Texture2D((512, 512, 3), interpolation='linear') self.framebuffer = FrameBuffer(color, RenderBuffer((512, 512))) self.quad = Program(quad_vertex, quad_fragment, count=4) self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.quad['texture'] = color # OpenGL and Timer initalization # -------------------------------------- set_state(clear_color=(.3, .3, .35, 1), depth_test=True) self.timer = app.Timer('auto', connect=self.on_timer, start=True) self._set_projection(self.size)
def init_uniforms(self): self.uniforms['u_alpha'] = self.alpha self.uniforms['u_diff_map'] = Texture2D( self.diff_map, interpolation=('linear_mipmap_linear', 'linear'), wrapping='repeat', mipmap_levels=10, internalformat='rgb32f') self.uniforms['u_spec_map'] = Texture2D( self.spec_map, interpolation=('linear_mipmap_linear', 'linear'), wrapping='repeat', mipmap_levels=10, internalformat='rgb32f') # TODO: Mipmapping here causes artifacts, but not doing it makes the # opject too specular. How can we fix this? self.uniforms['u_spec_shape_map'] = Texture2D( self.spec_shape_map, interpolation=('linear_mipmap_linear', 'linear'), wrapping='repeat', mipmap_levels=10, internalformat='rgb32f') self.uniforms['u_normal_map'] = Texture2D( self.normal_map, interpolation=('linear_mipmap_linear', 'linear'), wrapping='repeat', mipmap_levels=10, internalformat='rgb32f') self.uniforms['u_cdf_sampler'] = Texture2D(self.cdf_sampler.astype( np.float32), interpolation='linear', wrapping='clamp_to_edge', internalformat='r32f') self.uniforms['u_pdf_sampler'] = Texture2D(self.pdf_sampler.astype( np.float32), interpolation='linear', wrapping='clamp_to_edge', internalformat='r32f') self.uniforms['u_sigma_range'] = (self.sigma_min, self.sigma_max)
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()
def __init__( self, data=None, method='auto', grid=(1, 1), cmap='viridis', clim='auto', gamma=1.0, interpolation='nearest', **kwargs, ): self._data = None self._gamma = gamma # load 'float packed rgba8' interpolation kernel # to load float interpolation kernel use # `load_spatial_filters(packed=False)` kernel, self._interpolation_names = load_spatial_filters() self._kerneltex = Texture2D(kernel, interpolation='nearest') # The unpacking can be debugged by changing "spatial-filters.frag" # to have the "unpack" function just return the .r component. That # combined with using the below as the _kerneltex allows debugging # of the pipeline # self._kerneltex = Texture2D(kernel, interpolation='linear', # internalformat='r32f') # create interpolation shader functions for available # interpolations fun = [ Function(_interpolation_template % n) for n in self._interpolation_names ] self._interpolation_names = [ n.lower() for n in self._interpolation_names ] self._interpolation_fun = dict(zip(self._interpolation_names, fun)) self._interpolation_names.sort() self._interpolation_names = tuple(self._interpolation_names) # overwrite "nearest" and "bilinear" spatial-filters # with "hardware" interpolation _data_lookup_fn self._interpolation_fun['nearest'] = Function(_texture_lookup) self._interpolation_fun['bilinear'] = Function(_texture_lookup) if interpolation not in self._interpolation_names: raise ValueError("interpolation must be one of %s" % ', '.join(self._interpolation_names)) self._interpolation = interpolation # check texture interpolation if self._interpolation == 'bilinear': texture_interpolation = 'linear' else: texture_interpolation = 'nearest' self._method = method self._grid = grid self._texture_limits = None self._need_texture_upload = True self._need_vertex_update = True self._need_colortransform_update = True self._need_interpolation_update = True self._texture = Texture2D(np.zeros((1, 1, 4)), interpolation=texture_interpolation) self._subdiv_position = VertexBuffer() self._subdiv_texcoord = VertexBuffer() # impostor quad covers entire viewport vertices = np.array( [[-1, -1], [1, -1], [1, 1], [-1, -1], [1, 1], [-1, 1]], dtype=np.float32, ) self._impostor_coords = VertexBuffer(vertices) self._null_tr = NullTransform() self._init_view(self) super(ImageVisual, self).__init__(vcode=VERT_SHADER, fcode=FRAG_SHADER) self.set_gl_state('translucent', cull_face=False) self._draw_mode = 'triangles' # define _data_lookup_fn as None, will be setup in # self._build_interpolation() self._data_lookup_fn = None self.clim = clim self.cmap = cmap if data is not None: self.set_data(data) self.freeze()
def texture(self, texture): self._texture = texture self.fshader['u_texture'] = Texture2D(texture)
def __init__(self, text, canvas, position=1, color=(0.1, 0.0, 0.7)): ''' Give this the - text to be written - main app.canvas - position (1-9, or which button position this should occupy) ''' # State Controller State.register_button(position, text) self.position = position self.canvas = canvas self.projection = np.eye(4) self.view = np.eye(4) self.model = np.eye(4) height, width = 5.0, 15.0 # Meters orientation_vector = (1, 1, 0) unit_orientation_angle = np.array(orientation_vector) / np.linalg.norm( orientation_vector) scale_factor = 0.2 lowest_button = -5.2 midset = 0.2 scale(self.model, scale_factor) yrotate(self.model, -60) # rotate(self.model, 30, *unit_orientation_angle) offset = (position * ((height + midset) * scale_factor)) translate(self.model, -7.4, lowest_button + offset, -10) pixel_to_length = 10 self.size = map(lambda o: pixel_to_length * o, [width, height]) # 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, 0], [1, 0], [1, 1], [0, 1], ], dtype=np.float32) self.indices = IndexBuffer([ 0, 1, 2, 2, 3, 0, ]) self.program = Program(self.button_vertex_shader, self.button_fragment_shader) self.program['vertex_position'] = self.vertices self.program['default_texcoord'] = self.tex_coords self.program['view'] = self.view self.program['model'] = self.model self.program['projection'] = self.projection self.program['background_color'] = color self.program['highlighted'] = 0 # self.texture = Texture2D(shape=(1000, 1000) + (3,)) # self.text_buffer = FrameBuffer(self.texture, RenderBuffer((1000, 1000))) self.texture = Texture2D(shape=(500, 1500) + (3, )) self.text_buffer = FrameBuffer(self.texture, RenderBuffer((500, 1500))) self.program['texture'] = self.texture self.text = text self.make_text(self.text) self.first = True
def test_set_undersized_data(self): data = np.zeros((10, 10), dtype=np.uint8) T = Texture2D(data=data) T.set_data(np.ones((5, 5), np.uint8)) assert T.shape == (5, 5, 1) assert len(T._pending_data) == 1
def set_data(self, *args, **kwargs): data = self.validate(*args, **kwargs) pos = data.pos.astype(np.float64) assert pos.ndim == 2 assert pos.shape[1] == 2 assert pos.dtype == np.float64 # Concatenate all strings. text = data.text lengths = list(map(len, text)) text = ''.join(text) a_char_index = self._get_glyph_indices(text) n_glyphs = len(a_char_index) tex = self._tex glyph_height = tex.shape[0] // 6 glyph_width = tex.shape[1] // 16 glyph_size = (glyph_width, glyph_height) # Position of all glyphs. a_position = np.repeat(pos, lengths, axis=0) if not len(lengths): a_glyph_index = np.zeros((0, )) else: a_glyph_index = np.concatenate([np.arange(n) for n in lengths]) a_quad_index = np.arange(6) a_anchor = data.anchor a_position = np.repeat(a_position, 6, axis=0) a_glyph_index = np.repeat(a_glyph_index, 6) a_quad_index = np.tile(a_quad_index, n_glyphs) a_char_index = np.repeat(a_char_index, 6) a_anchor = np.repeat(a_anchor, lengths, axis=0) a_anchor = np.repeat(a_anchor, 6, axis=0) a_lengths = np.repeat(lengths, lengths) a_lengths = np.repeat(a_lengths, 6) n_vertices = n_glyphs * 6 # Transform the positions. if data.data_bounds is not None: data_bounds = data.data_bounds data_bounds = np.repeat(data_bounds, lengths, axis=0) data_bounds = np.repeat(data_bounds, 6, axis=0) assert data_bounds.shape == (n_vertices, 4) self.data_range.from_bounds = data_bounds pos_tr = self.transforms.apply(a_position) assert pos_tr.shape == (n_vertices, 2) else: pos_tr = a_position assert pos_tr.shape == (n_vertices, 2) assert a_glyph_index.shape == (n_vertices, ) assert a_quad_index.shape == (n_vertices, ) assert a_char_index.shape == (n_vertices, ) assert a_anchor.shape == (n_vertices, 2) assert a_lengths.shape == (n_vertices, ) self.program['a_position'] = pos_tr.astype(np.float32) self.program['a_glyph_index'] = a_glyph_index.astype(np.float32) self.program['a_quad_index'] = a_quad_index.astype(np.float32) self.program['a_char_index'] = a_char_index.astype(np.float32) self.program['a_anchor'] = a_anchor.astype(np.float32) self.program['a_lengths'] = a_lengths.astype(np.float32) self.program['u_glyph_size'] = glyph_size # TODO: color self.program['u_tex'] = Texture2D(tex[::-1, :])
def test_resize_unresizable(self): data = np.zeros((10, 10), dtype=np.uint8) T = Texture2D(data=data, resizable=False) # with self.assertRaises(RuntimeError): # T.resize((5, 5)) self.assertRaises(RuntimeError, T.resize, (5, 5))
def test_set_whole_data(self): data = np.zeros((10, 10), dtype=np.uint8) T = Texture2D(data=data) T.set_data(np.ones((10, 10), np.uint8)) assert T.shape == (10, 10, 1)
def test_init(self): data = np.zeros((10, 10), dtype=np.uint8) T = Texture2D(data=data) assert 'Texture2D' in repr(T) assert T._shape == (10, 10, 1) assert T.glsl_type == ('uniform', 'sampler2D')
def test_width_height(self): data = np.zeros((10, 20), dtype=np.uint8) T = Texture2D(data=data) assert T.width == 20 assert T.height == 10
def test_invalid_views(self): data = np.zeros((10, 10), dtype=np.uint8) T = Texture2D(data=data) Z = T[5:, 5:] T.resize((5, 5)) assert Z._valid is False
def __init__(self, canvas, background_color=(0,0,0,0), text_color=(1,1,1)): # State.register_button(position, text) self.canvas = canvas self.text = None self.tcolor = (text_color[0],text_color[1],text_color[2], 1) self.bcolor = background_color self.timer = 0 self.fade_timer = 0 self.projection = np.eye(4) self.view = np.eye(4) self.model = np.eye(4) height, width = 5.0, 15.0 # Meters orientation_vector = (1, 1, 0) unit_orientation_angle = np.array(orientation_vector) / np.linalg.norm(orientation_vector) scale_factor = 0.4 lowest_button = -5.2 midset = 0.2 position = -2 scale(self.model, scale_factor) #yrotate(self.model, -90) # rotate(self.model, 30, *unit_orientation_angle) offset = (position * ((height + midset) * scale_factor)) translate(self.model, -4.4, lowest_button + offset, -10) self.size = (int(height*100), int(width*100)) # 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, 0], [1, 0], [1, 1], [0, 1], ], dtype=np.float32) self.indices = IndexBuffer([ 0, 1, 2, 2, 3, 0, ]) self.program = Program(self.toast_vertex_shader, self.toast_fragment_shader) self.program['vertex_position'] = self.vertices self.program['default_texcoord'] = self.tex_coords self.program['view'] = self.view self.program['model'] = self.model self.program['projection'] = self.projection self.program['background_color'] = (0,0,0,0) # no background yet self.program['text_color'] = (0,0,0,0) # no text yet # self.texture = Texture2D(shape=(1000, 1000) + (3,)) # self.text_buffer = FrameBuffer(self.texture, RenderBuffer((1000, 1000))) self.texture = Texture2D(shape=self.size + (3,)) self.text_buffer = FrameBuffer(self.texture, RenderBuffer(self.size)) self.program['texture'] = self.texture #self.program['text_color'] = self.tcolor # set the tcolor #self.program['background_color'] = self.bcolor # set the tcolor self.first = False # do not draw text until needed self.make_text('Default Text') # Create Empty text object
def __init__(self): self.projection = np.eye(4) self.view = np.eye(4) self.model = np.eye(4) height, width = 3.0, 6.0 scale_factor = 0.2 x_offset = -8 y_offset = 2 pixel_to_length = 10 color = (1.0, 1.0, 1.0) scale(self.model, scale_factor) yrotate(self.model, -60) translate(self.model, x_offset, y_offset, -10) size = (int(height * 100), int(width * 100)) 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, 0], [1, 0], [1, 1], [0, 1], ], dtype=np.float32) self.indices = IndexBuffer([ 0, 1, 2, 2, 3, 0, ]) self.program = Program(self.battery_vertex_shader, self.battery_fragment_shader) self.texture = Texture2D(shape=size + (3, )) self.text_buffer = FrameBuffer(self.texture, RenderBuffer(size)) images = [] images.append( Image.open( os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_low_color.png'))) images.append( Image.open( os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_used_color.png'))) images.append( Image.open( os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_full_color.png'))) self.level_texture = {} # texture for each level for x in range(0, len(images)): default_image = images[x] default_image = default_image.rotate(-90) default_image = default_image.resize(size) default_image = default_image.transpose(Image.FLIP_TOP_BOTTOM) default_image_array = np.asarray(default_image) self.level_texture[x + 1] = default_image_array #default_image_array = imageio.imread(os.path.join(Paths.get_path_to_visar(), 'visar', 'images', 'battery', 'battery_full_color.png')) # self.default_tex = Texture2D(data=default_image_array) # self.default_tex = Texture2D(shape=size + (3,)) # self.default_tex.set_data(self.level_texture[3]) self.program['vertex_position'] = self.vertices self.program['default_texcoord'] = self.tex_coords self.program['view'] = self.view self.program['model'] = self.model self.program['projection'] = self.projection self.program['hide'] = 0 # self.tex_program = Program(self.tex_vert_shader, self.battery_fragment_shader) # self.tex_program['vertex_position'] = self.vertices # self.tex_program['default_texcoord'] = self.tex_coords # self.tex_program['hide'] = 0 # self.tex_program['texture'] = self.default_tex self.flag = True # flag to update the texture self.level = 3 # level of the battery 1 - 3 full_middle_split = 75 # split between levels 2 and 3 middle_low_split = 25 # split between levels 1 and 2 fault_tolerance = 5 self.full_lower = full_middle_split - fault_tolerance # lower limit for going from 3 to 2 self.middle_upper = full_middle_split + fault_tolerance # upper limit for going from 2 to 3 self.middle_lower = middle_low_split - fault_tolerance # lower limit for going from 2 to 1 self.low_upper = middle_low_split + fault_tolerance # upper limit for going from 1 to 2
def load_texture(): global slate slate = Texture2D(imread("planet.png"), wrapping='repeat')
def on_initialize(self, event): # Build cube data # -------------------------------------- texcoord = [(0, 0), (0, 1), (1, 0), (1, 1)] vertices = [(-1, -1, 0), (-1, +1, 0), (+1, -1, 0), (+1, +1, 0)] vertices = VertexBuffer(vertices) #self.listener.waitForTransform("/robot", "/wrist_joint", rospy.Time(), rospy.Duration(4)) """while not rospy.is_shutdown(): try: now = rospy.Time.now() self.listener.waitForTransform("/wrist_joint", "/robot", rospy.Time(), rospy.Duration(4)) (trans,rot) = listener.lookupTransform("/wrist_joint", "/robot", rospy.Time(), rospy.Duration(4)) """ #pos, rot = self.listener.lookupTransform("/wrist_joint", "/robot", rospy.Time(0)) #print list(pos) camera_pitch = 0.0 # Degrees self.rotate = [camera_pitch, 0, 0] #self.translate = list(pos) self.translate = [0, 0, -5] # Build program # -------------------------------------- view = np.eye(4, dtype=np.float32) model = np.eye(4, dtype=np.float32) scale(model, 10, 10, 10) self.phi = 0 self.cube = Program(cube_vertex, cube_fragment) self.cube['position'] = vertices # 4640 x 2256 imtex = cv2.imread(os.path.join(img_path, 'rubixFront.jpg')) self.cube['texcoord'] = texcoord self.cube["texture"] = np.uint8(np.clip(imtex + np.random.randint(-60, 20, size=imtex.shape), 0, 255)) + 5 self.cube["texture"].interpolation = 'linear' self.cube['model'] = model self.cube['view'] = view color = Texture2D((640, 640, 3), interpolation='linear') self.framebuffer = FrameBuffer(color, RenderBuffer((640, 640))) self.quad = Program(quad_vertex, quad_fragment) 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 self.objects = [self.cube] # 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.pub_timer = app.Timer(0.1, connect=self.send_ros_img, start=True) self._set_projection(self.size)