root / PyOpenGL-Demo / GLUT / tom / arraytest.py @ 2
Historique | Voir | Annoter | Télécharger (2,37 ko)
| 1 |
"""GLUT replacement for the original arraytest.py demonstration code
|
|---|---|
| 2 |
"""
|
| 3 |
|
| 4 |
# This is statement is required by the build system to query build info
|
| 5 |
if __name__ == '__build__': |
| 6 |
raise Exception |
| 7 |
|
| 8 |
__version__='$Revision: 1.1.1.1 $'[11:-2] |
| 9 |
__date__ = '$Date: 2007/02/15 19:25:11 $'[6:-2] |
| 10 |
|
| 11 |
|
| 12 |
from OpenGL.GL import * |
| 13 |
from OpenGL.GLU import * |
| 14 |
from OpenGL.GLUT import * |
| 15 |
import time, sys |
| 16 |
try:
|
| 17 |
from numpy import * |
| 18 |
from numpy.random import * |
| 19 |
except ImportError, err: |
| 20 |
try:
|
| 21 |
from Numeric import * |
| 22 |
from RandomArray import * |
| 23 |
except ImportError, err: |
| 24 |
print "This demo requires the numpy or Numeric extension, sorry" |
| 25 |
import sys |
| 26 |
sys.exit() |
| 27 |
|
| 28 |
n=50
|
| 29 |
|
| 30 |
def shuffle(a,b): |
| 31 |
return ravel(transpose(reshape(concatenate([a,b]), (2,len(a))))) |
| 32 |
|
| 33 |
def buildArrays( ): |
| 34 |
a = arange(0,n)
|
| 35 |
vertex = shuffle(cos(2*pi*a/n), sin(2*pi*a/n)) |
| 36 |
vertex.shape = (n, 2)
|
| 37 |
color = random(n*3)
|
| 38 |
color.shape = (n, 3)
|
| 39 |
return vertex,color
|
| 40 |
|
| 41 |
vertex,color = buildArrays() |
| 42 |
|
| 43 |
def drawArrays( ): |
| 44 |
glVertexPointerd(vertex) |
| 45 |
glColorPointerd(color) |
| 46 |
glEnableClientState(GL_VERTEX_ARRAY) |
| 47 |
glEnableClientState(GL_COLOR_ARRAY) |
| 48 |
glDisable(GL_LIGHTING) |
| 49 |
try:
|
| 50 |
glDrawArrays(GL_LINE_LOOP, 0, n)
|
| 51 |
finally:
|
| 52 |
glEnable(GL_LIGHTING) |
| 53 |
|
| 54 |
def display( swap=1, clear=1): |
| 55 |
"""Callback function for displaying the scene
|
| 56 |
|
| 57 |
This defines a unit-square environment in which to draw,
|
| 58 |
i.e. width is one drawing unit, as is height
|
| 59 |
"""
|
| 60 |
glClearColor(0.5, 0.5, 0.5, 0) |
| 61 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
| 62 |
|
| 63 |
# establish the projection matrix (perspective)
|
| 64 |
glMatrixMode(GL_PROJECTION) |
| 65 |
glLoadIdentity() |
| 66 |
glOrtho(-1, 1, -1, 1, -1, 1) |
| 67 |
|
| 68 |
# and then the model view matrix
|
| 69 |
glMatrixMode(GL_MODELVIEW) |
| 70 |
glLoadIdentity() |
| 71 |
rotation() |
| 72 |
|
| 73 |
drawArrays() |
| 74 |
|
| 75 |
glutSwapBuffers() |
| 76 |
|
| 77 |
def idle( ): |
| 78 |
glutPostRedisplay() |
| 79 |
|
| 80 |
starttime = time.time() |
| 81 |
|
| 82 |
def rotation( period = 10): |
| 83 |
"""Do rotation of the scene at given rate"""
|
| 84 |
angle = (((time.time()-starttime)%period)/period)* 360
|
| 85 |
glRotate( angle, 0,1,0) |
| 86 |
return angle
|
| 87 |
|
| 88 |
def key_pressed(*args): |
| 89 |
# If escape is pressed, kill everything.
|
| 90 |
if args[0] == '\033': |
| 91 |
sys.exit() |
| 92 |
|
| 93 |
|
| 94 |
if __name__ == "__main__": |
| 95 |
print """You should see a polynomial curve rotating about the origin.""" |
| 96 |
import sys |
| 97 |
glutInit(sys.argv) |
| 98 |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) |
| 99 |
glutCreateWindow('Array Drawing Demo')
|
| 100 |
glutDisplayFunc(display) |
| 101 |
glutIdleFunc(display) |
| 102 |
glutKeyboardFunc(key_pressed) |
| 103 |
# note need to do this to properly render faceted geometry
|
| 104 |
glutMainLoop() |