Example #1
0
def on_draw(dt):
    global phi, theta, duration, i, cube_prog, indices

    window.clear()
    gl.glEnable(gl.GL_DEPTH_TEST)
    cube_prog.draw(gl.GL_TRIANGLES, indices)

    # Rotate cube
    theta += random.randint(0, 300)  # degrees
    phi += random.randint(0, 300)  # degrees
    view = cube_prog['u_view'].reshape(4, 4)
    model = np.eye(4, dtype=np.float32)
    glm.rotate(model, theta, 0, 0, 1)
    glm.rotate(model, phi, 0, 1, 0)
    cube_prog['u_model'] = model
    cube_prog['u_normal'] = np.array(np.matrix(np.dot(view, model)).I.T)

    if i < count:
        framebuffer = np.zeros((window.height, window.width * 3),
                               dtype=np.uint8)
        gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                        gl.GL_UNSIGNED_BYTE, framebuffer)
        png.from_array(framebuffer, 'RGB').save("color/" + str(dice_val) +
                                                "_" + str(i) + '.png')
        i += 1
    else:
        app.quit()
        i = 0
Example #2
0
def on_draw(dt):
    global phi, theta, writer, duration

    window.clear()
    gl.glEnable(gl.GL_DEPTH_TEST)
    cube.draw(gl.GL_TRIANGLES, faces)

    # Write one frame
    if writer is not None:
        if duration > 0:
            gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                            gl.GL_UNSIGNED_BYTE, fbuffer)
            writer.write_frame(fbuffer)
            duration -= dt
        else:
            writer.close()
            writer = None

    # Make cube rotate
    theta += 0.5  # degrees
    phi += 0.5  # degrees
    model = np.eye(4, dtype=np.float32)
    glm.rotate(model, theta, 0, 0, 1)
    glm.rotate(model, phi, 0, 1, 0)
    cube['model'] = model
Example #3
0
def draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj):

    program = gloo.Program(_depth_vertex_code, _depth_fragment_code)
    program.bind(vertex_buffer)
    program['u_mv'] = _compute_model_view(mat_model, mat_view)
    program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj)

    # Frame buffer object
    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.glEnable(gl.GL_CULL_FACE)
    gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled
    gl.glClearColor(0.0, 0.0, 0.0, 0.0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    # Rendering
    program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth)
    depth.shape = shape[0], shape[1], 4
    depth = depth[::-1, :]
    depth = depth[:, :, 0] # Depth is saved in the first channel

    fbo.deactivate()

    return depth
Example #4
0
    def render(self, r, t, r_type="quat"):
        if r_type == "quat":
            R = quat2mat(r)
        elif r_type == "mat":
            R = r
        self.render_kernel["u_view"] = self._get_view_mtx(R, t)
        app.run(framecount=0)
        rgb_buffer = np.zeros((self.height, self.width, 4), dtype=np.float32)
        gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl.GL_FLOAT,
                        rgb_buffer)

        rgb_gl = np.copy(rgb_buffer)
        rgb_gl.shape = 480, 640, 4
        rgb_gl = rgb_gl[::-1, :]
        rgb_gl = np.round(rgb_gl[:, :, :3] * 255).astype(
            np.uint8)  # Convert to [0, 255]
        bgr_gl = rgb_gl[:, :, [2, 1, 0]]

        depth_buffer = np.zeros((self.height, self.width), dtype=np.float32)
        gl.glReadPixels(0, 0, self.width, self.height, gl.GL_DEPTH_COMPONENT,
                        gl.GL_FLOAT, depth_buffer)
        depth_gl = np.copy(depth_buffer)
        depth_gl.shape = 480, 640
        depth_gl = depth_gl[::-1, :]
        depth_bg = depth_gl == 1
        depth_gl = 2 * self.zFar * self.zNear / (self.zFar + self.zNear -
                                                 (self.zFar - self.zNear) *
                                                 (2 * depth_gl - 1))
        depth_gl[depth_bg] = 0
        return bgr_gl, depth_gl
Example #5
0
    def on_key_press(self, k, modifiers):
        """" Default key handler that close window on escape """
        if k == key.ESCAPE:
            self.close()
            return True
        elif k == key.F10:
            import os, sys
            import numpy as np
            from glumpy.ext import png
            framebuffer = np.zeros((self.height, self.width * 3), dtype=np.uint8)
            gl.glReadPixels(0, 0, self.width, self.height,
                            gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer)

            basename = os.path.basename(os.path.realpath(sys.argv[0]))
            dirname = os.path.dirname(os.path.realpath(sys.argv[0]))
            basename = '.'.join(basename.split('.')[:-1])
            filename = os.path.join(dirname,"%s.png" % basename)
            png.from_array(framebuffer[::-1], 'RGB').save(filename)
#            index = 0
#            filename = "%s-%04d.png" % (basename,index)
#            while os.path.exists(os.path.join(dirname, filename)):
#                index += 1
#                filename = "%s-%04d.png" % (basename, index)
#            png.from_array(framebuffer, 'RGB').save(filename)
            return True
Example #6
0
    def on_key_press(self, k, modifiers):
        """" Default key handler that close window on escape """
        if k == key.ESCAPE:
            self.close()
            return True
        elif k == key.F10:
            import os, sys
            import numpy as np
            from glumpy.ext import png
            framebuffer = np.zeros((self.height, self.width * 3),
                                   dtype=np.uint8)
            gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGB,
                            gl.GL_UNSIGNED_BYTE, framebuffer)

            basename = os.path.basename(os.path.realpath(sys.argv[0]))
            dirname = os.path.dirname(os.path.realpath(sys.argv[0]))
            basename = '.'.join(basename.split('.')[:-1])
            filename = os.path.join(dirname, "%s.png" % basename)
            png.from_array(framebuffer[::-1], 'RGB').save(filename)
            #            index = 0
            #            filename = "%s-%04d.png" % (basename,index)
            #            while os.path.exists(os.path.join(dirname, filename)):
            #                index += 1
            #                filename = "%s-%04d.png" % (basename, index)
            #            png.from_array(framebuffer, 'RGB').save(filename)
            return True
Example #7
0
    def on_draw(dt):
        # window.clear()
        global rgb
        extent_shape = (int(shape[0] * ssaa), int(shape[1] * ssaa))
        # Frame buffer object
        color_buf = np.zeros((extent_shape[0], extent_shape[1], 4),
                             np.float32).view(gloo.TextureFloat2D)
        depth_buf = np.zeros((extent_shape[0], extent_shape[1]),
                             np.float32).view(gloo.DepthTexture)
        fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
        fbo.activate()

        # OpenGL setup
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glViewport(0, 0, extent_shape[1], extent_shape[0])

        gl.glDisable(gl.GL_CULL_FACE)
        program.draw(gl.GL_TRIANGLES, I)

        rgb = np.zeros((extent_shape[0], extent_shape[1], 4), dtype=np.float32)
        gl.glReadPixels(0, 0, extent_shape[1], extent_shape[0], gl.GL_RGBA,
                        gl.GL_FLOAT, rgb)
        rgb.shape = extent_shape[0], extent_shape[1], 4
        rgb = rgb[::-1, :]
        rgb = np.round(rgb[:, :, :3] * 255).astype(
            np.uint8)  # Convert to [0, 255]

        import cv2
        rgb = cv2.resize(rgb, shape, interpolation=cv2.INTER_AREA)

        fbo.deactivate()
        def on_key_press(symbol, modifiers):
            """

            :param symbol:
            :param modifiers:
            :return:
            """
            '''
            if symbol == 88:  # x
                self.view_orth_vector = np.array([self.radius, 0, 0])
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 89:  # y
                self.view_orth_vector = np.array([0, self.radius, 0])
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 90:  # z
                self.view_orth_vector = np.array([0, 0, self.radius])
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 70:  # f
                self.view_orth_vector = -self.view_orth_vector
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 80:  # p
                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))
            # self.program['color_sel'] = 1 - self.program['color_sel']
            '''
            #print(symbol)
            if symbol == 67:  # c --> change color
                for program_object in self.programs:
                    if program_object.name == 'ProgramSFM3DRegion':
                        program_object.program['color_sel'] = 1 - program_object.program['color_sel']
            elif symbol == 65:  # a --> align sfm to google
                for program_object in self.programs:
                    if program_object.name == 'ProgramSFM3DRegion' or program_object.name == 'programTrajectory':
                        program_object.align_flip()
            elif symbol == 73:  # i --> inverse google according anchor
                for program_object in self.programs:
                    if program_object.name == 'ProgramSV3DRegion':
                        program_object.apply_anchor_flip()
            elif symbol == 89:  # y --> rotate google according anchor yaw
                for program_object in self.programs:
                    if program_object.name == 'ProgramSV3DRegion':
                        program_object.apply_yaw_flip()
            elif symbol == 80:  # p --> print scrren
                gl.glReadPixels(0, 0, window.width, window.height,
                                gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer)
                #png.from_array(framebuffer, 'RGB').save('screenshot{}.png'.format(self.scIdx))
                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)

                self.scIdx += 1
Example #9
0
        def on_key_press(symbol, modifiers):
            """

            :param symbol:
            :param modifiers:
            :return:
            """
            '''
            if symbol == 88:  # x
                self.view_orth_vector = np.array([self.radius, 0, 0])
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 89:  # y
                self.view_orth_vector = np.array([0, self.radius, 0])
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 90:  # z
                self.view_orth_vector = np.array([0, 0, self.radius])
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 70:  # f
                self.view_orth_vector = -self.view_orth_vector
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 80:  # p
                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))
            # self.program['color_sel'] = 1 - self.program['color_sel']
            '''
            #print(symbol)
            if symbol == 67:  # c --> change color
                for program_object in self.programs:
                    if program_object.name == 'ProgramSFM3DRegion':
                        program_object.program['color_sel'] = 1 - program_object.program['color_sel']
            elif symbol == 65:  # a --> align sfm to google
                for program_object in self.programs:
                    if program_object.name == 'ProgramSFM3DRegion' or program_object.name == 'programTrajectory':
                        program_object.align_flip()
            elif symbol == 73:  # i --> inverse google according anchor
                for program_object in self.programs:
                    if program_object.name == 'ProgramSV3DRegion':
                        program_object.apply_anchor_flip()
            elif symbol == 89:  # y --> rotate google according anchor yaw
                for program_object in self.programs:
                    if program_object.name == 'ProgramSV3DRegion':
                        program_object.apply_yaw_flip()
            elif symbol == 80:  # p --> print scrren
                gl.glReadPixels(0, 0, window.width, window.height,
                                gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer)
                #png.from_array(framebuffer, 'RGB').save('screenshot{}.png'.format(self.scIdx))
                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)

                self.scIdx += 1
Example #10
0
def draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj):

    program = gloo.Program(_depth_vertex_code, _depth_fragment_code)
    program.bind(vertex_buffer)
    program['u_mv'] = _compute_model_view(mat_model, mat_view)
    program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj)

    # Frame buffer object
    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.glEnable(gl.GL_CULL_FACE)
    gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled
    gl.glClearColor(0.0, 0.0, 0.0, 0.0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    # Rendering
    program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth)
    depth.shape = shape[0], shape[1], 4
    depth = depth[::-1, :]
    depth = depth[:, :, 0] # Depth is saved in the first channel

    fbo.deactivate()

    return depth
Example #11
0
def on_draw(dt):
    global phi, theta, writer, duration

    window.clear()
    gl.glEnable(gl.GL_DEPTH_TEST)
    cube.draw(gl.GL_TRIANGLES, faces)

    # Write one frame
    if writer is not None:
        if duration > 0:
            gl.glReadPixels(0, 0, window.width, window.height,
                            gl.GL_RGB, gl.GL_UNSIGNED_BYTE, fbuffer)
            writer.write_frame(fbuffer)
            duration -= dt
        else:
            writer.close()
            writer = None

    # Make cube rotate
    theta += 0.5 # degrees
    phi += 0.5 # degrees
    model = np.eye(4, dtype=np.float32)
    glm.rotate(model, theta, 0, 0, 1)
    glm.rotate(model, phi, 0, 1, 0)
    cube['model'] = model
Example #12
0
        def on_key_press(symbol, modifiers):
            if symbol == 88:  # x
                self.view_orth_vector = np.array([self.radius, 0, 0])
                self.program['u_view'] = glm.translation(
                    self.view_orth_vector[0], self.view_orth_vector[1],
                    self.view_orth_vector[2])
            elif symbol == 89:  # y
                self.view_orth_vector = np.array([0, self.radius, 0])
                self.program['u_view'] = glm.translation(
                    self.view_orth_vector[0], self.view_orth_vector[1],
                    self.view_orth_vector[2])
            elif symbol == 90:  # z
                self.view_orth_vector = np.array([0, 0, self.radius])
                self.program['u_view'] = glm.translation(
                    self.view_orth_vector[0], self.view_orth_vector[1],
                    self.view_orth_vector[2])
            elif symbol == 70:  # f
                self.view_orth_vector = -self.view_orth_vector
                self.program['u_view'] = glm.translation(
                    self.view_orth_vector[0], self.view_orth_vector[1],
                    self.view_orth_vector[2])
            elif symbol == 80:  # p
                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))
    def render(self,
               r,
               t,
               light_position,
               light_intensity,
               class_name,
               brightness_k=0,
               r_type='quat'):
        '''
        :param r: 
        :param t: 
        :param light_position: 
        :param light_intensity: 
        :param brightness_k: choose which brightness in __init__ 
        :param r_type: 
        :return: 
        '''
        if r_type == 'quat':
            R = RT_transform.quat2mat(r)
        elif r_type == 'mat':
            R = r
        self.class_name = class_name
        self.brightness_k = brightness_k
        self.render_kernels[
            self.class_name][brightness_k]['u_view'] = self._get_view_mtx(
                R, t)
        self.render_kernels[
            self.class_name][brightness_k]['u_light_position'] = light_position
        self.render_kernels[self.class_name][brightness_k]['u_normal'] = \
                                    np.array(np.matrix(np.dot(
                                        self.render_kernels[self.class_name][brightness_k]['u_view'].reshape(4,4),
                                        self.render_kernels[self.class_name][brightness_k]['u_model'].reshape(4,4))).I.T)
        self.render_kernels[self.class_name][brightness_k][
            'u_light_intensity'] = light_intensity

        app.run(framecount=0, framerate=0)
        rgb_buffer = np.zeros((self.height, self.width, 4), dtype=np.float32)
        gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl.GL_FLOAT,
                        rgb_buffer)

        rgb_gl = np.copy(rgb_buffer)
        rgb_gl.shape = 480, 640, 4
        rgb_gl = rgb_gl[::-1, :]
        rgb_gl = np.round(rgb_gl[:, :, :3] * 255).astype(
            np.uint8)  # Convert to [0, 255]
        rgb_gl = rgb_gl[:, :, [2, 1, 0]]

        depth_buffer = np.zeros((self.height, self.width), dtype=np.float32)
        gl.glReadPixels(0, 0, self.width, self.height, gl.GL_DEPTH_COMPONENT,
                        gl.GL_FLOAT, depth_buffer)
        depth_gl = np.copy(depth_buffer)
        depth_gl.shape = 480, 640
        depth_gl = depth_gl[::-1, :]
        depth_bg = depth_gl == 1
        depth_gl = 2 * self.zFar * self.zNear / (self.zFar + self.zNear -
                                                 (self.zFar - self.zNear) *
                                                 (2 * depth_gl - 1))
        depth_gl[depth_bg] = 0
        return rgb_gl, depth_gl
    def render(self,
               r,
               t,
               light_position,
               light_intensity,
               brightness_k=0,
               r_type="quat"):
        """
        :param r:
        :param t:
        :param light_position:
        :param light_intensity:
        :param brightness_k: choose which brightness in __init__
        :param r_type:
        :return:
        """
        if r_type == "quat":
            R = quat2mat(r)
        elif r_type == "mat":
            R = r
        self.brightness_k = brightness_k
        self.render_kernels[brightness_k]["u_view"] = self._get_view_mtx(R, t)
        self.render_kernels[brightness_k]["u_light_position"] = light_position
        self.render_kernels[brightness_k]["u_normal"] = np.array(
            np.matrix(
                np.dot(
                    self.render_kernels[brightness_k]["u_view"].reshape(4, 4),
                    self.render_kernels[brightness_k]["u_model"].reshape(4, 4),
                )).I.T)
        self.render_kernels[brightness_k][
            "u_light_intensity"] = light_intensity

        app.run(framecount=0)
        rgb_buffer = np.zeros((self.height, self.width, 4), dtype=np.float32)
        gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl.GL_FLOAT,
                        rgb_buffer)

        rgb_gl = np.copy(rgb_buffer)
        rgb_gl.shape = 480, 640, 4
        rgb_gl = rgb_gl[::-1, :]
        rgb_gl = np.round(rgb_gl[:, :, :3] * 255).astype(
            np.uint8)  # Convert to [0, 255]
        bgr_gl = rgb_gl[:, :, [2, 1, 0]]

        depth_buffer = np.zeros((self.height, self.width), dtype=np.float32)
        gl.glReadPixels(0, 0, self.width, self.height, gl.GL_DEPTH_COMPONENT,
                        gl.GL_FLOAT, depth_buffer)
        depth_gl = np.copy(depth_buffer)
        depth_gl.shape = 480, 640
        depth_gl = depth_gl[::-1, :]
        depth_bg = depth_gl == 1
        depth_gl = 2 * self.zFar * self.zNear / (self.zFar + self.zNear -
                                                 (self.zFar - self.zNear) *
                                                 (2 * depth_gl - 1))
        depth_gl[depth_bg] = 0
        return bgr_gl, depth_gl
