Advertisement
Guest User

Untitled

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