python-3.x 为什么glVertexAttribPointer在尝试使用pyOpenGL和glfw在屏幕上绘制一个点时抛出1282错误?

c9x0cxw0  于 2022-12-05  发布在  Python
关注(0)|答案(1)|浏览(155)

我已经将程序分成了三个不同的文件,但是我不明白为什么在第70行的glVertexAttribPointer上会出现错误。
main.py

import glfw
import Shaders
from OpenGL.GL import *
from OpenGL.GLUT import *
from Math_3d import Vector2f

class Window:
    def __init__(self, width: int, height: int, title: str):
        if not glfw.init():
            raise Exception("glfw can not be initialized")
        glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
        glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
        glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

        self._win = glfw.create_window(width, height, title, None, None)

        if not self._win:
            glfw.terminate()
            raise Exception("glfw window can not be created")

        glfw.set_window_pos(self._win, 400, 200)
        glfw.make_context_current(self._win)

    def createshaders(self):
        # Request program and shader slots from the GPU
        program = glCreateProgram()
        vertex = glCreateShader(GL_VERTEX_SHADER)
        fragment = glCreateShader(GL_FRAGMENT_SHADER)
        # Set shader sources
        glShaderSource(vertex, Shaders.vertex_code)
        glShaderSource(fragment, Shaders.fragment_code)
        # Compile shaders
        glCompileShader(vertex)
        glCompileShader(fragment)
        if not glGetShaderiv(vertex, GL_COMPILE_STATUS):
            report_shader = glGetShaderInfoLog(vertex)
            print(report_shader)
            raise RuntimeError("Vertex shader compilation error")

        if not glGetShaderiv(fragment, GL_COMPILE_STATUS):
            report_frag = glGetShaderInfoLog(fragment)
            print(report_frag)
            raise RuntimeError("Fragment shader compilation error")

        # Link objects to program
        glAttachShader(program, vertex)
        glAttachShader(program, fragment)
        glLinkProgram(program)

        if not glGetProgramiv(program, GL_LINK_STATUS):
            print(glGetProgramInfoLog(program))
            raise RuntimeError('Linking error')
        # Get rid of shaders
        glDetachShader(program, vertex)
        glDetachShader(program, fragment)
        # Make default program to run
        glUseProgram(program)

        # Vertex Buffer Object
        # Create point vertex data
        v2f_1 = Vector2f(0.0, 0.0)
        # Request a buffer slot from GPU
        buffer = glGenBuffers(1)
        # Make this buffer the default one
        glBindBuffer(GL_ARRAY_BUFFER, buffer)
        strides = v2f_1.data.strides[0]
        loc = glGetAttribLocation(program, 'position')
        glEnableVertexAttribArray(loc)
        glVertexAttribPointer(loc, 2, GL_FLOAT, False, strides, None)
        # glBufferData(GL_ARRAY_BUFFER, v2f_1, v2f_1, GL_DYNAMIC_DRAW)

    def renderscene(self):
        while not glfw.window_should_close(self._win):
            glfw.poll_events()
            glClear(GL_COLOR_BUFFER_BIT)
            glDrawArrays(GL_POINTS, 0, 1)
            glfw.swap_buffers(self._win)

        glfw.terminate()

if __name__ == '__main__':
    win = Window(1024, 768, "GLFW Window")
    win.createshaders()  # Create and initialize shaders and initialize Vertex Buffer Object
    win.renderscene()  # Swap buffer and render scene

Shaders.py

vertex_code = """
    attribute vec2 position;
    void main()
    {
        gl_Position = vec4(position, 0.0, 1.0);
    }   
    """

fragment_code = """
    void main() 
    { 
        gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); 
    }
    """

数学_3d.py

import numpy as np

class Vector2f:
    def __init__(self, x, y):
        self.data = np.array([x, y], dtype=np.float32)

if __name__ == '__main__':
    vec2 = Vector2f(0.0, 0.0)
    print(vec2.data)
    print(type(vec2.data.strides[0]))
    print(vec2.data.strides[0])

我试过调试第70行,但是在使用PyCharm时没有得到任何好的结果。对此有什么建议吗?最接近的答案是根据61491497和56957118我的目标。

ua4mk5z4

ua4mk5z41#

您使用的是核心配置文件OpenGL Contextglfw.OPENGL_CORE_PROFILE)。因此,您必须创建一个顶点数组对象:

class Window:
    # [...]

    def createshaders(self):
        # [...]

        v2f_1 = Vector2f(0.0, 0.0)
        # Request a buffer slot from GPU
        buffer = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, buffer)
        strides = v2f_1.data.strides[0]
        glBufferData(GL_ARRAY_BUFFER, v2f_1.data, GL_DYNAMIC_DRAW)
        
        vao = glGenVertexArrays(1)
        glBindVertexArray(vao)
        
        loc = glGetAttribLocation(program, 'position')
        glEnableVertexAttribArray(loc)
        glVertexAttribPointer(loc, 2, GL_FLOAT, False, strides, None)

此外,您需要更改背景颜色或片段颜色,因为您将无法在黑色背景上看到黑点。例如,红色:

gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);

相关问题