Python vispy.gloo.VertexBuffer() Examples

The following are 12 code examples of vispy.gloo.VertexBuffer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module vispy.gloo , or try the search function .
Example #1
Source File: visuals.py    From wonambi with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, meshdata):
        Visual.__init__(self, VERT_SHADER, FRAG_SHADER)

        v = meshdata.get_vertices(indexed='faces').astype(float32)
        self._vertices = VertexBuffer(v)

        v_norm = meshdata.get_vertex_normals(indexed='faces').astype(float32)
        self._normals = VertexBuffer(v_norm)

        v_col = meshdata.get_vertex_colors(indexed='faces').astype(float32)
        self._colors = VertexBuffer(v_col)

        self.set_light(position=(1., 0., 0.),
                       ambient=.2,
                       diffuse=.8,
                       )
        self._draw_mode = 'triangles'
        # self.set_gl_state('opaque', depth_test=True, cull_face=True)
        self.set_gl_state('translucent', depth_test=True, cull_face=False, blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) 
Example #2
Source File: renderer.py    From patch_linemod with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, vertices, faces, size, K, R, t, clip_near, clip_far,
                 bg_color=(0.0, 0.0, 0.0, 0.0), ambient_weight=0.1,
                 render_rgb=True, render_depth=True):
        """
        mode is from ['rgb', 'depth', 'rgb+depth']
        """
        app.Canvas.__init__(self, show=False, size=size)

        #gloo.gl.use_gl('gl2 debug')

        self.size = size
        self.shape = (self.size[1], self.size[0])
        self.bg_color = bg_color
        self.ambient_weight = ambient_weight
        self.render_rgb = render_rgb
        self.render_depth = render_depth

        self.rgb = np.array([])
        self.depth = np.array([])

        # Model matrix
        self.mat_model = np.eye(4, dtype=np.float32) # From object space to world space

        # View matrix (transforming also the coordinate system from OpenCV to
        # OpenGL camera space)
        self.mat_view = np.eye(4, dtype=np.float32) # From world space to eye space
        self.mat_view[:3, :3], self.mat_view[:3, 3] = R, t.squeeze()
        yz_flip = np.eye(4, dtype=np.float32)
        yz_flip[1, 1], yz_flip[2, 2] = -1, -1
        self.mat_view = yz_flip.dot(self.mat_view) # OpenCV to OpenGL camera system
        self.mat_view = self.mat_view.T # OpenGL expects column-wise matrix format

        # Projection matrix
        self.mat_proj = _compute_calib_proj(K, 0, 0, size[0], size[1], clip_near, clip_far)

        # Create buffers
        self.vertex_buffer = gloo.VertexBuffer(vertices)
        self.index_buffer = gloo.IndexBuffer(faces.flatten().astype(np.uint32))

        # We manually draw the hidden canvas
        self.update() 
Example #3
Source File: canvas.py    From harvesters_gui with Apache License 2.0 5 votes vote down vote up
def apply_magnification(self):
        #
        canvas_w, canvas_h = self.physical_size
        gloo.set_viewport(0, 0, canvas_w, canvas_h)

        #
        ratio = self._magnification
        w, h = self._width, self._height

        self._program['u_projection'] = ortho(
            self._coordinate[0],
            canvas_w * ratio + self._coordinate[0],
            self._coordinate[1],
            canvas_h * ratio + self._coordinate[1],
            -1, 1
        )

        x, y = int((canvas_w * ratio - w) / 2), int((canvas_h * ratio - h) / 2)  # centering x & y

        #
        self._data['a_position'] = np.array(
            [[x, y], [x + w, y], [x, y + h], [x + w, y + h]]
        )

        #
        self._program.bind(gloo.VertexBuffer(self._data)) 
Example #4
Source File: model.py    From ssd-6d with MIT License 5 votes vote down vote up
def _compute_bbox(self):

        self.bb = []
        minx, maxx = min(self.vertices[:, 0]), max(self.vertices[:, 0])
        miny, maxy = min(self.vertices[:, 1]), max(self.vertices[:, 1])
        minz, maxz = min(self.vertices[:, 2]), max(self.vertices[:, 2])
        self.bb.append([minx, miny, minz])
        self.bb.append([minx, maxy, minz])
        self.bb.append([minx, miny, maxz])
        self.bb.append([minx, maxy, maxz])
        self.bb.append([maxx, miny, minz])
        self.bb.append([maxx, maxy, minz])
        self.bb.append([maxx, miny, maxz])
        self.bb.append([maxx, maxy, maxz])
        self.bb = np.asarray(self.bb, dtype=np.float32)
        self.diameter = max(pdist(self.bb, 'euclidean'))

        # Set up rendering data
        colors = [[1, 0, 0],[1, 1, 0], [0, 1, 0], [0, 1, 1],
                  [0, 0, 1], [0, 1, 0], [0.5, 0, 0.5], [0, 0.5, 0.5]]
        indices = [0, 1, 0, 2, 3, 1, 3, 2,
                   4, 5, 4, 6, 7, 5, 7, 6,
                   0, 4, 1, 5, 2, 6, 3, 7]

        vertices_type = [('a_position', np.float32, 3), ('a_color', np.float32, 3)]
        collated = np.asarray(list(zip(self.bb, colors)), vertices_type)
        self.bb_vbuffer = gloo.VertexBuffer(collated)
        self.bb_ibuffer = gloo.IndexBuffer(indices) 
Example #5
Source File: renderer2d.py    From p5 with GNU General Public License v3.0 5 votes vote down vote up
def initialize_renderer(self):
		self.fbuffer = FrameBuffer()

		vertices = np.array([[-1.0, -1.0],
							 [+1.0, -1.0],
							 [-1.0, +1.0],
							 [+1.0, +1.0]],
							np.float32)
		texcoords = np.array([[0.0, 0.0],
							  [1.0, 0.0],
							  [0.0, 1.0],
							  [1.0, 1.0]],
							 dtype=np.float32)

		self.fbuf_vertices = VertexBuffer(data=vertices)
		self.fbuf_texcoords = VertexBuffer(data=texcoords)

		self.fbuffer_prog = Program(src_fbuffer.vert, src_fbuffer.frag)
		self.fbuffer_prog['texcoord'] = self.fbuf_texcoords
		self.fbuffer_prog['position'] = self.fbuf_vertices

		self.vertex_buffer = VertexBuffer()
		self.index_buffer = IndexBuffer()

		self.default_prog = Program(src_default.vert, src_default.frag)
		self.texture_prog = Program(src_texture.vert, src_texture.frag)
		self.texture_prog['texcoord'] = self.fbuf_texcoords

		self.reset_view() 
Example #6
Source File: renderer2d.py    From p5 with GNU General Public License v3.0 5 votes vote down vote up
def render_image(self, image, location, size):
		"""Render the image.

		:param image: image to be rendered
		:type image: builtins.Image

		:param location: top-left corner of the image
		:type location: tuple | list | builtins.Vector

		:param size: target size of the image to draw.
		:type size: tuple | list | builtins.Vector
		"""
		self.flush_geometry()

		self.texture_prog['fill_color'] = self.tint_color if self.tint_enabled else self.COLOR_WHITE
		self.texture_prog['transform'] = self.transform_matrix.T.flatten()

		x, y = location
		sx, sy = size
		imx, imy = image.size
		data = np.zeros(4,
						dtype=[('position', np.float32, 2),
							   ('texcoord', np.float32, 2)])
		data['texcoord'] = np.array([[0.0, 1.0],
									 [1.0, 1.0],
									 [0.0, 0.0],
									 [1.0, 0.0]],
									dtype=np.float32)
		data['position'] = np.array([[x, y + sy],
									 [x + sx, y + sy],
									 [x, y],
									 [x + sx, y]],
									dtype=np.float32)

		self.texture_prog['texture'] = image._texture
		self.texture_prog.bind(VertexBuffer(data))
		self.texture_prog.draw('triangle_strip') 
Example #7
Source File: renderer3d.py    From p5 with GNU General Public License v3.0 5 votes vote down vote up
def initialize_renderer(self):
		self.fbuffer = FrameBuffer()

		vertices = np.array([[-1.0, -1.0],
							 [+1.0, -1.0],
							 [-1.0, +1.0],
							 [+1.0, +1.0]],
							np.float32)
		texcoords = np.array([[0.0, 0.0],
							  [1.0, 0.0],
							  [0.0, 1.0],
							  [1.0, 1.0]],
							 dtype=np.float32)

		self.fbuf_vertices = VertexBuffer(data=vertices)
		self.fbuf_texcoords = VertexBuffer(data=texcoords)

		self.fbuffer_prog = Program(src_fbuffer.vert, src_fbuffer.frag)
		self.fbuffer_prog['texcoord'] = self.fbuf_texcoords
		self.fbuffer_prog['position'] = self.fbuf_vertices

		self.vertex_buffer = VertexBuffer()
		self.index_buffer = IndexBuffer()

		self.default_prog = Program(src_default.vert, src_default.frag)

		self.reset_view() 
Example #8
Source File: renderer.py    From eccv18-rgb_pose_refinement with MIT License 5 votes vote down vote up
def __init__(self, size, cam):

        app.Canvas.__init__(self, show=False, size=size)
        self.shape = (size[1], size[0])
        self.yz_flip = np.eye(4, dtype=np.float32)
        self.yz_flip[1, 1], self.yz_flip[2, 2] = -1, -1

        self.set_cam(cam)

        # Set up shader programs
        self.program_col = gloo.Program(_vertex_code_colored, _fragment_code_colored)
        self.program_bbox = gloo.Program(_vertex_code_colored, _fragment_code_bbox)
        self.program_tex = gloo.Program(_vertex_code_textured, _fragment_code_textured)
        self.program_bg = gloo.Program(_vertex_code_background, _fragment_code_background)

        # Texture where we render the color/depth and its FBO
        self.col_tex = gloo.Texture2D(shape=self.shape + (3,))
        self.fbo = gloo.FrameBuffer(self.col_tex, gloo.RenderBuffer(self.shape))
        self.fbo.activate()
        gloo.set_state(depth_test=True, blend=False, cull_face=True)
        gl.glEnable(gl.GL_LINE_SMOOTH)
        gloo.set_clear_color((0.0, 0.0, 0.0))
        gloo.set_viewport(0, 0, *self.size)

        # Set up background render quad in NDC
        quad = [[-1, -1], [1, -1], [1, 1], [-1, 1]]
        tex = [[0, 1], [1, 1], [1, 0], [0, 0]]
        vertices_type = [('a_position', np.float32, 2), ('a_texcoord', np.float32, 2)]
        collated = np.asarray(list(zip(quad, tex)), vertices_type)
        self.bg_vbuffer = gloo.VertexBuffer(collated)
        self.bg_ibuffer = gloo.IndexBuffer([0, 1, 2, 0, 2, 3]) 
Example #9
Source File: model.py    From eccv18-rgb_pose_refinement with MIT License 5 votes vote down vote up
def _set_bbox_buffer(self, colors):
        indices = [0, 1, 0, 2, 3, 1, 3, 2,
                   4, 5, 4, 6, 7, 5, 7, 6,
                   0, 4, 1, 5, 2, 6, 3, 7]

        vertices_type = [('a_position', np.float32, 3), ('a_color', np.float32, 3)]
        collated = np.asarray(list(zip(self.bb, colors)), vertices_type)
        self.bb_vbuffer = gloo.VertexBuffer(collated)
        self.bb_ibuffer = gloo.IndexBuffer(indices) 
Example #10
Source File: model.py    From eccv18-rgb_pose_refinement with MIT License 5 votes vote down vote up
def _setup_coordinate_system(self):
        self.cs = [[0, 0, 0], [self.diameter * 0.4, 0, 0], [0, 0, 0], [0, self.diameter * 0.45, 0], [0, 0, 0],
                   [0, 0, self.diameter * 0.4]]
        colors = [[0.8, 0, 0], [0.8, 0, 0], [0, 0.8, 0], [0, 0.8, 0], [0, 0, 0.8], [0, 0, 0.8]]
        indices = [0, 1, 2, 3, 4, 5]
        vertices_type = [('a_position', np.float32, 3), ('a_color', np.float32, 3)]
        collated = np.asarray(list(zip(self.cs, colors)), vertices_type)
        self.cs_vbuffer = gloo.VertexBuffer(collated)
        self.cs_ibuffer = gloo.IndexBuffer(indices) 
Example #11
Source File: model.py    From Pix2Pose with MIT License 4 votes vote down vote up
def _compute_bbox(self,color_type=0):

        self.bb = []
        minx, maxx = min(self.vertices[:, 0]), max(self.vertices[:, 0])
        miny, maxy = min(self.vertices[:, 1]), max(self.vertices[:, 1])
        minz, maxz = min(self.vertices[:, 2]), max(self.vertices[:, 2])
        self.bb.append([minx, miny, minz])
        self.bb.append([minx, maxy, minz])
        self.bb.append([minx, miny, maxz])
        self.bb.append([minx, maxy, maxz])
        self.bb.append([maxx, miny, minz])
        self.bb.append([maxx, maxy, minz])
        self.bb.append([maxx, miny, maxz])
        self.bb.append([maxx, maxy, maxz])
        self.bb = np.asarray(self.bb, dtype=np.float32)
        #self.diameter = max(pdist(self.bb, 'euclidean'))

        # Set up rendering data
        if(color_type==0):
            colors = [[1, 0, 0],[1, 1, 0], [0, 1, 0], [0, 1, 1],
                      [0, 0, 1], [0, 1, 0], [0.5, 0, 0.5], [0, 0.5, 0.5]]
        elif(color_type==1):
            colors = [[0, 0, 1],[0, 0, 1], [0, 0, 1], [0, 0, 1],
                      [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]]
        elif(color_type==2):
            colors = [[0, 1, 0],[0, 1, 0], [0, 1, 0], [0, 1, 0],
                      [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0]]
        elif(color_type==3):
            colors = [[1, 1, 1],[1, 1, 1], [1, 1, 1], [1, 1, 1],
                      [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
        else:
            colors = [[0, 1,0],[0, 1, 0], [0, 1, 0], [0, 1, 0],
                      [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0]]

        indices = [0, 1, 0, 2, 3, 1, 3, 2,
                   4, 5, 4, 6, 7, 5, 7, 6,
                   0, 4, 1, 5, 2, 6, 3, 7]

        vertices_type = [('a_position', np.float32, 3), ('a_color', np.float32, 3)]
        collated = np.asarray(list(zip(self.bb, colors)), vertices_type)
        self.bb_vbuffer = gloo.VertexBuffer(collated)
        self.bb_ibuffer = gloo.IndexBuffer(indices) 
Example #12
Source File: multi_iso_visual.py    From glue-vispy-viewers with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def __init__(self, vol, clim=None, relative_step_size=0.8, threshold=0.8, step=4, cmap='hot',
                 emulate_texture=False):
        tex_cls = TextureEmulated3D if emulate_texture else Texture3D

        # Storage of information of volume
        self._vol_shape = ()
        self._clim = None
        self._need_vertex_update = True

        # Set the colormap
        self._cmap = get_colormap(cmap)
        # self._cmap = TransGrays()

        # Create gloo objects
        self._vertices = VertexBuffer()
        self._texcoord = VertexBuffer(
            np.array([
                [0, 0, 0],
                [1, 0, 0],
                [0, 1, 0],
                [1, 1, 0],
                [0, 0, 1],
                [1, 0, 1],
                [0, 1, 1],
                [1, 1, 1],
            ], dtype=np.float32))
        self._tex = tex_cls((10, 10, 10), interpolation='linear',
                            wrapping='clamp_to_edge')

        # Create program
        Visual.__init__(self, vcode=VERT_SHADER, fcode=FRAG_SHADER)
        self.shared_program['u_volumetex'] = self._tex
        self.shared_program['a_position'] = self._vertices
        self.shared_program['a_texcoord'] = self._texcoord
        self.shared_program['u_threshold'] = threshold
        self.shared_program['level'] = step
        self._draw_mode = 'triangle_strip'
        self._index_buffer = IndexBuffer()

        # Only show back faces of cuboid. This is required because if we are
        # inside the volume, then the front faces are outside of the clipping
        # box and will not be drawn.
        self.set_gl_state('translucent', cull_face=False)

        # Set data
        self.set_data(vol, clim)

        # Set params
        self.method = 'iso'
        self.relative_step_size = relative_step_size
        self.threshold = threshold if (threshold is not None) else vol.mean()
        self.step = step
        self.freeze()