Cette solution intégre le dessin d'une petite sphère en position (0.0, 0.0, 0.0) pour bien vérifier le centrage de l'axe de vision.
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Fevrier 2002 */
/* Utilisation des cameras en OpenGL */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleMenus.h"
static float anglex = 0.0F ;
static int scn = 0 ;
static int anim = 0 ;
void wireCylindre(float r,float h,int n,int m) {
glPushMatrix();
GLUquadricObj *qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_LINE);
gluCylinder(qobj,r,r,h,n,m);
gluDeleteQuadric(qobj);
glPopMatrix();
}
void scene(void) {
glPushMatrix();
glutWireSphere(0.1,18,18);
glTranslatef(3.0f,3.0f,0.0f);
glRotatef(anglex,1.0F,0.0F,0.0F) ;
glutWireCube(2.0);
glPopMatrix();
glPushMatrix();
glTranslatef(-3.0f,3.0f,0.0f);
glRotatef(anglex,1.0F,0.0F,0.0F) ;
glutWireSphere(1.0,18,18);
glPopMatrix();
glPushMatrix();
glTranslatef(3.0f,-3.0f,0.0f);
glRotatef(anglex-90.0F,1.0F,0.0F,0.0F) ;
wireCylindre(1.0,2.0,18,18);
glPopMatrix();
glPushMatrix();
glTranslatef(-3.0f,-3.0f,0.0f);
glRotatef(anglex-90.0F,1.0F,0.0F,0.0F) ;
glutWireCone(1.0,2.0,18,18);
glPopMatrix();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20,1.0,5.0,40.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
switch ( scn ) {
case 0 : glRotatef(180.0F,-0.325F,-0.325F,-0.888F);
glTranslatef(-20.0F,-20.0F,-20.0F);
break;
case 1 : glRotatef(-54.7356F,-1.0F,1.0F,0.0F);
glTranslatef(-20.0F,-20.0F,-20.0F);
break;
case 2 : gluLookAt(20.0,20.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0);
break; }
scene();
glFlush();
glutSwapBuffers();
}
void myinit (void) {
glClearColor(0.5F,0.5F,0.5F,0.0F);
glColor4fv(couleurBlanc());
glEnable(GL_DEPTH_TEST);
}
void reshape(int w, int h) {
glViewport(0,0,w,h);
}
void idle(void) {
anglex += 0.25F;
glutPostRedisplay() ;
}
void key(unsigned char key,int x,int y) {
switch ( key) {
case 0x0D : scn = (scn+1) % 3 ;
glutPostRedisplay();
break ;
case 0x20 : anim = !anim ;
glutIdleFunc((anim) ? idle : NULL);
anglex = 0.0F;
glutPostRedisplay();
break ;
case 0x1B : exit(0) ;
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Cameras pour la perspective");
myinit();
creationMenuBasique();
glutKeyboardFunc(key);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}