root / PyOpenGL-Demo / GLUT / tom / text.py @ 2
Historique | Voir | Annoter | Télécharger (2,81 ko)
| 1 |
"""GLUT replacement for the original text.py demonstration code
|
|---|---|
| 2 |
|
| 3 |
Note:
|
| 4 |
Has no navigation code ATM.
|
| 5 |
"""
|
| 6 |
|
| 7 |
# This is statement is required by the build system to query build info
|
| 8 |
if __name__ == '__build__': |
| 9 |
raise Exception |
| 10 |
|
| 11 |
__version__='$Revision: 1.1.1.1 $'[11:-2] |
| 12 |
__date__ = '$Date: 2007/02/15 19:25:13 $'[6:-2] |
| 13 |
|
| 14 |
|
| 15 |
from OpenGL.GL import * |
| 16 |
from OpenGL.GLU import * |
| 17 |
from OpenGL.GLUT import * |
| 18 |
import time, sys |
| 19 |
from logo import define_logo |
| 20 |
|
| 21 |
def createList( ): |
| 22 |
"""Create display list for the text"""
|
| 23 |
newList = glGenLists(1);
|
| 24 |
glNewList(newList, GL_COMPILE); |
| 25 |
try:
|
| 26 |
define_logo() |
| 27 |
finally:
|
| 28 |
glEndList() |
| 29 |
return newList
|
| 30 |
|
| 31 |
|
| 32 |
def light(): |
| 33 |
"""Setup light 0 and enable lighting"""
|
| 34 |
glLightfv(GL_LIGHT0, GL_AMBIENT, GLfloat_4(0.0, 1.0, 0.0, 1.0)) |
| 35 |
glLightfv(GL_LIGHT0, GL_DIFFUSE, GLfloat_4(1.0, 1.0, 1.0, 1.0)) |
| 36 |
glLightfv(GL_LIGHT0, GL_SPECULAR, GLfloat_4(1.0, 1.0, 1.0, 1.0)) |
| 37 |
glLightfv(GL_LIGHT0, GL_POSITION, GLfloat_4(1.0, 1.0, 1.0, 0.0)); |
| 38 |
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, GLfloat_4(0.2, 0.2, 0.2, 1.0)) |
| 39 |
glEnable(GL_LIGHTING) |
| 40 |
glEnable(GL_LIGHT0) |
| 41 |
|
| 42 |
def display( swap=1, clear=1): |
| 43 |
"""Callback function for displaying the scene
|
| 44 |
|
| 45 |
This defines a unit-square environment in which to draw,
|
| 46 |
i.e. width is one drawing unit, as is height
|
| 47 |
"""
|
| 48 |
if clear:
|
| 49 |
glClearColor(0.5, 0.5, 0.5, 0) |
| 50 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
| 51 |
|
| 52 |
# establish the projection matrix (perspective)
|
| 53 |
glMatrixMode(GL_PROJECTION) |
| 54 |
glLoadIdentity() |
| 55 |
x,y,width,height = glGetDoublev(GL_VIEWPORT) |
| 56 |
gluPerspective( |
| 57 |
45, # field of view in degrees |
| 58 |
width/float(height or 1), # aspect ratio |
| 59 |
.25, # near clipping plane |
| 60 |
200, # far clipping plane |
| 61 |
) |
| 62 |
|
| 63 |
# and then the model view matrix
|
| 64 |
glMatrixMode(GL_MODELVIEW) |
| 65 |
glLoadIdentity() |
| 66 |
gluLookAt( |
| 67 |
0,1,30, # eyepoint |
| 68 |
10,0,0, # center-of-view |
| 69 |
0,1,0, # up-vector |
| 70 |
) |
| 71 |
light() |
| 72 |
rotation() |
| 73 |
|
| 74 |
glFrontFace(GL_CCW) |
| 75 |
glEnable(GL_CULL_FACE) # added by jfp to use with new logo.py
|
| 76 |
glEnable(GL_DEPTH_TEST) |
| 77 |
glMaterialfv(GL_FRONT, GL_DIFFUSE, GLfloat_4(1., 1., 0., 0.)) |
| 78 |
|
| 79 |
## define_logo()
|
| 80 |
global TEXT_DISPLAY_LIST
|
| 81 |
try:
|
| 82 |
glCallList( TEXT_DISPLAY_LIST ) |
| 83 |
except NameError: |
| 84 |
TEXT_DISPLAY_LIST = createList() |
| 85 |
|
| 86 |
if swap:
|
| 87 |
glutSwapBuffers() |
| 88 |
|
| 89 |
def idle( ): |
| 90 |
glutPostRedisplay() |
| 91 |
def key_pressed(*args): |
| 92 |
# If escape is pressed, kill everything.
|
| 93 |
if args[0] == '\033': |
| 94 |
sys.exit() |
| 95 |
|
| 96 |
starttime = time.time() |
| 97 |
|
| 98 |
def rotation( period = 10): |
| 99 |
"""Do rotation of the scene at given rate"""
|
| 100 |
angle = (((time.time()-starttime)%period)/period)* 360
|
| 101 |
glRotate( angle, 0,1,0) |
| 102 |
return angle
|
| 103 |
|
| 104 |
if __name__ == "__main__": |
| 105 |
print """You should see polygonal text rotating slowly.""" |
| 106 |
import sys |
| 107 |
glutInit(sys.argv) |
| 108 |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) |
| 109 |
glutCreateWindow('Polygonal Geometry Demo')
|
| 110 |
glutDisplayFunc(display) |
| 111 |
glutKeyboardFunc(key_pressed) |
| 112 |
glutIdleFunc(display) |
| 113 |
# note need to do this to properly render faceted geometry
|
| 114 |
glutMainLoop() |