Example #15
0
    def on_draw(dt):
        nonlocal frame, x, y, z, theta, phi, gamma

        # Export screenshot
        gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                        gl.GL_UNSIGNED_BYTE, framebuffer)
        if frame > 2:  # Skip empty zero frame
            if (frame) % 3 == 0:
                pbar.update()
                gt.append(
                    [f'{(frame-3)//3:05d}.png', x, y, z, theta, phi, gamma])
                png.from_array(framebuffer, 'RGB').save(
                    root / 'images' / f'{(frame-3)//3:05d}.png')
            elif (frame) % 3 == 1:
                png.from_array(framebuffer, 'RGB').save(
                    root / 'no_rotation' / f'{(frame-4)//3:05d}.png')
            elif (frame) % 3 == 2:
                png.from_array(framebuffer, 'RGB').save(
                    root / 'no_translation' / f'{(frame-5)//3:05d}.png')

        if (frame - 1) % 3 == 0:
            theta = np.random.random_sample() * 360
            x, y = np.random.random_sample(2) * (max_xy - min_xy) + min_xy
            if not fixed_z:
                z = np.random.random_sample() * (max_z - min_z) + min_z
            if shape == 'cube':
                phi = np.random.random_sample() * 180
            if shape in shapes:
                phi = np.random.random_sample() * 180
                gamma = np.random.random_sample() * 360

        window.clear()

        # Fill cube
        gl.glDisable(gl.GL_BLEND)
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glEnable(gl.GL_POLYGON_OFFSET_FILL)
        cube['u_color'] = 1, 1, 1, 1
        cube.draw(gl.GL_TRIANGLES, I)

        # Rotate cube
        view = cube['u_view'].reshape(4, 4)
        model = np.eye(4, dtype=np.float32)
        if (frame - 1) % 3 != 1:
            glm.rotate(model, theta, 0, 0, 1)
            glm.rotate(model, phi, 0, 1, 0)
            glm.rotate(model, gamma, 1, 0, 0)

        # Translate cube
        if (frame - 1) % 3 != 2:
            glm.translate(model, x, y, z)

        cube['u_model'] = model
        cube['u_normal'] = np.array(np.matrix(np.dot(view, model)).I.T)

        frame += 1
Example #16
0
 def on_draw(dt):
     self.window.clear()
     gl.glDisable(gl.GL_BLEND)
     gl.glEnable(gl.GL_DEPTH_TEST)
     self.render_kernel_list[self.cls_idx].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)
Example #17
0
def draw_color(shape, vertex_buffers, index_buffers, mat_models, mat_views,
               mat_projs, ambient_weight, light_color, bg_color):
    assert (len(vertex_buffers) == len(index_buffers))
    assert (len(vertex_buffers) == len(mat_models))
    assert (len(vertex_buffers) == len(mat_views))
    assert (len(vertex_buffers) == len(mat_projs))

    program = gloo.Program(_color_vertex_code, _color_fragment_code)

    # Frame buffer object
    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()

    # OpenGL setup
    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glEnable(gl.GL_CULL_FACE)
    gl.glCullFace(gl.GL_BACK)  # Back-facing polygons will be culled
    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])

    for i in xrange(len(vertex_buffers)):
        vertex_buffer = vertex_buffers[i]
        index_buffer = index_buffers[i]
        mat_model = mat_models[i]
        mat_view = mat_views[i]
        mat_proj = mat_projs[i]

        program.bind(vertex_buffer)
        program['u_light_eye_pos'] = [0, 0, 0]
        program['light_color'] = [
            light_color[0], light_color[1], light_color[2]
        ]
        program['u_light_ambient_w'] = ambient_weight
        program['u_mv'] = _compute_model_view(mat_model, mat_view)
        program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view,
                                                    mat_proj)
        # Rendering
        program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    rgb = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, rgb)
    rgb.shape = shape[0], shape[1], 4
    rgb = rgb[::-1, :]
    rgb = np.round(rgb[:, :, :3] * 255).astype(np.uint8)  # Convert to [0, 255]

    fbo.deactivate()

    return rgb
Example #18
0
 def write_current_frame(self):
     gl.glReadPixels(
         0,
         0,
         self.window.width,
         self.window.height,
         gl.GL_RGB,
         gl.GL_UNSIGNED_BYTE,
         self.frame_buffer,
     )
     self.writer.write_frame(np.flipud(self.frame_buffer))
Example #19
0
def draw_label(shape, vertex_buffers, index_buffers, mat_models, mat_views,
               mat_projs, labels):
    assert (len(vertex_buffers) == len(index_buffers))
    assert (len(vertex_buffers) == len(mat_models))
    assert (len(vertex_buffers) == len(mat_views))
    assert (len(vertex_buffers) == len(mat_projs))
    assert (labels is not None)
    assert (len(vertex_buffers) == len(labels))

    program = gloo.Program(_label_vertex_code, _label_fragment_code)

    # Frame buffer object
    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()

    # OpenGL setup
    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glEnable(gl.GL_CULL_FACE)
    gl.glCullFace(gl.GL_BACK)  # Back-facing polygons will be culled
    gl.glClearColor(0.0, 0.0, 0.0, 0.0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    for i in range(len(vertex_buffers)):
        vertex_buffer = vertex_buffers[i]
        index_buffer = index_buffers[i]
        mat_model = mat_models[i]
        mat_view = mat_views[i]
        mat_proj = mat_projs[i]
        label = labels[i]
        program.bind(vertex_buffer)
        program['u_mv'] = _compute_model_view(mat_model, mat_view)
        # program['u_nm'] = compute_normal_matrix(model, view)
        program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view,
                                                    mat_proj)
        program['label'] = label
        # Rendering
        program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    label_map = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT,
                    label_map)
    label_map.shape = shape[0], shape[1], 4
    label_map = label_map[::-1, :]
    label_map = label_map[:, :, 0]
    fbo.deactivate()

    return label_map
        def on_draw(dt):
            window.clear()
            for program_object in self.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.programIdx1, self.programIdx2), my_texture)
Example #21
0
def on_draw(dt):
    window.clear()
    for shape in shapes:
        shape.draw(gl.GL_TRIANGLE_STRIP)

    for curve_loop in curves_loop:
        curve_loop.draw(gl.GL_LINE_LOOP)

    for curve in curves:
        curve.draw(gl.GL_LINES)

    gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                    gl.GL_UNSIGNED_BYTE, framebuffer)
    png.from_array(np.flipud(framebuffer), 'RGB').save('wardisland.png')
Example #22
0
    def on_draw(dt):
        nonlocal depth
        color_buf = np.zeros((imgsize, imgsize, 4),
                             np.float32).view(gloo.TextureFloat2D)
        depth_buf = np.zeros((imgsize, imgsize),
                             np.float32).view(gloo.DepthTexture)
        fbo = gloo.FrameBuffer(color=color_buf, depth=depth_buf)
        fbo.activate()

        window.clear()

        # Fill cube
        gl.glDisable(gl.GL_BLEND)
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glEnable(gl.GL_POLYGON_OFFSET_FILL)
        gl.glClearColor(0.0, 0.0, 0.0, 0.0)

        # Rotate cube
        model = np.eye(4, dtype=np.float32)

        # model = R_ @ model
        glm.rotate(model, R[0], 0, 0, 1)
        glm.rotate(model, R[1], 0, 1, 0)
        glm.rotate(model, R[2], 1, 0, 0)

        # Translate cube
        glm.translate(model, *t)

        cube['u_model'] = model
        # cube['u_normal'] = np.array(np.matrix(np.dot(view, model)).I.T)

        cube.draw(gl.GL_TRIANGLES, I)
        # depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
        # gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth)
        # depth.shape = shape[0], shape[1], 4
        # depth = depth[::-1, :]
        # depth = depth[:, :, 0] # Depth is saved in the first channel

        # Export screenshot
        gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                        gl.GL_FLOAT, depthbuffer)

        # png.from_array(np.floor((depthbuffer - 0) / depthbuffer.max() * 255).astype(np.uint8),
        #                'RGB').save('./images' +
        #                            f'depth{time.time()}.png')

        fbo.deactivate()
        fbo.delete()
        depth = depthbuffer.reshape((128, 128, 3))[::-1, :, 0]
Example #23
0
def draw_depth(shape, vertex_buffer, index_buffer, mat_model, mat_view,
               mat_proj):

    assert type(mat_view) is list, 'Requires list of arrays.'

    program = gloo.Program(_depth_vertex_code, _depth_fragment_code)
    program.bind(vertex_buffer)

    # Frame buffer object
    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()

    # OpenGL setup
    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glClearColor(0.0, 0.0, 0.0, 0.0)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glViewport(0, 0, shape[1], shape[0])

    for i in range(len(mat_view)):

        program['u_mv'] = _compute_model_view(mat_model, mat_view[i])
        program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view[i],
                                                    mat_proj)

        # Keep the back-face culling disabled because of objects which do not have
        # well-defined surface (e.g. the lamp from the dataset of Hinterstoisser)
        gl.glDisable(gl.GL_CULL_FACE)
        # gl.glEnable(gl.GL_CULL_FACE)
        # gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled

        # Rendering
        program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, depth)
    depth.shape = shape[0], shape[1], 4
    depth = depth[::-1, :]
    depth = depth[:, :, 0]  # Depth is saved in the first channel

    fbo.deactivate()

    return depth
 def on_key_press(symbol, modifiers):
     global toggleStop, time
     # print('Key pressed (symbol=%s, modifiers=%s)' % (symbol, modifiers))
     if symbol == 83:
         gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                         gl.GL_UNSIGNED_BYTE, framebuffer)
         screen_filename = filename[:-3] + '_' + str(time) + '.png'
         png.from_array(framebuffer, 'RGB').save(screen_filename)
         print('... screen capture: {}'.format(screen_filename))
     if symbol == app.window.key.ENTER:
         reset(transform)
     if symbol == app.window.key.SPACE:
         if toggleStop:
             toggleStop = False
         else:
             toggleStop = True
