 

/* Implantation de l'interactivite souris       */
/* en OpenGL                                    */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Septembre 2011                               */
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* Variables et constantes globales             */
static float px = 150.0F;
static float py = 150.0F;
static int move = 0;
/* Fonction d'initialisation des parametres     */
/* OpenGL ne changeant pas au cours de la vie   */
/* du programme                                 */
void init(void) {
}
/* Scene dessinee                               */
void scene(void) {
  glPushMatrix();
  glTranslatef(px,py,0.0);
  glutSolidSphere(10.0,36,36);
  glPopMatrix();
}
/* Fonction executee lors d'un changement       */
/* de la taille de la fenetre OpenGL            */
void reshape(int tx,int ty) {
  glViewport(0,0,tx,ty); 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0,tx,ty,0,-20.0,20.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin                      */
void display(void) {
  glClearColor(0.5F,0.5F,0.5F,0.5F);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur : %d\n",error);
}
/* Fonction executee lors du deplacement        */
/* de la souris bouton appuyé                   */
void motion(int x,int y) {
  if ( move ) {
    px = x;
    py = y;
    glutPostRedisplay(); }
}
/* Fonction executee lors de l'utilisation      */
/* des boutons de la souris                     */
void mouse(int bouton,int etat,int x,int y) {
  if ( bouton == GLUT_LEFT_BUTTON ) {
    px = x;
    py = y;
    glutPostRedisplay(); }
  if ( bouton == GLUT_RIGHT_BUTTON ) {
    if ( etat == GLUT_UP )
      if ( move ) {
        move = 0;
        px = x;
        py = y;
        glutPostRedisplay(); }
    if ( etat == GLUT_DOWN )
      if ( (px-x)*(px-x)+(py-y)*(py-y) <= 100.0F ) {
        move = 1;
        px = x;
        py = y;
        glutPostRedisplay(); } }
}
/* Fonction principale                          */
int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(300,300); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Interactivité souris"); 
  init();
  glutMotionFunc(motion);
  glutMouseFunc(mouse);
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}