pyopengl如何渲染文字

发布于 2025-02-05 05:05:44 字数 376 浏览 2 评论 0原文

我使用此功能渲染文本,但是第一个参数将报告错误。我不知道如何使用它

”在此处输入图像描述”

glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, "text to render")
  ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type

I use this function to render text, but the first parameter will report an error. I don't know how to use it

enter image description here

glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, "text to render")
  ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱暴击 2025-02-12 05:05:45

glutbitmapcharacter的第二个参数是代表一个字符的积分数字。您必须使用 -loop的来编写字符串,然后用 ord

for c in text:
    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ord(c))

当使用glutbitmapstring时,该参数必须是二进制格式的字符串(b“ text to render” /代码>)。例如:

text = b"Hello World"
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, text)

encdode> encdode 字符串

text = "Hello World"
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, text.encode('ascii'))

最小示例:

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

def text(x, y, color, text):
    glColor3fv(color)
    glWindowPos2f(x, y)

    #for c in text:
    #    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ord(c))

    glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, text.encode('ascii'))

def display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    text(100, 100, (1, 0, 0), "Hello World!")
    glutSwapBuffers()
    glutPostRedisplay()

glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
glutInitWindowSize(400, 200)
glutCreateWindow(b"OpenGL Window")
glutDisplayFunc(display)
glutMainLoop()

The 2nd argument of glutBitmapCharacter is an integral number representing one character. You have to use a for-loop to write a string and convert each character with ord:

for c in text:
    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ord(c))

When using glutBitmapString, the argument must be a string in binary format (b"text to render"). e.g.:

text = b"Hello World"
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, text)

or encdode the string

text = "Hello World"
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, text.encode('ascii'))

Minimal example:

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

def text(x, y, color, text):
    glColor3fv(color)
    glWindowPos2f(x, y)

    #for c in text:
    #    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ord(c))

    glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, text.encode('ascii'))

def display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    text(100, 100, (1, 0, 0), "Hello World!")
    glutSwapBuffers()
    glutPostRedisplay()

glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
glutInitWindowSize(400, 200)
glutCreateWindow(b"OpenGL Window")
glutDisplayFunc(display)
glutMainLoop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文