root / PyOpenGL-Demo / GLUT / tom / cone.py @ 2
Historique | Voir | Annoter | Télécharger (3,01 ko)
| 1 |
"""GLUT replacement for the original checker.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:11 $'[6:-2] |
| 13 |
|
| 14 |
import OpenGL |
| 15 |
OpenGL.ERROR_ON_COPY = True
|
| 16 |
|
| 17 |
from OpenGL.GL import * |
| 18 |
from OpenGL.GLU import * |
| 19 |
from OpenGL.GLUT import * |
| 20 |
import time, sys |
| 21 |
|
| 22 |
def drawCone( position = (0,-1,0), radius=1, height=2, slices=50,stacks=10 ): |
| 23 |
glPushMatrix() |
| 24 |
try:
|
| 25 |
glTranslatef(*position) |
| 26 |
glRotatef(250, 1, 0, 0) |
| 27 |
glutSolidCone(radius, height, slices, stacks ) |
| 28 |
finally:
|
| 29 |
glPopMatrix() |
| 30 |
|
| 31 |
def coneMaterial( ): |
| 32 |
"""Setup material for cone"""
|
| 33 |
glMaterialfv(GL_FRONT, GL_AMBIENT, GLfloat_4(0.2, 0.2, 0.2, 1.0)) |
| 34 |
glMaterialfv(GL_FRONT, GL_DIFFUSE, GLfloat_4(0.8, 0.8, 0.8, 1.0)) |
| 35 |
glMaterialfv(GL_FRONT, GL_SPECULAR, GLfloat_4(1.0, 0.0, 1.0, 1.0)) |
| 36 |
glMaterialfv(GL_FRONT, GL_SHININESS, GLfloat(50.0))
|
| 37 |
def light(): |
| 38 |
"""Setup light 0 and enable lighting"""
|
| 39 |
glLightfv(GL_LIGHT0, GL_AMBIENT, GLfloat_4(0.0, 1.0, 0.0, 1.0)) |
| 40 |
glLightfv(GL_LIGHT0, GL_DIFFUSE, GLfloat_4(1.0, 1.0, 1.0, 1.0)) |
| 41 |
glLightfv(GL_LIGHT0, GL_SPECULAR, GLfloat_4(1.0, 1.0, 1.0, 1.0)) |
| 42 |
glLightfv(GL_LIGHT0, GL_POSITION, GLfloat_4(1.0, 1.0, 1.0, 0.0)); |
| 43 |
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, GLfloat_4(0.2, 0.2, 0.2, 1.0)) |
| 44 |
glEnable(GL_LIGHTING) |
| 45 |
glEnable(GL_LIGHT0) |
| 46 |
def depth(): |
| 47 |
"""Setup depth testing"""
|
| 48 |
glDepthFunc(GL_LESS) |
| 49 |
glEnable(GL_DEPTH_TEST) |
| 50 |
|
| 51 |
def display( swap=1, clear=1): |
| 52 |
"""Callback function for displaying the scene
|
| 53 |
|
| 54 |
This defines a unit-square environment in which to draw,
|
| 55 |
i.e. width is one drawing unit, as is height
|
| 56 |
"""
|
| 57 |
if clear:
|
| 58 |
glClearColor(0.5, 0.5, 0.5, 0) |
| 59 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
| 60 |
|
| 61 |
# establish the projection matrix (perspective)
|
| 62 |
glMatrixMode(GL_PROJECTION) |
| 63 |
glLoadIdentity() |
| 64 |
x,y,width,height = glGetDoublev(GL_VIEWPORT) |
| 65 |
gluPerspective( |
| 66 |
45, # field of view in degrees |
| 67 |
width/float(height or 1), # aspect ratio |
| 68 |
.25, # near clipping plane |
| 69 |
200, # far clipping plane |
| 70 |
) |
| 71 |
|
| 72 |
# and then the model view matrix
|
| 73 |
glMatrixMode(GL_MODELVIEW) |
| 74 |
glLoadIdentity() |
| 75 |
gluLookAt( |
| 76 |
0,1,5, # eyepoint |
| 77 |
0,0,0, # center-of-view |
| 78 |
0,1,0, # up-vector |
| 79 |
) |
| 80 |
light() |
| 81 |
depth() |
| 82 |
coneMaterial() |
| 83 |
|
| 84 |
rotation() |
| 85 |
drawCone() |
| 86 |
if swap:
|
| 87 |
glutSwapBuffers() |
| 88 |
|
| 89 |
def idle( ): |
| 90 |
glutPostRedisplay() |
| 91 |
|
| 92 |
starttime = time.time() |
| 93 |
|
| 94 |
def rotation( period = 10): |
| 95 |
"""Do rotation of the scene at given rate"""
|
| 96 |
angle = (((time.time()-starttime)%period)/period)* 360
|
| 97 |
glRotate( angle, 0,1,0) |
| 98 |
return angle
|
| 99 |
def key_pressed(*args): |
| 100 |
# If escape is pressed, kill everything.
|
| 101 |
if args[0] == '\033': |
| 102 |
sys.exit() |
| 103 |
|
| 104 |
def main(): |
| 105 |
print """You should see a high-resolution cone rotating slowly.""" |
| 106 |
import sys |
| 107 |
glutInit(sys.argv) |
| 108 |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) |
| 109 |
glutCreateWindow('Rotating Cone')
|
| 110 |
glutDisplayFunc(display) |
| 111 |
glutKeyboardFunc(key_pressed) |
| 112 |
glutIdleFunc(display) |
| 113 |
# note need to do this to properly render faceted geometry
|
| 114 |
glutMainLoop() |
| 115 |
|
| 116 |
if __name__ == "__main__": |
| 117 |
main() |
| 118 |
|