Guest User

Untitled

a guest
May 27th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 KB | None | 0 0
  1. /*
  2. * Mat_info.cxx
  3. *
  4. * Created on: 4 janv. 2012
  5. * Author: schflo
  6. */
  7.  
  8. #include <GL/glut.h>
  9. #include "math.h"
  10. #include <cmath>
  11.  
  12. using namespace std;
  13.  
  14. bool signe = 0;
  15. bool signe2 = 0;
  16. int vitesse = O.5;
  17. bool presse = 0;
  18.  
  19. namespace
  20. {
  21.     GLfloat teta1 = 20;
  22.     GLfloat teta2 = 30 ;
  23.     GLfloat Rotation[16]= { cos(teta1), sin(teta1), 0, 0,
  24.                            -sin(teta1), cos(teta1), 0, 0,
  25.                                0,        0,       1, 0,
  26.                                0,        0,       0, 1 };
  27.  
  28.     GLfloat Translation[16] = { 1,    0,    00,
  29.                                 0,    1,    0,  0,
  30.                                 0,    0,    1,  0,
  31.                                0.43, 0.25, 0.0, 1 };
  32.  
  33.     GLfloat Echelle[16] = { 0.4,  0,   0,  0,
  34.                              0,  0.4,  0,  0,
  35.                              0,   0,  0.4, 0,
  36.                              0,   0,   0,  1 };
  37.  
  38.     GLfloat Q[16] = {    0,         0,      1, 0,
  39.                         0.5,    -sqrt(3)/2, 0, 0,
  40.                      sqrt(3)/2,    0.5,     0, 0,
  41.                          0,         0,      0, 1 };
  42.  
  43.     GLfloat Qt[16] = { 0,      0.5,   sqrt(3)/2, 0,
  44.                        0, -sqrt(3)/2,    0.5,    0,
  45.                        1,       0,        0,     0,
  46.                        0,       0,        0,     1 };
  47.  
  48.     GLfloat Rotation2[16]= { cos(teta2), sin(teta2), 0, 0,
  49.                             -sin(teta2), cos(teta2), 0, 0,
  50.                                  0,          0,      1, 0,
  51.                                  0,          0,      0, 1 };
  52.  
  53.     void Carre ()
  54.     {
  55.         glClearColor(0.0,0.0,0.0,0.0);
  56.         glClear(GL_COLOR_BUFFER_BIT);
  57.         glLoadMatrixf(Echelle);
  58.         glMultMatrixf(Translation);
  59.         glMultMatrixf(Rotation);
  60.         glMultMatrixf(Q);
  61.         glMultMatrixf(Rotation2);
  62.         glMultMatrixf(Qt);
  63.         glPointSize(2.0);
  64.  
  65.         typedef struct
  66.         {
  67.             float x;
  68.             float y;
  69.             float z;
  70.             float r;
  71.             float g;
  72.             float b;
  73.         } point;
  74.  
  75.         point p[8]= {
  76.         { -0.5, -0.5,  0.5, 1.0, 0.0, 0.0 },
  77.         { -0.5,  0.5,  0.5, 0.0, 1.0, 0.0 },
  78.         {  0.5,  0.5,  0.5, 0.0, 0.0, 1.0 },
  79.         {  0.5, -0.5,  0.5, 1.0, 1.0, 1.0 },
  80.         { -0.5, -0.5, -0.5, 1.0, 0.0, 0.0 },
  81.         { -0.5,  0.5, -0.5, 0.0, 1.0, 0.0 },
  82.         {  0.5,  0.5, -0.5, 0.0, 0.0, 1.0 },
  83.         {  0.5, -0.5, -0.5, 1.0, 1.0, 1.0 }};
  84.  
  85.         int f[6][4]= {
  86.         { 0, 1, 2, 3 },
  87.         { 3, 2, 6, 7 },
  88.         { 4, 5, 6, 7 },
  89.         { 0, 1, 5, 4 },
  90.         { 1, 5, 6, 2 },
  91.         { 0, 4, 7, 3 }};
  92.  
  93.         for (int i (0) ; i < 6 ; ++i)
  94.         {
  95.             glBegin(GL_POLYGON);
  96.             for (int j (0) ; j < 4 ; ++j)
  97.             {
  98.                 glColor3f(p[f[i][j]].r,p[f[i][j]].g,p[f[i][j]].b);
  99.                 glVertex3f(p[f[i][j]].x,p[f[i][j]].y,p[f[i][j]].z);
  100.             }
  101.             glEnd();
  102.         }
  103.         glutSwapBuffers();
  104.  
  105.     } // Carre ()
  106.  
  107.     void Clavier (unsigned char key, int x, int y)
  108.     {
  109.         switch (key)
  110.         {
  111.           case 49:
  112.             vitesse=1;
  113.             break;
  114.           case 50:
  115.             vitesse=2;
  116.             break;
  117.           case 51:
  118.             vitesse=3;
  119.             break;
  120.           case 52:
  121.             vitesse=4;
  122.             break;
  123.           case 53:
  124.             vitesse=5;
  125.             break;
  126.           case 54:
  127.             vitesse=6;
  128.             break;
  129.           case 55:
  130.             vitesse=7;
  131.             break;
  132.           case 56:
  133.             vitesse=8;
  134.             break;
  135.           case 57:
  136.             vitesse=9;
  137.             break;
  138.         }
  139.  
  140.     } // Clavier ()
  141.  
  142.     void GestionFleches (int key, int x, int y)
  143.     {
  144.         switch (key)
  145.         {
  146.           case GLUT_KEY_LEFT:
  147.             signe=0;
  148.             break;
  149.           case GLUT_KEY_RIGHT:
  150.             signe=1;
  151.             break;
  152.           case GLUT_KEY_UP:
  153.             //signe2=0;
  154.             break;
  155.           case GLUT_KEY_DOWN:
  156.             //signe2=0;
  157.             break;
  158.         }
  159.  
  160.     } // GestionFleches ()
  161.  
  162.     void idle ()
  163.     {
  164.         if (!signe)
  165.             teta1 -= 0.001 * vitesse;
  166.         if (signe)
  167.             teta1 += 0.001 * vitesse;
  168.         if (!signe)
  169.             teta2 += 0.001 * vitesse;
  170.         if (signe)
  171.             teta2 -= 0.001 * vitesse;
  172.  
  173.         Rotation[0] =  cos(teta1);
  174.         Rotation[1] =  sin(teta1);
  175.         Rotation[4] = -sin(teta1);
  176.         Rotation[5] =  cos(teta1);
  177.         glLoadMatrixf(Rotation);
  178.  
  179.         Rotation2[0] =  cos(teta2);
  180.         Rotation2[1] =  sin(teta2);
  181.         Rotation2[4] = -sin(teta2);
  182.         Rotation2[5] =  cos(teta2);
  183.         glLoadMatrixf(Rotation2);
  184.  
  185.         glutPostRedisplay();
  186.  
  187.     } // idle ()
  188.  
  189.     void reshape (int x, int y)
  190.     {
  191.         if (x < y)
  192.             glViewport(0, (y-x)/2, x, x);
  193.         else
  194.             glViewport((x-y)/2, 0, y, y) ;
  195.  
  196.     } // reshape ()
  197.  
  198.     int xold;
  199.     int yold;
  200.  
  201.     int anglex;
  202.     int angley;
  203.  
  204.     void souris (int boutton, int state, int x, int y)
  205.     {
  206.         if (boutton == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  207.         {
  208.             presse = true;
  209.  
  210. //          vitesse1 = 0;
  211. //          vitesse2 = 0;
  212.  
  213.             anglex = anglex + (x-xold);
  214.             angley = angley + (y-yold);
  215.         }
  216.         else
  217.             presse = false;
  218.  
  219. //      /* si on appuie sur le bouton gauche */
  220. //      if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  221. //      {
  222. //          presse = true;
  223. //          xold = x; /* on sauvegarde la position de la souris */
  224. //          yold=y;
  225. //      }
  226. //      /* si on relache le bouton gauche */
  227. //      if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
  228. //          presse = false; /* le booleen presse passe a 0 (faux) */
  229.  
  230.     } // souris()
  231.  
  232.     void mouvement_souris (int x, int y)
  233.     {
  234.         if (presse)
  235.         {
  236.             anglex = anglex + (x-xold);
  237.             angley = angley + (y-yold);
  238.         }
  239.  
  240.         glutPostRedisplay();
  241.  
  242. //      if (presse) /* si le bouton gauche est presse */
  243. //      {
  244. //          /* on modifie les angles de rotation de l’objet
  245. //          en fonction de la position actuelle de la souris et de la derniere
  246. //          position sauvegardee */
  247. //          anglex = anglex + (x-xold);
  248. //          angley = angley + (y-yold);
  249. //          glutPostRedisplay(); /* on demande un rafraichissement de l’affichage */
  250. //      }
  251. //      xold=x; /* sauvegarde des valeurs courante de le position de la souris */
  252. //      yold=y;
  253.  
  254.     } // mouvement_souris()
  255.  
  256.  
  257. } // namespace
  258.  
  259. int main (int argc, char * argv[])
  260. {
  261.     glutInit(&argc, argv);
  262.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  263.     glutCreateWindow("Carre");
  264.     glClearColor(1.0, 1.0, 1.0, 0.0);
  265.  
  266.     glutInitWindowPosition(200, 200);
  267.     glutInitWindowSize(250, 250);
  268.     glClearColor(0.0, 0.0, 0.0, 0.0);
  269.  
  270.     glClear(GL_COLOR_BUFFER_BIT);
  271.     glutDisplayFunc(Carre);
  272.     glutKeyboardFunc(Clavier);
  273.     glutSpecialFunc(GestionFleches);
  274.     glutIdleFunc(idle);
  275.     glutReshapeFunc(reshape);
  276.     glutMouseFunc(souris);
  277.     glutMotionFunc(mouvement_souris);
  278.  
  279.     glutMainLoop();
  280.  
  281.     return 0;
  282.  
  283. } // main ()
Add Comment
Please, Sign In to add comment