Exemple 3: afficher un cube en perspective
Ce programme utilise la matrice de visualisation objet pour afficher les arêtes d’un cube avec une rotation selon deux axes contrôlés par le clavier.
Fichier : examples/prog3.cpp /*
Rotation d'un cube en utilisant la matrice d'objet. On actualise*
l'affichage dès que l'angle a changé.
*/
#include "glut_import.h"
#include "application.h"
#include "vect.h"
class DemoWindow:public Window{
float phi,theta;
public:
DemoWindow():Window("Fenêtre de démonstration"){};
void onCreate(){
phi=0;
theta=0;
printf("Utiliser les fleches de direction du calvier pour faire tourner le cube\nAppuyer sur [ESCAPE] pour quitter\n");
};
void onSpecial(int key,int x,int y){
switch (key){
case GLUT_KEY_LEFT:
theta-=10;
break;
case GLUT_KEY_RIGHT:
theta+=10;
break;
case GLUT_KEY_DOWN:
phi-=10;
break;
case GLUT_KEY_UP:
phi+=10;
break;
}
redisplay();
};
void onReshape(int width,int height){
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70,(float) width/height,0.1,10);
gluLookAt(0,0,4,0,0,0,0,1,0);
};
void onDisplay(){
int T[8][3]={
{-1,-1,-1},
{+1,-1,-1},{-1,+1,-1},{-1,-1,+1},
{+1,+1,-1},{-1,+1,+1},{+1,-1,+1},
{+1,+1,+1}
};
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(phi,1,0,0);
glRotatef(theta,0,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex3iv(T[0]);
glVertex3iv(T[1]);
glVertex3iv(T[0]);
glVertex3iv(T[2]);
glVertex3iv(T[0]);
glVertex3iv(T[3]);
glVertex3iv(T[1]);
glVertex3iv(T[4]);
glVertex3iv(T[2]);
glVertex3iv(T[5]);
glVertex3iv(T[3]);
glVertex3iv(T[6]);
glVertex3iv(T[4]);
glVertex3iv(T[7]);
glVertex3iv(T[5]);
glVertex3iv(T[7]);
glVertex3iv(T[1]);
glVertex3iv(T[6]);
glVertex3iv(T[2]);
glVertex3iv(T[4]);
glVertex3iv(T[3]);
glVertex3iv(T[5]);
glVertex3iv(T[6]);
glVertex3iv(T[7]);
glEnd();
glFlush();
glutSwapBuffers();
};
};
int main(){
DemoWindow w;
Application::run();
} Ce document a été traduit de LaTeX par HeVeA |