#oc1.py #based on lorenz.py by Stan Blank 22mar05 #OpenGL by Alex Bourd and Matthew Stiak 9oct96 #IrisGL by G Francis 9apr89, last revision 9sep96 #Correction of 1988 SGI GUIDE p2-21 #replaces include from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * import sys class Scene: # constructor def __init__(self, argv): #assign the affine identity matrix #for mouse motion self.aff = (1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 0.0,0.0,0.0,1.0) #assign the vertices of the octahedron self.vv=( (1.0 , 0.0 , 0.0), (0.0,1.0,0.0), (0.0,0.0,1.0), (-1.0,0.0,0.0), (0.0,-1.0,0.0), (0.0,0.0,-1.0) ) #assign initial window and mouse settings self.wd = 800 self.ht = 800 self.MouseX = self.wd/2 self.MouseY = self.ht/2 self.brake = 512. self.initGlut(argv) # initialize GLUT def initGlut(self, argv): glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE) glutInitWindowPosition(50, 50) glutInitWindowSize(self.wd, self.ht) glutInit(argv) glutCreateWindow("Octahedron") glutKeyboardFunc(self.keyboard) glutDisplayFunc(self.display) glutIdleFunc(self.idle) glutReshapeFunc(self.reshape) glutPassiveMotionFunc(self.mousemotion) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-2.0,2.0,-2.0,2.0,-2.0,2.0) # typical display callback def display(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glMultMatrixf(self.aff) glBegin(GL_TRIANGLE_FAN) glColor3f(0,1,0); glVertex3fv(self.vv[1]) glColor3f(0,0,1); glVertex3fv(self.vv[0]) glColor3f(0,1,1); glVertex3fv(self.vv[5]) glColor3f(1,1,0); glVertex3fv(self.vv[3]) glColor3f(1,0,0); glVertex3fv(self.vv[2]) glColor3f(0,0,1); glVertex3fv(self.vv[0]) glEnd() glBegin(GL_TRIANGLE_FAN) glColor3f(1,0,1); glVertex3fv(self.vv[4]) glColor3f(0,0,1); glVertex3fv(self.vv[0]) glColor3f(0,1,1); glVertex3fv(self.vv[5]) glColor3f(1,1,0); glVertex3fv(self.vv[3]) glColor3f(1,0,0); glVertex3fv(self.vv[2]) glColor3f(0,0,1); glVertex3fv(self.vv[0]) glEnd() glutSwapBuffers() #end display() #typical keyboard callback def keyboard(self, key, x, y): if key == chr(27) or key == 'q': sys.exit(0) glutPostRedisplay() #end keyboard() #adjust to resizing of the window def reshape(self, width, height): glClearColor(0.0, 0.0, 0.0, 0.0) if height == 0: height = 1 self.wd = width self.ht = height glViewport(0,0,self.wd,self.ht) glMatrixMode(GL_PROJECTION) glLoadIdentity() if self.wd<=self.ht: glOrtho(-2.0,2.0,-2.0*self.ht/self.wd,2.0*self.ht/self.wd,-2.0,2.0) else: glOrtho(-2.0*self.wd/self.ht,2.0*self.wd/self.ht,-2.0,2.0,-2.0,2.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() #end reshape() def chaptrack(self): dx = (self.MouseX-self.wd/2.)/self.brake dy = (self.MouseY-self.ht/2.)/self.brake glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glRotatef(dx,0,1.0,0.0) glRotatef(dy,1.0,0.0,0.0) glMultMatrixf(self.aff) self.aff = glGetFloatv(GL_MODELVIEW_MATRIX) glPopMatrix() #end chaptrack() #traditional idle def idle(self): self.chaptrack() glutPostRedisplay() #end idle() #ditto traditional mousemotion def mousemotion(self, x,y): self.MouseX = x self.MouseY = y #end mousemotion() if __name__=='__main__': scene=Scene(sys.argv) glutMainLoop()