Beispiel #1
0
def setup_glfw(title="poolvr.py 0.0.1",
               window_size=(800,600),
               double_buffered=False,
               multisample=4,
               fullscreen=False):
    if not glfw.Init():
        raise Exception('failed to initialize glfw')
    if not double_buffered:
        glfw.WindowHint(glfw.DOUBLEBUFFER, False)
        glfw.SwapInterval(0)
    if multisample:
        glfw.WindowHint(glfw.SAMPLES, multisample)
    width, height = window_size
    if fullscreen:
        window = glfw.CreateWindow(width, height, title, glfw.GetPrimaryMonitor())
    else:
        window = glfw.CreateWindow(width, height, title)
    if not window:
        glfw.Terminate()
        raise Exception('failed to create glfw window')
    glfw.MakeContextCurrent(window)
    _logger.info('GL_VERSION: %s', gl.glGetString(gl.GL_VERSION))
    renderer = OpenGLRenderer(window_size=(width, height), znear=0.1, zfar=1000)
    def on_resize(renderer, window, width, height):
        gl.glViewport(0, 0, width, height)
        renderer.window_size[:] = (width, height)
        renderer.update_projection_matrix()
    from functools import partial
    on_resize = partial(on_resize, renderer)
    glfw.SetWindowSizeCallback(window, on_resize)
    renderer.init_gl()
    on_resize(window, window_size[0], window_size[1])
    return window, renderer
Beispiel #2
0
def setup_glfw(width=800,
               height=600,
               double_buffered=False,
               title="poolvr.py 0.0.1",
               multisample=0):
    if not glfw.Init():
        raise Exception('failed to initialize glfw')
    if not double_buffered:
        glfw.WindowHint(glfw.DOUBLEBUFFER, False)
        glfw.SwapInterval(0)
    if multisample:
        glfw.WindowHint(glfw.SAMPLES, multisample)
    window = glfw.CreateWindow(width, height, title)
    if not window:
        glfw.Terminate()
        raise Exception('failed to create glfw window')
    glfw.MakeContextCurrent(window)
    _logger.info('GL_VERSION: %s', gl.glGetString(gl.GL_VERSION))
    renderer = OpenGLRenderer(window_size=(width, height),
                              znear=0.1,
                              zfar=1000)

    def on_resize(window, width, height):
        gl.glViewport(0, 0, width, height)
        renderer.window_size = (width, height)
        renderer.update_projection_matrix()

    glfw.SetWindowSizeCallback(window, on_resize)
    return window, renderer
def setup_glfw(width=800, height=600, double_buffered=False):
    if not glfw.Init():
        raise Exception('failed to initialize glfw')
    if not double_buffered:
        glfw.WindowHint(glfw.DOUBLEBUFFER, False)
        glfw.SwapInterval(0)
    window = glfw.CreateWindow(width, height, "gltfview")
    if not window:
        glfw.Terminate()
        raise Exception('failed to create glfw window')
    glfw.MakeContextCurrent(window)
    _logger.info('GL_VERSION: %s' % gl.glGetString(gl.GL_VERSION))
    return window
Beispiel #4
0
def main():
    appName = 'First App'

    glfw.Init()
    glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3)
    # glfw.WindowHint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
    # glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

    window = glfw.CreateWindow(800, 800, appName)
    glfw.SetWindowSizeCallback(window, window_size_callback)
    glfw.SetKeyCallback(window, key_callback)
    glfw.MakeContextCurrent(window)
    glfw.SwapInterval(1)

    try:
        initOpenGL()
    except Exception as e:
        print(e)
        exit(1)

    prevTime = time()
    frameCount = 0
    frameTimeSum = 0
    lastUpdateTime = prevTime
    while not glfw.WindowShouldClose(window):
        glfw.PollEvents()
        display(window)
        glfw.SwapBuffers(window)
        curTime = time()
        frameTime = (curTime - prevTime)
        # if frameTime != 0: print(1 / frameTime)
        sleep(0.016)

        if curTime - lastUpdateTime > 1:
            glfw.SetWindowTitle(
                window, '%s, FPS: %s' %
                (appName, str(round(frameCount / frameTimeSum))))
            frameCount = 0
            frameTimeSum = 0
            lastUpdateTime = curTime

        frameTimeSum += frameTime
        frameCount += 1
        prevTime = curTime

    glfw.DestroyWindow(window)
    glfw.Terminate()
Beispiel #5
0
 def init_gl(self):
     if self._is_initialized:
         return
     if not glfw.Init():
         raise RuntimeError("GLFW Initialization failed")
     glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 4)
     glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 1)
     glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
     glfw.WindowHint(glfw.DOUBLEBUFFER, False)
     glfw.SwapInterval(0)
     self.window = glfw.CreateWindow(self.renderer.window_size[0],
                                     self.renderer.window_size[1],
                                     self.title)
     if self.window is None:
         glfw.Terminate()
         raise RuntimeError("GLFW Window creation failed")
     glfw.SetKeyCallback(self.window, self.key_callback)
     glfw.MakeContextCurrent(self.window)
     if self.renderer is not None:
         self.renderer.init_gl()
     self._is_initialized = True
Beispiel #6
0
    def init(self):
        assert glfw.Init(), 'Glfw Init failed!'
        self.window = glfw.CreateWindow(self._window_size[0],
                                        self._window_size[1], self._title,
                                        None)
        glfw.WindowHint(glfw.SAMPLES, 4)
        glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
        glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3)
        glfw.WindowHint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
        glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

        glfw.MakeContextCurrent(self.window)
        glfw.SetInputMode(self.window, glfw.STICKY_KEYS, True)

        # Enable depth test
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_TEXTURE_3D)
        # Accept fragment if it closer to the camera than the former one
        glDepthFunc(GL_LESS)
        # disable vsync
        glfw.SwapInterval(0)
        self._init_shaders()
