glGetAttribLocation导致无效操作

时间:2018-06-11 20:18:53

标签: python opengl pygame glsl pyopengl

我试图通过pyopengl和pygame使用python制作月球的模型/模拟。我在.obj文件中使用了月球的开源3d模型,并尝试将其合并到我的代码中。但由于某种原因,标签

会发生错误
  

OpenGL.error.GLError:GLError(err = 1282,description = b'invalid   operation',baseOperation = glGetAttribLocation,cArguments =(6,   b'position \ X00' )

我的实现分为不同的类(如OOP),但主要的类,即着色器,月亮类和我使用该程序的类如下所示。 OBJ加载程序直接来自官方的pygame网站。

顶点着色器

#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 newColor;
layout (location = 2) in vec2 verTexCoord;
out vec3 color;
out vec2 fragTexCoord;
// other methods like scaling, rotation
void main(){
    vec4 pos = vec4(position, 1.0);
    // apply translation, rotation and scaling 
    gl_Position = pos;
    fragTexCoord = verTexCoord;
    color = newColor;
}

片段着色器

#version 330
in vec3 color;
in vec2 fragCoord;
out vec4 outColor;
uniform sampler2D ourTexture;
void main(){
    outColor = texture2D(ourTexture, fragCoord);
}

月球

from objloader import *
class Moon(Shape):
    def __init__(self):
        Shape.__init__(self)
        self.obj = OBJ("Moon\moon.obj", swapyz=True)
        self.drawing_type = GL_POLYGON
        self.setup_shaders()
        self.setup_buffers()
    def setup_shaders(self):
        # Get and Compile the shaders here
        self.program = glCreateProgram()
        glAttachShader(self.program, vertexShader)
        glAttachShader(self.program, fragmentShader)
        glLinkProgram(self.program)

    def setup_buffers(self):
        self.vao = glGenVertexArrays(1)
        self.vbo = glGenBuffers(1)
        v = []
        # buildlist will give a list that contains the moons vertices including the texture coordinates. 
        v = self.buildList(self.obj)
        self.vertexes = np.stack(v)
        self.vertex_length = len(self.vertexes)
        glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
        glBufferData(GL_ARRAY_BUFFER, self.vertexes.nbytes, self.vertexes, GL_STATIC_DRAW)
        glBindVertexArray(self.vao)
        # This is the line the error occurs at.
        positionLocation = glGetAttribLocation(self.program, "position")

没有必要,但这里是Shape类和绘图文本所在的函数(它与Shape不同的类)。

class Shape():
    def __init__(self):
        self.vao = None
        self.vbo = None
        self.program = None
        self.vertexes = None
        self.drawing_type = None
        self.vertex_length = None

绘图功能

def draw(self):
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    for shape in self.shapes:
        glUseProgram(shape.program)
        glBindVertexArray(shape.vao)
        glDrawArrays(shape.drawing_type, 0, shape.vertex_length)
        glBindVertexArray(0)

1 个答案:

答案 0 :(得分:1)

glGetAttribLocation给出了无效的操作错误,因为程序对象self.program未正确链接。

程序未链接,因为顶点着色器的输出变量与片段着色器的输入变量不匹配。

顶点着色器:

out vec3 color;
out vec2 fragTexCoord;

片段着色器:

in vec3 color;
in vec2 fragCoord;


像这样更改片段着色器,以解决问题:

#version 330

in vec3 color;
in vec2 fragTexCoord; // "fragTexCoord" insted of "fragCoord"

out vec4 outColor;
uniform sampler2D ourTexture;
void main(){
    outColor = texture2D(ourTexture, fragTexCoord); 
}

注意,如果已成功链接程序对象,则可以glGetProgramiv( self.program, GL_LINK_STATUS )检查:

glLinkProgram( self.program )
if not glGetProgramiv( self.__prog, GL_LINK_STATUS ):
    print( 'link error:' )
    print( glGetProgramInfoLog( self.__prog ) )

为了完整起见,如果已成功编译着色器对象,则可以glGetShaderiv( shaderObj, GL_COMPILE_STATUS )检查:

glCompileShader( shaderObj )
if not glGetShaderiv( shaderObj, GL_COMPILE_STATUS ):
    print( 'compile error:' )
    print( glGetShaderInfoLog( shaderObj ) )
相关问题