Fichier source : DeplacementCamera.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2007 */
/* Un programme OpenGL de deplacement */
/* d'une camera au sein d'une scene */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "DeplacementCameraScene.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;
/* 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.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* 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 alphanumerique du clavier */
void keyboard(unsigned char key,int x,int y) {
switch ( key ) {
case 0x0D : l = !l ;
glutPostRedisplay();
break;
case 0x1B : exit(0); }
}
/* Fonction executee lors de la frappe */
/* d'une touche non alphanumerique du clavier */
void special(int key,int x,int y) {
switch ( key ) {
case GLUT_KEY_UP : px += 0.1F*sin(angle);
pz += 0.1F*cos(angle);
glutPostRedisplay();
break;
case GLUT_KEY_DOWN : px -= 0.1F*sin(angle);
pz -= 0.1F*cos(angle);
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT : angle -= 0.03F;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT : angle += 0.03F;
glutPostRedisplay();
break; }
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(450,200);
glutInitWindowPosition(50,50);
glutCreateWindow("Deplacement de camera");
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}
Fichier source : DeplacementCameraScene.h
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2007 */
/* Une scene OpenGL */
#ifndef DEPLACEMENT_CAMERA_SCENE
#define DEPLACEMENT_CAMERA_SCENE
void scene(int l);
#endif
Fichier source : DeplacementCameraScene.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2007 */
/* Une scene OpenGL */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* Scene dessinee */
void scene(int l) {
int i;
const GLfloat l_pos[] = { 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);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
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);
srand(0);
for ( i = 0 ; i < 100 ; i++ ) {
float x = -10.0F+((float) (rand()%1000)/50.0F);
float z = -10.0F+((float) (rand()%1000)/50.0F);
glPushMatrix();
glTranslatef(x,1.0F,z);
glRotatef(rand()%360,0.0F,1.0F,0.0F);
glScalef(0.1F,2.0F,0.1F);
glutSolidCube(1.0);
glPopMatrix(); }
glPopMatrix();
}