Fichier source : GestionCameras.cpp
/* Gestion des cameras OpenGL */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Fevrier 2018 */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* Constantes globales */
static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F };
static const float vert[] = { 0.0F,1.0F,0.0F,1.0F };
static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F };
static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F };
static const float cyan[] = { 0.0F,1.0F,1.0F,1.0F };
static const float magenta[] = { 1.0F,0.0F,1.0F,1.0F };
static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F };
static const float grisClair[] = { 0.75F,0.75F,0.75F,1.0F };
static const float grisMoyen[] = { 0.5F,0.5F,0.5F,1.0F };
static const float grisFonce[] = { 0.25F,0.25F,0.25F,1.0F };
static const float noir[] = { 0.0F,0.0F,0.0F,1.0F };
/* Variables globales */
static int camera = 0; // Flag de switch entre versions de caméra
static int cFond = 0; // Numero de la couleur de fond (0: gris, 1: blanc, 2:noir)
static int pMode = 1; // Flag de switch entre modes d'affichage wireframe et fill
static int wTx = 480; // Resolution horizontale de la fenetre
static int wTy = 480; // Resolution verticale de la fenetre
static int wPx = 50; // Position horizontale de la fenetre
static int wPy = 50; // Position verticale de la fenetre
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
void init(void) {
const GLfloat shininess[] = { 50.0 };
glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
glMaterialfv(GL_FRONT,GL_SHININESS,shininess);
glLightfv(GL_LIGHT0,GL_DIFFUSE,grisClair);
glLightfv(GL_LIGHT1,GL_DIFFUSE,grisClair);
glLightfv(GL_LIGHT2,GL_DIFFUSE,grisClair);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
}
/* Scene dessinee */
void scene(void) {
glPushMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc);
glutSolidSphere(2.0,72,72);
glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge);
glutSolidCone(1.0,10.0,36,10);
glRotatef(90.0F,0.0F,1.0F,0.0F);
glMaterialfv(GL_FRONT,GL_DIFFUSE,vert);
glutSolidCone(1.0,10.0,36,10);
glRotatef(90.0F,0.0F,1.0F,0.0F);
glMaterialfv(GL_FRONT,GL_DIFFUSE,bleu);
glutSolidCone(1.0,10.0,36,10);
glRotatef(90.0F,0.0F,1.0F,0.0F);
glMaterialfv(GL_FRONT,GL_DIFFUSE,jaune);
glutSolidCone(1.0,10.0,36,10);
glRotatef(90.0F,1.0F,0.0F,0.0F);
glMaterialfv(GL_FRONT,GL_DIFFUSE,magenta);
glutSolidCone(1.0,10.0,36,10);
glRotatef(180.0F,1.0F,0.0F,0.0F);
glMaterialfv(GL_FRONT,GL_DIFFUSE,cyan);
glutSolidCone(1.0,10.0,36,10);
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
void display(void) {
printf("D\n");
const float *fond;
switch (cFond) {
case 0 :
fond = grisMoyen;
break;
case 1 :
fond = blanc;
break;
case 2 :
fond = noir;
break; }
glClearColor(fond[0],fond[1],fond[2],fond[3]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const GLfloat light0_position[] = { 0.0,0.0,0.0,1.0 };
const GLfloat light1_position[] = { -1.0,1.0,1.0,0.0 };
const GLfloat light2_position[] = { 1.0,-1.0,1.0,0.0 };
glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
glLightfv(GL_LIGHT1,GL_POSITION,light1_position);
glLightfv(GL_LIGHT2,GL_POSITION,light2_position);
glPolygonMode(GL_FRONT_AND_BACK,(pMode == 1) ? GL_FILL : GL_LINE);
glPushMatrix();
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention erreur %d\n",error);
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* Question (b) */
void reshapeOrthographique(int wx,int wy) {
printf("RO\n");
wTx = wx;
wTy = wy;
glViewport(0,0,wx,wy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( wy > wx ) {
double ty = (10.0*wy)/wx;
glOrtho(-10.0,10.0,-ty,ty,-20.0,20.0); }
else {
double tx = (10.0*wx)/wy;
glOrtho(-tx,tx,-10.0,10.0,-20.0,20.0); }
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* Question (c) */
void reshapePerspective1(int wx,int wy) {
double dist = 100.0;
double angle = 2.0*10.0/dist*180.0/3.14159;
printf("RP1 %6.2lf %6.2lf\n",dist,angle);
wTx = wx;
wTy = wy;
glViewport(0,0,wx,wy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( wy < wx )
gluPerspective(angle,(double) wx/wy,dist-12.0,dist+12.0);
else
gluPerspective(angle*((double) wy/wx),(double) wx/wy,dist-12.0,dist+12.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,100.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* Question (d) */
void reshapePerspective2(int wx,int wy) {
double dist = sqrt(20000.0);
double angle = 2.0*10.0/dist*180.0/3.14159;
printf("RP2 %6.2lf %6.2lf\n",dist,angle);
wTx = wx;
wTy = wy;
glViewport(0,0,wx,wy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( wy < wx )
gluPerspective(angle,(double) wx/wy,dist-12.0,dist+12.0);
else
gluPerspective(angle*((double) wy/wx),(double) wx/wy,dist-12.0,dist+12.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(100.0,0.0,100.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* Question (e) */
void reshapePerspective3(int wx,int wy) {
double dist = sqrt(30000.0);
double angle = 2.0*10.0/dist*180.0/3.14159;
printf("RP3 %6.2lf %6.2lf\n",dist,angle);
wTx = wx;
wTy = wy;
glViewport(0,0,wx,wy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( wy < wx )
gluPerspective(angle,(double) wx/wy,dist-12.0,dist+12.0);
else
gluPerspective(angle*((double) wy/wx),(double) wx/wy,dist-12.0,dist+12.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(100.0,100.0,100.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
/* Fonction executee lors de l'appui */
/* d'une touche alphanumerique du clavier */
void keyboard(unsigned char key,int x,int y) {
printf("K\n");
switch (key) {
case 0x0D :
{ camera = (camera+1)%4;
switch ( camera ) {
case 0 : {
glutReshapeFunc(reshapeOrthographique);
reshapeOrthographique(wTx,wTy);
glutPostRedisplay(); }
break;
case 1 : {
glutReshapeFunc(reshapePerspective1);
reshapePerspective1(wTx,wTy);
glutPostRedisplay(); }
break;
case 2 : {
glutReshapeFunc(reshapePerspective2);
reshapePerspective2(wTx,wTy);
glutPostRedisplay(); }
break;
case 3 : {
glutReshapeFunc(reshapePerspective3);
reshapePerspective3(wTx,wTy);
glutPostRedisplay(); }
break; } }
break;
case 'z' :
{ pMode = !pMode;
glutPostRedisplay(); }
break;
case 'f' :
{ cFond = (cFond+1)%3;
glutPostRedisplay(); }
break;
case 'F' :
{ static int full = 0;
static int wFx;
static int wFy;
full = !full;
if ( full ) {
wFx = glutGet(GLUT_WINDOW_WIDTH);
wFy = glutGet(GLUT_WINDOW_HEIGHT);
wPx = glutGet(GLUT_WINDOW_X);
wPy = glutGet(GLUT_WINDOW_Y);
glutFullScreen(); }
else {
glutPositionWindow(wPx,wPy);
glutReshapeWindow(wFx,wFy); } }
break;
case 0x1B :
exit(0);
break; }
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(wTx,wTy);
glutInitWindowPosition(wPx,wPy);
glutCreateWindow("Gestion de caméra en OpenGL ");
init();
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshapeOrthographique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}