【问题标题】:pyOpenGL check for boolpyOpenGL 检查布尔值
【发布时间】:2017-01-22 21:02:42
【问题描述】:

这是我的源代码https://paste.fedoraproject.org/428184/89404314/

我得到的错误是这样的:

C:\Python27>python.exe wx_gl_vbo_001.py
Traceback (most recent call last):
  File "wx_gl_vbo_001.py", line 63, in <module>
    MyApp(redirect = False).MainLoop()
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 8628, in __init__
    self._BootstrapApp()
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 8196, in _BootstrapApp
    return _core_.PyApp__BootstrapApp(*args, **kwargs)
  File "wx_gl_vbo_001.py", line 60, in OnInit
    canvas = MyCanvas(frame)
  File "wx_gl_vbo_001.py", line 16, in __init__
    }""", GL_VERTEX_SHADER)
  File "C:\Python27\lib\site-packages\OpenGL\latebind.py", line 44, in __call__
    self._finalCall = self.finalise()
  File "C:\Python27\lib\site-packages\OpenGL\extensions.py", line 245, in finalise
    self.__name__,
OpenGL.error.NullFunctionError: Attempt to call an undefined alternate function (glCompileShader, glCompileShaderARB), check for bool(glCompileShader) before calling

dir(shaders) 自带这个功能:

 'compileProgram', 'compileShader', 'found', 'fragment_shader', 'geometry_shader4', 'get_program_binary', 'glAttachShader', 'glBindAttribLocation', 'glCompileShader', 'glCreateProgram', 'glCreateShader', 

【问题讨论】:

  • 还没有深入查看您的代码,但您似乎调用的是 shaders.CompileShader 而不是 shaders.compileShader。此外,看起来您编写的着色器实际上是 120 版着色器,因此请尝试切换着色器版本。据我所知,像 gl_ModelViewProjectionMatrix 这样的变量在现代版本的 OpenGL 中不再存在!
  • PyPI 和二进制文件出现了一些问题我遇到了另一个错误:尝试调用未定义的函数 glutInit,在调用之前检查 bool(glutInit)

标签: python-2.7 wxpython shader pyopengl


【解决方案1】:

很抱歉这么晚才回复您。这是一个使用 wxpython 工具包(我以前从未尝试过)使用 opengl 渲染三角形的演示。除了我在 cmets 中注意到的内容外,我看到您没有使用 vbo 数组的全部内容,并且代码中的某些内容(例如“self.haveInited”)是不必要的。这是一个使用 OpenGL 2.1 渲染 vbo 三角形的示例。

import wx
from wx import glcanvas
import sys
from OpenGL.GL import *
from OpenGL.arrays import vbo
from OpenGL.GL import shaders
import numpy as np

class MyCanvas(glcanvas.GLCanvas):
    def __init__(self, parent):
        glcanvas.GLCanvas.__init__(self, parent, -1)
        self.context = glcanvas.GLContext(self)
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        self.SetCurrent(self.context)
        self.InitGL()
        self.OnDraw()

    def InitGL(self):
        glClearColor(0, 0, 0, 1)
        VERTEX_SHADER = shaders.compileShader("""
        #version 120
        void main() 
        {
            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        }
        """, GL_VERTEX_SHADER)
        FRAGMENT_SHADER = shaders.compileShader("""
        #version 120
        void main() 
        {
            gl_FragColor = vec4(0, 1, 0, 1);
        }
        """, GL_FRAGMENT_SHADER)
        self.shader = shaders.compileProgram(VERTEX_SHADER,FRAGMENT_SHADER)
        self.vbo = vbo.VBO(np.array([
            [  0, 1, 0 ],
            [ -1,-1, 0 ],
            [  1,-1, 0 ],
        ],'f'))


    def OnDraw(self):
        self.OnSize()
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        shaders.glUseProgram(self.shader)
        self.vbo.bind()
        glEnableClientState(GL_VERTEX_ARRAY)
        glVertexPointerf(self.vbo)
        glDrawArrays(GL_TRIANGLES, 0, 9)
        self.vbo.unbind()
        glDisableClientState(GL_VERTEX_ARRAY)
        shaders.glUseProgram(0)
        self.SwapBuffers()

    def OnSize(self):
        size = self.size = self.GetClientSize()
        glViewport(0, 0, size.width, size.height)

class MyApp(wx.App):
    def __init__(self):
        wx.App.__init__(self)

    def OnInit(self):
        frame = wx.Frame(None, title="OpenGL Test", size=(400, 300))
        frame.Show(True)
        c = MyCanvas(frame)
        return True

app = MyApp()
app.MainLoop()

希望对你有帮助!

【讨论】:

  • 是的,您的代码运行良好,可以帮助我。我的代码很旧,我不知道在开发此源代码中使用的 python 模块时所做的所有更改。随意添加任何信息。谢谢。
猜你喜欢
  • 2022-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
  • 2020-01-19
相关资源
最近更新 更多