TP5 : Matrice de visualisation
Énoncé :2007/TP/TP5.pdf
Sources :2007/TP/TP5-src.zip
Exécutables pour Windows :2007/TP/TP5-win32exe.zip
Fichier : 2007/TP/arm.cpp
#include "glut_import.h" /********************************************************************* * GLOBAL DATA *********************************************************************/ struct { int Theta1,Theta2,Theta3,Theta4,Theta5; } Data={ 0,0,0,0,0 }; /********************************************************************* * GLUT CALLBACKS *********************************************************************/ void reshapeFunc(int width,int height){ glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); /* Vue en perspective de même ratio (width/height) que la fenêtre */ gluPerspective(70,(float) width/height,1,20); } /* Cette fonction affiche un pavé de taille length x diameter x diameter parallèle aux axes du repère courant, dont l'extrémité gauche est disposée sur l'origine du repère courant, et déplace ensuite l'origine sur l'extrémité droite du pavé. */ void tube(float length,float diameter){ /* On sauvegarde la matrice du repère courant */ glPushMatrix(); /* Translation/dilatation pour placer les bornes du pavé au bon endroit */ glScalef(length,diameter,diameter); glTranslatef(0.5,0,0); /* Affichage du pavé */ glutSolidCube(1); /* On revient à la matrice de départ */ glPopMatrix(); /* On déplace l'origine sur l'extrémité droite du pavé */ glTranslatef(length,0,0); } void displayFunc(void){ /* On efface */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* On réinitialise la matrice */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* On place la caméra */ gluLookAt(0,0,10,0,0,0,0,1,0); /* La rotation d'angle Theta1 est appliquée à la scène en entier */ glRotatef(Data.Theta1,0,1,0); /* Rotation d'angle Theta2 est ensuite appliquée à la scène en entier */ glRotatef(Data.Theta2,1,0,0); /* On place notre origine locale à l'extrémité gauche du bras (-7,0,0) */ glTranslatef(-7,0,0); /* 1er segment du bras */ tube(4,0.6); /* Sphère au bout du 1er segment */ glutSolidSphere(0.5,100,100); /* Rotation du 2ème segment par rapport à l'origine locale, c'est à dire l'extrémité droite du 1er segment */ glRotatef(Data.Theta3,0,0,1); /* 2ème segment du bras */ tube(4,0.5); /* Sphère au bout du 2ème segment */ glutSolidSphere(0.4,100,100); /* Rotation du 3ème segment par rapport à l'origine locale, c'est à dire l'extrémité droite du 2ème segment */ glRotatef(Data.Theta4,0,0,1); /* 3ème segment du bras */ tube(4,0.4); /* Sphère au bout du 3ème segment */ glutSolidSphere(0.3,100,100); /* On sauvegarde la matrice avant de commencer la première branche de la pince */ glPushMatrix(); /* Angle du premier segment de la branche */ glRotatef(Data.Theta5+30,0,0,1); /* Premier segment de la première branche */ tube(1.5,0.2); /* Sphère au bout du premier segment */ glutSolidSphere(0.2,100,100); /* Angle du second segment de la première branche */ glRotatef(-40,0,0,1); /* Second segment de la première branche */ tube(1,0.2); /* On revient à la matrice juste avant la première branche */ glPopMatrix(); /* Angle du premier segment de la seconde branche */ glRotatef(-Data.Theta5-30,0,0,1); /* Premier segment de la seconde branche */ tube(1.5,0.2); /* Sphère au bout du premier segment */ glutSolidSphere(0.2,100,100); /* Angle du second segment de la seconde branche */ glRotatef(40,0,0,1); /* Second segment de la seconde branche */ tube(1,0.2); glFlush(); glutSwapBuffers(); } void keyboardFunc(unsigned char key,int x,int y){ switch (key){ case '0': Data.Theta1++; break; case '1': Data.Theta1--; break; case '2': Data.Theta2++; break; case '3': Data.Theta2--; break; case '4': Data.Theta3++; break; case '5': Data.Theta3--; break; case '6': Data.Theta4++; break; case '7': Data.Theta4--; break; case '8': Data.Theta5++; break; case '9': Data.Theta5--; break; case 'Q': case 'q': exit(0); } glutPostRedisplay(); } /********************************************************************* * MAIN PROGRAM *********************************************************************/ int main(int argc,char *argv[]){ int a=800,b=600,x,y; glutInit(&argc,argv); x=glutGet(GLUT_SCREEN_WIDTH); y=glutGet(GLUT_SCREEN_HEIGHT); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowPosition((x-a)/2,(y-b)/2); glutInitWindowSize(a,b); glutCreateWindow("Robot arm"); glutReshapeFunc(reshapeFunc); glutDisplayFunc(displayFunc); glutKeyboardFunc(keyboardFunc); glEnable(GL_LIGHTING); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glutMainLoop(); }
Dernière modification le 18/3/2010
Ce document a été traduit de LaTeX par HeVeA