pygame + opengl =显示文本

ubbxdtey  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(196)

我需要在窗口中显示文本,我找到了这样的解决方案,但是它什么也画不出来

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

pdtvr36n

pdtvr36n1#

使用glWindowPos而不是glRasterPos。当glRasterPos的坐标由当前模型视图和投影矩阵转换时,glWindowPos直接更新当前栅格位置的x和y坐标。
另请参阅PyGame和OpenGL即时模式(传统OpenGL)-文本
最小示例:

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

相关问题