【问题标题】:PyOpenGL - A tutorial about shaders that doesn't workPyOpenGL - 关于不起作用的着色器的教程
【发布时间】:2013-06-09 06:04:33
【问题描述】:

这是来自:http://pyopengl.sourceforge.net/context/tutorials/shader_1.xhtml的示例

它正在创建一个 vbo,对其进行绑定,并使用着色器运行它,但在此过程中的某个地方,它无法正常工作。我在互联网上搜索了很多,但没有找到任何关于我的问题的准确答案(我最好的选择是关于 StackOverflow 上的这个主题:Why is this tutorial example of a shader not displaying any geometry as it is supposed to? 但即使作者正在研究同一个教程,他也没有遇到同样的问题,自己解决了……)

from OpenGLContext import testingcontext
BaseContext = testingcontext.getInteractive()

from OpenGL.GL import *

from OpenGL.arrays import vbo

from OpenGLContext.arrays import *

from OpenGL.GL import shaders

class TestContext( BaseContext ):

    def OnInit( self ):
        VERTEX_SHADER = shaders.compileShader("""#version 330
        void main() {
                     gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
                     }""", GL_VERTEX_SHADER)

        FRAGMENT_SHADER = shaders.compileShader("""#version 330
        void main() {
                 gl_FragColor = vec4( 0, 3, 6, 1 );
                 }""", GL_FRAGMENT_SHADER)

        self.shader = shaders.compileProgram(VERTEX_SHADER,FRAGMENT_SHADER)

        self.vbo = vbo.VBO(
                        array( [
                            [  0, 1, 0 ],
                            [ -1,-1, 0 ],
                            [  1,-1, 0 ],
                            [  2,-1, 0 ],
                            [  4,-1, 0 ],
                            [  4, 1, 0 ],
                            [  2,-1, 0 ],
                            [  4, 1, 0 ],
                            [  2, 1, 0 ],
                        ],'f')
                      )

        def Render( self, mode):
            shaders.glUseProgram(self.shader)
        try:
            self.vbo.bind()

            try:
                glEnableClientState(GL_VERTEX_ARRAY);
                glVertexPointerf(self.vbo)
                glDrawArrays(GL_TRIANGLES, 0, 9)
            finally:
                self.vbo.unbind()
                glDisableClientState(GL_VERTEX_ARRAY);
        finally:
                shaders.glUseProgram(0)

if __name__ == "__main__":
    TestContext.ContextMainLoop()

从 windows 命令行运行这个程序给我这个错误:

Traceback(most recent call last):
    File "openGL-test.py', line 70, in <module>
        TestContext.ContextMainLoop()
    File "C:\Python27\lib\site-package\openglcontext-2.2.0a2-py2.7.egg\OpenGLContext\glutcontext.py", line 159, in ContextMainLoop
        render = cls( *args, **named)
    File "C:\Python27\lib\site-package\openglcontext-2.2.0a2-py2.7.egg\OpenGLContext\glutcontext.py", line 35, in __init__
        glutInitDisplayMode( self.DISPLAYMODE )
    File "C:\Python27\lib\site-package\OpenGL\platform\baseplatform.py", line 384, in __call__
        self.__name__, self.__name__,
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInitDisplayMode, check for bool(glutInitDisplayMode) before calling

知道这意味着什么吗?我必须准确地说,我两个月前开始学习 Python,所以我还是个新手,即使我在实习期间不得不做很多工作。 (嗬,这是我在 StackOverflow 上的第一个问题 :)

【问题讨论】:

    标签: python-2.7 shader glut pyopengl


    【解决方案1】:

    失败是因为您没有安装 GLUT。

    GLUT 是 GL Utility Toolkit,一个非常广泛使用的简单框架,用于创建在 MS Windows、Mac、Linux、SGI 上运行的 OpenGL 程序......所有函数和常量都有一个 glut 或 GLUT_ 前缀,而 glutInitDisplayMode 是通常是第一个被调用的函数。

    [删除安装说明]

    好的,您已完成安装,但仍然无法正常工作。这意味着当您安装了 GLUT 时,Python 程序无法加载 GLUT DLL。动态链接,天哪。

    查找 glut32.dll 的安装位置。

    单个程序的快速而肮脏的解决方案是将 glut DLL 复制到与程序本身相同的目录中。

    GLUT 是 32 位的(AFAIK,除非您自己构建),如果您有 64 位版本的 Windows 7,这可能会很棘手。网上的建议是,在 64 位 Windows 上,32 位 DLL 应该在 C 中: \Windows\SysWoW64\ 和 C:\Windows\System32\ 中的 64 位 DLL

    (说真的。我没有错误地交换两个目录名。)

    如果这不起作用,我就没有主意了。

    【讨论】:

    • 已安装的软件包是我检查的第一件事,并且按照教程序言中的要求安装了所有内容。顺便说一句,GLUT 包含在 Win32 和 Win64 二进制安装程序中:pyopengl.sourceforge.net/documentation/installation.html
    • 我会在今天下午试试这个,然后告诉你它是否有效;-) 无论如何感谢您的帮助
    • windows可以从here下载dll,解压后将该目录添加到PATH变量中。
    【解决方案2】:

    一些消息!抱歉耽搁了,这周不得不为学校研究一个巨大的数学理论。

    因此,正如 Hugh Fisher 所问,我检查了 glut32.dll 的安装位置。当我发现它没有安装在任何地方时,真是令人惊讶!所以我首先下载了 glut32.dll 并将其放入 c:\Windows\System32\,但由于它不起作用,我将其放入 Windows\SysWOW64\。而且......它的工作原理!所以解决方案似乎是:

    • 查找 glut32.dll 的位置(注意,System32 文件中有一个 glu32.dll,不一样!)
    • 如果不存在这样的文件,download it
    • 如果您有 32 位 Windows,请将下载的 glut32.dll 放在 c:\Windows\System32\ 中
    • 如果是 64 位 Windows,则将其放在 c:\Windows\SysWOW64\ 中

    感谢 Hugh Fisher 的解决方案。

    【讨论】:

    • 这解决了我遇到的一个问题。确实,按照 pyopengl.sourceforge.net/documentation/installation.html 中的文档,似乎安装了 GLUT,但找不到 glut32.dll。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    相关资源
    最近更新 更多