我正在尝试制作一个在python 3中使用pygame和OpenGL的游戏,但我仍然得到了同样的错误:
OpenGL.error.GLError: GLError( err = 1282, description = b'invalid operation', baseOperation = glClear, cArguments = (16640,) )
这是我的代码:
A部分-创建和配置
Surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.OPENGL) glViewport(0, 0, WIDTH, HEIGHT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-8.0, 8.0, -6.0, 6.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glShadeModel(GL_SMOOTH) glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glDisable(GL_DEPTH_TEST) glDisable(GL_LIGHTING) glDepthFunc(GL_LEQUAL) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) glEnable(GL_BLEND)
B部分-创造纹理
class Texture(): # simple texture class # designed for 32 bit png images (with alpha channel) def __init__(self,fileName): self.texID=0 self.LoadTexture(fileName) def LoadTexture(self,fileName): textureSurface = pygame.image.load(fileName).convert_alpha() textureData = pygame.image.tostring(textureSurface, "RGBA", True) self.w, self.h = textureSurface.get_size() self.texID=glGenTextures(1) glBindTexture(GL_TEXTURE_2D, self.texID) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR) glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData ) except Exception as E: print(E) print ("can't open the texture: %s"%(fileName)) def __del__(self): glDeleteTextures(self.texID) def get_width(self): return self.w def get_height(self): return self.h
C部分-预置屏幕
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity()