问题
I want to display some simple text on the HUD of my 3D environment in OpenGL. All sources say that I should use the bitmap fonts included in GLUT, however, my program can't seem the find/load the fonts.
I have included all of the correct libraries and have double checked that the fonts.py file is definitely in the ...\Python37\...\OpenGL\GLUT\ directory, but when I type GLUT_BITMAP_TIMES_ROMAN_24
(or any of the other bitmap fonts), it is highlighted as an error in my code.
This is the function I call to display the text:
def text(self, x, y, r, g, b, text):
glColor3f(r, g, b)
glRasterPos2f(x, y)
for i in range(len(text)):
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, text[i])
And this is how I call the function:
epoch_str = "Epoch: {0}".format(self.epoch)
self.text(x, y, 1.0, 1.0, 1.0, epoch_str)
As mentioned, the GLUT_BITMAP_TIMES_ROMAN_24
is highlighted in my IDE (PyCharm) as an error, and I get the following error if I try to run it:
C:\Users\mickp\AppData\Local\Programs\Python\Python37\python.exe C:/Users/mickp/PycharmProjects/Test/U_Matrix.py
Traceback (most recent call last):
File "C:\Users\mickp\AppData\Local\Programs\Python\Python37\lib\site-packages\OpenGL\GLUT\special.py", line 130, in safeCall
return function( *args, **named )
File "C:/Users/mickp/PycharmProjects/Test/U_Matrix.py", line 142, in render
self.text(x, y, 1.0, 1.0, 1.0, epoch_str)
File "C:/Users/mickp/PycharmProjects/Test/U_Matrix.py", line 79, in text
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, text[i])
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type
GLUT Display callback <bound method U_MATRIX.render of <__main__.U_MATRIX object at 0x000002877FF45438>> with (),{} failed: returning None argument 2: <class 'TypeError'>: wrong type
Process finished with exit code 1
I really don't understand what the problem could be?
Edit: The above error has been fixed, it was a separate problem. The underlying problem with GLUT_BITMAP_TIMES_ROMAN_24
is still there. See:
IDE shows an error for GLUT_BITMAP_TIMES_ROMAN_24
Edit 2: Full Code (it's not too long):
Edit 3: Removed full code (it was too long). Adding the following fixed my problem:
# added before
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
gluOrtho2D(0.0, width, 0.0, height)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
# these two lines were here
epoch_str = "Epoch: {0}".format(self.epoch)
self.text(x+10, y+10, 0.0, 1.0, 0.0, epoch_str)
# added after
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glEnable(GL_TEXTURE_2D)
The text was actually just hidden as I had rendered it into the 3D environment behind an object, as opposed to in front of the 3D environment.
回答1:
The issue is not the 1st parameter GLUT_BITMAP_TIMES_ROMAN_24
, but it is the 2nd parameter text[i]
. The parameter to glutBitmapCharacter has to be an integral value (int
), which represents the character.
The chracter has to be converted to an ordinary number (int
) by ord:
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ord(text[i]))
or
for c in text:
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ord(c))
If the text doesn't appear on the screen, then reset the projection and model view matrix and draw the text at position (0, 0) for debug reasons:
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
self.text(0.0, 0.0, 1.0, 1.0, 1.0, epoch_str)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
来源:https://stackoverflow.com/questions/55957159/why-is-my-opengl-program-unable-to-load-the-glut-bitmap-fonts