Statistics
| Revision:

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

History | View | Annotate | Download (6 kB)

1
# NeHe Tutorial Lesson: 43 - FreeType fonts in OpenGL
2
#
3
# Ported to PyOpenGL 2.0 by Brian Leair 18 Jan 2004
4
#
5
# This code was created by Jeff Molofee 2000
6
#
7
# The port was based on the PyOpenGL tutorials and from 
8
# PyOpenGLContext (tests/glprint.py)
9
#
10
# If you've found this code useful, feel free to let me know 
11
# at (Brian Leair telcom_sage@yahoo.com).
12
#
13
# See original source and C based tutorial at http://nehe.gamedev.net
14
#
15
# Note:
16
# -----
17
# This code is not an ideal example of Pythonic coding or use of OO 
18
# techniques. It is a simple and direct exposition of how to use the 
19
# Open GL API in Python via the PyOpenGL package. It also uses GLUT, 
20
# a high quality platform independent library. Due to using these APIs, 
21
# this code is more like a C program using procedural programming.
22
#
23
# To run this example you will need:
24
# Python         - www.python.org (v 2.3 as of 1/2004)
25
# PyOpenGL         - pyopengl.sourceforge.net (v 2.0.1.07 as of 1/2004)
26
# Numeric Python        - (v.22 of "numpy" as of 1/2004) numpy.sourceforge.net
27
# Python Image Library        - http://www.pythonware.com/products/pil/ (v1.1.4 or later)
28
#
29
# Make sure to get versions of Numeric, PyOpenGL, and PIL to match your
30
# version of python.
31
#
32
#
33

    
34
from OpenGL.GL import *
35
from OpenGL.GLUT import *
36
from OpenGL.GLU import *
37

    
38
# Imports specific to Lesson 43
39
import glFreeType 
40
from math import cos
41

    
42
import sys
43

    
44
# Python 2.2 defines these directly
45
try:
46
        True
47
except NameError:
48
        True = 1==1
49
        False = 1==0
50

    
51

    
52
# Some api in the chain is translating the keystrokes to this octal string
53
# so instead of saying: ESCAPE = 27, we use the following.
54
ESCAPE = '\033'
55

    
56
# Number of the glut window.
57
window = 0
58

    
59
our_font = None
60

    
61
# A general OpenGL initialization function.  Sets all of the initial parameters. 
62
def InitGL(Width, Height):                                # We call this right after our OpenGL window is created.
63
        global our_font
64
        glShadeModel(GL_SMOOTH)                                # Enables Smooth Color Shading
65
        glClearColor(0.0, 0.0, 0.0, 0.5)        # This Will Clear The Background Color To Black
66
        glClearDepth(1.0)                                        # Enables Clearing Of The Depth Buffer
67
        glEnable(GL_DEPTH_TEST)                                # Enables Depth Testing
68
        glEnable(GL_TEXTURE_2D)                                # Enables texture mapping
69
        glDepthFunc(GL_LEQUAL)                                # The Type Of Depth Test To Do
70
        glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Really Nice Perspective Calculations
71

    
72
        # Currently omitting the wgl based font. See lesson13.py for example use of wgl font.
73
        # FYI, the ttf font file "Test.ttf" in lesson43 is the typeface "Arial Black Italic".
74
        # our_font = glFreeType.font_data ("ARBLI___.ttf", 16)
75
        our_font = glFreeType.font_data ("Test.ttf", 16)
76

    
77
        return True
78
        
79

    
80

    
81
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
82
def ReSizeGLScene(Width, Height):
83
        if Height == 0:                                                # Prevent A Divide By Zero If The Window Is Too Small 
84
                Height = 1
85

    
86
        glViewport(0, 0, Width, Height)                # Reset The Current Viewport And Perspective Transformation
87
        glMatrixMode(GL_PROJECTION)
88
        glLoadIdentity()
89
        # // field of view, aspect ratio, near and far
90
        # This will squash and stretch our objects as the window is resized.
91
        gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
92

    
93
        glMatrixMode(GL_MODELVIEW)
94
        glLoadIdentity()
95

    
96
cnt1 = 0
97
# The main drawing function. 
98
def DrawGLScene():
99
        global cnt1
100
        global our_font
101

    
102
        # Clear The Screen And The Depth Buffer
103
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
104
        glLoadIdentity()                                        # Reset The View 
105
        # Step back (away from objects)
106
        glTranslatef (0.0, 0.0, -1.0)
107

    
108
        # Currently - NYI - No WGL text
109
        # Blue Text
110
        # glColor3ub(0, 0, 0xff)
111
        #
112
        # // Position The WGL Text On The Screen
113
        # glRasterPos2f(-0.40f, 0.35f);
114
        # glPrint("Active WGL Bitmap Text With NeHe - %7.2f", cnt1);        
115

    
116
        # Red Text
117
        glColor3ub (0xff, 0, 0)
118

    
119
        glPushMatrix ()
120
        glLoadIdentity ()
121
        # Spin the text, rotation around z axe == will appears as a 2d rotation of the text on our screen
122
        glRotatef (cnt1, 0, 0, 1)
123
        glScalef (1, 0.8 + 0.3* cos (cnt1/5), 1)
124
        glTranslatef (-180, 0, 0)
125
        our_font.glPrint (320, 240, "Active FreeType Text - %7.2f" % (cnt1))
126
        glPopMatrix ()
127

    
128
        # //Uncomment this to test out print's ability to handle newlines.
129
        # our_font.glPrint (320, 240, "Here\nthere\nbe\n\nnewlines %f\n." % (cnt1))
130

    
131
        cnt1 += 0.051
132
        # cnt2 += 0.005
133

    
134
        glutSwapBuffers()
135
        return
136

    
137

    
138
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
139
def keyPressed(*args):
140
        global window
141
        global our_font
142
        # If escape is pressed, kill everything.
143
        if args[0] == ESCAPE:
144
                our_font.release ()
145
                sys.exit()
146

    
147
def main():
148
        global window
149
        # pass arguments to init
150
        glutInit(sys.argv)
151

    
152
        # Select type of Display mode:   
153
        #  Double buffer 
154
        #  RGBA color
155
        # Alpha components supported 
156
        # Depth buffer
157
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
158
        
159
        # get a 640 x 480 window 
160
        glutInitWindowSize(640, 480)
161
        
162
        # the window starts at the upper left corner of the screen 
163
        glutInitWindowPosition(0, 0)
164
        
165
        # Okay, like the C version we retain the window id to use when closing, but for those of you new
166
        # to Python (like myself), remember this assignment would make the variable local and not global
167
        # if it weren't for the global declaration at the start of main.
168
        window = glutCreateWindow("NeHe & Sven Olsen's TrueType Font Tutorial")
169

    
170
        # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
171
        # set the function pointer and invoke a function to actually register the callback, otherwise it
172
        # would be very much like the C version of the code.        
173
        glutDisplayFunc(DrawGLScene)
174
        
175
        # Uncomment this line to get full screen.
176
        #glutFullScreen()
177

    
178
        # When we are doing nothing, redraw the scene.
179
        glutIdleFunc(DrawGLScene)
180
        
181
        # Register the function called when our window is resized.
182
        glutReshapeFunc(ReSizeGLScene)
183
        
184
        # Register the function called when the keyboard is pressed.  
185
        glutKeyboardFunc(keyPressed)
186

    
187
        # Initialize our window. 
188
        InitGL(640, 480)
189

    
190
        # Start Event Processing Engine        
191
        glutMainLoop()
192

    
193
# Print message to console, and kick off the main to get it rolling.
194
if __name__ == "__main__":
195
        print "Hit ESC key to quit."
196
        main()