我正在尝试制作一个在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):
try:
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()
glDisable(GL_LIGHTING)
glEnable(GL_TEXTURE_2D)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
D部分-添加纹理
def blit(texture, x, y):
glPushMatrix()
glTranslatef(x, y, 0.0)
glBindTexture(GL_TEXTURE_2D, texture.texID)
我查了一下,显然PyOpenGL 3在尝试渲染多个纹理时有这个未修复的错误。我使用python3.3,不想降级到2.x,而且我找不到用于python3的OpenGL 2。我做错什么了吗?
发布于 2021-05-22 19:51:45
OpenGL上下文是线程本地的。如果您试图从另一个线程调用OpenGL语句而不使OpenGL上下文当前,您将得到一个INVALID_OPERATION错误。
不幸的是,PyGame没有提供一个函数来显式地使OpenGL上下文当前。
另一种可能是您错过了在某个地方关闭一个glBegin
/glEndsequence with
glEnd`‘。
https://stackoverflow.com/questions/26642082
复制相似问题