Example #25
0
 def on_draw(dt):
     if self.vertices is None:
         return
     gl.glEnable(gl.GL_DEPTH_TEST)
     self.framebuffer.activate()
     window.clear()
     self.mesh_shader.draw(gl.GL_TRIANGLES)
     if self.click is not None:
         gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1)
         r, g, b, a = gl.glReadPixels(self.click[0], self.click[1], 1,
                                      1, gl.GL_RGBA,
                                      gl.GL_UNSIGNED_BYTE)
         if type(r) is not int: r = ord(r)
         if type(g) is not int: g = ord(g)
         if type(b) is not int: b = ord(b)
         index = b + 256 * g + 256 * 256 * r
         self.mesh_shader['select_id'] = index
         self.click = None
         if self.click_callback is None:
             print('Click callback function not defined')
         else:
             self.click_callback(index - 1)
     self.framebuffer.deactivate()
     window.clear()
     self.render()
Example #26
0
 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
 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
Example #28
0
        def on_draw(dt):
            window.clear()
            for program_object in self.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.programIdx1, self.programIdx2),
                my_texture)
Example #29
0
def draw_label(shape, vertex_buffer, index_buffer, texture, mat_model,
               mat_view, mat_proj, ambient_weight, bg_color, shading,
               inst_ids):

    assert type(mat_view) is list, 'Requires list of arrays.'

    program = gloo.Program(_label_vertex_code, _label_fragment_code)
    program.bind(vertex_buffer)

    # FBO
    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()

    # OpenGL setup
    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])

    print('inst ids = ' + str(inst_ids))
    for i in range(len(mat_view)):
        program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view[i],
                                                    mat_proj)
        program['inst_id'] = inst_ids[
            i] / 255.  # input instance-id is mapped to range [0,1]

        gl.glDisable(gl.GL_CULL_FACE)

        program.draw(gl.GL_TRIANGLES, index_buffer)

    label = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, label)
    label.shape = shape[0], shape[1], 4
    label = label[::-1, :]
    label = np.round(label[:, :, 0] * 255).astype(
        np.uint8)  # Label is saved in the first channel

    fbo.deactivate()

    return label
Example #30
0
 def finish(self):
     im = gl.glReadPixels(0, 0, self.size[0], self.size[1], gl.GL_RGBA,
                          gl.GL_FLOAT)
     rgb = np.copy(np.frombuffer(
         im, np.float32)).reshape(self.shape +
                                  (4, ))[::-1, :]  # Read buffer and flip Y
     im = gl.glReadPixels(0, 0, self.size[0], self.size[1],
                          gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
     dep = np.copy(np.frombuffer(
         im, np.float32)).reshape(self.shape +
                                  (1, ))[::-1, :]  # Read buffer and flip Y
     # Convert z-buffer to depth map
     mult = (self.clip_near * self.clip_far) / (self.clip_near -
                                                self.clip_far)
     addi = self.clip_far / (self.clip_near - self.clip_far)
     bg = dep == 1
     dep = mult / (dep + addi)
     dep[bg] = 0
     return rgb, dep
Example #31
0
def on_draw(dt):

    framebuffer.activate()
    gl.glViewport(0, 0, w, h)
    window.clear()
    scene.draw(gl.GL_TRIANGLE_STRIP)
    framebuffer.deactivate()

    gl.glViewport(0, 0, zoom * w, zoom * h)
    window.clear()
    output.draw(gl.GL_TRIANGLE_STRIP)

    image = np.zeros((window.height, window.width * 3), dtype=np.uint8)
    gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                    gl.GL_UNSIGNED_BYTE, image)
    image[...] = image[::-1, :]
    # filename = "triangle-sdf-filled.png"
    filename = "triangle-sdf-outlined.png"
    png.from_array(image, 'RGB').save(filename)
Example #32
0
    def _draw_rgb(self, obj_id, mat_model, mat_view, mat_proj):
        """Renders an RGB image.

    :param obj_id: ID of the object model to render.
    :param mat_model: 4x4 ndarray with the model matrix.
    :param mat_view: 4x4 ndarray with the view matrix.
    :param mat_proj: 4x4 ndarray with the projection matrix.
    :return: HxWx3 ndarray with the rendered RGB image.
    """
        # Update the OpenGL program.
        program = self.rgb_programs[obj_id]
        program['u_light_eye_pos'] = [0, 0, 0]  # Camera origin.
        program['u_light_ambient_w'] = self.light_ambient_weight
        program['u_mv'] = _calc_model_view(mat_model, mat_view)
        program['u_nm'] = _calc_normal_matrix(mat_model, mat_view)
        program['u_mvp'] = _calc_model_view_proj(mat_model, mat_view, mat_proj)

        # OpenGL setup.
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glClearColor(self.bg_color[0], self.bg_color[1], self.bg_color[2],
                        self.bg_color[3])
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glViewport(0, 0, self.width, self.height)

        # Keep the back-face culling disabled because of objects which do not have
        # well-defined surface (e.g. the lamp from the lm dataset).
        gl.glDisable(gl.GL_CULL_FACE)

        # Rendering.
        program.draw(gl.GL_TRIANGLES, self.index_buffers[obj_id])

        # Get the content of the FBO texture.
        rgb = np.zeros((self.height, self.width, 4), dtype=np.float32)
        gl.glReadPixels(0, 0, self.width, self.height, gl.GL_RGBA, gl.GL_FLOAT,
                        rgb)
        rgb.shape = (self.height, self.width, 4)
        rgb = rgb[::-1, :]
        rgb = np.round(rgb[:, :, :3] * 255).astype(
            np.uint8)  # Convert to [0, 255].

        return rgb
Example #33
0
    def draw_depth(self, shape, vertex_buffer, index_buffer, texture,
                   mat_model, mat_view, mat_proj, ambient_weight, bg_color,
                   shading):
        self.program['u_mv'] = _compute_model_view(mat_model, mat_view)
        self.program['u_mvp'] = _compute_model_view_proj(
            mat_model, mat_view, mat_proj)

        gl.glViewport(0, 0, shape[1], shape[0])

        # Rendering
        self.program.draw(gl.GL_TRIANGLES, index_buffer)

        # Retrieve the contents of the FBO texture
        depth = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
        gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT,
                        depth)
        depth.shape = shape[0], shape[1], 4
        depth = depth[::-1, :]
        depth = depth[:, :, 0]  # Depth is saved in the first channel

        return depth
Example #34
0
 def read_depth_pixels(self, activate=True):
     if activate:
         with self.activate():
             return self.read_depth_pixels(activate=False)
     pixels = gl.glReadPixels(0,
                              0,
                              self.width,
                              self.height,
                              gl.GL_DEPTH_COMPONENT,
                              gl.GL_FLOAT,
                              outputType=np.float32)
     pixels = pixels.reshape((self.height, self.width, -1))
     return pixels
Example #35
0
def on_draw(dt):
    global framebuffer, offset_index

    offset_name = list(offsets.keys())[offset_index]
    offset = offsets[offset_name]

    gl.glViewport(0, 0, width, height)
    framebuffer_2.activate()
    window.clear()
    framebuffer_2.deactivate()

    for dx, dy in offset:
        framebuffer_1.activate()
        window.clear()
        scene["offset"] = (2 * dx - 1) / width, (2 * dy - 1) / height
        scene.draw(gl.GL_TRIANGLE_STRIP)
        # scene.draw(gl.GL_LINE_LOOP)
        framebuffer_1.deactivate()

        framebuffer_2.activate()
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE)
        gl.glBlendColor(0, 0, 0, 1 / len(offset))
        ssaa.draw(gl.GL_TRIANGLE_STRIP)
        gl.glDisable(gl.GL_BLEND)
        framebuffer_2.deactivate()

    gl.glViewport(0, 0, window.width, window.height)
    window.clear()
    final.draw(gl.GL_TRIANGLE_STRIP)

    gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                    gl.GL_UNSIGNED_BYTE, framebuffer)
    framebuffer[...] = framebuffer[::-1, :]
    # filename = "triangle-ssaa-outlined-%s.png" % offset_name
    filename = "triangle-ssaa-filled-%s.png" % offset_name
    png.from_array(framebuffer, 'RGB').save(filename)
    offset_index += 1
Example #36
0
def draw_color(shape, vertex_buffer, index_buffer, mat_model, mat_view, mat_proj,
               ambient_weight, bg_color):

    program = gloo.Program(_color_vertex_code, _color_fragment_code)
    program.bind(vertex_buffer)
    program['u_light_eye_pos'] = [0, 0, 0]
    program['u_light_ambient_w'] = ambient_weight
    program['u_mv'] = _compute_model_view(mat_model, mat_view)
    # program['u_nm'] = compute_normal_matrix(model, view)
    program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj)

    # Frame buffer object
    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()

    # OpenGL setup
    gl.glEnable(gl.GL_DEPTH_TEST)
    gl.glEnable(gl.GL_CULL_FACE)
    gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled
    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])

    # Rendering
    program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    rgb = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, rgb)
    rgb.shape = shape[0], shape[1], 4
    rgb = rgb[::-1, :]
    rgb = np.round(rgb[:, :, :3] * 255).astype(np.uint8) # Convert to [0, 255]

    fbo.deactivate()

    return rgb
Example #37
0
def on_draw(dt):
    window.clear()
    cones.draw(gl.GL_TRIANGLES, I)

    # Read back image
    RGB = np.zeros((window.height, window.width, 3), dtype=np.uint8)
    gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB,
                    gl.GL_UNSIGNED_BYTE, RGB)
    V = (RGB[..., 0] * 256 * 256 + RGB[..., 1] * 256 + RGB[..., 2]).ravel()

    # Get individual Voronoi cells as a list of flatten indices
    # This works because we took care of having unique colors
    # See also StackOverflow:
    #  "Get a list of all indices of repeated elements in a numpy array"
    idx_sort = np.argsort(V)
    sorted_V = V[idx_sort]
    _, idx_start = np.unique(sorted_V, return_index=True)
    regions = np.split(idx_sort, idx_start[1:])

    shape = window.height, window.width
    for i, region in enumerate(regions[1:]):
        Y, X = np.unravel_index(region, shape)
        C["translate"][i] = X.mean(), Y.mean()
        def on_key_press(symbol, modifiers):
            if symbol == 88:  # x
                self.view_orth_vector = np.array([self.radius, 0, 0])
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 89:  # y
                self.view_orth_vector = np.array([0, self.radius, 0])
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 90:  # z
                self.view_orth_vector = np.array([0, 0, self.radius])
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 70:  # f
                self.view_orth_vector = -self.view_orth_vector
                self.program['u_view'] = glm.translation(self.view_orth_vector[0], self.view_orth_vector[1],
                                                         self.view_orth_vector[2])
            elif symbol == 80:  # p
                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))
Example #39
0
def on_draw(dt):
    gl.glEnable(gl.GL_DEPTH_TEST)

    framebuffer.activate()
    window.clear()
    program.draw(gl.GL_POINTS)
    if mouse is not None:
        gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1, gl.GL_FRONT)
        r,g,b,a = gl.glReadPixels(mouse[0],mouse[1],1,1, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE)
        index = ord(b) + 256*ord(g) + 256*256*ord(r)
        if index < len(program):
            program["bg_color"][index] = 0,0,0,1
    framebuffer.deactivate()
    gl.glDisable(gl.GL_DEPTH_TEST)
    quad.draw(gl.GL_TRIANGLE_STRIP)
