为什么使用GTKGLAarea时PyOpenGL中未定义glGenVertexArrays

jtjikinw  于 2023-04-20  发布在  其他
关注(0)|答案(4)|浏览(134)

我有下面的测试代码下面,我试图得到一个基本的OpenGL Gtk3 GLArea的例子工作.
下面的错误是我目前的症结所在,从我所读到的内容来看,这可能意味着这些功能在上下文中不可用,但阅读有关GLArea的内容时,它似乎不会让你选择上下文,听起来它应该默认为正确的上下文。
这个问题可能是GLArea或PyOpenGL的问题,不幸的是,我能找到的所有示例目前都是C语言的,如果能在Python中找到一个基本的示例,那就太好了。
无论如何,花了很多时间试图找出这个问题,所以将是伟大的,如果任何人都可以帮助克服至少这个错误。

Traceback (most recent call last):
File "gtkglarea.py", line 91, in on_configure_event
self.vertex_array_object = glGenVertexArrays(1)
File "/usr/lib/python3/dist-packages/OpenGL/platform/baseplatform.py", line 407, in call
self.name, self.name,
OpenGL.error.NullFunctionError: Attempt to call an undefined function glGenVertexArrays, check for bool(glGenVertexArrays) before calling

例如,它也是一个要点https://gist.github.com/olymk2/5b3e49ac83130e580bd9983f2e5d49c3

#!/usr/bin/python
import os
import sys

from OpenGL.GLU import *
from OpenGL import GLX
from OpenGL import GL as GL
from ctypes import *
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk

from OpenGL.arrays import vbo
from OpenGL.GL import shaders
from OpenGL.raw.GL.ARB.vertex_array_object import glGenVertexArrays, \
                                                  glBindVertexArray

from numpy import array
import numpy as np

VERTEX_SHADER = """
    #version 330
    in vec4 position;
    void main()
    {
        gl_Position = position;
    }"""

FRAGMENT_SHADER = """
    #version 330
    out vec4 fragColor;
    void main()
    {
        fragColor = vec4(1.0, 0.0, 0.0, 1.0);
    }
    """

class application_gui:
    """Tutorial 01 Create and destroy a window"""
    # glwrap = gtkglarea()
    def __init__(self):
        self.window = Gtk.Window()
        self.canvas = Gtk.GLArea()
        self.canvas.set_required_version(3, 3)
        self.test_features()

        self.vertices = [
            0.6,  0.6, 0.0, 1.0,
            -0.6,  0.6, 0.0, 1.0,
            0.0, -0.6, 0.0, 1.0]

        self.vertices = np.array(self.vertices, dtype=np.float32)

        self.canvas.connect('realize', self.on_configure_event)
        self.canvas.connect('render', self.on_draw)
        self.canvas.set_double_buffered(False)

        self.window.connect('delete_event', Gtk.main_quit)
        self.window.connect('destroy', lambda quit: Gtk.main_quit())

        self.window.add(self.canvas)
        self.window.show_all()

        self.on_configure_event(self.canvas)

    def test_features(self):
        print('Testing features')
        print('glGenVertexArrays Available %s' % bool(glGenVertexArrays))
        print('Alpha Available %s' % bool(self.canvas.get_has_alpha()))
        print('Depth buffer Available %s' % bool(self.canvas.get_has_depth_buffer()))


    def on_configure_event(self, widget):
        print('realize event')

        widget.make_current()
        # widget.attach_buffers()
        context = widget.get_context()

        print('is legacy context %s' % Gdk.GLContext.is_legacy(context))
        print('configure errors')
        print(widget.get_error())

        vs = shaders.compileShader(VERTEX_SHADER, GL.GL_VERTEX_SHADER)
        fs = shaders.compileShader(FRAGMENT_SHADER, GL.GL_FRAGMENT_SHADER)
        self.shader = shaders.compileProgram(vs, fs)

        self.vertex_array_object = glGenVertexArrays(1)
        GL.glBindVertexArray( self.vertex_array_object )

        # Generate buffers to hold our vertices
        self.vertex_buffer = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vertex_buffer)

        # Get the position of the 'position' in parameter of our shader and bind it.
        self.position = GL.glGetAttribLocation(self.shader, 'position')
        GL.glEnableVertexAttribArray(self.position)

        # Describe the position data layout in the buffer
        GL.glVertexAttribPointer(self.position, 4, GL.GL_FLOAT, False, 0, ctypes.c_void_p(0))

        # Send the data over to the buffer
        GL.glBufferData(GL.GL_ARRAY_BUFFER, 48, self.vertices, GL.GL_STATIC_DRAW)

        # Unbind the VAO first (Important)
        GL.glBindVertexArray( 0 )

        # Unbind other stuff
        GL.glDisableVertexAttribArray(self.position)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)

        print('errors')
        print(widget.get_error())

        return True

    def on_draw(self, widget, *args):
        print('render event')
        print(widget.get_error())
        #Create the VBO

        widget.attach_buffers()

        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glUseProgram(self.shader)

        GL.glBindVertexArray( self.vertex_array_object )
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3)
        GL.glBindVertexArray( 0 )

        GL.glUseProgram(0)
        glFlush()
        return True

