pyOpenGL 检查 bool

pyOpenGL check for bool

这是我的源代码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', 

抱歉这么晚才回复你。这是一个使用 wxpython 工具包(我以前从未尝试过)用 opengl 渲染三角形的演示。除了我在评论中注意到的内容之外,我还看到您没有使用 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()

希望对您有所帮助!