/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Janvier 2004 */ /* Un programme de deplacement d'une sphere */ /* par drag and drop et clic de souris */ #include #include #include #include #include #include static int lumiere = 0; static float px = 100.0F; static float py = 100.0F; static float destx = 100.0F; static float desty = 100.0F; static float r = 10.0F; static GLfloat couleurBlanc[] = { 1.0F,1.0F,1.0F,1.0F }; static GLfloat couleurRouge[] = { 1.0F,0.0F,0.0F,1.0F }; static GLfloat couleurVert[] = { 0.0F,1.0F,0.0F,1.0F }; static GLfloat couleurBleu[] = { 0.0F,0.0F,1.0F,1.0F }; static GLfloat couleurNoir[] = { 0.0F,0.0F,0.0F,1.0F }; static int dragdrop = 0; void init(void) { GLfloat mat_shininess[] = { 50.0F }; GLfloat light0_position[] = { 1.0F,1.0F,1.0F,0.0F }; GLfloat light1_position[] = { -1.0F,1.0F,1.0F,0.0F }; GLfloat light2_position[] = { 1.0F,-1.0F,1.0F,0.0F }; glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurRouge); glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurVert); glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurBleu); glLightfv(GL_LIGHT0,GL_POSITION,light0_position); glLightfv(GL_LIGHT1,GL_POSITION,light1_position); glLightfv(GL_LIGHT2,GL_POSITION,light2_position); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_AUTO_NORMAL); glClearColor(0.0F,0.0F,0.0F,1.0F); } void scene(void) { glPushMatrix(); glTranslatef(px,py,0.0F); glutSolidSphere(r,60,60); glPopMatrix(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if ( lumiere ) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING); glPushMatrix(); scene(); glPopMatrix(); glFlush(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("\nAttention erreur %d\n",error); glutSwapBuffers(); } void idle(void) { float d = pow((double) ((px-destx)*(px-destx)+(py-desty)*(py-desty)),0.5); if ( d < 2.0 ) { px = destx; py = desty; glutIdleFunc(NULL); } else { px -= 2*(px-destx)/d; py -= 2*(py-desty)/d; } glutPostRedisplay() ; } void souris(int bouton,int etat,int x,int y) { if ( etat == GLUT_DOWN ) { if ( (px-x)*(px-x)+(py-y)*(py-y) < r*r ) { dragdrop = 1; glutIdleFunc(NULL); } else { destx = x; desty = y; glutIdleFunc(idle); } } if ( etat == GLUT_UP ) dragdrop = 0; } void motion(int x,int y) { if ( dragdrop ) { px = x; py = y; glutPostRedisplay(); } } void key(unsigned char key,int x,int y) { switch ( key ) { case 32 : lumiere = !lumiere; glutPostRedisplay(); break; } } void reshape(int w,int l) { glViewport(0,0,w,l); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,w,l,0,-100.0F,100.0F); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Interface utilisateur en GLUt"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutMouseFunc(souris); glutMotionFunc(motion); glutDisplayFunc(display); glutMainLoop(); return(0); }