Beispiel #7
0
 def set_swap_interval(self, interval):
     glfw.SwapInterval(interval)
Beispiel #8
0
def show(game,
         title='poolvr.py   ***   GLViewer',
         window_size=(800, 600),
         gl_clear_color=(0.24, 0.18, 0.08, 0.0),
         before_frame_cb=None,
         after_frame_cb=None,
         double_buffered=True,
         playback_rate=1.0,
         screenshots_dir='',
         use_billboards=False):
    if not glfw.Init():
        raise Exception('failed to initialize glfw')
    if not double_buffered:
        glfw.WindowHint(glfw.DOUBLEBUFFER, False)
        glfw.SwapInterval(0)
    window = glfw.CreateWindow(window_size[0], window_size[1], title)
    if not window:
        raise Exception('failed to create glfw window')
    glfw.MakeContextCurrent(window)
    _logger.info('GL_VERSION: %s', gl.glGetString(gl.GL_VERSION))
    renderer = OpenGLRenderer(window_size=window_size, znear=0.1, zfar=1000)

    def on_resize(window, width, height):
        gl.glViewport(0, 0, width, height)
        renderer.window_size = (width, height)
        renderer.update_projection_matrix()

    glfw.SetWindowSizeCallback(window, on_resize)

    table = game.table
    physics = game.physics
    ball_meshes = table.setup_balls(game.ball_radius,
                                    game.ball_colors[:9],
                                    game.ball_positions,
                                    striped_balls=set(range(9,
                                                            game.num_balls)),
                                    use_bb_particles=use_billboards)
    ball_shadow_meshes = [mesh.shadow_mesh for mesh in ball_meshes]
    camera_world_matrix = renderer.camera_matrix
    camera_position = camera_world_matrix[3, :3]
    camera_position[1] = table.height + 0.19
    camera_position[2] = 0.183 * table.length
    cue = PoolCue()
    cue.position[1] = table.height + 0.1

    process_keyboard_input = init_keyboard(window)

    meshes = [table.mesh, floor_mesh, skybox_mesh] + ball_meshes  #+ [cue]
    for mesh in meshes:
        mesh.init_gl(force=True)

    ball_positions = game.ball_positions
    ball_quaternions = game.ball_quaternions
    ball_mesh_positions = [mesh.world_matrix[3, :3] for mesh in ball_meshes]
    ball_mesh_rotations = [mesh.world_matrix[:3, :3].T for mesh in ball_meshes]
    ball_shadow_mesh_positions = [
        mesh.world_matrix[3, :3] for mesh in ball_shadow_meshes
    ]
    gl.glViewport(0, 0, window_size[0], window_size[1])
    gl.glClearColor(*gl_clear_color)
    gl.glEnable(gl.GL_DEPTH_TEST)

    _logger.info('entering render loop...')
    stdout.flush()

    nframes = 0
    max_frame_time = 0.0
    lt = glfw.GetTime()
    t0 = physics.events[0].t
    t1 = physics.events[-1].t + min(2.0, physics.events[-1].T)
    pt = t0

    while not glfw.WindowShouldClose(window) and pt <= t1:

        t = glfw.GetTime()
        dt = t - lt
        lt = t
        glfw.PollEvents()
        process_keyboard_input(dt, camera_world_matrix, cue=cue)
        renderer.process_input()

        with renderer.render(meshes=meshes) as frame_data:
            camera_position = frame_data.get('camera_position',
                                             renderer.camera_position)
            physics.eval_positions(pt, out=ball_positions)
            physics.eval_quaternions(pt, out=ball_quaternions)
            ball_positions[
                ~physics.
                on_table] = camera_position  # hacky way to only show balls that are on table
            for i, pos in enumerate(ball_positions):
                ball_mesh_positions[i][:] = pos
                ball_shadow_mesh_positions[i][0::2] = pos[0::2]
            for i, quat in enumerate(ball_quaternions):
                set_matrix_from_quaternion(quat, ball_mesh_rotations[i])

        physics.step(dt)

        max_frame_time = max(max_frame_time, dt)
        if nframes == 0:
            st = glfw.GetTime()
        nframes += 1
        pt += dt * playback_rate
        glfw.SwapBuffers(window)

    _logger.info(
        '...exited render loop: average FPS: %f, maximum frame time: %f',
        (nframes - 1) / (t - st), max_frame_time)

    mWidth, mHeight = glfw.GetWindowSize(window)
    gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1)
    pixels = gl.glReadPixels(0, 0, mWidth, mHeight, gl.GL_RGB,
                             gl.GL_UNSIGNED_BYTE)
    pil_image = PIL.Image.frombytes('RGB', (mWidth, mHeight), pixels)
    pil_image = pil_image.transpose(PIL.Image.FLIP_TOP_BOTTOM)
    filename = title.replace(' ', '_') + '-screenshot.png'
    filepath = os.path.join(screenshots_dir, filename)
    pil_image.save(filepath)
    _logger.info('..saved screen capture to "%s"', filepath)

    try:
        renderer.shutdown()
        _logger.info('...shut down renderer')
    except Exception as err:
        _logger.error(err)

    glfw.DestroyWindow(window)
    glfw.Terminate()