我试图通过pyopengl和pygame使用python建立月球的模型/模拟。我在.obj文件中使用了月亮的开源3D模型,并尝试将其合并到我的代码中。但是由于某些原因,标记为会发生错误


  OpenGL.error.GLError:GLError(err = 1282,description = b'invalid
  操作”,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)

最佳答案

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 ) )

关于python - glGetAttribLocation导致无效操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50805413/

10-11 22:46