配置pyglfw

时间:2016-01-06 11:55:15

标签: python opengl glfw

我正在尝试使用Python创建OpenGL上下文。我试图使用python绑定GLFW,但我无法让它们工作。我在GLFW主页的https://github.com/rougier/pyglfw找到了绑定。

运行测试程序时出现以下错误:

python HelloOpenGL.py
Traceback (most recent call last):
  File "HelloOpenGL.py", line 2, in <module>
    import glfw             #Windowing Toolkit - GLFW
  File "C:\<...>\glfw.py", line 60, in <module>
    raise OSError('GLFW library not found')
OSError: GLFW library not found

我怀疑我需要一个glfw dll(我可能错了)。我试过复制我用于C ++ GLFW的DLL,但我得到了同样的错误。我已经尝试使用GNU编译器编译的GLFW 3.1的32位和64位dll。我使用的是Windows 10 64位操作系统和Python 3.4。

我也遇到过这个问题:Configuring glfw for Python in Eclipse。答案特别无益,因为问题与安装pyglfw无关,而是设置其他依赖项。我最初使用pip来安装pyglfw,但它无法正常工作,python无法找到模块;我手动安装了pyglfw,它正在运行。

问题:有人可以提供设置pyglfw的说明吗?我一直找不到任何相关内容。我需要知道需要哪些依赖项才能使它工作。

以下是测试程序:

import OpenGL.GL as gl  #OpenGL
import glfw             #Windowing Toolkit - GLFW

glfw.init()

if (glfw.OpenWindow(800, 600, 5, 6, 5, 0, 8, 0, glfw.FULLSCREEN) != True):
    glfw.Terminate(); # calls glfwTerminate() and exits
glfw.SetWindowTitle("The GLFW Window");

2 个答案:

答案 0 :(得分:3)

我刚刚在几秒钟前发现了这一点,结果发现在64位系统上使用32位python,你需要将DLL放在C:\ Windows \ SysWOW64 中,然后python可以找到它。

答案 1 :(得分:1)

我打开了pyglfw模块。由于模块搜索GLFW DLL的方式,这是Windows系统上会出现的问题。模块使用ctypes.util.find_library()搜索库路径,_glfw = ctypes.WinDLL('glfw3') 搜索PATH环境变量中的目录,而不是工作目录。

我的解决方案是在pyglfw中对DLL进行硬编码。这可以使用以下代码完成:

# First if there is an environment variable pointing to the library
if 'GLFW_LIBRARY' in os.environ:
    if os.path.exists(os.environ['GLFW_LIBRARY']):
        _glfw_file = os.path.realpath(os.environ['GLFW_LIBRARY'])

# Else, try to find it
if _glfw_file is None:
    order = ['glfw', 'glfw3']
    for check in order:
        _glfw_file = ctypes.util.find_library(check)
        if _glfw_file is not None:
            break

# Else, we failed and exit
if _glfw_file is None:
    raise OSError('GLFW library not found')

# Load it
_glfw = ctypes.CDLL(_glfw_file)

这将加载glfw3.dll,只要它放在同一目录中即可。 (对于旧版本的GLFW,DLL是glfw.dll)

此代码应替换原始代码中的第45-53行:

import os

basepath = os.path.dirname(os.path.abspath(__file__))
dllspath = os.path.join(basepath, 'dlls')
os.environ['PATH'] = dllspath + os.pathsep + os.environ['PATH']

这个问题:find_library() in ctypes详细说明了在Windows上加载库的解决方案。

这概述了另一种解决方案,即在运行时设置搜索路径:

  

您可以在运行时动态地将DLL目录添加到PATH(in   与Linux加载程序在启动时缓存LD_LIBRARY_PATH相反。   例如,假设您的DLL依赖项位于&#34; dlls&#34;子目录   你的包裹。您可以按如下方式添加此目录:

{{1}}