root / PyOpenGL-Demo / NeHe / lesson44 / glFont.py @ 2
Historique | Voir | Annoter | Télécharger (4,59 ko)
| 1 |
# glFont.py -- SImple font class that uses a single texture represent 16x16 tiles
|
|---|---|
| 2 |
# for a font.
|
| 3 |
# // Code writen by: Vic Hollis 09/07/2003
|
| 4 |
# // I don't mind if you use this class in your own code. All I ask is
|
| 5 |
# // that you give me and Giuseppe D'Agata credit for it if you do.
|
| 6 |
# // And plug NeHe while your at it! :P Thanks go to Giuseppe D'Agata
|
| 7 |
# // for the code that this class is based off of. Thanks Enjoy.
|
| 8 |
# //////////////////////////////////////////////////////////////////////
|
| 9 |
# // glFont.cpp: implementation of the glFont class.
|
| 10 |
# //////////////////////////////////////////////////////////////////////
|
| 11 |
|
| 12 |
from OpenGL.GL import * |
| 13 |
|
| 14 |
class glFont: |
| 15 |
def __init__ (self): |
| 16 |
self.m_FontTexture = 0 |
| 17 |
self.m_ListBase = 0 |
| 18 |
|
| 19 |
def __del__ (self): |
| 20 |
self.release ()
|
| 21 |
return
|
| 22 |
|
| 23 |
def release (self): |
| 24 |
""" We've made a separate resoruce-deallocation method
|
| 25 |
so that the client can return system resources when the
|
| 26 |
want to explcitly. Python will eventually garbage collect,
|
| 27 |
once all refs to the object go away. This method
|
| 28 |
allows the client to retain the object, and yet free up the
|
| 29 |
gl resources.
|
| 30 |
"""
|
| 31 |
if (self.m_FontTexture != 0): |
| 32 |
glDeleteTextures (self.m_FontTexture)
|
| 33 |
if (self.m_ListBase != 0): |
| 34 |
glDeleteLists (self.m_ListBase, 256) |
| 35 |
return
|
| 36 |
|
| 37 |
def SetFontTexture (self, tex): |
| 38 |
if (tex != 0): # // If the texture is valid |
| 39 |
self.m_FontTexture = tex; # // Set the font texture |
| 40 |
else:
|
| 41 |
# Client should not pass an invalid texture.
|
| 42 |
raise RuntimeError, "SetFontTexture passed invalid texture (ID == 0)" |
| 43 |
return
|
| 44 |
|
| 45 |
def BuildFont (self, Scale): |
| 46 |
self.m_ListBase=glGenLists(256); # // Creating 256 Display Lists |
| 47 |
if (self.m_FontTexture != 0): |
| 48 |
glBindTexture(GL_TEXTURE_2D, self.m_FontTexture); # // Select Our Font Texture |
| 49 |
for loop in xrange (256): # // Loop Through All 256 Lists |
| 50 |
cx=float(loop%16)/16.0; # // X Position Of Current Character |
| 51 |
cy=float(loop/16)/16.0; # // Y Position Of Current Character |
| 52 |
|
| 53 |
glNewList(self.m_ListBase+loop,GL_COMPILE); # // Start Building A List |
| 54 |
# List start
|
| 55 |
glBegin(GL_QUADS); # // Use A Quad For Each Character
|
| 56 |
glTexCoord2f(cx, 1 - cy - 0.0625); # // Texture Coord (Bottom Left) |
| 57 |
glVertex2f(0,0); # // Vertex Coord (Bottom Left) |
| 58 |
glTexCoord2f(cx + 0.0625, 1 - cy - 0.0625); # // Texture Coord (Bottom Right) |
| 59 |
glVertex2f(16 * Scale,0); # // Vertex Coord (Bottom Right) |
| 60 |
glTexCoord2f(cx + 0.0625, 1 - cy); # // Texture Coord (Top Right) |
| 61 |
glVertex2f(16 * Scale, 16 * Scale); # // Vertex Coord (Top Right) |
| 62 |
glTexCoord2f(cx, 1 - cy); # // Texture Coord (Top Left) |
| 63 |
glVertex2f(0, 16 * Scale); # // Vertex Coord (Top Left) |
| 64 |
glEnd(); # // Done Building Our Quad (Character)
|
| 65 |
glTranslated(10*Scale,0,0); # // Move To The Right Of The Character |
| 66 |
glEndList(); # // Done Building The Display List
|
| 67 |
# List end
|
| 68 |
|
| 69 |
def glPrintf (self, x,y, set, text): |
| 70 |
glEnable(GL_TEXTURE_2D); # // Enable 2d Textures
|
| 71 |
glEnable(GL_BLEND); # // Enable Blending
|
| 72 |
glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR); |
| 73 |
glBindTexture(GL_TEXTURE_2D, self.m_FontTexture); # // Select Our Font Texture |
| 74 |
glDisable(GL_DEPTH_TEST); # // Disables Depth Testing
|
| 75 |
glMatrixMode(GL_PROJECTION); # // Select The Projection Matrix
|
| 76 |
glPushMatrix(); # // Store The Projection Matrix
|
| 77 |
glLoadIdentity(); # // Reset The Projection Matrix
|
| 78 |
glOrtho(0,self.m_WindowWidth,0,self.m_WindowHeight,-1,1); # // Set Up An Ortho Screen |
| 79 |
glMatrixMode(GL_MODELVIEW); # // Select The Modelview Matrix
|
| 80 |
glPushMatrix(); # // Store The Modelview Matrix
|
| 81 |
glLoadIdentity(); # // Reset The Modelview Matrix
|
| 82 |
glTranslated(x,y,0); # // Position The Text (0,0 - Bottom Left) |
| 83 |
glListBase(self.m_ListBase-32+(128*set)); # // Choose The Font Set (0 or 1) |
| 84 |
# glCallLists(len(text),GL_BYTE,text); # // Write The Text To The Screen
|
| 85 |
# function can figure out the count and TYP
|
| 86 |
glCallLists(text); # // Write The Text To The Screen
|
| 87 |
glMatrixMode(GL_PROJECTION); # // Select The Projection Matrix
|
| 88 |
glPopMatrix(); # // Restore The Old Projection Matrix
|
| 89 |
glMatrixMode(GL_MODELVIEW); # // Select The Modelview Matrix
|
| 90 |
glPopMatrix(); # // Restore The Old Projection Matrix
|
| 91 |
glEnable(GL_DEPTH_TEST); |
| 92 |
glDisable(GL_BLEND); |
| 93 |
glDisable(GL_TEXTURE_2D); |
| 94 |
return
|
| 95 |
|
| 96 |
def SetWindowSize (self, width, height): |
| 97 |
self.m_WindowWidth = width
|
| 98 |
self.m_WindowHeight = height
|
| 99 |
return
|
| 100 |
|
| 101 |
def GetTexture (self): |
| 102 |
return self.m_FontTexture |
| 103 |
|
| 104 |
def GetListBase (self): |
| 105 |
return self.m_ListBase |
| 106 |
|
| 107 |
|