#!/usr/bin/env python"""Draw a cube on the screen. every frame we orbitthe camera around by a small amount and it appearsthe object is spinning. note i've setup some simpledata structures here to represent a multicolored cube,we then go through a semi-unopimized loop to drawthe cube points onto the screen. opengl does all thehard work for us. :]"""importpygamefrompygame.localsimport*try:fromOpenGL.GLimport*fromOpenGL.GLUimport*except:print'The GLCUBE example requires PyOpenGL'raiseSystemExit#some simple data for a colored cube#here we have the 3D point position and color#for each corner. then we have a list of indices#that describe each face, and a list of indieces#that describes each edgeCUBE_POINTS=((0.5,-0.5,-0.5),(0.5,0.5,-0.5),(-0.5,0.5,-0.5),(-0.5,-0.5,-0.5),(0.5,-0.5,0.5),(0.5,0.5,0.5),(-0.5,-0.5,0.5),(-0.5,0.5,0.5))#colors are 0-1 floating valuesCUBE_COLORS=((1,0,0),(1,1,0),(0,1,0),(0,0,0),(1,0,1),(1,1,1),(0,0,1),(0,1,1))CUBE_QUAD_VERTS=((0,1,2,3),(3,2,7,6),(6,7,5,4),(4,5,1,0),(1,5,7,2),(4,0,3,6))CUBE_EDGES=((0,1),(0,3),(0,4),(2,1),(2,3),(2,7),(6,3),(6,4),(6,7),(5,1),(5,4),(5,7),)defdrawcube():"draw the cube"allpoints=zip(CUBE_POINTS,CUBE_COLORS)glBegin(GL_QUADS)forfaceinCUBE_QUAD_VERTS:forvertinface:pos,color=allpoints[vert]glColor3fv(color)glVertex3fv(pos)glEnd()glColor3f(1.0,1.0,1.0)glBegin(GL_LINES)forlineinCUBE_EDGES:forvertinline:pos,color=allpoints[vert]glVertex3fv(pos)glEnd()defmain():"run the demo"#initialize pygame and setup an opengl displaypygame.init()pygame.display.set_mode((640,480),OPENGL|DOUBLEBUF)glEnable(GL_DEPTH_TEST)#use our zbuffer#setup the cameraglMatrixMode(GL_PROJECTION)gluPerspective(45.0,640/480.0,0.1,100.0)#setup lensglTranslatef(0.0,0.0,-3.0)#move backglRotatef(25,1,0,0)#orbit higherwhile1:#check for quit'n eventsevent=pygame.event.poll()ifevent.type==QUITor(event.type==KEYDOWNandevent.key==K_ESCAPE):break#clear screen and move cameraglClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)#orbit camera around by 1 degreeglRotatef(1,0,1,0)drawcube()pygame.display.flip()pygame.time.wait(10)if__name__=='__main__':main()