Advertisement
HugoBallee

bezier.cpp

Mar 15th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 KB | None | 0 0
  1. // File main.cpp
  2. // Hugo Gallée
  3. // Thu Mar 15 17:07:20 CET 2012
  4.  
  5. /* Boule le long de la courbe
  6.  * Tableau des points permettant de gérer les points de controles
  7.  * On sélectionne le point en se déplaçant avec p et o, ...
  8.  * On sélectionne ensuite si on veut faire monter,
  9.  * descendre amener vers la gauche ou la droite le point.
  10.  *   d : translation à droite
  11.  *   a : à gauche
  12.  *   w : en haut
  13.  *   s : en bas
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include <stdlib.h>
  19. #include <GL/glut.h>
  20. #include "struct.h"
  21.  
  22. /* au cas ou M_PI ne soit defini */
  23. #ifndef M_PI
  24. #define M_PI 3.14159265358979323846
  25. #endif
  26.  
  27. #define ESC 27
  28.  
  29. #define PAS_COURBE 0.05
  30.  
  31. #define speed 0.5
  32.  
  33. float tx = 0.0;
  34. float ty = 0.0;
  35.  
  36. // Tableau des points de contrôles en global ...
  37. Point3 TabPC[4];
  38. // Ordre de la courbre  : Ordre
  39. // Degré de la courbe = Ordre - 1
  40. int Ordre = 4;
  41.  
  42. // Point de controle selectionné
  43. int numPoint = 0;
  44.  
  45. // Fonction Factorielle
  46. int fact(int n) {
  47.   if (n < 2) {
  48.     return 1;
  49.   } else {
  50.     return n * fact(n - 1);
  51.   }
  52. }
  53.  
  54. float Bernstein(int i, int n, float t) {
  55.   return (fact(n) / (fact(i) * fact(n - i)))
  56.     * (pow(t, i) * pow((1 - t), (n - i)));
  57. }
  58.  
  59. /* initialisation d'OpenGL*/
  60. static void init() {
  61.   glClearColor(0.0, 0.0, 0.0, 0.0);
  62.  
  63.   // Initialisation des points de contrôles
  64.   // On choisit de les intialiser selon une ligne
  65.   for (int i = 0; i < Ordre; i++) {
  66.     float a = 30.0 * i / (Ordre - 1) - 15.0;
  67.     TabPC[i] = Point3(a,a,a);
  68.   }
  69. }
  70.  
  71.  
  72. /* Dessin */
  73. void display(void) {
  74.   glClear(GL_COLOR_BUFFER_BIT);
  75.  
  76.   glMatrixMode(GL_MODELVIEW);
  77.   glLoadIdentity();
  78.   //glTranslatef(tx,ty,0.0);
  79.  
  80.   Point3 Ptemp;
  81.  
  82.   TabPC[numPoint]=TabPC[numPoint]+Point3(tx,ty,0);
  83.  
  84.   // Enveloppe des points de controles
  85.   glColor3f (1.0, 0.0, 0.0);
  86.   glBegin(GL_LINE_STRIP);
  87.     for (int i =0; i < Ordre; i++) {
  88.       glVertex3f(TabPC[i].x, TabPC[i].y, TabPC[i].z);
  89.     }
  90.   glEnd();
  91.  
  92.   // Affichage du point de controle courant
  93.   // On se déplace ensuite avec + et -
  94.   // ° d'un point de contrôle au suivant (+)
  95.   // ° d'un point de contrôle au précédent (-)
  96.   glColor3f (0.0, 0.0, 1.0);
  97.   glBegin(GL_LINE_LOOP);
  98.     glVertex3f(TabPC[numPoint].x+0.1, TabPC[numPoint].y+0.1, TabPC[numPoint].z);
  99.     glVertex3f(TabPC[numPoint].x+0.1, TabPC[numPoint].y-0.1, TabPC[numPoint].z);
  100.     glVertex3f(TabPC[numPoint].x-0.1, TabPC[numPoint].y-0.1, TabPC[numPoint].z);
  101.     glVertex3f(TabPC[numPoint].x-0.1, TabPC[numPoint].y+0.1, TabPC[numPoint].z);
  102.   glEnd();
  103.  
  104.   // Dessiner ici la courbe de Bézier.
  105.   // Vous devez avoir implémenté Bernstein précédemment.
  106.  
  107.   glColor3f (0.0, 1.0, 0.0);
  108.   glBegin(GL_LINE_STRIP);
  109.     float t = 0;
  110.     while (t < 1.0 + PAS_COURBE) {
  111.       Ptemp = Point3(0.0,0.0,0.0);
  112.       for (int i = 0; i < Ordre; i++) {
  113.     Ptemp = Ptemp + TabPC[i] * Bernstein(i, Ordre - 1, t);
  114.       }
  115.       glVertex3f(Ptemp.x, Ptemp.y, Ptemp.z);
  116.       t += PAS_COURBE;
  117.     }
  118.   glEnd();
  119.  
  120.   glEnd();
  121.   glFlush();
  122. }
  123.  
  124. /* Au cas ou la fenetre est modifiee ou deplacee */
  125. void reshape(int w, int h) {
  126.   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  127.   glMatrixMode(GL_PROJECTION);
  128.   glLoadIdentity();
  129.   glOrtho(-20, 20, -20, 20, -100, 100);
  130.   glMatrixMode(GL_MODELVIEW);
  131.   glLoadIdentity();
  132. }
  133.  
  134. void keyboard(unsigned char key, int x, int y) {
  135.   switch (key) {
  136.   case 'p':
  137.     if (numPoint < Ordre-1)
  138.       numPoint = numPoint + 1;
  139.     else
  140.       numPoint = 0;
  141.     tx=0;
  142.     ty=0;
  143.     break;
  144.   case 'o':
  145.     if (numPoint > 0)
  146.       numPoint = numPoint - 1;
  147.     else
  148.       numPoint = Ordre-1;
  149.     tx=0;
  150.     ty=0;
  151.     break;
  152.    
  153.   case 'd':
  154.     tx=speed;
  155.     ty=0;
  156.     break;
  157.   case 'a':
  158.     tx=-speed;
  159.     ty=0;
  160.     break;
  161.   case 'w':
  162.     ty=speed;
  163.     tx=0;
  164.     break;
  165.   case 's':
  166.     ty=-speed;
  167.     tx=0;
  168.     break;
  169.   case ESC:
  170.     exit(0);
  171.     break;
  172.    
  173.   default :
  174.     tx=0;
  175.     ty=0;
  176.     break;
  177.   }
  178.   glutPostRedisplay();
  179. }
  180.  
  181. int main(int argc, char **argv) {
  182.   glutInitWindowSize(400, 400);
  183.   glutInit(&argc, argv);
  184.   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  185.   glutCreateWindow("Courbe de Bézier");
  186.   init();
  187.   glutReshapeFunc(reshape);
  188.   glutKeyboardFunc(keyboard);
  189.   glutDisplayFunc(display);
  190.   glutMainLoop();
  191.   //printf("4! = %d\n", fact(4));
  192.   return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement