Ejemplo n.º 1
0
def make_window(image_fn, size, title=None, cleanup=True):
    """Return a glumpy app.Window with standard settings"""
    app.use('glfw')
    config = app.configuration.Configuration()
    config.major_version = 3
    config.minor_version = 2
    config.profile = "core"
    window = app.Window(int(size[0]),
                        int(size[1]),
                        title or '',
                        config=config,
                        vsync=True)

    @window.event
    def on_draw(dt):
        window.set_title('fps: {}'.format(window.fps).encode('ascii'))
        image_fn()

    if cleanup:

        @window.event
        def on_close():
            for stream in streams:
                stream.stop_stream()
                stream.close()
            audio.terminate()
            for shell in shells:
                shell.ex('exit()')
            sys.exit(0)

    return window
Ejemplo n.º 2
0
    def __init__(self, width=512, height=256):
        window = app.Window(width=width, height=height,
                            color=(0.0, 0.0, 0.0, 1.00))
        self.programs = []
        framebuffer = np.zeros((window.height, window.width * 3), dtype=np.uint8)
        self.first = True # Why this need self?
        @window.event
        def on_draw(dt):
            window.clear()
            for program_object in self.programs:
                program = program_object.program
                if program_object.draw_mode == gl.GL_TRIANGLES:
                    program.draw(program_object.draw_mode, program_object.tri)
            if self.first:
                print('First')
                gl.glReadPixels(0, 0, window.width, window.height,
                                gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer)
                my_texture = np.reshape(framebuffer, (window.height, window.width, 3))
                # Some unknown reason
                # The buffer didn't match what I see in the window
                my_texture = np.flipud(my_texture)
                scipy.misc.imsave('yolo.png', my_texture)
                #png.from_array(my_texture, 'RGB').save('screenshot{}.png'.format(1))
                self.first = False

        @window.event
        def on_resize(width, height):
            pass

        @window.event
        def on_init():
            gl.glEnable(gl.GL_DEPTH_TEST)
Ejemplo n.º 3
0
    def add_window(cls,
                   width: int = 500,
                   height: int = 500,
                   title: str = None,
                   color: list = [1, 1, 1, 1]):

        # make title of the window
        if title is None:
            title = 'window' + str(len(cls.WINDOWS) + 1)
        else:
            title = title
        new_window = app.Window(width=width,
                                height=height,
                                title=title,
                                color=color)

        # run default initiation
        @new_window.event
        def on_init():
            pass

        @new_window.event
        def on_resize(width: int, height: int):
            pass

        # insert window to the window dictionary(self.WINDOWS)
        cls.WINDOWS[title] = new_window
        print(f'___ new window {title} MADE')
Ejemplo n.º 4
0
    def init_window(self):
        window = app.Window(width=1920,
                            height=1080,
                            color=(0.30, 0.30, 0.35, 1.00))

        @window.event
        def on_init():
            gl.glEnable(gl.GL_DEPTH_TEST)
            gl.glPolygonOffset(1, 1)
            gl.glEnable(gl.GL_LINE_SMOOTH)
            gl.glLineWidth(0.55)

        @window.event
        def on_draw(dt):
            window.clear()
            self.on_draw(dt)

        @window.event
        def on_resize(width, height):
            program = self.program
            program['projection'] = glm.perspective(45.0,
                                                    width / float(height), 0.1,
                                                    100.0)

        self.window = window
Ejemplo n.º 5
0
    def __init__(self, model_folder, K, width=640, height=480, zNear=0.25, zFar=6.0):
        self.width = width
        self.height = height
        self.zNear = zNear
        self.zFar = zFar
        self.K = K
        self.model_folder = model_folder

        log.info("Loading mesh")
        vertices, indices = data.objload("{}/textured.obj"
                                         .format(model_folder), rescale=False)
        self.render_kernel = gloo.Program(vertex, fragment)
        self.render_kernel.bind(vertices)
        log.info("Loading texture")
        self.render_kernel['u_texture'] = np.copy(data.load("{}/texture_map.png"
                                                            .format(model_folder))[::-1, :, :])

        self.render_kernel['u_model'] = np.eye(4, dtype=np.float32)
        u_projection = self.my_compute_calib_proj(K, width, height, zNear, zFar)
        self.render_kernel['u_projection'] = np.copy(u_projection)

        self.window = app.Window(width=width, height=height, visible=False)
        @self.window.event
        def on_draw(dt):
            global trans
            self.window.clear()
            gl.glDisable(gl.GL_BLEND)
            gl.glEnable(gl.GL_DEPTH_TEST)
            self.render_kernel.draw(gl.GL_TRIANGLES)

        @self.window.event
        def on_init():
            gl.glEnable(gl.GL_DEPTH_TEST)
def main():

    vertex = """
    attribute vec2 position;
    void main (void)
    {
        gl_Position = vec4(position, 0.0, 1.0);
    }
    """

    fragment = open('/tmp/tt.fs').read()

    app.use('glfw')

    window = app.Window(width=640, height=480)

    @window.event
    def on_draw(dt):
        quad['time'] += dt * .5
        window.clear()
        quad.draw(gl.GL_TRIANGLE_STRIP)
        title = 'FPS:%f' % (clock.get_fps())
        window.set_title(title)

    quad = gloo.Program(vertex, fragment, count=4)
    quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
    quad['resolution'] = [window.width, window.height]
    quad['time'] = 0
    app.run()
Ejemplo n.º 7
0
    def __init__(self, init_f, per_frame_f, width=512, height=512):
        """
        constructor for the GLWindow object
        :param init_f: function to initialize window with, should return a float tensor in format:
                       [batch_size, channels, height, width] with values between -1 and 1
        :param per_frame_f: function run per frame to update window,
                            takes current frame as argument and should return then next frame
                            as a float tensor in format: [batch_size, channels, height, width]
        :param width: Width of the window in pixels
        :param height: Height of the window in pixels
        """

        # create window with OpenGL context
        self.init_f = init_f
        self.per_frame_f = per_frame_f

        app.use('glfw')
        window = app.Window(width, height, fullscreen=False)

        self.window = window
        self.setup()

        @window.event
        def on_draw(dt):
            global state
            self.window.set_title(str(self.window.fps).encode("ascii"))

            tex = screen['tex']
            h, w = tex.shape[:2]

            # mutate state in torch
            img = self.per_frame_f(
                state).detach()  # prevent autograd from filling memory

            # convert into proper format
            tensor = img.squeeze().transpose(0, 2)
            tensor = tensor.transpose(0, 1).data  # put in texture order
            tensor = torch.cat((tensor, tensor[:, :, 0].unsqueeze(2)),
                               2)  # add the alpha channel
            tensor[:, :, 3] = 1  # set alpha

            # check that tensor order matches texture:
            tensor = (255 *
                      tensor).byte().contiguous()  # convert to ByteTensor

            # copy from torch into buffer
            assert tex.nbytes == tensor.numel() * tensor.element_size()
            with self.cuda_activate(cuda_buffer) as ary:
                cpy = pycuda.driver.Memcpy2D()
                cpy.set_src_device(tensor.data_ptr())
                cpy.set_dst_array(ary)
                cpy.width_in_bytes = cpy.src_pitch = cpy.dst_pitch = tex.nbytes // h
                cpy.height = h
                cpy(aligned=False)
                torch.cuda.synchronize()

            # draw to screen
            self.window.clear()
            screen.draw(gl.GL_TRIANGLE_STRIP)
Ejemplo n.º 8
0
 def __init__(self, figsize=(10, 10), dpi=72, color=(.95, .95, .95, 1)):
     width = int(round(figsize[0] * dpi))
     height = int(round(figsize[1] * dpi))
     self.window = app.Window(width=width,
                              height=height,
                              color=color,
                              title="Figure (matplotlib API)")
     self.viewport = app.Viewport()
Ejemplo n.º 9
0
    def __init__(self,
                 model_folder,
                 K,
                 width=640,
                 height=480,
                 zNear=0.25,
                 zFar=6.0):
        self.width = width
        self.height = height
        self.zNear = zNear
        self.zFar = zFar
        self.K = K
        self.model_folder = model_folder

        self.rgb_buffer = np.zeros((self.height, self.width, 4),
                                   dtype=np.float32)
        self.depth_buffer = np.zeros((self.height, self.width),
                                     dtype=np.float32)

        log.info("Loading mesh")
        vertices, indices = data.objload(
            "{}/textured.obj".format(model_folder), rescale=False)
        self.render_kernel = gloo.Program(self.vertex, self.fragment)
        self.render_kernel.bind(vertices)
        log.info("Loading texture")
        self.render_kernel["u_texture"] = np.copy(
            data.load("{}/texture_map.png".format(model_folder))[::-1, :, :])

        self.render_kernel["u_model"] = np.eye(4, dtype=np.float32)
        u_projection = self.my_compute_calib_proj(K, width, height, zNear,
                                                  zFar)
        self.render_kernel["u_projection"] = np.copy(u_projection)

        self.window = app.Window(width=width, height=height, visible=False)
        print("self.window: ", self.window)
        print("self.render_kernel at init: ", self.render_kernel)

        @self.window.event
        def on_draw(dt):
            self.window.clear()
            gl.glDisable(gl.GL_BLEND)
            gl.glEnable(gl.GL_DEPTH_TEST)
            self.render_kernel.draw(gl.GL_TRIANGLES)
            gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA,
                            gl.GL_FLOAT, self.rgb_buffer)
            gl.glReadPixels(
                0,
                0,
                self.width,
                self.height,
                gl.GL_DEPTH_COMPONENT,
                gl.GL_FLOAT,
                self.depth_buffer,
            )

        @self.window.event
        def on_init():
            gl.glEnable(gl.GL_DEPTH_TEST)
def widowSetting():
    window = app.Window(1024, 1024, color=(0, 0, 0, 1))
    framebuffer = np.zeros((window.height, window.width * 3), dtype=np.uint8)

    @window.event
    def on_draw(dt):
        window.clear()
        # Filled program_cube
        program_ptCloud.draw(gl.GL_POINTS)

        # Make program_cube rotate
        program_ptCloud['u_model'] = matrix_model()

    def matrix_model():
        model = np.eye(4, dtype=np.float32)
        #model *= size
        glm.rotate(model, theta, 1, 0, 0)
        glm.rotate(model, -phi, 0, 1, 0)
        #glm.translate(model, -phi/100, -theta/100, 0)
        glm.scale(model, size)
        #model[3,3] = 1
        return model

    @window.event
    def on_resize(width, height):
        ratio = width / float(height)
        program_ptCloud['u_projection'] = glm.perspective(
            45.0, ratio, 0.001, 10000.0)

    @window.event
    def on_init():
        gl.glEnable(gl.GL_DEPTH_TEST)

    @window.event
    def on_mouse_scroll(x, y, dx, dy):
        global zoom, size
        size += dy * 0.1
        if size < 0:
            size = 0.1
        #zoom += dy*1
        #program_ptCloud['u_view'] = glm.translation(0, 0, zoom)

    @window.event
    def on_mouse_drag(x, y, dx, dy, button):
        global theta, phi
        theta += dy  # degrees
        phi -= dx  # degrees

    @window.event
    def on_key_press(symbol, modifiers):
        gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                        gl.GL_UNSIGNED_BYTE, framebuffer)
        png.from_array(framebuffer, 'RGB').save('screenshot.png')
        #print('Key pressed (symbol=%s, modifiers=%s)'% (symbol,modifiers))
        program_ptCloud['color_sel'] = 1 - program_ptCloud['color_sel']

    app.run()
Ejemplo n.º 11
0
def render_depth(model, im_size, K, R, t, clip_near=100, clip_far=2000):
    assert ({'pts', 'faces'}.issubset(set(model.keys())))
    shape = (im_size[1], im_size[0])

    # --------------------------------------------- Create buffers ----------------------------------------
    colors = np.ones((model['pts'].shape[0], 3), np.float32) * 0.5

    vertices_type = [('a_position', np.float32, 3),
                     ('a_color', np.float32, colors.shape[1])]
    vertices = np.array(list(zip(model['pts'], colors)), vertices_type)

    vertex_buffer = vertices.view(gloo.VertexBuffer)
    index_buffer = model['faces'].flatten().astype(np.uint32).view(gloo.IndexBuffer)


    # --------------------------------------------- Model matrix ------------------------------------------
    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)
    mat_view = np.eye(4, dtype=np.float32)  # From world space to eye space
    mat_view[:3, :3], 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
    mat_view = yz_flip.dot(mat_view)  # OpenCV to OpenGL camera system
    mat_view = mat_view.T  # OpenGL expects column-wise matrix format

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

    # --------------------------------------------- Create window ------------------------------------------
        # config = app.configuration.Configuration()
        # Number of samples used around the current pixel for multisample
        # anti-aliasing (max is 8)
        # config.samples = 8
        # config.profile = "core"
        # window = app.Window(config=config, visible=False)
    window = app.Window(visible=False)

    global rgb, depth
    rgb = None
    depth = None

    @window.event
    def on_draw(dt):
        window.clear()
        
        # Render depth image
        global depth
        depth = draw_depth(shape, vertex_buffer, index_buffer, mat_model,
                           mat_view, mat_proj)

    app.run(framecount=0) # The on_draw function is called framecount+1 times
    window.close()

    return depth
    def __init__(
        self,
        model_path,
        texture_path,
        K,
        width=640,
        height=480,
        zNear=0.25,
        zFar=6.0,
        brightness_ratios=[0.7],
    ):
        self.width = width
        self.height = height
        self.zNear = zNear
        self.zFar = zFar
        self.K = K
        self.model_path = model_path

        log.info("Loading mesh")
        vertices, indices = data.objload("{}".format(model_path), rescale=True)
        vertices["position"] = vertices["position"] / 10.0

        self.render_kernels = []
        for brightness_ratio in brightness_ratios:
            fragment = get_fragment(brightness_ratio=brightness_ratio)
            render_kernel = gloo.Program(vertex, fragment)
            render_kernel.bind(vertices)

            log.info("Loading texture")
            render_kernel["u_texture"] = np.copy(
                data.load("{}".format(texture_path))[::-1, :, :])

            render_kernel["u_model"] = np.eye(4, dtype=np.float32)
            u_projection = self.my_compute_calib_proj(K, width, height, zNear,
                                                      zFar)
            render_kernel["u_projection"] = np.copy(u_projection)

            render_kernel["u_light_intensity"] = 1, 1, 1
            self.render_kernels.append(render_kernel)
        self.brightness_k = 0  # init

        self.window = app.Window(width=width, height=height, visible=False)

        @self.window.event
        def on_draw(dt):
            self.window.clear()
            gl.glDisable(gl.GL_BLEND)
            gl.glEnable(gl.GL_DEPTH_TEST)
            # print('brightness_k', self.brightness_k) # this function runs when running app.run()
            self.render_kernels[self.brightness_k].draw(gl.GL_TRIANGLES)

        @self.window.event
        def on_init():
            gl.glEnable(gl.GL_DEPTH_TEST)
Ejemplo n.º 13
0
def widowSetting():
    window = app.Window(1024,1024, color=(1,1,1,1))
    @window.event
    def on_draw(dt):
        window.clear()
        points.draw()


    window.attach(points["transform"])
    window.attach(points["viewport"])
    app.run()    
Ejemplo n.º 14
0
    def __init__(self,
                 width,
                 height,
                 mode='rgb+depth',
                 shading='phong',
                 bg_color=(0.0, 0.0, 0.0, 0.0)):
        """Constructor.

    :param width: Width of the rendered image.
    :param height: Height of the rendered image.
    :param mode: Rendering mode ('rgb+depth', 'rgb', 'depth').
    :param shading: Type of shading ('flat', 'phong').
    :param bg_color: Color of the background (R, G, B, A).
    """
        super(RendererPython, self).__init__(width, height)

        self.mode = mode
        self.shading = shading
        self.bg_color = bg_color

        # Indicators whether to render RGB and/or depth image.
        self.render_rgb = self.mode in ['rgb', 'rgb+depth']
        self.render_depth = self.mode in ['depth', 'rgb+depth']

        # Structures to store object models and related info.
        self.models = {}
        self.model_bbox_corners = {}
        self.model_textures = {}

        # Rendered images.
        self.rgb = None
        self.depth = None

        # Window for rendering.
        self.window = app.Window(visible=False)

        # Per-object vertex and index buffer.
        self.vertex_buffers = {}
        self.index_buffers = {}

        # Per-object OpenGL programs for rendering of RGB and depth images.
        self.rgb_programs = {}
        self.depth_programs = {}

        # The frame buffer object.
        rgb_buf = np.zeros((self.height, self.width, 4),
                           np.float32).view(gloo.TextureFloat2D)
        depth_buf = np.zeros((self.height, self.width),
                             np.float32).view(gloo.DepthTexture)
        self.fbo = gloo.FrameBuffer(color=rgb_buf, depth=depth_buf)

        # Activate the created frame buffer object.
        self.fbo.activate()
Ejemplo n.º 15
0
    def __init__(self, class_name_list, model_folder_dict, K, width=640, height=480, zNear=0.25, zFar=6.0, brightness_ratios=[0.4, 0.3, 0.2]):
        self.width = width
        self.height = height
        self.zNear = zNear
        self.zFar = zFar
        self.K = K
        self.model_folder_dict = model_folder_dict
        self.class_name_list = class_name_list

        self.render_kernels = {} # self.render_kernels[class_name][brightness_ratio]
        for cls_name in class_name_list:
            if cls_name == "__background__":
                continue
            model_folder = model_folder_dict[cls_name]
            log.info("Loading mesh")
            vertices, indices = data.objload("{}/textured.obj"
                                             .format(model_folder), rescale=False)
            if not cls_name in self.render_kernels.keys():
                self.render_kernels[cls_name] = []
            for brightness_ratio in brightness_ratios:
                print('class_name: {}, brightness_ratio: {}, model_folder: {}'.format(cls_name, brightness_ratio,
                                                                                   model_folder))
                fragment = get_fragment(brightness_ratio=brightness_ratio)
                render_kernel = gloo.Program(vertex, fragment)
                render_kernel.bind(vertices)

                log.info("Loading texture")
                render_kernel['u_texture'] = np.copy(data.load("{}/texture_map.png"
                                                                    .format(model_folder))[::-1, :, :])

                render_kernel['u_model'] = np.eye(4, dtype=np.float32)
                u_projection = self.my_compute_calib_proj(K, width, height, zNear, zFar)
                render_kernel['u_projection'] = np.copy(u_projection)

                render_kernel['u_light_intensity'] = 1, 1, 1
                self.render_kernels[cls_name].append(render_kernel)

        self.class_name = class_name_list[-1]
        self.brightness_k = 0 # init

        self.window = app.Window(width=width, height=height, visible=False)

        @self.window.event
        def on_draw(dt):
            self.window.clear()
            gl.glDisable(gl.GL_BLEND)
            gl.glEnable(gl.GL_DEPTH_TEST)
            # print('brightness_k', self.brightness_k) # this function runs when running app.run()
            self.render_kernels[self.class_name][self.brightness_k].draw(gl.GL_TRIANGLES)

        @self.window.event
        def on_init():
            gl.glEnable(gl.GL_DEPTH_TEST)
Ejemplo n.º 16
0
    def make_window(self):
        """ Create the application window """

        window = app.Window(title=self.title,
                            width=self.width,
                            height=self.height,
                            color=self.bg_color)
        window.set_handler('on_draw', self.on_draw)
        window.set_handler('on_init', self.on_init)
        window.set_handler('on_resize', self.on_resize)
        window.set_handler('on_close', partial(self.on_close,
                                               caller=self.title))
        return window
Ejemplo n.º 17
0
    def _run(self):
        window = app.Window()

        @window.event
        def on_init():
            # FIXME: it doesn't work here, so it' has been duplicated in the main cycle
            gl.glEnable(gl.GL_DEPTH_TEST)

        @window.event
        def on_resize(width, height):
            ratio = width / float(height)
            self._scene.set_camera_projection(45.0, ratio, 2.0, 100.0)

        @window.event
        def on_draw(dt):
            window.clear()
            if dt == 0:
                return
            # ensure that depth test is enabled
            gl.glEnable(gl.GL_DEPTH_TEST)
            # rotate cube
            try:
                rotation_matrix = np.eye(4, dtype=np.float32)
                glm.rotate(
                    rotation_matrix,
                    angle=self.angle * self._RAD_TO_DEG_K,
                    x=self.x,
                    y=self.y,
                    z=self.z
                )
                self._rotation_matrix[...] = rotation_matrix
            except Exception as e:
                logger.error(e)
            # draw scene
            self._scene.draw()

        @window.event
        def on_close():
            print("Window has been closed", file=sys.stderr)
            sys.exit(1)

        backend = app.__backend__
        clock = app.__init__(backend=backend)
        clock.set_fps_limit(30)
        count = len(backend.windows())
        while count:
            count = backend.process(clock.tick())
            if not self._active:
                break
        with suppress(Exception):
            window.close()
Ejemplo n.º 18
0
    def __init__(self,
                 model_folder,
                 K,
                 width=640,
                 height=480,
                 zNear=0.25,
                 zFar=6.0,
                 brightness_ratios=[0.4, 0.3, 0.2]):
        self.width = width
        self.height = height
        self.zNear = zNear
        self.zFar = zFar
        self.K = K
        self.model_folder = model_folder

        log.info("Loading brain mesh")
        vertices, indices = data.objload(
            "{}/textured.obj".format(model_folder), rescale=False)
        self.render_kernels = []
        for brightness_ratio in brightness_ratios:
            fragment = get_fragment(brightness_ratio=brightness_ratio)
            render_kernel = gloo.Program(vertex, fragment)
            render_kernel.bind(vertices)

            log.info("Loading brain texture")
            render_kernel['u_texture'] = np.copy(
                data.load(
                    "{}/texture_map.png".format(model_folder))[::-1, :, :])

            render_kernel['u_model'] = np.eye(4, dtype=np.float32)
            u_projection = self.my_compute_calib_proj(K, width, height, zNear,
                                                      zFar)
            render_kernel['u_projection'] = np.copy(u_projection)

            render_kernel['u_light_intensity'] = 1, 1, 1
            self.render_kernels.append(render_kernel)
        self.brightness_k = 0  # init

        self.window = app.Window(width=width, height=height, visible=False)

        @self.window.event
        def on_draw(dt):
            self.window.clear()
            gl.glDisable(gl.GL_BLEND)
            gl.glEnable(gl.GL_DEPTH_TEST)
            self.render_kernels[self.brightness_k].draw(gl.GL_TRIANGLES)

        @self.window.event
        def on_init():
            gl.glEnable(gl.GL_DEPTH_TEST)
Ejemplo n.º 19
0
    def run(self):
        app.use('glfw')
        self.window = app.Window(height=self.h, width=self.w, fullscreen=False)
        self.window.push_handlers(on_draw=self.on_draw)
        self.window.push_handlers(on_close=self.on_close)

        import pycuda.gl.autoinit
        import pycuda.gl

        tex, cuda_buffer = create_shared_texture(self.w, self.h, 4)
        self.tex = tex
        self.cuda_buffer = cuda_buffer
        self.screen_program = get_screen_program(tex)

        app.run(framerate=self.max_framerate)
Ejemplo n.º 20
0
    def __init__(self, model_path_list, texture_path, K, width=640, height=480, zNear=0.25, zFar=6.0, brightness_ratios=[0.7]):
        self.width = width
        self.height = height
        self.zNear = zNear
        self.zFar = zFar
        self.K = K
        self.model_path_list = model_path_list
        self.model_path = model_path_list[0]
        self.render_kernels = {model_path: [] for model_path in model_path_list}

        log.info("Loading mesh")
        for model_i, model_path in enumerate(model_path_list):
            print('loading model: {}/{}, {}'.format(model_i+1, len(model_path_list), model_path))
            vertices, indices = data.objload("{}"
                                             .format(model_path), rescale=True)
            vertices['position'] = vertices['position'] / 10.


            for brightness_ratio in brightness_ratios:
                fragment = get_fragment(brightness_ratio=brightness_ratio)
                render_kernel = gloo.Program(vertex, fragment)
                render_kernel.bind(vertices)

                log.info("Loading texture")
                render_kernel['u_texture'] = np.copy(data.load("{}"
                                                               .format(texture_path))[::-1, :, :])

                render_kernel['u_model'] = np.eye(4, dtype=np.float32)
                u_projection = self.my_compute_calib_proj(K, width, height, zNear, zFar)
                render_kernel['u_projection'] = np.copy(u_projection)

                render_kernel['u_light_intensity'] = 1, 1, 1
                self.render_kernels[model_path].append(render_kernel)
        print('************Finish loading models in Render_Py_Light_ModelNet_Multi********************')
        self.brightness_k = 0  # init

        self.window = app.Window(width=width, height=height, visible=False)

        @self.window.event
        def on_draw(dt):
            self.window.clear()
            gl.glDisable(gl.GL_BLEND)
            gl.glEnable(gl.GL_DEPTH_TEST)
            self.render_kernels[self.model_path][self.brightness_k].draw(gl.GL_TRIANGLES)

        @self.window.event
        def on_init():
            gl.glEnable(gl.GL_DEPTH_TEST)
Ejemplo n.º 21
0
 def __init__(self, args, fragment=None, winsize=None, title=None):
     self.fps = args.fps
     self.record = args.record
     self.old_program = None
     if fragment is None:
         fragment = args.fragment
     if winsize:
         args.winsize = list(map(int, winsize))
     self.title = title
     self.load_program(fragment, args.export)
     self.params = args.params
     if self.params:
         self.program_params = set(self._params.keys()).intersection(
             set(self.params.keys())) - {"mods"}
     else:
         self.params = self._params
         self.program_params = set(self._params.keys()) - {"mods"}
     self.iMat = self.params.get("iMat")
     # Gimbal mode, always looking at the center
     self.gimbal = True
     if self.gimbal:
         self.params.setdefault("horizontal_angle", 0.)
         self.params.setdefault("vertical_angle", 0.)
         self.params.setdefault("distance", 10.)
     if self.iMat is not None:
         self.horizontal_angle = self.params.get("horizontal_angle", 0.)
         self.vertical_angle = self.params.get("vertical_angle", 0.)
         self.setDirection()
         if self.gimbal:
             self.position = np.array([.0, .0, self.params["distance"]])
         else:
             self.position = np.array([.0, .0, 10.2])
     if title != "Map":
         self.controller = Controller(self.params, default={})
     else:
         self.controller = None
     self.screen = app.Window(width=args.winsize[0],
                              height=args.winsize[1],
                              title=title)
     super().__init__(args.winsize, self.screen)
     if self.controller:
         self.controller.set(self.screen, self)
     self.screen.attach(self)
     self.paused = False
     self.prev_params = {}
Ejemplo n.º 22
0
    def run(self):
        app.use('glfw')
        self.window = app.Window(height=self.h, width=self.w, fullscreen=False)
        self.window.push_handlers(on_draw=self.on_draw)
        self.window.push_handlers(on_close=self.on_close)

        self.state2 = torch.ones((self.h, self.w, 4), dtype=torch.uint8, device=torch.device('cuda:0')) * 128
        self.state2[:, :, 1] = 255

        import pycuda.gl.autoinit
        import pycuda.gl

        tex, cuda_buffer = create_shared_texture(self.w, self.h, 4)
        self.tex = tex
        self.cuda_buffer = cuda_buffer
        self.screen_program = get_screen_program(tex)

        app.run(framerate=self.max_framerate)
Ejemplo n.º 23
0
 def run(self):
     app.use("qt5")
     self.window = app.Window(width=self.width, height=self.height)
     #self.window = app.Window()
     self.window.attach(self)
     #app.clock.set_fps_limit(20)
     clock = app.__init__(backend=app.__backend__)
     if False:
         with record(self.window, "cube.mp4", fps=20):
             app.run(framerate=20)
     else:
         while True:
             #dt = clock.tick()
             #self.program1['time'] = dt*5
             time.sleep(0.01)
             app.__backend__.process(0.05)
             if self.finish:
                 return
Ejemplo n.º 24
0
    def __init__(self,
                 model,
                 im_size,
                 texture=None,
                 bg_color=(0.0, 0.0, 0.0, 0.0),
                 ambient_weight=0.5,
                 shading='flat',
                 mode='rgb+depth'):

        # Set texture / color of vertices
        texture_uv = np.zeros((model['pts'].shape[0], 2), np.float32)
        colors = np.ones((model['pts'].shape[0], 3), np.float32) * 0.5

        # Set the vertex data
        vertices_type = [('a_position', np.float32, 3),
                         ('a_normal', np.float32, 3),
                         ('a_color', np.float32, colors.shape[1]),
                         ('a_texcoord', np.float32, 2)]
        vertices = np.array(
            list(zip(model['pts'], model['normals'], colors, texture_uv)),
            vertices_type)

        # Create buffers
        self.vertex_buffer = vertices.view(gloo.VertexBuffer)
        self.index_buffer = model['faces'].flatten().astype(np.uint32).view(
            gloo.IndexBuffer)
        self.window = app.Window(visible=False)

        self.program = gloo.Program(_normal_vertex_code, _normal_fragment_code)
        self.program.bind(self.vertex_buffer)

        scale = max(im_size)
        shape = (scale, scale)
        color_buf = np.zeros((shape[0], shape[1], 4),
                             np.float32).view(gloo.TextureFloat2D)
        depth_buf = np.zeros((shape[0], shape[1]),
                             np.float32).view(gloo.DepthTexture)
        fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
        fbo.activate()
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3])
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glViewport(0, 0, shape[1], shape[0])
        gl.glDisable(gl.GL_CULL_FACE)
Ejemplo n.º 25
0
def draw(fx):

    window = app.Window(width=800, height=800, color=(1,1,1,1))

    pos, rad, col = next(fx)
    n = len(rad)

    program = gloo.Program(vertex, fragment, count=n)
    view = np.eye(4, dtype=np.float32)
    glm.translate(view, 0, 0, -5)

    program['position'] = pos
    program['radius']   = rad
    program['fg_color'] = 0,0,0,1
    program['bg_color'] = col
    program['linewidth'] = 1.0
    program['antialias'] = 1.0
    program['model'] = np.eye(4, dtype=np.float32)
    program['projection'] = np.eye(4, dtype=np.float32)
    program['view'] = view

    @window.event
    def on_draw(dt):
        global theta, phi
        program['position'], program['radius'], program['bg_color'] = next(fx)

        window.clear()
        program.draw(gl.GL_POINTS)
        theta += .5
        phi += .5
        model = np.eye(4, dtype=np.float32)
        glm.rotate(model, theta, 0, 0, 1)
        glm.rotate(model, phi, 0, 1, 0)
        program['model'] = model

    @window.event
    def on_resize(width,height):
        program['projection'] = glm.perspective(45.0, width / float(height), 1.0, 1000.0)

    gl.glEnable(gl.GL_DEPTH_TEST)
    app.run()   
Ejemplo n.º 26
0
def main():
    window = app.Window(width=800, height=800, color=(1, 1, 1, 1))

    protein = gloo.Program(vertex, fragment)
    protein['light_position'] = 0., 0., 2.
    protein["transform"] = Trackball(Position())
    protein.bind(data.get("protein.npy").view(gloo.VertexBuffer))
    protein['color'] *= .25
    protein['color'] += .75

    @window.event
    def on_draw(dt):
        window.clear()
        protein.draw(gl.GL_POINTS)

    @window.event
    def on_init():
        gl.glEnable(gl.GL_DEPTH_TEST)

    window.attach(protein["transform"])
    app.run()
Ejemplo n.º 27
0
    def __init__(self, width=512, height=256):
        window = app.Window(width=width,
                            height=height,
                            color=(0.0, 0.0, 0.0, 1.00))
        framebuffer = np.zeros((window.height, window.width * 3),
                               dtype=np.uint8)
        self.programMulti, self.programIdx = [], 0
        self.first = True  # Why this need self?

        @window.event
        def on_draw(dt):
            for programSingle_idx, programSingle in enumerate(
                    self.programMulti):
                print(programSingle_idx)
                window.clear()
                for program_object in programSingle:
                    program = program_object.program
                    if program_object.draw_mode == gl.GL_TRIANGLES:
                        program.draw(program_object.draw_mode,
                                     program_object.tri)

                gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                                gl.GL_UNSIGNED_BYTE, framebuffer)
                my_texture = np.reshape(framebuffer,
                                        (window.height, window.width, 3))
                # Some unknown reason
                # The buffer didn't match what I see in the window
                my_texture = np.flipud(my_texture)
                scipy.misc.imsave(
                    '{:d}_{:d}.png'.format(self.programIdx, programSingle_idx),
                    my_texture)
            window.close()

        @window.event
        def on_resize(width, height):
            pass

        @window.event
        def on_init():
            gl.glEnable(gl.GL_DEPTH_TEST)
Ejemplo n.º 28
0
def display_cloud(cloud):
    window = app.Window(width=800, height=800, color=(1, 1, 1, 1))

    @window.event
    def on_draw(dt):
        global theta, phi, translate
        window.clear()
        program.draw(gl.GL_POINTS)
        theta += .5
        phi += .5
        model = np.eye(4, dtype=np.float32)
        glm.rotate(model, theta, 0, 0, 1)
        glm.rotate(model, phi, 0, 1, 0)
        program['model'] = model

    @window.event
    def on_resize(width, height):
        program['projection'] = glm.perspective(45.0, width / float(height),
                                                1.0, 1000.0)

    n = len(cloud)
    program = gloo.Program(vertex, fragment, count=n)
    view = np.eye(4, dtype=np.float32)
    glm.translate(view, 0, 0, -5)

    program['position'] = cloud
    program['radius'] = [5] * n
    program['fg_color'] = 0, 0, 0, 1
    colors = np.random.uniform(0.75, 1.00, (n, 4))
    colors[:, 3] = 1
    program['bg_color'] = colors
    program['linewidth'] = 1.0
    program['antialias'] = 1.0
    program['model'] = np.eye(4, dtype=np.float32)
    program['projection'] = np.eye(4, dtype=np.float32)
    program['view'] = view

    gl.glEnable(gl.GL_DEPTH_TEST)
    app.run()
Ejemplo n.º 29
0
from math import *

import Mesh
myMesh = Mesh.Mesh()
print(myMesh.nV)
print(myMesh.nF)

myMesh.load('truck.txt', ply=True, normalize=True)
print(myMesh.nV)
print(myMesh.nF)

asp = 1.0
angle = 0.0
cur_time = -1.0

window = app.Window(700, 600, "hello")


def cameraLensSet(ratio=1.0):
    # camera lens 설정
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()  # 기본 렌즈 설정
    gluPerspective(60, ratio, 0.01, 100)  # y 시야각, 종횡비, 가까운 면, 먼 면
    glClearColor(1.0, 0.0, 0.0, 1.0)  # RGBA


@window.event
def on_draw(dt):
    global angle, cur_time

    old_time = cur_time
Ejemplo n.º 30
0
# Minimize the model, and move the camera-view back
glm.scale(model, 0.5, 1, 0.1)
glm.translate(view, 0, 0, -5)

# Pass all the matrix to the model
cube['u_model'] = model
cube['u_view'] = view
cube['u_projection'] = projection
cube["u_light_position"] = 0, 0, -2
cube["u_light_intensity"] = 1, 1, 1

# Initiaze the window
phi = 0.5
theta = 0.1
kappa = 1
window = app.Window(800, 600)


@window.event
def on_resize(width, height):
    global projection

    ratio = width / float(height)
    projection = glm.perspective(45.0, ratio, 2.0, 100.0)
    cube['u_projection'] = projection


@window.event
def on_draw(dt):
    global phi, theta, model