 
 
 
/* 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 int px = 0;
static int py = 0;
static float rx = 0.0F;
static float ry = 0.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();
  glRotatef(rx,1.0F,0.0F,0.0F);
  glRotatef(ry,0.0F,1.0F,0.0F);
  glutWireTeapot(5.0);
  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(-tx*10.0/ty,tx*10.0/ty,-10.0,10.0,-200.0,200.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 ) {
    ry += (x-px);
    rx -= (y-py);
    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 ) {
    if ( etat == GLUT_UP )
      if ( move ) {
        move = 0;
        px = x;
        py = y;
        glutPostRedisplay(); }
    if ( etat == GLUT_DOWN ) {
      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);
}