Statistiques
| Révision :

root / PyOpenGL-Demo / broken / NeHe / lesson44 / glFont.py

Historique | Voir | Annoter | Télécharger (4,57 ko)

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