Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*********************************************************/
  3. /*        TP1: primitives 2D et transformations          */
  4. /*********************************************************/
  5. /*                                                       */
  6. /*       ESGI: Algorithmiques pour l'infographies        */
  7. /*                                                       */
  8. /*********************************************************/
  9. /*                                                       */
  10. /*  Objectif: afficher des formes 2D et les transformer  */
  11. /*                                                       */
  12. /*********************************************************/
  13.  
  14.  
  15.  
  16. #include<windows.h>
  17.  
  18. #ifdef __APPLE__
  19. #include <GLUT/glut.h>
  20. #else
  21. #include <GL/glut.h>
  22. #endif
  23.  
  24.  
  25. #include<stdlib.h>
  26. #include<stdio.h>
  27. #include<math.h>
  28.  
  29.  
  30.  
  31.  
  32. float angle = 0.0;
  33. float genou = 0.0;
  34. float ankle = 0.0;
  35. int etatgenou = 0;
  36. int etat = 1;
  37. float state = 0;
  38. float cameraAngle = 10.0;
  39. float X = 0;
  40. float Y = 0;
  41. float Z = 20;
  42. float P = 0;
  43. float L = 0;
  44. float R = Z;
  45. GLUquadric* cylinder = gluNewQuadric();
  46.  
  47. /* prototypes de fonctions */
  48. void initRendering();                           // Initialisation du rendu
  49. void display();                             // modélisation
  50. void reshape(int w,int h);                      // visualisation
  51. void update(int value);                         // mise à jour: appelle Timer pour l'animation de la scène
  52. void keyboard(unsigned char key, int x, int y); // fonction clavier
  53. void depCamera();
  54. void cuisse();
  55. void jambe();
  56. void pied();
  57. void bras();
  58. void avantbras();
  59. void hand();
  60.  
  61.  
  62. /* Programme principal */
  63. int main(int argc,       // argc: nombre d'arguments, argc vaut au moins 1
  64.           char **argv){  // argv: tableau de chaines de caractères, argv[0] contient le nom du programme lancé (plus un éventuel chemin)
  65.  
  66.  
  67.     /* Initialisation de glut et creation de la fenetre */
  68.     glutInit(&argc, argv);                       // Initialisation
  69.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); // mode d'affichage RGB, et test prafondeur
  70.     glutInitWindowSize(800, 800);                // dimension fenêtre
  71.     glutInitWindowPosition (100, 100);           // position coin haut gauche
  72.     glutCreateWindow("TP1: formes 2D et transformation");  // nom
  73.  
  74.     /* Initialisation d'OpenGL */
  75.     initRendering();
  76.  
  77.     /* Enregistrement des fonctions de rappel
  78.      => initialisation des fonctions callback appelées par glut */
  79.     glutDisplayFunc(display);
  80.     glutReshapeFunc(reshape);
  81.     glutKeyboardFunc(keyboard);
  82.     glutTimerFunc(20,update, 0);
  83.  
  84.     /* rq: le callback de fonction (fonction de rappel) est une fonction qui est passée en argument à une
  85.     autre fonction. Ici, le main fait usage des deux fonctions de rappel (qui fonctionnent en même temps)
  86.     alors qu'il ne les connaît pas par avance.*/
  87.  
  88.  
  89.     /* Entrée dans la boucle principale de glut, traitement des évènements */
  90.     glutMainLoop();         // lancement de la boucle de réception des évènements
  91.     return 0;               // pour finir
  92. }
  93.  
  94.  
  95. /* Initialisation d'OpenGL pour le rendu de la scène */
  96. void initRendering() {
  97.  
  98.     /* Active le z-buffer */
  99.     glEnable(GL_DEPTH_TEST);
  100.  
  101.     /* Activation des couleurs */
  102.     glEnable(GL_COLOR_MATERIAL);
  103.  
  104.     /* définit la couleur d'effacement et la couleur de fond */
  105.     glClearColor(0.0, 0.0, 0.0, 0.0);
  106.  
  107.     /*  définit la taille d'un pixel*/
  108.     glPointSize(2.0);
  109.  
  110. }
  111.  
  112. /* Création de la scène */
  113. void display(){
  114.  
  115.     /* Efface les tampons de couleur et de profondeur de l'image avec la couleur de fond.
  116.     rq: buffer: mémoire tampon, c'est donc une surface en memoire utilisée pour stocker une image*/
  117.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  118.  
  119.     /* la mat de visualisation-modélisation est modifiable par défaut */
  120.     glMatrixMode(GL_MODELVIEW);
  121.  
  122.     /* on charge l'identité dans la matrice de modélisation-visualisation*/
  123.     glLoadIdentity();
  124.  
  125.  
  126.     /* Permet de créer un point de vue. Définit une matrice de modélisation-visualisation et la multiplie
  127.     à droite de lma matrice active, ici l'identité*/
  128.     gluLookAt(X, Y, Z,      // position caméra
  129.               0.0, 0.0, 0.0,      // point de mire
  130.               0.0, 1.0, 0.0);     // vecteur d'orientation caméra
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.     /* A vous de jouer */
  138.     glPushMatrix();
  139.  
  140.         //début du corp
  141.         glTranslatef(0, 5, 0);
  142.         //début du torse
  143.         glPushMatrix();
  144.             glColor3f(1, 1, 1);
  145.             glScalef(2, 0.9, 1.4);
  146.             glutSolidSphere(1.0, 800.0, 800.0);
  147.         glPopMatrix();
  148.         //début du milieu du corp
  149.         glRotatef(90, 1, 0, 0);
  150.         glPushMatrix();
  151.             glScalef(2, 1.4, 2);
  152.             glColor3f(0.5, 1, 1);
  153.             gluCylinder(cylinder, 1.0, 1, 0.3, 800, 800);
  154.         glPopMatrix();
  155.  
  156.         glTranslatef(0, 0, 0.6);
  157.         glPushMatrix();
  158.             glScalef(2, 1.4, 2);
  159.             glColor3f(0.5, 0.5, 1);
  160.             gluCylinder(cylinder, 1, 0.8, 2.2, 800, 800);
  161.         glPopMatrix();
  162.  
  163.         glTranslatef(0, 0, 4.4);
  164.         glPushMatrix();
  165.             glScalef(2, 1.4, 2);
  166.             glColor3f(0.5, 0.5, 0.5);
  167.             gluCylinder(cylinder, 0.8, 0.9, 1.2, 800, 800);
  168.         glPopMatrix();
  169.         glTranslatef(0, 0, 2.4);
  170.         glPushMatrix();
  171.             glScalef(2, 1.4, 2);
  172.             glColor3f(0.5, 1, 1);
  173.             gluCylinder(cylinder, 0.9, 0.9, 0.3, 800, 800);
  174.         glPopMatrix();
  175.         glRotatef(-90, 1, 0, 0);
  176.         //fin du milieu du corp
  177.         glTranslatef(0, -0.6, 0);
  178.         //début de la hanche
  179.         glPushMatrix();
  180.             glColor3f(1, 1, 1);
  181.             glScalef(2, 0, 1.4);
  182.             glutSolidSphere(0.9, 800.0, 800.0);
  183.         glPopMatrix();
  184.  
  185.         glPushMatrix();
  186.             glPushMatrix();
  187.                 glRotatef(angle, 0.2, -1, 0);
  188.                 cuisse();
  189.                 jambe();
  190.                 pied();
  191.             glPopMatrix();
  192.                 glScalef(-1.0, 1.0, 1.0);
  193.                 glRotatef(-angle, 0.2, -1, 0);
  194.                 cuisse();
  195.                 jambe();
  196.                 pied();
  197.         glPopMatrix();
  198.  
  199.         glTranslatef(0, 8, 0);
  200.         glPushMatrix();
  201.             glPushMatrix();
  202.             bras();
  203.             avantbras();
  204.             hand();
  205.             glPopMatrix();
  206.             glScalef(-1.0, 1.0, 1.0);
  207.             bras();
  208.             avantbras();
  209.             hand();
  210.         glPopMatrix();
  211.  
  212.         glRotatef(-90, 1, 0, 0);
  213.         glTranslatef(0, 0, 2);
  214.         glPushMatrix();
  215.             glColor3f(0, 0, 1);
  216.             glutSolidSphere(1.5, 600, 600);
  217.         glPopMatrix();
  218.     glPopMatrix();
  219.  
  220.  
  221.     /* On swap (échange) les buffers, càd, on fait passer l'image calculée et dessinée
  222.     dans le back buffer au buffer qui va l'afficher: le front buffer (en général), c'est le bouble buffering
  223.     Cela évite une image animée sacadée, si elle était directement tracée dans le front buffer*/
  224.     glutSwapBuffers();
  225.  
  226.     /* On force l'affichage */
  227.     glFlush(); // nettoie les fenêtres précédentes
  228. }
  229.  
  230.  
  231.  
  232. /*  Mise en forme de la scène pour l'affichage */
  233. void reshape(int w,  // w: largeur fenêtre
  234.              int h)  // h: hauteur fenêtre
  235. {
  236.     /* Viewport: cadrage. Spécifie la région (position et taille en px) que l'image observée de la scène occupe
  237.     à l'écran => réduction et agrandissement de l'image possible*/
  238.     glViewport(0, 0,(GLsizei) w, (GLsizei) h);
  239.  
  240.     /* Spécifie que la matrice de projection va être modifiée  */
  241.     glMatrixMode(GL_PROJECTION);
  242.     glLoadIdentity();             // matrice courante: l'identité
  243.     //glOrtho(-2.0, 2.0, -2.0, 2.0, 1.5, 20.0);
  244.     //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
  245.  
  246.     /* Perspective avec point de fuite */
  247.     gluPerspective(60.0,                       // angle d'ouverture vertical caméra
  248.                    (GLfloat) w / (GLfloat) h,  // ratio largeur-hauteur
  249.                    1.0,                        // plan proche z=1
  250.                    200.0);                     // plan éloigné z=200
  251.  
  252.  
  253. }
  254.  
  255. /* Fonction de gestion du clavier */
  256. void keyboard(unsigned char key,       // Touche qui a été pressée
  257.                     int x, int y) {    // Coordonnées courante de la souris
  258.  
  259.         switch (key){
  260.  
  261.             case 'p':   /* affichage du carré plein*/
  262.                 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  263.                 glutPostRedisplay();
  264.                 break;
  265.  
  266.             case 'f':   /* affichage en mode fil de fer*/
  267.                 glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  268.                 glutPostRedisplay();
  269.                 break;
  270.  
  271.             case 'l':   /* affichage en mode sommets seuls*/
  272.                 glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);
  273.                 glutPostRedisplay();
  274.                 break;
  275.  
  276.             case 'z':   /* affichage en mode sommets seuls*/
  277.                 L += 0.1;
  278.                 if(L < 88.5){
  279.                     depCamera();
  280.                 }else{
  281.                     L -= 0.1;
  282.                 }
  283.                 glutPostRedisplay();
  284.                 break;
  285.  
  286.             case 's':   /* affichage en mode sommets seuls*/
  287.                 L -= 0.1;
  288.                 if(L > -88.5){
  289.                     depCamera();
  290.                 }else{
  291.                     L += 0.1;
  292.                 }
  293.                 glutPostRedisplay();
  294.                 break;
  295.  
  296.             case 'd':   /* affichage en mode sommets seuls*/
  297.                 P += 0.1;
  298.                 if(P <= 180){
  299.                     depCamera();
  300.                 }else{
  301.                     P -= 0.1;
  302.                 }
  303.                 glutPostRedisplay();
  304.                 break;
  305.  
  306.             case 'q':   /* affichage en mode sommets seuls*/
  307.                 P -= 0.1;
  308.                 if(P >= -180){
  309.                     depCamera();
  310.                 }else{
  311.                     P += 0.1;
  312.                 }
  313.                 glutPostRedisplay();
  314.                 break;
  315.  
  316.             case 'g':   /* affichage en mode sommets seuls*/
  317.                 R += 0.1;
  318.                 depCamera();
  319.                 glutPostRedisplay();
  320.                 break;
  321.  
  322.             case 't':   /* affichage en mode sommets seuls*/
  323.                 R -= 0.1;
  324.                 depCamera();
  325.                 glutPostRedisplay();
  326.                 break;
  327.  
  328.             case 'm':   /* Quitter le programme */
  329.                 exit(0);
  330.         }
  331. }
  332.  
  333. void update(int value){
  334.     if(etat == 1.0){
  335.         if(angle < 45){
  336.             angle += 10;
  337.         }else{
  338.             etat = 0.0;
  339.         }
  340.     }else{
  341.         if(angle > -45){
  342.             angle -= 10;
  343.         }else{
  344.             etat = 1.0;
  345.         }
  346.     }
  347.     if(etatgenou == 0){
  348.         genou += 10;
  349.         if(genou > 45 || genou < -45){
  350.             etatgenou = 1;
  351.         }
  352.     }else{
  353.         genou -= 10;
  354.         if(genou == 0){
  355.             etatgenou = 0;
  356.         }
  357.     }
  358.     /*glRotatef(-cameraAngle, 0.1, -0.1, 1.0);
  359.     gluLookAt(0.0, 0.0, 5.0,      // position caméra
  360.               0.0, 0.0, 0.0,      // point de mire
  361.               0.0, 1.0, 0.0);*/
  362.     glutPostRedisplay();
  363.     glutTimerFunc(1,update, 0);
  364. }
  365.  
  366. void depCamera(){
  367.     X = R * cos(L) * sin(P);
  368.     Y = R * sin(L);
  369.     Z = R * cos(L) * cos(P);
  370. }
  371.  
  372. void cuisse(){
  373.     glPushMatrix();
  374.         glTranslatef(-0.9, 0, 0);
  375.         glPushMatrix();
  376.             glColor3f(0.5, 0.5, 1);
  377.             glScalef(1, 0.7, 1);
  378.             glutSolidSphere(0.9, 800.0, 800.0);
  379.         glPopMatrix();
  380.             glRotatef(90, 1, 0, 0);
  381.             //début cuisse gauche
  382.             glPushMatrix();
  383.                 glColor3f(0.5, 1, 0.5);
  384.                 glScalef(1, 1, 1);
  385.                 gluCylinder(cylinder, 0.9, 0.75, 4, 800, 800);
  386.             glPopMatrix();
  387.         glPopMatrix();
  388. }
  389.  
  390. void jambe(){
  391.     glPushMatrix();
  392.         glTranslatef(-0.9, 0, 0);
  393.         glRotatef(90, 1, 0, 0);
  394.         glTranslatef(0, 0, 4);
  395.         glPushMatrix();
  396.             glColor3f(0.5, 1, 1);
  397.             glutSolidSphere(0.75, 800.0, 800.0);
  398.         glPopMatrix();
  399.         glPushMatrix();
  400.             glColor3f(0.5, 0.5, 1);
  401.             glScalef(1, 1, 1);
  402.             gluCylinder(cylinder, 0.75, 0.6, 3, 800, 800);
  403.         glPopMatrix();
  404.     glPopMatrix();
  405. }
  406.  
  407. void pied(){
  408.     glPushMatrix();
  409.         glTranslatef(-0.9, 0, 0);
  410.         glTranslatef(0, -4, 0);
  411.         glTranslatef(0, -3, 0);
  412.         glPushMatrix();
  413.             glColor3f(0, 0.5, 1);
  414.             glScalef(1, 1, 1);
  415.             glutSolidSphere(0.6, 800.0, 800.0);
  416.         glPopMatrix();
  417.         glRotatef(ankle, -1, 1, 0);
  418.         glPushMatrix();
  419.             glColor3f(0, 1, 1);
  420.             glScalef(1, 1, 1);
  421.             gluCylinder(cylinder, 0.6, 0.3, 2, 800, 800);
  422.         glPopMatrix();
  423.         glPushMatrix();
  424.             glTranslatef(0, 0, 2);
  425.             glColor3f(0, 0.5, 1);
  426.             glScalef(1, 1, 1.6);
  427.             glutSolidSphere(0.3, 800.0, 800.0);
  428.         glPopMatrix();
  429.     glPopMatrix();
  430. }
  431.  
  432. void bras(){
  433. glPushMatrix();
  434.     glTranslatef(0.8, 0, 0);
  435.     glRotatef(90, 0, 1, 0);
  436.     glPushMatrix();
  437.         glTranslatef(0, 0, 0.3);
  438.         glColor3f(1, 0, 0.5);
  439.         gluCylinder(cylinder, 0.7, 0.7, 1.5, 800, 800);
  440.     glPopMatrix();
  441.     glTranslatef(0, 0, 1.3);
  442.     //glRotatef(angle, 0, 0, 1);
  443.     glPushMatrix();
  444.         glTranslatef(0, 0, 0.5);
  445.         glColor3f(1, 0, 1);
  446.         glutSolidSphere(0.7, 800, 800);
  447.     glPopMatrix();
  448.     glTranslatef(0, 0, 0.5);
  449.     glRotatef(90, 1, 0, 0);
  450.     glPushMatrix();
  451.         glColor3f(1, 1, 0.5);
  452.         gluCylinder(cylinder, 0.7, 0.7, 4, 800, 800);
  453.     glPopMatrix();
  454. glPopMatrix();
  455. }
  456.  
  457. void avantbras(){
  458.     glPushMatrix();
  459.         glTranslatef(2.6, 0, 0);
  460.         glRotatef(90, 1, 0, 0);
  461.         glTranslatef(0, 0, 4);
  462.         glPushMatrix();
  463.             glColor3f(1, 0, 1);
  464.             glutSolidSphere(0.7, 800, 800);
  465.         glPopMatrix();
  466.         glPushMatrix();
  467.             glColor3f(1, 0.5, 1);
  468.             gluCylinder(cylinder, 0.7, 0.6, 3.6, 800, 800);
  469.         glPopMatrix();
  470.     glPopMatrix();
  471. }
  472.  
  473. void hand(){
  474.     glPushMatrix();
  475.         glTranslatef(2.6, 0, 0);
  476.         glRotatef(90, 1, 0, 0);
  477.         glTranslatef(0, 0, 7.6);
  478.         glPushMatrix();
  479.             glColor3f(0, 1, 0.5);
  480.             glutSolidSphere(0.6, 600, 600);
  481.         glPopMatrix();
  482.         glTranslatef(0, 0, 0.8);
  483.         glPushMatrix();
  484.             glColor3f(1, 0, 1);
  485.             glScalef(1, 1, 1.5);
  486.             glutSolidCube(1.2);
  487.         glPopMatrix();
  488.     glPopMatrix();
  489. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement