Python pygame+;opengl=显示文本
我需要在窗口中显示文本,我找到了这样的解决方案,但它不会绘制任何内容Python pygame+;opengl=显示文本,python,opengl,pygame,pyopengl,Python,Opengl,Pygame,Pyopengl,我需要在窗口中显示文本,我找到了这样的解决方案,但它不会绘制任何内容 def drawText(x,y,text): 位置=(x,y,0) font=pygame.font.SysFont('arial',64) textSurface=font.render(text,True,(255255,66255),(0,66,0255)) textData=pygame.image.tostring(textSurface,“RGBA”,True) glRasterPos3d(*位置) glDraw
def drawText(x,y,text):
位置=(x,y,0)
font=pygame.font.SysFont('arial',64)
textSurface=font.render(text,True,(255255,66255),(0,66,0255))
textData=pygame.image.tostring(textSurface,“RGBA”,True)
glRasterPos3d(*位置)
glDrawPixels(textSurface.get_width()、textSurface.get_height()、GL_RGBA、GL_UNSIGNED_BYTE、textData)
有没有办法只在pygame窗口中显示文本?使用代替。当当前模型视图和投影矩阵变换glRasterPos
的坐标时,glWindowPos
直接更新当前光栅位置的x和y坐标
另见
最简单的例子:
导入pygame
从pygame.locals导入*
从OpenGL.GL导入*
从OpenGL.GLU导入*
垂直度=((1,-1,-1),(1,1,-1),(-1,1,-1),(-1,-1,-1),
(1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1))
边=((0,1)、(0,3)、(0,4)、(2,1)、(2,3)、(2,7)、(6,3)、(6,4)、(6,7)、(5,1)、(5,4)、(5,7))
def drawCube():
全局边
glBegin(GLU线)
对于边中的边:
对于边中的顶点:
glVertex3fv(垂直[顶点])
格伦德()
def drawText(x、y、text):
textSurface=font.render(text,True,(255,255,66,255),(0,66,0,255))
textData=pygame.image.tostring(textSurface,“RGBA”,True)
glWindowPos2d(x,y)
glDrawPixels(textSurface.get_width()、textSurface.get_height()、GL_RGBA、GL_UNSIGNED_BYTE、textData)
pygame.init()
clock=pygame.time.clock()
显示=(400300)
pygame.display.set_模式(显示,DOUBLEBUF | OPENGL)
font=pygame.font.SysFont('arial',64)
透视图(45,(显示[0]/显示[1]),0.1,50.0)
glTranslatef(0.0,0.0,-5)
运行=真
运行时:
时钟滴答(100)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
glRotatef(1,3,1,1)
glClear(GL_颜色_缓冲区_位| GL_深度_缓冲区_位)
drawCube()
drawText(140、120,“立方体”)
pygame.display.flip()
pygame.quit()
退出()
对于具有透明背景的文本,需要使用以下命令将文本表面转换为每像素格式:
textSurface=font.render(text,True,(255,255,66,255)).convert_alpha()
此外,您还必须启用:
glEnable(GLU混合)
glBlendFunc(GL_SRC_ALPHA,GL_ONE_减去GL_SRC_ALPHA)
最简单的例子:
导入pygame
从pygame.locals导入*
从OpenGL.GL导入*
从OpenGL.GLU导入*
垂直度=((1,-1,-1),(1,1,-1),(-1,1,-1),(-1,-1,-1),
(1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1))
边=((0,1)、(0,3)、(0,4)、(2,1)、(2,3)、(2,7)、(6,3)、(6,4)、(6,7)、(5,1)、(5,4)、(5,7))
def drawCube():
全局边
glBegin(GLU线)
对于边中的边:
对于边中的顶点:
glVertex3fv(垂直[顶点])
格伦德()
def drawText(x、y、text):
textSurface=font.render(text,True,(255,255,66,255)).convertu alpha()
textData=pygame.image.tostring(textSurface,“RGBA”,True)
glWindowPos2d(x,y)
glDrawPixels(textSurface.get_width()、textSurface.get_height()、GL_RGBA、GL_UNSIGNED_BYTE、textData)
pygame.init()
clock=pygame.time.clock()
显示=(400300)
pygame.display.set_模式(显示,DOUBLEBUF | OPENGL)
font=pygame.font.SysFont('arial',64)
glEnable(GL_混合)
glBlendFunc(GL_SRC_ALPHA,GL_ONE_减去GL_SRC_ALPHA)
透视图(45,(显示[0]/显示[1]),0.1,50.0)
glTranslatef(0.0,0.0,-5)
运行=真
运行时:
时钟滴答(100)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
glRotatef(1,3,1,1)
glClear(GL_颜色_缓冲区_位| GL_深度_缓冲区_位)
drawCube()
drawText(140、120,“立方体”)
pygame.display.flip()
pygame.quit()
退出()
非常感谢,它很有效!但我不能在设置中更改alpha通道,它显示任何值的不透明文本。。。我可以调整透明度吗?@我已经扩展了答案。