Fichier source : AnimationCameraEtScene2.cpp
/* Un programme OpenGL de deplacement */
/* d'une camera au sein d'une scene animee */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2009 */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* Variables globales */
static int l = 1;
static float px = 0.0F;
static float py = 0.5F;
static float pz = 0.0F;
static float angle = 0.0F;
static float ouv = 60.0F;
static int anim = 0;
static float x[100];
static float z[100];
static float ryl = 0.0F;
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* Configuration d'une camera de visualisation */
/* en projection en perspective */
void reshape(int tx,int ty) {
/* Configuration du viewport d'affichage */
glViewport(0,0,tx,ty);
/* Choix et configuration de la camera */
/* de visualisation */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(ouv,(double) tx/ty,0.01,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* Scene dessinee */
void cylindre(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 scene(int l) {
int i;
const GLfloat l_pos[] = { 5.0F,1.0F,0.0F,1.0F };
const GLfloat blanc[4] = { 1.0F,1.0F,1.0F,1.0F };
const GLfloat c1[4] = { 1.0F,1.0F,1.0F,1.0F };
const GLfloat c2[4] = { 0.1F,0.7F,0.6F,1.0F };
const GLfloat c3[4] = { 1.0F,1.0F,0.0F,1.0F };
if ( l )
glEnable(GL_LIGHTING);
else
glDisable(GL_LIGHTING);
glLightfv(GL_LIGHT1,GL_DIFFUSE,blanc);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glPushMatrix();
glRotatef(ryl,0.0F,1.0F,0.0F);
glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
glPopMatrix();
glPushMatrix();
glRotatef(ryl,0.0F,1.0F,0.0F);
glTranslatef(5.0F,1.0F,0.0F);
glEnable(GL_LIGHT1);
glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc);
glutSolidSphere(0.1,36,36);
glDisable(GL_LIGHT1);
glPopMatrix();
glPushMatrix();
glNormal3f(0.0F,1.0F,0.0F);
glMaterialfv(GL_FRONT,GL_DIFFUSE,c1);
glColor4fv(c1);
glBegin(GL_QUADS);
for ( i = 0 ; i < 20 ; i++ ) {
for ( int j = 0 ; j < 20 ; j++ ) {
if ( (i+j)%2 == 1 ) {
glVertex3f(i-10.0F,0.0F,j-10.0F);
glVertex3f(i-9.0F,0.0F,j-10.0F);
glVertex3f(i-9.0F,0.0F,j-9.0F);
glVertex3f(i-10.0F,0.0F,j-9.0F); } } }
glEnd();
glMaterialfv(GL_FRONT,GL_DIFFUSE,c2);
glColor4fv(c2);
glBegin(GL_QUADS);
for ( i = 0 ; i < 20 ; i++ ) {
for ( int j = 0 ; j < 20 ; j++ ) {
if ( (i+j)%2 == 0 ) {
glVertex3f(i-10.0F,0.0F,j-10.0F);
glVertex3f(i-9.0F,0.0F,j-10.0F);
glVertex3f(i-9.0F,0.0F,j-9.0F);
glVertex3f(i-10.0F,0.0F,j-9.0F); } } }
glEnd();
glMaterialfv(GL_FRONT,GL_DIFFUSE,c3);
glColor4fv(c3);
for ( i = 0 ; i < 100 ; i++ ) {
glPushMatrix();
glTranslatef(x[i],1.0F,z[i]);
cylindre(2.0,0.05,20,20);
glPopMatrix(); }
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
void display(void) {
glClearColor(0.6F,0.6F,0.6F,1.0F) ;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
glPushMatrix();
gluLookAt(px,py,pz,
px+sin(angle),py,pz+cos(angle),
0.0,1.0,0.0);
/* Affichage de la scene */
scene(1);
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention, erreur OpenGL %d\n",error);
}
/* Fonction executee lors de la frappe */
/* d'une touche non alphanumerique du clavier */
int positionAcceptable(float px,float pz) {
for ( int i = 0 ; i < 100 ; i++ ) {
float dx = px-x[i];
float dz = pz-z[i];
float d = sqrt(dx*dx+dz*dz);
if ( d < 0.06F )
return(0); }
return(1);
}
void special(int key,int x,int y) {
switch ( key ) {
case GLUT_KEY_UP : { float npx = px + 0.1F*sin(angle);
float npz = pz + 0.1F*cos(angle);
if ( positionAcceptable(npx,npz) ) {
px = npx;
pz = npz;
glutPostRedisplay(); } }
break;
case GLUT_KEY_DOWN : { float npx = px - 0.1F*sin(angle);
float npz = pz - 0.1F*cos(angle);
if ( positionAcceptable(npx,npz) ) {
px = npx;
pz = npz;
glutPostRedisplay(); } }
break;
case GLUT_KEY_RIGHT : angle -= 0.03F;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT : angle += 0.03F;
glutPostRedisplay();
break; }
}
/* Fonction executee lorsqu'aucun evenement */
/* n'est en attente de gestion */
void idle(void) {
ryl += 1.0F;
glutPostRedisplay();
}
/* Fonction executee lors de la frappe */
/* d'une touche alphanumerique du clavier */
void keyboard(unsigned char key,int x,int y) {
switch ( key ) {
case 0x0D : { anim = !anim;
glutIdleFunc((anim) ? idle : NULL); }
break;
case 0x20 : l = !l ;
glutPostRedisplay();
break;
case 0x1B : exit(0); }
}
/* Fonction principale */
int main(int argc,char **argv) {
srand(0);
for ( int i = 0 ; i < 100 ; i++ ) {
x[i] = -10.0F+((float) (rand()%1000)/50.0F);
z[i] = -10.0F+((float) (rand()%1000)/50.0F); }
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(450,200);
glutInitWindowPosition(50,50);
glutCreateWindow("Deplacement de camera et scene animee");
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}