【问题标题】:pygame + opengl = display textpygame + opengl = 显示文本
【发布时间】:2022-03-29 00:17:39
【问题描述】:

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

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 窗口中显示文本?

【问题讨论】:

    标签: python opengl pygame pyopengl


    【解决方案1】:

    使用glWindowPos 代替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()
    

    【讨论】:

      猜你喜欢
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多