glDrawArrays返回无效操作1282 - pyOpenGL

时间:2018-03-15 15:57:32

标签: python opengl pyopengl

我无法想象我的生活。我需要第二双眼睛......或者更好的大脑。我想要得到这个" Hello Triangle" python示例工作。我一直在从c教程中翻译它。但是,无论我做什么,我都会收到这个错误。

Traceback (most recent call last):
File ".../demo/tester.py", line 107, in <module>
  if __name__ == '__main__': main()
File ".../demo/tester.py", line 87, in main
  glDrawArrays(GL_TRIANGLES, 0, 3)
File ".../venv/lib/python3.6/site packages/OpenGL/platform/baseplatform.py", line 402, in __call__
  return self( *args, **named )
File ".../venv/lib/python3.6/site-packages/OpenGL/error.py", line 232, in glCheckError
  baseOperation = baseOperation,
OpenGL.error.GLError: GLError(
  err = 1282,
  description = b'invalid operation',
  baseOperation = glDrawArrays,
  cArguments = (GL_TRIANGLES, 0, 3)

我的代码如下。我在Mac上运行,因此您会注意到PC中可能不需要的一些东西。一切正常,直到glDrawArrays。我知道openGL for C与使用pyOpenGL的python之间的一些函数有点不同。我一直在交叉引用文档,以确保我没有尝试像Python中的C程序员那样编写。

try:
    from AppKit import NSApp, NSApplication
except:
    pass

import sys
import cyglfw3 as glfw
import numpy as np
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.arrays import vbo as glvbo



def main():
    glfw.Init()
    glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    glfw.WindowHint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)

    window = glfw.CreateWindow(800, 600, 'LearnOpenGL', None)
    if window is None:
        glfw.Terminate()
        exit()

    glfw.MakeContextCurrent(window)
    glfw.SetFramebufferSizeCallback(window, framebuffer_size_callback())

    # Setting up a vertex buffer
    verts = glvbo.VBO(
    np.array([[0.0, 0.5, 0.0], [0.5, -0.5, 0.0], [-0.5, -0.5, 0.0]], 'f')
)

    vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    glBufferData(GL_ARRAY_BUFFER, verts.size, verts, GL_STATIC_DRAW)

    vao = glGenVertexArrays(1)
    glBindVertexArray(vao)
    glEnableVertexAttribArray(vao)
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)

    vertex_shader = """
        #version 330 core
        in vec3 vp;
        void main() {
            gl_Position = vec4(vp, 1.0);
        }"""

    fragment_shader = """
        #version 330 core
        out vec4 frag_color;
        void main() {
            frag_color = vec4(0.5, 0.0, 0.5, 1.0);
        }"""

    vs = glCreateShader(GL_VERTEX_SHADER)
    glShaderSource(vs, vertex_shader)
    glCompileShader(vs)

    if glGetShaderiv(vs, GL_COMPILE_STATUS) != GL_TRUE:
        raise Exception("vertex shader did not compile")

    fs = glCreateShader(GL_FRAGMENT_SHADER)
    glShaderSource(fs, fragment_shader)
    glCompileShader(fs)

    if glGetShaderiv(fs, GL_COMPILE_STATUS) != GL_TRUE:
        raise Exception("fragment shader did not compile")

    sp = glCreateProgram()
    glAttachShader(sp, fs)
    glAttachShader(sp, vs)
    glLinkProgram(sp)

    if glGetProgramiv(sp, GL_LINK_STATUS) != GL_TRUE:
        raise Exception("program did not link")

    while not glfw.WindowShouldClose(window):

        processInput(window)

        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT)

        glUseProgram(sp)
        glBindVertexArray(vao)
        glDrawArrays(GL_TRIANGLES, 0, 3)    # This is where things happen

        glfw.PollEvents()
        glfw.SwapBuffers(window)

    glfw.Terminate()
    exit()

def framebuffer_size_callback():
    glViewport(0, 0, 800, 600)

def processInput(window):
    if glfw.GetKey(window, glfw.KEY_ESCAPE) == glfw.PRESS:
        glfw.SetWindowShouldClose(window, True)

if __name__ == '__main__': main()

我想在一天结束时做的就是绘制一个三角形:(

1 个答案:

答案 0 :(得分:2)

我确定有2个问题。

  1. 正如BDL所说,glEnableVertexAttribArray未正确设置。它需要一个索引而不是一个顶点数组对象。
  2. glBufferData大小不正确,因为需要以字节为单位提供大小。在线快速搜索显示(numpy.size * numpy.itemsize)返回数组的大小。
  3. 完成这两件事......我有一个三角形。