Example #40
0
def on_draw(dt):
    global index
    framebuffer.activate()
    window.clear()
    polys.draw(), paths.draw()
    if mouse is not None:
        gl.glReadBuffer(gl.GL_COLOR_ATTACHMENT1, gl.GL_FRONT)
        r,g,b,a = gl.glReadPixels(mouse[0],mouse[1], 1, 1, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE)
        if type(r) is not int: r = ord(r)
        if type(g) is not int: g = ord(g)
        if type(b) is not int: b = ord(b)
        new_index = b + 256*g + 256*256*r
        if -1 < new_index < len(polys):
            index = new_index
            polys["color"] = 1,1,1,0.75
            polys[index]["color"] = 1,1,1,0
    framebuffer.deactivate()
    quad.draw(gl.GL_TRIANGLE_STRIP)
def on_draw(dt):
    window.clear()
    gl.glReadPixels(0, 0, window.width, window.height,
                    gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer)
    png.from_array(framebuffer, 'RGB').save('screenshot'+ str(dt) +'.png')
Example #42
0
 def write_frame(writer):
     gl.glReadPixels(0, 0, window.width, window.height,
                     gl.GL_RGB, gl.GL_UNSIGNED_BYTE, fbuffer)
     writer.write_frame(np.flipud(fbuffer))
Example #43
0
def on_draw(dt):
    window.clear()
    gl.glReadPixels(0, 0, window.width, window.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, framebuffer)
    png.from_array(framebuffer, "RGB").save("screenshot.png")
Example #44
0
def run(clock=None, framerate=None, interactive=None,
        duration = sys.maxint, framecount = sys.maxint):
    """ Run the main loop

    Parameters
    ----------
    clock : Clock
        clock to use to run the app (gives the elementary tick)

    framerate : int
        frames per second

    duration : float
        Duration after which the app will be stopped

    framecount : int
        Number of frame to display before stopping.
    """
    global __running__

    clock = __init__(clock=clock, framerate=framerate, backend=__backend__)
    options = parser.get_options()

    if interactive is None:
        interactive = options.interactive

    writer = None
    if options.record:
        from glumpy.ext.ffmpeg_writer import FFMPEG_VideoWriter
        framerate = 60
        window = __backend__.windows()[0]
        width, height = window.width, window.height
        filename = "movie.mp4"
        writer = FFMPEG_VideoWriter(filename, (width, height), fps=framerate)
        fbuffer = np.zeros((height, width, 3), dtype=np.uint8)
        data = fbuffer.copy()
        log.info("Recording movie in '%s'" % filename)

    if interactive:
        # Set interactive python session
        os.environ['PYTHONINSPECT'] = '1'
        import readline
        readline.parse_and_bind("tab: complete")

        def run():
            while not stdin_ready():
                __backend__.process(clock.tick())
            return 0
        inputhook_manager.set_inputhook(run)

    else:
        __running__ = True
        count = len(__backend__.windows())
        while count and duration > 0 and framecount > 0 and __running__:
            dt = clock.tick()
            duration -= dt
            framecount -= 1
            count = __backend__.process(dt)

            # Record one frame (if there is writer available)
            if writer is not None:
                gl.glReadPixels(0, 0, window.width, window.height,
                                gl.GL_RGB, gl.GL_UNSIGNED_BYTE, fbuffer)
                data[...] = fbuffer[::-1,:,:]
                writer.write_frame(data)

        if writer is not None:
            writer.close()
Example #45
0
def draw_color(shape, vertex_buffer, index_buffer, texture, mat_model, mat_view,
               mat_proj, ambient_weight, bg_color, shading):

    # Set shader for the selected shading
    if shading == 'flat':
        color_fragment_code = _color_fragment_flat_code
    else: # 'phong'
        color_fragment_code = _color_fragment_phong_code

    program = gloo.Program(_color_vertex_code, color_fragment_code)
    program.bind(vertex_buffer)
    program['u_light_eye_pos'] = [0, 0, 0] # Camera origin
    program['u_light_ambient_w'] = ambient_weight
    program['u_mv'] = _compute_model_view(mat_model, mat_view)
    program['u_nm'] = _compute_normal_matrix(mat_model, mat_view)
    program['u_mvp'] = _compute_model_view_proj(mat_model, mat_view, mat_proj)
    if texture is not None:
        program['u_use_texture'] = int(True)
        program['u_texture'] = texture
    else:
        program['u_use_texture'] = int(False)
        program['u_texture'] = np.zeros((1, 1, 4), np.float32)

    # Frame buffer object
    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()

    # OpenGL setup
    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.glEnable(gl.GL_BLEND)
    # gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
    # gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST)
    # gl.glHint(gl.GL_POLYGON_SMOOTH_HINT, gl.GL_NICEST)
    # gl.glDisable(gl.GL_LINE_SMOOTH)
    # gl.glDisable(gl.GL_POLYGON_SMOOTH)
    # gl.glEnable(gl.GL_MULTISAMPLE)

    # Keep the back-face culling disabled because of objects which do not have
    # well-defined surface (e.g. the lamp from the dataset of Hinterstoisser)
    gl.glDisable(gl.GL_CULL_FACE)
    # gl.glEnable(gl.GL_CULL_FACE)
    # gl.glCullFace(gl.GL_BACK) # Back-facing polygons will be culled

    # Rendering
    program.draw(gl.GL_TRIANGLES, index_buffer)

    # Retrieve the contents of the FBO texture
    rgb = np.zeros((shape[0], shape[1], 4), dtype=np.float32)
    gl.glReadPixels(0, 0, shape[1], shape[0], gl.GL_RGBA, gl.GL_FLOAT, rgb)
    rgb.shape = shape[0], shape[1], 4
    rgb = rgb[::-1, :]
    rgb = np.round(rgb[:, :, :3] * 255).astype(np.uint8) # Convert to [0, 255]

    fbo.deactivate()

    return rgb
 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']