#-*-coding:utf8-*- from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * from math import sin, cos, sqrt import sys def display(): glClearColor(0,0,0,0) glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(Xm,Ym,Zm,Xo,Yo,Zo,0,1,0) glColor3d(1,0,0) glutWireCube(1.) glTranslatef(0.0,0.8,0.0) glutSolidSphere(0.3,30,30) glFlush() def clavierSpecial(*argv): global Xm, Ym, Zm, Xo, Yo, Zo, A1, A2 if (argv[0]==GLUT_KEY_UP): OM=sqrt((Xo-Xm)*(Xo-Xm)+(Yo-Ym)*(Yo-Ym)+(Zo-Zm)*(Zo-Zm)) Xm=Xm+0.1*(Xo-Xm)/OM Ym=Ym+0.1*(Yo-Ym)/OM Zm=Zm+0.1*(Zo-Zm)/OM Xo=Xo+0.1*(Xo-Xm)/OM Yo=Yo+0.1*(Yo-Ym)/OM Zo=Zo+0.1*(Zo-Zm)/OM if (argv[0]==GLUT_KEY_DOWN): OM=sqrt((Xo-Xm)*(Xo-Xm)+(Yo-Ym)*(Yo-Ym)+(Zo-Zm)*(Zo-Zm)) Xm=Xm-0.1*(Xo-Xm)/OM Ym=Ym-0.1*(Yo-Ym)/OM Zm=Zm-0.1*(Zo-Zm)/OM Xo=Xo-0.1*(Xo-Xm)/OM Yo=Yo-0.1*(Yo-Ym)/OM Zo=Zo-0.1*(Zo-Zm)/OM if (argv[0]==GLUT_KEY_RIGHT): OM=sqrt((Xo-Xm)*(Xo-Xm)+(Yo-Ym)*(Yo-Ym)+(Zo-Zm)*(Zo-Zm)) Xm=Xm-0.1*(Zo-Zm)/OM Ym=Ym-0.1*(Yo-Ym)/OM Zm=Zm-0.1*(-Xo+Xm)/OM Xo=Xo-0.1*(Zo-Zm)/OM Yo=Yo-0.1*(Yo-Ym)/OM Zo=Zo-0.1*(-Xo+Xm)/OM if (argv[0]==GLUT_KEY_LEFT): OM=sqrt((Xo-Xm)*(Xo-Xm)+(Yo-Ym)*(Yo-Ym)+(Zo-Zm)*(Zo-Zm)) Xm=Xm+0.1*(Zo-Zm)/OM Ym=Ym+0.1*(Yo-Ym)/OM Zm=Zm+0.1*(-Xo+Xm)/OM Xo=Xo+0.1*(Zo-Zm)/OM Yo=Yo+0.1*(Yo-Ym)/OM Zo=Zo+0.1*(-Xo+Xm)/OM glutPostRedisplay() def keyboard(*args): if args[0] == 'q': sys.exit() def souris(*argv): global X, Y, Xm, Ym, Zm, Xo, Yo, Zo, A1, A2 Xo=Xm+(Xo-Xm)*cos(-A2)-(Zo-Zm)*sin(-A2) Zo=Zm+(Xo-Xm)*sin(-A2)+(Zo-Zm)*cos(-A2) Xo=Xm+(Xo-Xm)*cos(-A1)-(Yo-Ym)*sin(-A1) Yo=Ym+(Xo-Xm)*sin(-A1)+(Yo-Ym)*cos(-A1) # on donne les nouvelles valeurs aux angles A2=(float(argv[0])-float(X)/2)/(float(X)*3.141592) A1=(float(Y)/2-float(argv[1]))/(float(Y)*3.141592) # on fait enfin subir a O une rotation d'axe MY, angle A2, puis d'axe MX, angle A1 : Xo=Xm+(Xo-Xm)*cos(A2)-(Zo-Zm)*sin(A2) Zo=Zm+(Xo-Xm)*sin(A2)+(Zo-Zm)*cos(A2) Xo=Xm+(Xo-Xm)*cos(A1)-(Yo-Ym)*sin(A1) Yo=Ym+(Xo-Xm)*sin(A1)+(Yo-Ym)*cos(A1) glutPostRedisplay() def reshape(*args): glViewport(0,0,args[0],args[1]) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60.0,float(args[0])/args[1],1.,10.) def idle(*args): global X, Xm, Ym, Zm, Xo, Yo, Zo, A1, A2 A=A2/3.141592*X+X/2 if A<10: Xo=Xm+(Xo-Xm)*cos(-A2)-(Zo-Zm)*sin(-A2) Zo=Zm+(Xo-Xm)*sin(-A2)+(Zo-Zm)*cos(-A2) A2=A2-0.01 Xo=Xm+(Xo-Xm)*cos(A2)-(Zo-Zm)*sin(A2) Zo=Zm+(Xo-Xm)*sin(A2)+(Zo-Zm)*cos(A2) glutPostRedisplay() def main(): global X, Y, Xm, Ym, Zm, Xo, Yo, Zo, A1, A2 X, Y = 980, 660 Xm, Ym, Zm =-3, 0, 0 Xo, Yo, Zo = 0, 0, 0 A1, A2 = 0., 0. glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA) glutInitWindowPosition(20,20) glutInitWindowSize(320,320) glutInitWindowSize(X,Y) glutCreateWindow("Une scene") glutDisplayFunc(display) glutReshapeFunc(reshape) glutPassiveMotionFunc(souris) glutSpecialFunc(clavierSpecial) glutKeyboardFunc(keyboard) glutIdleFunc(idle) glutMainLoop() main()