示例#1
0
    def __init__(self, window_width, window_height, samples, window_title):
        self.window_title = window_title
        assert glfw.Init(), 'Glfw Init failed!'
        glfw.WindowHint(glfw.SAMPLES, samples)
        self.windowID = glfw.CreateWindow(window_width, window_height,
                                          self.window_title, None)
        assert self.windowID, 'Could not create Window!'
        glfw.MakeContextCurrent(self.windowID)
        assert glInitBindlessTextureNV(), 'Bindless Textures not supported!'
        self.framebuf_width, self.framebuf_height = glfw.GetFramebufferSize(
            self.windowID)

        def framebuffer_size_callback(window, w, h):
            self.framebuf_width, self.framebuf_height = w, h

        glfw.SetFramebufferSizeCallback(self.windowID,
                                        framebuffer_size_callback)

        def key_callback(window, key, scancode, action, mode):
            if action == glfw.PRESS:
                if key == glfw.KEY_ESCAPE:
                    glfw.SetWindowShouldClose(window, True)

        glfw.SetKeyCallback(self.windowID, key_callback)

        self.previous_second = glfw.GetTime()
        self.frame_count = 0.0

        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
        glDisable(GL_STENCIL_TEST)
        glDisable(GL_BLEND)
        glDisable(GL_CULL_FACE)
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glEnable(GL_LINE_SMOOTH)
        glEnable(GL_POLYGON_SMOOTH)
def init_keyboard(window):
    glfw.SetKeyCallback(window, __on_keydown)
    theta = 0.0

    def process_keyboard_input(dt, camera_world_matrix, cue=None):
        nonlocal theta
        theta += KB_TURN_SPEED * dt * (key_state[glfw.KEY_LEFT] -
                                       key_state[glfw.KEY_RIGHT])
        sin, cos = np.sin(theta), np.cos(theta)
        camera_world_matrix[0, 0] = cos
        camera_world_matrix[0, 2] = -sin
        camera_world_matrix[2, 0] = sin
        camera_world_matrix[2, 2] = cos
        camera_world_matrix[3,
                            0] += dt * KB_MOVE_SPEED * (key_state[glfw.KEY_D] -
                                                        key_state[glfw.KEY_A])
        camera_world_matrix[3,
                            1] += dt * KB_MOVE_SPEED * (key_state[glfw.KEY_Q] -
                                                        key_state[glfw.KEY_Z])
        camera_world_matrix[3,
                            2] += dt * KB_MOVE_SPEED * (key_state[glfw.KEY_S] -
                                                        key_state[glfw.KEY_W])
        if cue is not None:
            fb = key_state[glfw.KEY_I] - key_state[glfw.KEY_K]
            lr = key_state[glfw.KEY_L] - key_state[glfw.KEY_J]
            ud = key_state[glfw.KEY_U] - key_state[glfw.KEY_M]
            cue.world_matrix[:3, :3] = cue.rotation.T
            cue.velocity = KB_CUE_MOVE_SPEED * (fb * cue.world_matrix[1, :3] +
                                                lr * cue.world_matrix[0, :3] +
                                                ud * cue.world_matrix[2, :3])
            cue.position += cue.velocity * dt

    return process_keyboard_input
示例#3
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()
示例#4
0
    def init(self):
        assert not self._initialized
        glfw.SetMouseButtonCallback(self._window, self._on_mouse_button)
        self._double_clicks.set_double_click_callback(
            self._on_mouse_double_click)
        glfw.SetScrollCallback(self._window, self._on_scroll)
        glfw.SetCursorPosCallback(self._window, self._on_mouse_move)
        glfw.SetKeyCallback(self._window, self._on_keyboard)
        glfw.SetDropCallback(self._window, self._on_drop)

        glfw.SetWindowSizeCallback(self._window, self._on_resize)
        glfw.SetWindowCloseCallback(self._window,
                                    lambda window: self._fps_limiter.stop())

        yield from self._gl_executor.init_gl_context()
        yield from self._renderer.init()
        self._initialized = True
示例#5
0
 def setup_loop(self, scene, camera):
     scene.set_camera(camera)
     scene.update_minmax()
     camera.compute_matrices()
     print("Starting rendering...")
     callbacks = EventCollection(scene, camera)
     # register key callbacks defined in
     # yt.visualization.volume_rendering.input_events
     callbacks.add_key_callback("close_window", "escape")
     callbacks.add_key_callback("zoomin", "w")
     callbacks.add_key_callback("zoomout", "s")
     callbacks.add_key_callback("closeup", "z")
     callbacks.add_key_callback("cmap_cycle", "c")
     callbacks.add_key_callback("reset", "r")
     callbacks.add_key_callback("camera_orto", "o")
     callbacks.add_key_callback("camera_proj", "p")
     callbacks.add_key_callback("shader_max", "1")
     callbacks.add_key_callback("shader_proj", "2")
     callbacks.add_key_callback("shader_test", "3")
     callbacks.add_key_callback("print_limits", "g")
     callbacks.add_key_callback("print_help", "h")
     callbacks.add_key_callback("debug_buffer", "d")
     callbacks.add_key_callback("cmap_max_up", "right_bracket")
     callbacks.add_key_callback("cmap_max_down", "left_bracket")
     callbacks.add_key_callback("cmap_min_up", "semicolon")
     callbacks.add_key_callback("cmap_min_down", "apostrophe")
     callbacks.add_key_callback("cmap_toggle_log", "l")
     mouse_callbacks = MouseRotation()
     callbacks.add_mouse_callback(
         mouse_callbacks.start_rotation, glfw.MOUSE_BUTTON_LEFT
     )
     callbacks.add_mouse_callback(
         mouse_callbacks.stop_rotation, glfw.MOUSE_BUTTON_LEFT, action="release"
     )
     callbacks.add_framebuffer_callback("framebuffer_size_callback")
     callbacks.add_render_callback(mouse_callbacks.do_rotation)
     glfw.SetFramebufferSizeCallback(self.window, callbacks.framebuffer_call)
     glfw.SetKeyCallback(self.window, callbacks.key_call)
     glfw.SetMouseButtonCallback(self.window, callbacks.mouse_call)
     callbacks.draw = True
     return callbacks
示例#6
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
示例#7
0
    def __init__(self):
        glfw.Init()  #Init glfw
        glfw.WindowHint(
            glfw.CONTEXT_VERSION_MAJOR,
            3)  #set openGL context version to 3.3 (the latest we can support)
        glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3)
        glfw.WindowHint(
            glfw.OPENGL_FORWARD_COMPAT, GL_TRUE
        )  #tell OSX we don't give a shit about compatibility and want the newer GLSL versions
        glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

        self.width, self.height = 1440, 900
        self.aspect = self.width / float(self.height)
        self.win = glfw.CreateWindow(self.width, self.height, "test")
        #self.win = glfw.CreateWindow(self.width, self.height, "test", glfw.GetPrimaryMonitor(), None)
        self.exitNow = False
        glfw.MakeContextCurrent(self.win)

        glViewport(0, 0, self.width, self.height)
        glEnable(GL_DEPTH_TEST)
        glClearColor(0.5, 0.5, 0.5, 1.0)

        #glfw.SetMouseButtonCallback(self.win, self.onMouseButton)
        glfw.SetKeyCallback(self.win, self.keyPressed)
        #glfw.SetWindowSizeCallback(self.win, self.onSize)

        self.objs = []
        self.callbacks = {'keyPressed': [], 'keyReleased': [], 'logic': []}
        self.objs.append(Box(0, 0))
        player = Player()
        self.objs.append(player)
        self.callbacks['keyPressed'].append(player.getCallback('keyPressed'))
        self.callbacks['keyReleased'].append(player.getCallback('keyReleased'))
        self.callbacks['logic'].append(player.getCallback('logic'))
        #self.callbacks['keyReleased'].append(player.getCallbacks(['keyReleased']))
        for obj in self.objs:
            obj.geomArray = numpy.array(obj.geom, numpy.float32)
            obj.vao = vertex_array_object.glGenVertexArrays(1)
            glBindVertexArray(obj.vao)
            obj.vBuff = glGenBuffers(1)
            glBindBuffer(GL_ARRAY_BUFFER, obj.vBuff)
            glBufferData(GL_ARRAY_BUFFER, 4 * len(obj.geom), obj.geomArray,
                         GL_DYNAMIC_DRAW)
            glEnableVertexAttribArray(0)
            glVertexAttribPointer(
                0, 3, GL_FLOAT, GL_FALSE, 0,
                None)  #set the size & type of the argument to the shader

            #compile shaders
            obj.vertShader = shaders.compileShader(obj.vertShaderString,
                                                   GL_VERTEX_SHADER)
            obj.fragShader = shaders.compileShader(obj.fragShaderString,
                                                   GL_FRAGMENT_SHADER)
            try:
                obj.shader = shaders.compileProgram(obj.vertShader,
                                                    obj.fragShader)
            except (GLError, RuntimeError) as err:
                print err

            glBindVertexArray(0)  #unbind our VAO

        self.colorOffset = 255 * 3
        self.OnInit()
示例#8
0
    opengl_logger.setLevel(logging.INFO)
    pil_logger = logging.getLogger('PIL')
    pil_logger.setLevel(logging.WARNING)
    text_drawer = TextRenderer()
    glfw.Init()
    w, h = 800, 600
    window = glfw.CreateWindow(w, h, os.path.basename(__file__))
    glfw.MakeContextCurrent(window)
    gl.glClearColor(0.1, 0.1, 0.2, 0.0)
    text_drawer.init_gl()

    def on_keydown(window, key, scancode, action, mods):
        if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
            glfw.SetWindowShouldClose(window, gl.GL_TRUE)

    glfw.SetKeyCallback(window, on_keydown)

    def on_resize(window, width, height):
        gl.glViewport(0, 0, width, height)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        text_drawer.set_screen_size((width, height))

    glfw.SetWindowSizeCallback(window, on_resize)
    on_resize(window, w, h)
    _logger.debug('starting render loop...')
    nframes = 0
    dt = float('inf')
    avg_fps = 0.0
    t = lt = st = glfw.GetTime()
    while not glfw.WindowShouldClose(window):
        glfw.PollEvents()
示例#9
0
def setup_controls(window=None, camera_world_matrix=None,
                   move_speed=None, turn_speed=0.5,
                   screen_capture_prefix='screen-capture'):
    if move_speed is None:
        move_speed = 1.5 * abs(camera_world_matrix[3,3] / camera_world_matrix[2,2])
    _logger.debug('move_speed = %s', move_speed)
    _logger.info('''

  KEYBOARD CONTROLS: W/S/A/D ----------- move Fwd/Bwd/Lft/Rgt
                     Q/Z --------------- move Up/Down
                     <-/-> (arrow keys)- turn Lft/Rgt

                     C ----------------- capture screenshot

                     Esc --------------- quit
''')
    camera_position = camera_world_matrix[3, :3]
    camera_rotation = camera_world_matrix[:3, :3]
    dposition = np.zeros(3, dtype=np.float32)
    rotation = np.eye(3, dtype=np.float32)
    key_state = defaultdict(bool)
    key_state_chg = defaultdict(bool)
    def on_keydown(window, key, scancode, action, mods):
        if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
            glfw.SetWindowShouldClose(window, gl.GL_TRUE)
        elif action == glfw.PRESS:
            if not key_state[key]:
                key_state_chg[key] = True
            else:
                key_state_chg[key] = False
            key_state[key] = True
        elif action == glfw.RELEASE:
            if key_state[key]:
                key_state_chg[key] = True
            else:
                key_state_chg[key] = False
            key_state[key] = False
    glfw.SetKeyCallback(window, on_keydown)
    def on_mousedown(window, button, action, mods):
        pass
    glfw.SetMouseButtonCallback(window, on_mousedown)

    _capture_wait = 0.0
    _num_captures = 0
    def process_input(dt):
        nonlocal _capture_wait, _num_captures
        glfw.PollEvents()
        if _capture_wait > 0:
            _capture_wait -= dt
        elif key_state[glfw.KEY_C]:
            _logger.info('''capturing screen...
            camera position: %s''', camera_position)
            save_screen(window, '.'.join([screen_capture_prefix, '%03d' % _num_captures, 'png']))
            _num_captures += 1
            _capture_wait = 0.25
        dposition[:] = 0.0
        if key_state[glfw.KEY_W]:
            dposition[2] -= dt * move_speed
        if key_state[glfw.KEY_S]:
            dposition[2] += dt * move_speed
        if key_state[glfw.KEY_A]:
            dposition[0] -= dt * move_speed
        if key_state[glfw.KEY_D]:
            dposition[0] += dt * move_speed
        if key_state[glfw.KEY_Q]:
            dposition[1] += dt * move_speed
        if key_state[glfw.KEY_Z]:
            dposition[1] -= dt * move_speed
        theta = 0.0
        if key_state[glfw.KEY_LEFT]:
            theta -= dt * turn_speed
        if key_state[glfw.KEY_RIGHT]:
            theta += dt * turn_speed
        if theta:
            rotation[0,0] = np.cos(theta)
            rotation[2,2] = rotation[0,0]
            rotation[0,2] = np.sin(theta)
            rotation[2,0] = -rotation[0,2]
            camera_rotation[...] = rotation.dot(camera_world_matrix[:3,:3])
        if dposition.any():
            camera_position[:] += camera_rotation.T.dot(dposition)
    return process_input
示例#10
0
 def add_key_callback(self, callback):
     if not hasattr(self, '_key_callbacks'):
         self._key_callbacks = []
         glfw.SetKeyCallback(self._window, self.__key_callback__)
     self._key_callbacks.append(callback)
示例#11
0
def view_gltf(gltf, uri_path, scene_name=None, openvr=False, window_size=None):
    if scene_name is None:
        scene_name = gltf['scene']
    if window_size is None:
        window_size = [800, 600]
    window = setup_glfw(width=window_size[0], height=window_size[1],
                        double_buffered=not openvr)
    def on_resize(window, width, height):
        window_size[0], window_size[1] = width, height
    glfw.SetWindowSizeCallback(window, on_resize)
    if openvr and OpenVRRenderer is not None:
        vr_renderer = OpenVRRenderer()
    # text_drawer = TextDrawer()

    gl.glClearColor(0.01, 0.01, 0.17, 1.0);

    shader_ids = gltfu.setup_shaders(gltf, uri_path)
    gltfu.setup_programs(gltf, shader_ids)
    gltfu.setup_textures(gltf, uri_path)
    gltfu.setup_buffers(gltf, uri_path)

    scene = gltf.scenes[scene_name]
    nodes = [gltf.nodes[n] for n in scene.nodes]
    for node in nodes:
        gltfu.update_world_matrices(node, gltf)

    camera_world_matrix = np.eye(4, dtype=np.float32)
    projection_matrix = np.array(matrix44.create_perspective_projection_matrix(np.rad2deg(55), window_size[0]/window_size[1], 0.1, 1000),
                                 dtype=np.float32)

    for node in nodes:
        if 'camera' in node:
            camera = gltf['cameras'][node['camera']]
            if 'perspective' in camera:
                perspective = camera['perspective']
                projection_matrix = np.array(matrix44.create_perspective_projection_matrix(np.rad2deg(perspective['yfov']), perspective['aspectRatio'],
                                                                                           perspective['znear'], perspective['zfar']),
                                             dtype=np.float32)
            elif 'orthographic' in camera:
                raise Exception('TODO')
            camera_world_matrix = node['world_matrix']
            break
    camera_position = camera_world_matrix[3, :3]
    camera_rotation = camera_world_matrix[:3, :3]
    dposition = np.zeros(3, dtype=np.float32)
    rotation = np.eye(3, dtype=np.float32)

    key_state = defaultdict(bool)

    def on_keydown(window, key, scancode, action, mods):
        if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
            glfw.SetWindowShouldClose(window, gl.GL_TRUE)
        elif action == glfw.PRESS:
            key_state[key] = True
        elif action == glfw.RELEASE:
            key_state[key] = False

    glfw.SetKeyCallback(window, on_keydown)

    def on_mousedown(window, button, action, mods):
        pass
    glfw.SetMouseButtonCallback(window, on_mousedown)

    move_speed = 2.0
    turn_speed = 0.5

    def process_input(dt):
        glfw.PollEvents()
        dposition[:] = 0.0
        if key_state[glfw.KEY_W]:
            dposition[2] -= dt * move_speed
        if key_state[glfw.KEY_S]:
            dposition[2] += dt * move_speed
        if key_state[glfw.KEY_A]:
            dposition[0] -= dt * move_speed
        if key_state[glfw.KEY_D]:
            dposition[0] += dt * move_speed
        if key_state[glfw.KEY_Q]:
            dposition[1] += dt * move_speed
        if key_state[glfw.KEY_Z]:
            dposition[1] -= dt * move_speed
        theta = 0.0
        if key_state[glfw.KEY_LEFT]:
            theta -= dt * turn_speed
        if key_state[glfw.KEY_RIGHT]:
            theta += dt * turn_speed
        rotation[0,0] = np.cos(theta)
        rotation[2,2] = rotation[0,0]
        rotation[0,2] = np.sin(theta)
        rotation[2,0] = -rotation[0,2]
        camera_rotation[...] = rotation.dot(camera_world_matrix[:3,:3])
        camera_position[:] += camera_rotation.T.dot(dposition)

    # sort nodes from front to back to avoid overdraw (assuming opaque objects):
    nodes = sorted(nodes, key=lambda node: np.linalg.norm(camera_position - node['world_matrix'][3, :3]))

    _logger.info('starting render loop...')
    sys.stdout.flush()
    gltfu.num_draw_calls = 0
    nframes = 0
    lt = glfw.GetTime()
    dt_max = 0.0
    while not glfw.WindowShouldClose(window):
        t = glfw.GetTime()
        dt = t - lt
        dt_max = max(dt, dt_max)
        lt = t
        process_input(dt)
        if openvr:
            vr_renderer.process_input()
            vr_renderer.render(gltf, nodes, window_size)
        else:
            gl.glViewport(0, 0, window_size[0], window_size[1])
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
            view_matrix = np.linalg.inv(camera_world_matrix)
            gltfu.set_material_state.current_material = None
            gltfu.set_technique_state.current_technique = None
            for node in nodes:
                gltfu.draw_node(node, gltf,
                                projection_matrix=projection_matrix,
                                view_matrix=view_matrix)
            # text_drawer.draw_text("%f" % dt, color=(1.0, 1.0, 0.0, 0.0),
            #                       view_matrix=view_matrix,
            #                       projection_matrix=projection_matrix)
        if nframes == 0:
            _logger.info("num draw calls per frame: %d", gltfu.num_draw_calls)
            sys.stdout.flush()
            gltfu.num_draw_calls = 0
            st = glfw.GetTime()
        nframes += 1
        glfw.SwapBuffers(window)
    _logger.info('FPS (avg): %f', ((nframes - 1) / (t - st)))
    _logger.info('MAX FRAME RENDER TIME: %f', dt_max)
    sys.stdout.flush()

    if openvr:
        vr_renderer.shutdown()
    glfw.DestroyWindow(window)
    glfw.Terminate()
assert glfw.Init(), 'Glfw Init failed!'
glfw.WindowHint(glfw.SAMPLES, samples)
window = glfw.CreateWindow(window_width, window_height, window_title, None)
assert window, 'Could not create Window!'
glfw.MakeContextCurrent(window)
assert glInitBindlessTextureNV(), 'Bindless Textures not supported!'
framebuf_width, framebuf_height = glfw.GetFramebufferSize(window)
def framebuffer_size_callback(window, w, h):
    global framebuf_width, framebuf_height
    framebuf_width, framebuf_height = w, h
glfw.SetFramebufferSizeCallback(window, framebuffer_size_callback)
def key_callback(window, key, scancode, action, mode):
    if action == glfw.PRESS:
        if key == glfw.KEY_ESCAPE:
            glfw.SetWindowShouldClose(window, True)
glfw.SetKeyCallback(window, key_callback)

previous_second = glfw.GetTime(); frame_count = 0.0
def update_fps_counter(window):
    global previous_second, frame_count
    current_second = glfw.GetTime()
    elapsed_seconds = current_second - previous_second
    if elapsed_seconds > 1.0:
        previous_second = current_second
        fps = float(frame_count) / float(elapsed_seconds)
        glfw.SetWindowTitle(window, '%s @ FPS: %.2f' % (window_title, fps))
        frame_count = 0.0
    frame_count += 1.0

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
glDisable(GL_STENCIL_TEST)
示例#13
0
    def __init__(self,
                 window_width,
                 window_height,
                 samples=1,
                 window_title="",
                 monitor=1,
                 show_at_center=True,
                 offscreen=False):
        self.window_title = window_title
        assert glfw.Init(), "Glfw Init failed!"
        glfw.WindowHint(glfw.SAMPLES, samples)
        if offscreen:
            glfw.WindowHint(glfw.VISIBLE, False)
        mon = glfw.GetMonitors()[monitor] if monitor != None else None
        self.windowID = glfw.CreateWindow(window_width, window_height,
                                          self.window_title, mon)
        assert self.windowID, "Could not create Window!"
        glfw.MakeContextCurrent(self.windowID)

        if not glInitBindlessTextureNV():
            raise RuntimeError("Bindless Textures not supported")

        self.framebuf_width, self.framebuf_height = glfw.GetFramebufferSize(
            self.windowID)
        self.framebuffer_size_callback = []

        def framebuffer_size_callback(window, w, h):
            self.framebuf_width, self.framebuf_height = w, h
            for callback in self.framebuffer_size_callback:
                callback(w, h)

        glfw.SetFramebufferSizeCallback(self.windowID,
                                        framebuffer_size_callback)

        self.key_callback = []

        def key_callback(window, key, scancode, action, mode):
            if action == glfw.PRESS:
                if key == glfw.KEY_ESCAPE:
                    glfw.SetWindowShouldClose(window, True)
            for callback in self.key_callback:
                callback(key, scancode, action, mode)

        glfw.SetKeyCallback(self.windowID, key_callback)

        self.mouse_callback = []

        def mouse_callback(window, xpos, ypos):
            for callback in self.mouse_callback:
                callback(xpos, ypos)

        glfw.SetCursorPosCallback(self.windowID, mouse_callback)

        self.mouse_button_callback = []

        def mouse_button_callback(window, button, action, mods):
            for callback in self.mouse_button_callback:
                callback(button, action, mods)

        glfw.SetMouseButtonCallback(self.windowID, mouse_button_callback)

        self.scroll_callback = []

        def scroll_callback(window, xoffset, yoffset):
            for callback in self.scroll_callback:
                callback(xoffset, yoffset)

        glfw.SetScrollCallback(self.windowID, scroll_callback)

        self.previous_second = glfw.GetTime()
        self.frame_count = 0.0

        if show_at_center:
            monitors = glfw.GetMonitors()
            assert monitor >= 0 and monitor < len(
                monitors), "Invalid monitor selected."
            vidMode = glfw.GetVideoMode(monitors[monitor])
            glfw.SetWindowPos(
                self.windowID,
                vidMode.width / 2 - self.framebuf_width / 2,
                vidMode.height / 2 - self.framebuf_height / 2,
            )
示例#14
0
        if key == glfw.KEY_RIGHT:
            cam_angle_xz-=0.4
            print("RIGHT")
        elif key == glfw.KEY_LEFT:
            cam_angle_xz+=0.4
            print("LEFT")
        elif key == glfw.KEY_UP:
            cam_angle_xy+=0.4
            print("UP")
        elif key == glfw.KEY_DOWN:
            cam_angle_xy-=0.4
            print("DOWN")
    camera.x=cam_radius * (math.cos(cam_angle_xz))
    camera.y=cam_radius * (math.sin(cam_angle_xy))
    camera.z=cam_radius * (math.sin(cam_angle_xz))


glfw.SetKeyCallback(window, key_event)
print(camera.x,camera.y,camera.z)

while not glfw.WindowShouldClose(window):
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    #LOOK AT
    view=glm.lookAt(camera, glm.vec3(0, 0, 0), glm.vec3(0, 1, 0))
    glize(newscene.rootnode)
    glfw.SwapBuffers(window)
    clock.tick(5)
    glfw.WaitEvents()

glfw.Terminate()
示例#15
0
 def key_callback(self, value):
     self._key_callback = value
     if self._win:
         glfw.SetKeyCallback(self._win, self._key_callback)
示例#16
0
    def __init__(self, width, height, title):
        glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 4)
        glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 1)
        glfw.WindowHint(glfw.OPENGL_FORWARD_COMPAT, 1)
        glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
        glfw.WindowHint(glfw.RESIZABLE, True)
        glfw.WindowHint(glfw.RED_BITS, 8)
        glfw.WindowHint(glfw.GREEN_BITS, 8)
        glfw.WindowHint(glfw.BLUE_BITS, 8)
        # glfw.WindowHint(glfw.ALPHA_BITS, 8)
        glfw.WindowHint(glfw.DEPTH_BITS, 24)
        glfw.WindowHint(glfw.STENCIL_BITS, 8)

        self.window = glfw.CreateWindow(width, height, title)
        self.framebufferSize = (width, height)

        f = 240
        pos = np.array([3, 3, 3])
        dir = np.array([-1, -1, -1])
        up = np.array([0, 0, 1])
        near = 0.2
        far = 100
        self.playerCamera = PlayerCamera(f, self.framebufferSize, pos, dir, up, near, far)
        self.currentCamera = self.playerCamera
        self.cameraLocked = True
        self.mainRender = None
        self.cairoSavePath = None

        def key_callback(window, key, scancode, action, mods):
            if (key == glfw.KEY_ESCAPE and action == glfw.PRESS):
                glfw.SetWindowShouldClose(window, True)

            if (action == glfw.PRESS and key == glfw.KEY_L):
                self.cameraLocked = not self.cameraLocked
                print 'cameraLocked:', self.cameraLocked

                glfw.SetInputMode(window, glfw.CURSOR, glfw.CURSOR_NORMAL if self.cameraLocked else glfw.CURSOR_DISABLED)

                # if not self.cameraLocked:
                # Ensure that locking/unlocking doesn't move the view:
                windowWidth, windowHeight = glfw.GetWindowSize(window)
                glfw.SetCursorPos(window, windowWidth / 2, windowHeight / 2)
                self.currentCamera.lastCursor = np.array(glfw.GetCursorPos(window), np.float32)
                self.currentCamera.lastUpdateTime = glfw.GetTime()

            if (action == glfw.PRESS and key == glfw.KEY_R):
                self.mainRender.renderCairo(self.playerCamera, self.cairoSavePath)

            pass
            # # print(
                # "keybrd: key=%s scancode=%s action=%s mods=%s" %
                # (key, scancode, action, mods))

        def char_callback(window, char):
            pass
            # print("unichr: char=%s" % char)

        def scroll_callback(window, off_x, off_y):
            pass
            # print("scroll: x=%s y=%s" % (off_x, off_y))

        def mouse_button_callback(window, button, action, mods):
            pass
            # print("button: button=%s action=%s mods=%s" % (button, action, mods))

        def cursor_enter_callback(window, status):
            pass
            # print("cursor: status=%s" % status)

        def cursor_pos_callback(window, pos_x, pos_y):
            pass
            # print("curpos: x=%s y=%s" % (pos_x, pos_y))

        def window_size_callback(window, wsz_w, wsz_h):
            pass
            # print("window: w=%s h=%s" % (wsz_w, wsz_h))

        def window_pos_callback(window, pos_x, pos_y):
            pass
            # print("window: x=%s y=%s" % (pos_x, pos_y))

        def window_close_callback(window):
            pass
            # print("should: %s" % self.should_close)

        def window_refresh_callback(window):
            pass
            # print("redraw")

        def window_focus_callback(window, status):
            pass
            # print("active: status=%s" % status)

        def window_iconify_callback(window, status):
            pass
            # print("hidden: status=%s" % status)

        def framebuffer_size_callback(window, fbs_x, fbs_y):
            print("buffer: x=%s y=%s" % (fbs_x, fbs_y))
            self.framebufferSize = (fbs_x, fbs_y)
            self.playerCamera.framebufferSize = (fbs_x, fbs_y)
            glViewport(0, 0, self.currentCamera.framebufferSize[0], self.currentCamera.framebufferSize[1])
            pass

        glfw.SetKeyCallback(self.window, key_callback)
        glfw.SetCharCallback(self.window, char_callback)
        glfw.SetScrollCallback(self.window, scroll_callback)
        glfw.SetMouseButtonCallback(self.window, mouse_button_callback)
        glfw.SetCursorEnterCallback(self.window, cursor_enter_callback)
        glfw.SetCursorPosCallback(self.window, cursor_pos_callback)
        glfw.SetWindowSizeCallback(self.window, window_size_callback)
        glfw.SetWindowPosCallback(self.window, window_pos_callback)
        glfw.SetWindowCloseCallback(self.window, window_close_callback)
        glfw.SetWindowRefreshCallback(self.window, window_refresh_callback)
        glfw.SetWindowFocusCallback(self.window, window_focus_callback)
        glfw.SetWindowIconifyCallback(self.window, window_iconify_callback)
        glfw.SetFramebufferSizeCallback(self.window, framebuffer_size_callback)