Statistics
| Revision:

root / PyOpenGL-Demo / NeHe / lesson43 / lesson43.py @ 1

History | View | Annotate | Download (6 kB)

1 1 equemene
# NeHe Tutorial Lesson: 43 - FreeType fonts in OpenGL
2 1 equemene
#
3 1 equemene
# Ported to PyOpenGL 2.0 by Brian Leair 18 Jan 2004
4 1 equemene
#
5 1 equemene
# This code was created by Jeff Molofee 2000
6 1 equemene
#
7 1 equemene
# The port was based on the PyOpenGL tutorials and from
8 1 equemene
# PyOpenGLContext (tests/glprint.py)
9 1 equemene
#
10 1 equemene
# If you've found this code useful, feel free to let me know
11 1 equemene
# at (Brian Leair telcom_sage@yahoo.com).
12 1 equemene
#
13 1 equemene
# See original source and C based tutorial at http://nehe.gamedev.net
14 1 equemene
#
15 1 equemene
# Note:
16 1 equemene
# -----
17 1 equemene
# This code is not an ideal example of Pythonic coding or use of OO
18 1 equemene
# techniques. It is a simple and direct exposition of how to use the
19 1 equemene
# Open GL API in Python via the PyOpenGL package. It also uses GLUT,
20 1 equemene
# a high quality platform independent library. Due to using these APIs,
21 1 equemene
# this code is more like a C program using procedural programming.
22 1 equemene
#
23 1 equemene
# To run this example you will need:
24 1 equemene
# Python         - www.python.org (v 2.3 as of 1/2004)
25 1 equemene
# PyOpenGL         - pyopengl.sourceforge.net (v 2.0.1.07 as of 1/2004)
26 1 equemene
# Numeric Python        - (v.22 of "numpy" as of 1/2004) numpy.sourceforge.net
27 1 equemene
# Python Image Library        - http://www.pythonware.com/products/pil/ (v1.1.4 or later)
28 1 equemene
#
29 1 equemene
# Make sure to get versions of Numeric, PyOpenGL, and PIL to match your
30 1 equemene
# version of python.
31 1 equemene
#
32 1 equemene
#
33 1 equemene
34 1 equemene
from OpenGL.GL import *
35 1 equemene
from OpenGL.GLUT import *
36 1 equemene
from OpenGL.GLU import *
37 1 equemene
38 1 equemene
# Imports specific to Lesson 43
39 1 equemene
import glFreeType
40 1 equemene
from math import cos
41 1 equemene
42 1 equemene
import sys
43 1 equemene
44 1 equemene
# Python 2.2 defines these directly
45 1 equemene
try:
46 1 equemene
        True
47 1 equemene
except NameError:
48 1 equemene
        True = 1==1
49 1 equemene
        False = 1==0
50 1 equemene
51 1 equemene
52 1 equemene
# Some api in the chain is translating the keystrokes to this octal string
53 1 equemene
# so instead of saying: ESCAPE = 27, we use the following.
54 1 equemene
ESCAPE = '\033'
55 1 equemene
56 1 equemene
# Number of the glut window.
57 1 equemene
window = 0
58 1 equemene
59 1 equemene
our_font = None
60 1 equemene
61 1 equemene
# A general OpenGL initialization function.  Sets all of the initial parameters.
62 1 equemene
def InitGL(Width, Height):                                # We call this right after our OpenGL window is created.
63 1 equemene
        global our_font
64 1 equemene
        glShadeModel(GL_SMOOTH)                                # Enables Smooth Color Shading
65 1 equemene
        glClearColor(0.0, 0.0, 0.0, 0.5)        # This Will Clear The Background Color To Black
66 1 equemene
        glClearDepth(1.0)                                        # Enables Clearing Of The Depth Buffer
67 1 equemene
        glEnable(GL_DEPTH_TEST)                                # Enables Depth Testing
68 1 equemene
        glEnable(GL_TEXTURE_2D)                                # Enables texture mapping
69 1 equemene
        glDepthFunc(GL_LEQUAL)                                # The Type Of Depth Test To Do
70 1 equemene
        glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Really Nice Perspective Calculations
71 1 equemene
72 1 equemene
        # Currently omitting the wgl based font. See lesson13.py for example use of wgl font.
73 1 equemene
        # FYI, the ttf font file "Test.ttf" in lesson43 is the typeface "Arial Black Italic".
74 1 equemene
        # our_font = glFreeType.font_data ("ARBLI___.ttf", 16)
75 1 equemene
        our_font = glFreeType.font_data ("Test.ttf", 16)
76 1 equemene
77 1 equemene
        return True
78 1 equemene
79 1 equemene
80 1 equemene
81 1 equemene
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
82 1 equemene
def ReSizeGLScene(Width, Height):
83 1 equemene
        if Height == 0:                                                # Prevent A Divide By Zero If The Window Is Too Small
84 1 equemene
                Height = 1
85 1 equemene
86 1 equemene
        glViewport(0, 0, Width, Height)                # Reset The Current Viewport And Perspective Transformation
87 1 equemene
        glMatrixMode(GL_PROJECTION)
88 1 equemene
        glLoadIdentity()
89 1 equemene
        # // field of view, aspect ratio, near and far
90 1 equemene
        # This will squash and stretch our objects as the window is resized.
91 1 equemene
        gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
92 1 equemene
93 1 equemene
        glMatrixMode(GL_MODELVIEW)
94 1 equemene
        glLoadIdentity()
95 1 equemene
96 1 equemene
cnt1 = 0
97 1 equemene
# The main drawing function.
98 1 equemene
def DrawGLScene():
99 1 equemene
        global cnt1
100 1 equemene
        global our_font
101 1 equemene
102 1 equemene
        # Clear The Screen And The Depth Buffer
103 1 equemene
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
104 1 equemene
        glLoadIdentity()                                        # Reset The View
105 1 equemene
        # Step back (away from objects)
106 1 equemene
        glTranslatef (0.0, 0.0, -1.0)
107 1 equemene
108 1 equemene
        # Currently - NYI - No WGL text
109 1 equemene
        # Blue Text
110 1 equemene
        # glColor3ub(0, 0, 0xff)
111 1 equemene
        #
112 1 equemene
        # // Position The WGL Text On The Screen
113 1 equemene
        # glRasterPos2f(-0.40f, 0.35f);
114 1 equemene
        # glPrint("Active WGL Bitmap Text With NeHe - %7.2f", cnt1);
115 1 equemene
116 1 equemene
        # Red Text
117 1 equemene
        glColor3ub (0xff, 0, 0)
118 1 equemene
119 1 equemene
        glPushMatrix ()
120 1 equemene
        glLoadIdentity ()
121 1 equemene
        # Spin the text, rotation around z axe == will appears as a 2d rotation of the text on our screen
122 1 equemene
        glRotatef (cnt1, 0, 0, 1)
123 1 equemene
        glScalef (1, 0.8 + 0.3* cos (cnt1/5), 1)
124 1 equemene
        glTranslatef (-180, 0, 0)
125 1 equemene
        our_font.glPrint (320, 240, "Active FreeType Text - %7.2f" % (cnt1))
126 1 equemene
        glPopMatrix ()
127 1 equemene
128 1 equemene
        # //Uncomment this to test out print's ability to handle newlines.
129 1 equemene
        # our_font.glPrint (320, 240, "Here\nthere\nbe\n\nnewlines %f\n." % (cnt1))
130 1 equemene
131 1 equemene
        cnt1 += 0.051
132 1 equemene
        # cnt2 += 0.005
133 1 equemene
134 1 equemene
        glutSwapBuffers()
135 1 equemene
        return
136 1 equemene
137 1 equemene
138 1 equemene
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
139 1 equemene
def keyPressed(*args):
140 1 equemene
        global window
141 1 equemene
        global our_font
142 1 equemene
        # If escape is pressed, kill everything.
143 1 equemene
        if args[0] == ESCAPE:
144 1 equemene
                our_font.release ()
145 1 equemene
                sys.exit()
146 1 equemene
147 1 equemene
def main():
148 1 equemene
        global window
149 1 equemene
        # pass arguments to init
150 1 equemene
        glutInit(sys.argv)
151 1 equemene
152 1 equemene
        # Select type of Display mode:
153 1 equemene
        #  Double buffer
154 1 equemene
        #  RGBA color
155 1 equemene
        # Alpha components supported
156 1 equemene
        # Depth buffer
157 1 equemene
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
158 1 equemene
159 1 equemene
        # get a 640 x 480 window
160 1 equemene
        glutInitWindowSize(640, 480)
161 1 equemene
162 1 equemene
        # the window starts at the upper left corner of the screen
163 1 equemene
        glutInitWindowPosition(0, 0)
164 1 equemene
165 1 equemene
        # Okay, like the C version we retain the window id to use when closing, but for those of you new
166 1 equemene
        # to Python (like myself), remember this assignment would make the variable local and not global
167 1 equemene
        # if it weren't for the global declaration at the start of main.
168 1 equemene
        window = glutCreateWindow("NeHe & Sven Olsen's TrueType Font Tutorial")
169 1 equemene
170 1 equemene
        # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
171 1 equemene
        # set the function pointer and invoke a function to actually register the callback, otherwise it
172 1 equemene
        # would be very much like the C version of the code.
173 1 equemene
        glutDisplayFunc(DrawGLScene)
174 1 equemene
175 1 equemene
        # Uncomment this line to get full screen.
176 1 equemene
        #glutFullScreen()
177 1 equemene
178 1 equemene
        # When we are doing nothing, redraw the scene.
179 1 equemene
        glutIdleFunc(DrawGLScene)
180 1 equemene
181 1 equemene
        # Register the function called when our window is resized.
182 1 equemene
        glutReshapeFunc(ReSizeGLScene)
183 1 equemene
184 1 equemene
        # Register the function called when the keyboard is pressed.
185 1 equemene
        glutKeyboardFunc(keyPressed)
186 1 equemene
187 1 equemene
        # Initialize our window.
188 1 equemene
        InitGL(640, 480)
189 1 equemene
190 1 equemene
        # Start Event Processing Engine
191 1 equemene
        glutMainLoop()
192 1 equemene
193 1 equemene
# Print message to console, and kick off the main to get it rolling.
194 1 equemene
if __name__ == "__main__":
195 1 equemene
        print "Hit ESC key to quit."
196 1 equemene
        main()