gpt4 book ai didi

python - Pygame + OpenGL - 如何在 glBegin() 之后绘制文本?

转载 作者:太空宇宙 更新时间:2023-11-03 17:51:36 25 4
gpt4 key购买 nike

我在 StackOverflow 上的某个地方找到了这种跨平台的绘制文本的方法:

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

问题是我在调用 glBegin(GL_QUADS) 后无法调用 drawText。如何创建一个矩形并使用文本内容对其进行纹理化,然后显示它,以便即使在 glBegin 之后也可以调用此 drawText

最佳答案

我发现我无法更改 glBegin 内部的当前纹理,因此我必须重新设计部分代码。下面是使用 pygame 创建纹理的示例,基于 Nehe 教程第 6 课的 Python 版本:

#!/usr/bin/env python

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

ESCAPE = '\033'
window = 0
texture = 0

A_TEX_NUMBER = None
B_TEX_NUMBER = None

def GenTextureForText(text):
font = pygame.font.Font(None, 64)
textSurface = font.render(text, True, (255,255,255,255),
(0,0,0,255))
ix, iy = textSurface.get_width(), textSurface.get_height()
image = pygame.image.tostring(textSurface, "RGBX", True)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
i = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, i)
glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
return i

def InitGL(Width, Height):
global A_TEX_NUMBER, B_TEX_NUMBER
pygame.init()
A_TEX_NUMBER = GenTextureForText("a")
B_TEX_NUMBER = GenTextureForText("b")
glEnable(GL_TEXTURE_2D)
glClearColor(0.0, 0.0, 0.0, 0.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
done = 1

def DrawGLScene():

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0.0,0.0,-10.0)
glBindTexture(GL_TEXTURE_2D, B_TEX_NUMBER)
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0)
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0)
glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0)
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0)
glEnd()
glutSwapBuffers()

def keyPressed(*args):
if args[0] == ESCAPE:
glutDestroyWindow(window)
sys.exit()

def main():
global window
glutInit("")
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(640, 480)
glutInitWindowPosition(0, 0)
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
glutDisplayFunc(DrawGLScene)
glutIdleFunc(DrawGLScene)
glutKeyboardFunc(keyPressed)
InitGL(640, 480)
glutMainLoop()
print "Hit ESC key to quit."
main()

关于python - Pygame + OpenGL - 如何在 glBegin() 之后绘制文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29015999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com