Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File main.cpp
- // Hugo Gallée
- // Thu Mar 15 17:07:20 CET 2012
- /* Boule le long de la courbe
- * Tableau des points permettant de gérer les points de controles
- * On sélectionne le point en se déplaçant avec p et o, ...
- * On sélectionne ensuite si on veut faire monter,
- * descendre amener vers la gauche ou la droite le point.
- * d : translation à droite
- * a : à gauche
- * w : en haut
- * s : en bas
- */
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <GL/glut.h>
- #include "struct.h"
- /* au cas ou M_PI ne soit defini */
- #ifndef M_PI
- #define M_PI 3.14159265358979323846
- #endif
- #define ESC 27
- #define PAS_COURBE 0.05
- #define speed 0.5
- float tx = 0.0;
- float ty = 0.0;
- // Tableau des points de contrôles en global ...
- Point3 TabPC[4];
- // Ordre de la courbre : Ordre
- // Degré de la courbe = Ordre - 1
- int Ordre = 4;
- // Point de controle selectionné
- int numPoint = 0;
- // Fonction Factorielle
- int fact(int n) {
- if (n < 2) {
- return 1;
- } else {
- return n * fact(n - 1);
- }
- }
- float Bernstein(int i, int n, float t) {
- return (fact(n) / (fact(i) * fact(n - i)))
- * (pow(t, i) * pow((1 - t), (n - i)));
- }
- /* initialisation d'OpenGL*/
- static void init() {
- glClearColor(0.0, 0.0, 0.0, 0.0);
- // Initialisation des points de contrôles
- // On choisit de les intialiser selon une ligne
- for (int i = 0; i < Ordre; i++) {
- float a = 30.0 * i / (Ordre - 1) - 15.0;
- TabPC[i] = Point3(a,a,a);
- }
- }
- /* Dessin */
- void display(void) {
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- //glTranslatef(tx,ty,0.0);
- Point3 Ptemp;
- TabPC[numPoint]=TabPC[numPoint]+Point3(tx,ty,0);
- // Enveloppe des points de controles
- glColor3f (1.0, 0.0, 0.0);
- glBegin(GL_LINE_STRIP);
- for (int i =0; i < Ordre; i++) {
- glVertex3f(TabPC[i].x, TabPC[i].y, TabPC[i].z);
- }
- glEnd();
- // Affichage du point de controle courant
- // On se déplace ensuite avec + et -
- // ° d'un point de contrôle au suivant (+)
- // ° d'un point de contrôle au précédent (-)
- glColor3f (0.0, 0.0, 1.0);
- glBegin(GL_LINE_LOOP);
- glVertex3f(TabPC[numPoint].x+0.1, TabPC[numPoint].y+0.1, TabPC[numPoint].z);
- glVertex3f(TabPC[numPoint].x+0.1, TabPC[numPoint].y-0.1, TabPC[numPoint].z);
- glVertex3f(TabPC[numPoint].x-0.1, TabPC[numPoint].y-0.1, TabPC[numPoint].z);
- glVertex3f(TabPC[numPoint].x-0.1, TabPC[numPoint].y+0.1, TabPC[numPoint].z);
- glEnd();
- // Dessiner ici la courbe de Bézier.
- // Vous devez avoir implémenté Bernstein précédemment.
- glColor3f (0.0, 1.0, 0.0);
- glBegin(GL_LINE_STRIP);
- float t = 0;
- while (t < 1.0 + PAS_COURBE) {
- Ptemp = Point3(0.0,0.0,0.0);
- for (int i = 0; i < Ordre; i++) {
- Ptemp = Ptemp + TabPC[i] * Bernstein(i, Ordre - 1, t);
- }
- glVertex3f(Ptemp.x, Ptemp.y, Ptemp.z);
- t += PAS_COURBE;
- }
- glEnd();
- glEnd();
- glFlush();
- }
- /* Au cas ou la fenetre est modifiee ou deplacee */
- void reshape(int w, int h) {
- glViewport(0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-20, 20, -20, 20, -100, 100);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void keyboard(unsigned char key, int x, int y) {
- switch (key) {
- case 'p':
- if (numPoint < Ordre-1)
- numPoint = numPoint + 1;
- else
- numPoint = 0;
- tx=0;
- ty=0;
- break;
- case 'o':
- if (numPoint > 0)
- numPoint = numPoint - 1;
- else
- numPoint = Ordre-1;
- tx=0;
- ty=0;
- break;
- case 'd':
- tx=speed;
- ty=0;
- break;
- case 'a':
- tx=-speed;
- ty=0;
- break;
- case 'w':
- ty=speed;
- tx=0;
- break;
- case 's':
- ty=-speed;
- tx=0;
- break;
- case ESC:
- exit(0);
- break;
- default :
- tx=0;
- ty=0;
- break;
- }
- glutPostRedisplay();
- }
- int main(int argc, char **argv) {
- glutInitWindowSize(400, 400);
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutCreateWindow("Courbe de Bézier");
- init();
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- glutDisplayFunc(display);
- glutMainLoop();
- //printf("4! = %d\n", fact(4));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement