Le source : CameraOrthographique2.cpp
/* Affichage par visualisation au moyen */
/* d'une projection parallele orthographique */
/* avec rotation de la scene pour la visualiser */
/* en projection selon l'axe x */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Septembre 2011 */
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "Modules/ModuleAxes.h"
/* Variables et constantes globales */
static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F };
static const float gris[] = { 0.7F,0.7F,0.7F,1.0F };
static int objet = 0;
static int aff = 1;
/* Fonction executee lors de la frappe */
/* d'une touche du clavier */
void keyboard(unsigned char key,int x,int y) {
switch ( key ) {
case 0x20 :
aff = (aff+1)%2;
glutPostRedisplay();
break;
case 0x0D :
objet = (objet+1)%2;
glutPostRedisplay();
break;
case 0x1B :
exit(0);
break; }
}
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
void init(void) {
const GLfloat mat_shininess[] = { 50.0 };
glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT0,GL_DIFFUSE,gris);
glLightfv(GL_LIGHT1,GL_DIFFUSE,gris);
glLightfv(GL_LIGHT2,GL_DIFFUSE,gris);
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);
glEnable(GL_CULL_FACE);
}
/* Scene dessinee */
void mySolidCylinder(double h,double r,int n,int m) {
glPushMatrix();
glRotatef(90.0F,1.0F,0.0F,0.0F);
glTranslatef(0.0F,0.0F,-h/2);
GLUquadricObj *qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluCylinder(qobj,r,r,h,n,m);
gluDeleteQuadric(qobj);
glPopMatrix();
}
void motif(int objet) {
glPushMatrix();
glTranslatef(2.0F,0.0F,0.0F);
glutSolidSphere(0.5,36,36);
glPushMatrix();
glRotatef(30.0,0.0F,0.0F,1.0F);
glTranslatef(0.0F,1.0F,0.0F);
switch (objet) {
case 2 :
mySolidCylinder(2.0,0.15,20,20);
break;
case 1 :
glTranslatef(0.1F,0.0F,0.0F);
mySolidCylinder(2.0,0.05,20,20);
glTranslatef(-0.2F,0.0F,0.0F);
mySolidCylinder(2.0,0.05,20,20);
break;
case 0 :
mySolidCylinder(2.0,0.05,20,20);
break; }
glPopMatrix();
glTranslatef(0.6F,0.0F,0.0F);
glPushMatrix();
glRotatef(90.0,0.0F,0.0F,1.0F);
mySolidCylinder(1.2,0.05,20,20);
glPopMatrix();
glTranslatef(0.6F,0.0F,0.0F);
glutSolidSphere(0.25,36,36);
glPopMatrix();
}
void scene(void) {
glPushMatrix();
glTranslatef(10.0F,20.0F,0.0F);
glRotatef(90.0F,0.0F,1.0F,0.0F);
dessinAxes();
for ( int i = 0 ; i < 6 ; i++ ) {
float angle = i*60.0F;
glPushMatrix();
glRotatef(angle,0.0F,0.0F,1.0F);
motif((objet) ? i%2 : 2);
glPopMatrix(); }
glPopMatrix();
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* -> Ajustement de la camera de visualisation */
void reshape(int x,int y) {
float r = 3.3F;
glViewport(0,0,x,y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double fact =(double) x/y;
if ( fact >= 1.0 )
glOrtho(10.0F-(r+0.5F)*fact,10.0F+(r+0.5F)*fact,20.0F-(r+0.5F),20.0F+(r+0.5F),-r-0.5F,r+0.5F);
else
glOrtho(10.0F-(r+0.5F),10.0F+(r+0.5F),20.0F-(r+0.5F)/fact,20.0F+(r+0.5F)/fact,-r-0.5F,r+0.5F);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const GLfloat light0_position[] = { 1.0,1.0,1.0,0.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,(aff) ? GL_FILL : GL_LINE);
glPushMatrix();
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Erreur OpenGL: %d\n",error);
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(480,350);
glutInitWindowPosition(50,50);
glutCreateWindow("Camera de visualisation en projection parallele orthographique");
init();
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}