application = application_gui()
Gtk.main()
e4yzc0pl

e4yzc0pl1#

对我来说,这个问题似乎与在Wayland下运行有关。看起来如果PyOpenGL在Wayland下运行,并且有一个X11服务器运行,它将使用GLX而不是Wayland的EGL支持。关于GtkGLArea如何设置GL上下文的一些内容意味着GLX可以工作,但没有任何GL扩展,其中包括VAO。这似乎是PyOpenGL中的一个bug。
有两种方法可以解决这个问题:
1.设置PYOPENGL_PLATFORM,强制PyOpenGL使用EGL而不是GLX。
例如,在导入OpenGL之前:

if 'WAYLAND_DISPLAY' in os.environ and 'PYOPENGL_PLATFORM' not in os.environ:
    os.environ['PYOPENGL_PLATFORM'] = 'egl'

1.取消设置WAYLAND_DISPLAY,强制Gtk使用GLX而不是EGL。
例如,在导入Gtk之前:

if 'WAYLAND_DISPLAY' in os.environ:
    del os.environ['WAYLAND_DISPLAY']
yh2wf1be

yh2wf1be2#

下面是完整的工作示例,在@derhass的评论之后,我做了一些搜索,发现了Gdk.Screen,为什么我以前发现的示例中没有使用它,我不知道。
拼图中缺少的部分是这三行

screen = Gdk.Screen.get_default()
visual = Gdk.Screen.get_rgba_visual(screen)
self.window = Gtk.Window()
Gtk.Widget.set_visual(self.window, visual)

完整的工作示例,应该显示您的基本三角形在一个窗口,看起来像这样。

#!/usr/bin/python
# noqa: E402
import gi 
gi.require_version('Gtk', '3.0')
import numpy as np
from gi.repository import Gtk, Gdk
from OpenGL.GLU import *
from OpenGL import GL as GL

from OpenGL.GL import shaders
from OpenGL.raw.GL.ARB.vertex_array_object import glGenVertexArrays, \
                                                  glBindVertexArray

# from numpy import array

VERTEX_SHADER = """
    #version 330
    in vec4 position;
    void main()
    {
        gl_Position = position;
    }"""

FRAGMENT_SHADER = """
    #version 330
    out vec4 fragColor;
    void main()
    {
        fragColor = vec4(1.0, 0.0, 0.0, 1.0);
    }
    """

class application_gui:
    """Tutorial 01 Create and destroy a window"""
    # glwrap = gtkglarea()
    def __init__(self):
        screen = Gdk.Screen.get_default()
        visual = Gdk.Screen.get_rgba_visual(screen)

        print('is composite %s' % Gdk.Screen.is_composited(screen))

        self.window = Gtk.Window()
        Gtk.Widget.set_visual(self.window, visual)
        self.canvas = Gtk.GLArea()
        self.canvas.set_required_version(3, 3)
        self.test_features()

        self.vertices = [
            0.6,  0.6, 0.0, 1.0,
            -0.6,  0.6, 0.0, 1.0,
            0.0, -0.6, 0.0, 1.0]

        self.vertices = np.array(self.vertices, dtype=np.float32)

        self.canvas.connect('realize', self.on_configure_event)
        self.canvas.connect('render', self.on_draw)
        self.canvas.set_double_buffered(False)

        self.window.connect('delete_event', Gtk.main_quit)
        self.window.connect('destroy', lambda quit: Gtk.main_quit())

        self.window.add(self.canvas)
        self.window.show_all()

    def test_features(self):
        print('Testing features')
        print('glGenVertexArrays Available %s' % bool(glGenVertexArrays))
        print('Alpha Available %s' % bool(self.canvas.get_has_alpha()))
        print('Depth buffer Available %s' % bool(self.canvas.get_has_depth_buffer()))

    def on_configure_event(self, widget):
        print('realize event')
        widget.make_current()
        print(widget.get_error())

        vs = shaders.compileShader(VERTEX_SHADER, GL.GL_VERTEX_SHADER)
        fs = shaders.compileShader(FRAGMENT_SHADER, GL.GL_FRAGMENT_SHADER)
        self.shader = shaders.compileProgram(vs, fs)

        # Create a new Vertex Array Object
        self.vertex_array_object = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.vertex_array_object )

        # Generate a new array buffers for our vertices
        self.vertex_buffer = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vertex_buffer)

        # Get position variable form the shader and store
        self.position = GL.glGetAttribLocation(self.shader, 'position')
        GL.glEnableVertexAttribArray(self.position)

        # describe the data layout
        GL.glVertexAttribPointer(self.position, 4, GL.GL_FLOAT, False, 0, ctypes.c_void_p(0))

        # Copy data to the buffer
        GL.glBufferData(GL.GL_ARRAY_BUFFER, 48, self.vertices, GL.GL_STATIC_DRAW)

        # Unbind buffers once done
        GL.glBindVertexArray( 0 )
        GL.glDisableVertexAttribArray(self.position)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)

        return True

    def on_draw(self, widget, *args):
        print('render event')
        print(widget.get_error())

        # clear screen and select shader for drawing
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glUseProgram(self.shader)

        # bind and draw vertices
        GL.glBindVertexArray(self.vertex_array_object)
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3)
        GL.glBindVertexArray(0)

        GL.glUseProgram(0)
        GL.glFlush()
        return True

application = application_gui()
Gtk.main()
4dbbbstv

4dbbbstv3#

使用PyOpenGL和glfw时遇到此错误,修复程序为glfw.make_context_current(window)

bmp9r5qi

bmp9r5qi4#

我最近一直在开发一个使用pyopengl的软件。它一直工作得很好,直到我决定将我的个人电脑升级到SSD。
我不得不再次安装windows。但是在我的SSD上再次安装python之后,我的相同代码到现在为止运行得非常好,抛出了相同的错误(OpenGL.error.NullFunctionError:尝试调用未定义的函数glGenVertexArrays,请在调用前检查bool(glGenVertexArrays)。
天哪,我浪费了一整天的时间来找出问题所在。正如一些社区成员所建议的那样,甚至安装了不同版本的PyOpenGL和PyOpenGL加速,但都是徒劳的。我甚至更新了图形驱动程序(可能是NVIDIA)。
最后,解决问题的是Windows 10到Windows 11升级.所有其他故障排除对我没有用.所以我建议将您的Windows操作系统升级到最新.

相关问题