/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2006 */
/* Gestion de la souris en C + OpenGL + GLUT */
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCylindres.h"
static int dd = 0;
static int sx = 0;
static int sy = 0;
static float ax = 0.0F;
static float ay = 0.0F;
static int l = 1;
void mouse(int bouton,int etat,int x,int y) {
if ( bouton == GLUT_LEFT_BUTTON ) {
sx = x;
sy = y;
if ( etat == GLUT_DOWN ) {
dd = 1; }
if ( etat == GLUT_UP ) {
dd = 0; } }
}
void motion(int x,int y) {
if ( dd == 1 ) {
ay += (x-sx);
ax += (y-sy);
sx = x;
sy = y;
glutPostRedisplay(); }
}
void reshape(int tx,int ty) {
glViewport(0,0,tx,ty);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(4.5*(double) -tx/ty,4.5*(double) tx/ty,-4.5,4.5,-10.0,10.0) ;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case 0x0D : l = !l ;
glutPostRedisplay();
break;
case 0x1B : exit(0); }
}
void element() {
glPushMatrix();
glTranslatef(2.0F,0.0F,2.0F);
solidCylindre(0.25F,3.0F,10,5);
for ( int i = 0 ; i < 2 ; i++ ) {
glTranslatef(0.0F,2.0F,0.0F);
glutSolidCube(1.0F);
glRotatef(90.0F,0.0F,0.0F,1.0F);
glTranslatef(0.0F,2.0F,0.0F);
solidCylindre(0.25F,3.0F,10,5); }
glPopMatrix();
}
void scene() {
glPushMatrix();
element();
for ( int i = 1 ; i < 4 ; i++ ) {
glPushMatrix();
glRotatef(90.0F*i,1.0F,0.0F,0.0F);
element();
glPopMatrix(); }
glPopMatrix();
}
void display(void) {
if ( l )
glEnable(GL_LIGHTING);
else
glDisable(GL_LIGHTING);
glClearColor(0.8F,0.8F,0.8F,1.0F) ;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
glPushMatrix();
glRotatef(ax,1.0F,0.0F,0.0F);
glRotatef(ay,0.0F,1.0F,0.0F);
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention, erreur OpenGL %d\n",error);
}
void init(void) {
GLfloat l_pos[] = { 0.0F,0.0F,1.0F,0.0F };
GLfloat c[4] = { 0.5F,0.2F,0.6F,1.0F };
glMaterialfv(GL_FRONT,GL_DIFFUSE,c);
glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
glEnable(GL_LIGHT0);
glColor4fv(c) ;
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Gestion clavier");
init();
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(key);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}