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