pygame + opengl = 显示文字

pygame + opengl = display text

我需要在window中显示文字,我找到了这样的解决方案,但是没有绘制任何东西

def drawText(x, y, text):                                                
    position = (x, y, 0)
    font = pygame.font.SysFont('arial', 64)
    textSurface = font.render(text, True, (255,255,66,255), (0,66,0,255))
    textData = pygame.image.tostring(textSurface, "RGBA", True)
    glRasterPos3d(*position)
    glDrawPixels(textSurface.get_width(), textSurface.get_height(), GL_RGBA, GL_UNSIGNED_BYTE, textData)

有没有办法只在 pygame window 中显示文本?

使用glWindowPos instead of glRasterPosglRasterPos的坐标由当前模型视图和投影矩阵变换,而glWindowPos直接更新当前光栅位置的x和y坐标。

另见 PyGame and OpenGL immediate mode (Legacy OpenGL) - Text


最小示例:

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

verticies = ((1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, -1),
             (1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1))
edges = ((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():
    global edges
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()

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

display = (400, 300)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
font = pygame.font.SysFont('arial', 64)

gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -5)
run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    glRotatef(1, 3, 1, 1)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    drawCube()
    drawText(140, 120, "cube")
    pygame.display.flip()

pygame.quit()
exit()

对于透明背景的文本,您需要使用 convert_alpha():

将文本表面转换为每像素格式
textSurface = font.render(text, True, (255, 255, 66, 255)).convert_alpha()

此外,您必须启用 Blending:

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

最小示例:

replit.com/@Rabbid76/pygame-opengl-text

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

verticies = ((1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, -1),
             (1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1))
edges = ((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():
    global edges
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()

def drawText(x, y, text):                                                
    textSurface = font.render(text, True, (255, 255, 66, 255)).convert_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()

display = (400, 300)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
font = pygame.font.SysFont('arial', 64)

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -5)
run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    glRotatef(1, 3, 1, 1)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    drawCube()
    drawText(140, 120, "cube")
    pygame.display.flip()

pygame.quit()
exit()