# Example of GLUT text functions with a dynamically changing string # This program also uses multiple projections - a perspective # projection for the 3D teapot & text, and an ortho projection # for the bitmapped text at the bottom of the window # # Dave Pape # 27 September 2003 import sys, time, math from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * def drawStrokeString(text, font=GLUT_STROKE_ROMAN): for c in text: glutStrokeCharacter(font, ord(c)) def drawBitmapString(text, font=GLUT_BITMAP_TIMES_ROMAN_24): for c in text: glutBitmapCharacter(font, ord(c)) viewDistance = 30 viewRotY = 0 viewRotX = 0 def draw(): global viewRotX, viewRotY, viewDistance glClearColor(0.5, 0.7, 1.0, 0.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(50.0, 1.0, 1.0, 100.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslatef(0.0, 0.0, -viewDistance) glRotatef(viewRotX, 1.0, 0.0, 0.0) glRotatef(viewRotY, 0.0, 1.0, 0.0) height = abs(math.sin(time.time()) * 10) glPushMatrix() glTranslatef(0, height - 5, 0) glColor3f(0.5, 0.5, 0) glutWireTeapot(1) glTranslatef(2, 0, 0) glScalef(0.005, 0.005, 0.005) glColor3f(0, 0, 0) drawStrokeString("teapot height = %.2f" % height) glPopMatrix() glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, 1, 0, 1) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glColor3f(0.8, 0, 0) glRasterPos2f(0.3, 0.05) drawBitmapString("time = %f" % time.time()) glutSwapBuffers() def keyboard(key, x, y): global viewDistance if key == chr(27): sys.exit(0) elif key == '.': viewDistance = viewDistance - 1 elif key == ',': viewDistance = viewDistance + 1 glutPostRedisplay() def specialkey(key,x,y): global viewRotX, viewRotY if key == GLUT_KEY_LEFT: viewRotY = viewRotY + 3 elif key == GLUT_KEY_RIGHT: viewRotY = viewRotY - 3 elif key == GLUT_KEY_UP: viewRotX = viewRotX + 3 elif key == GLUT_KEY_DOWN: viewRotX = viewRotX - 3 glutPostRedisplay() def update(): glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(400,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialkey) glutIdleFunc(update) glEnable(GL_DEPTH_TEST) glutMainLoop()