Fractale(s) tpe (opengl // glut)

Soyez le premier à donner votre avis sur cette source.

Snippet vu 6 450 fois - Téléchargée 31 fois

Contenu du snippet

Bonjour à tous !

http://kenjimax.free.fr/fractales/tpe.zip

(Tous les programmes fonctionnent, mais je vous déconseille le "fractale(beta)" ;)

Ce sont des programmes très basiques, qui permettent de générer deux types de fractales (Mandelbrot [puissance 2 et 3] et un générateur de terrain très escarpé [type building et montagne]).
Le code est relativement simple, la partie sur mandelbrot est commenté, pas l'autre (pour l'instant).

Les commandes sont plutot simple :

Pour le programme mandelbrot :
- Pour quitter "echap"
- clique gauche de la souris = zoom +
- clique droit de la souris = zoom -
- touches "+" et "-" pour augmenter ou diminuer le nombre d'itérations (la précision du tracé) [c'est très important après un zoom]
- touches a,z,e,r pour régler la résolution (si vous obtenez un quadrillage, ou un zoom qui fonctionne mal)
- touche d pour revenir aux valeurs par défaut de zoom

Pour l'autre programme :
- Pour quitter "echap"
- Pour faire apparaitre le terrain, appuyer sur A
- Pour faire tournoyer l'objet, appuyer sur bouton gauche de la souris plus glissage
- Pour stoper la rotation automatique : bouton central de la souris
- Pour accelérer la rotation : bouton droit

Source / Exemple :


//montagneV2.exe :

/********************************************************/
/*                  montagnetpe.c                       */
/********************************************************/
/* Affiche a l'ecran un terrain accidenté               */
/********************************************************/

/*
- Pour faire apparaitre le terrain, appuyer sur A
- Pour faire tournoyer l'objet, appuyer sur bouton gauche de la souris plus glissage
- Pour stoper la rotation automatique : bouton central de la souris
- Pour accelérer la rotation : bouton droit

  • /
#include <GL/glut.h> #include <stdlib.h> typedef struct { float x; float y; float z; float r; float g; float b; } point; point p[][8]={ {{-0.125,-0.00125, 0.125,1.0,1.0,1.0}, {-0.125, 0.00125, 0.125,0.0,1.0,0.0}, { 0.125, 0.00125, 0.125,0.0,0.0,1.0}, { 0.125,-0.00125, 0.125,1.0,1.0,1.0}, {-0.125,-0.00125,-0.125,1.0,1.0,1.0}, {-0.125, 0.00125,-0.125,1.0,0.0,0.0}, { 0.125, 0.00125,-0.125,1.0,1.0,0.0}, { 0.125,-0.00125,-0.125,1.0,1.0,1.0}}, {{-0.125-0.25,-0.00125, 0.125,1.0,0.0,0.0}, {-0.125-0.25, 0.00125, 0.125,0.0,1.0,0.0}, { 0.125-0.25, 0.00125, 0.125,0.0,0.0,1.0}, { 0.125-0.25,-0.00125, 0.125,1.0,1.0,1.0}, {-0.125-0.25,-0.00125,-0.125,1.0,0.0,0.0}, {-0.125-0.25, 0.00125,-0.125,1.0,0.0,0.0}, { 0.125-0.25, 0.00125,-0.125,1.0,1.0,0.0}, { 0.125-0.25,-0.00125,-0.125,1.0,1.0,1.0}}, {{-0.125+0.25,-0.00125, 0.125,1.0,0.0,0.0}, {-0.125+0.25, 0.00125, 0.125,0.0,1.0,0.0}, { 0.125+0.25, 0.00125, 0.125,0.0,0.0,1.0}, { 0.125+0.25,-0.00125, 0.125,1.0,1.0,1.0}, {-0.125+0.25,-0.00125,-0.125,1.0,0.0,0.0}, {-0.125+0.25, 0.00125,-0.125,1.0,0.0,0.0}, { 0.125+0.25, 0.00125,-0.125,1.0,1.0,0.0}, { 0.125+0.25,-0.00125,-0.125,1.0,1.0,1.0}}, {{-0.125+0.25,-0.00125, 0.125+0.25,1.0,0.0,0.0}, {-0.125+0.25, 0.00125, 0.125+0.25,0.0,1.0,0.0}, { 0.125+0.25, 0.00125, 0.125+0.25,0.0,0.0,1.0}, { 0.125+0.25,-0.00125, 0.125+0.25,1.0,1.0,1.0}, {-0.125+0.25,-0.00125,-0.125+0.25,1.0,0.0,0.0}, {-0.125+0.25, 0.00125,-0.125+0.25,1.0,0.0,0.0}, { 0.125+0.25, 0.00125,-0.125+0.25,1.0,1.0,0.0}, { 0.125+0.25,-0.00125,-0.125+0.25,1.0,1.0,1.0}}, {{-0.125,-0.00125, 0.125-0.25,1.0,0.0,0.0}, {-0.125, 0.00125, 0.125-0.25,0.0,1.0,0.0}, { 0.125, 0.00125, 0.125-0.25,0.0,0.0,1.0}, { 0.125,-0.00125, 0.125-0.25,1.0,1.0,1.0}, {-0.125,-0.00125,-0.125-0.25,1.0,0.0,0.0}, {-0.125, 0.00125,-0.125-0.25,1.0,0.0,0.0}, { 0.125, 0.00125,-0.125-0.25,1.0,1.0,0.0}, { 0.125,-0.00125,-0.125-0.25,1.0,1.0,1.0}}, {{-0.125-0.25,-0.00125, 0.125-0.25,1.0,0.0,0.0}, {-0.125-0.25, 0.00125, 0.125-0.25,0.0,1.0,0.0}, { 0.125-0.25, 0.00125, 0.125-0.25,0.0,0.0,1.0}, { 0.125-0.25,-0.00125, 0.125-0.25,1.0,1.0,1.0}, {-0.125-0.25,-0.00125,-0.125-0.25,1.0,0.0,0.0}, {-0.125-0.25, 0.00125,-0.125-0.25,1.0,0.0,0.0}, { 0.125-0.25, 0.00125,-0.125-0.25,1.0,1.0,0.0}, { 0.125-0.25,-0.00125,-0.125-0.25,1.0,1.0,1.0}}, {{-0.125-0.25,-0.00125, 0.125+0.25,1.0,0.0,0.0}, {-0.125-0.25, 0.00125, 0.125+0.25,0.0,1.0,0.0}, { 0.125-0.25, 0.00125, 0.125+0.25,0.0,0.0,1.0}, { 0.125-0.25,-0.00125, 0.125+0.25,1.0,1.0,1.0}, {-0.125-0.25,-0.00125,-0.125+0.25,1.0,0.0,0.0}, {-0.125-0.25, 0.00125,-0.125+0.25,1.0,0.0,0.0}, { 0.125-0.25, 0.00125,-0.125+0.25,1.0,1.0,0.0}, { 0.125-0.25,-0.00125,-0.125+0.25,1.0,1.0,1.0}}, {{-0.125,-0.00125, 0.125+0.25,1.0,0.0,0.0}, {-0.125, 0.00125, 0.125+0.25,0.0,1.0,0.0}, { 0.125, 0.00125, 0.125+0.25,0.0,0.0,1.0}, { 0.125,-0.00125, 0.125+0.25,1.0,1.0,1.0}, {-0.125,-0.00125,-0.125+0.25,1.0,0.0,0.0}, {-0.125, 0.00125,-0.125+0.25,1.0,0.0,0.0}, { 0.125, 0.00125,-0.125+0.25,1.0,1.0,0.0}, { 0.125,-0.00125,-0.125+0.25,1.0,1.0,1.0}}, {{-0.125+0.25,-0.00125, 0.125-0.25,1.0,0.0,0.0}, {-0.125+0.25, 0.00125, 0.125-0.25,0.0,1.0,0.0}, { 0.125+0.25, 0.00125, 0.125-0.25,0.0,0.0,1.0}, { 0.125+0.25,-0.00125, 0.125-0.25,1.0,1.0,1.0}, {-0.125+0.25,-0.00125,-0.125-0.25,1.0,0.0,0.0}, {-0.125+0.25, 0.00125,-0.125-0.25,1.0,0.0,0.0}, { 0.125+0.25, 0.00125,-0.125-0.25,1.0,1.0,0.0}, { 0.125+0.25,-0.00125,-0.125-0.25,1.0,1.0,1.0}}, //entourage exterieur {{-0.125-0.5,-0.00125, 0.125,0.0,0.0,0.0}, {-0.125-0.5, 0.00125, 0.125,0.0,1.0,0.0}, { 0.125-0.5, 0.00125, 0.125,0.0,0.0,1.0}, { 0.125-0.5,-0.00125, 0.125,0.0,0.0,0.0}, {-0.125-0.5,-0.00125,-0.125,0.0,0.0,0.0}, {-0.125-0.5, 0.00125,-0.125,1.0,0.0,0.0}, { 0.125-0.5, 0.00125,-0.125,1.0,1.0,0.0}, { 0.125-0.5,-0.00125,-0.125,0.0,0.0,0.0}}, {{-0.125+0.5,-0.00125, 0.125,0.0,0.0,0.0}, {-0.125+0.5, 0.00125, 0.125,0.0,1.0,0.0}, { 0.125+0.5, 0.00125, 0.125,0.0,0.0,1.0}, { 0.125+0.5,-0.00125, 0.125,0.0,0.0,0.0}, {-0.125+0.5,-0.00125,-0.125,0.0,0.0,0.0}, {-0.125+0.5, 0.00125,-0.125,1.0,0.0,0.0}, { 0.125+0.5, 0.00125,-0.125,1.0,1.0,0.0}, { 0.125+0.5,-0.00125,-0.125,0.0,0.0,0.0}}, {{-0.125+0.5,-0.00125, 0.125+0.5,0.0,0.0,0.0}, {-0.125+0.5, 0.00125, 0.125+0.5,0.0,1.0,0.0}, { 0.125+0.5, 0.00125, 0.125+0.5,0.0,0.0,1.0}, { 0.125+0.5,-0.00125, 0.125+0.5,0.0,0.0,0.0}, {-0.125+0.5,-0.00125,-0.125+0.5,0.0,0.0,0.0}, {-0.125+0.5, 0.00125,-0.125+0.5,1.0,0.0,0.0}, { 0.125+0.5, 0.00125,-0.125+0.5,1.0,1.0,0.0}, { 0.125+0.5,-0.00125,-0.125+0.5,0.0,0.0,0.0}}, {{-0.125,-0.00125, 0.125-0.5,0.0,0.0,0.0}, {-0.125, 0.00125, 0.125-0.5,0.0,1.0,0.0}, { 0.125, 0.00125, 0.125-0.5,0.0,0.0,1.0}, { 0.125,-0.00125, 0.125-0.5,0.0,0.0,0.0}, {-0.125,-0.00125,-0.125-0.5,0.0,0.0,0.0}, {-0.125, 0.00125,-0.125-0.5,1.0,0.0,0.0}, { 0.125, 0.00125,-0.125-0.5,1.0,1.0,0.0}, { 0.125,-0.00125,-0.125-0.5,0.0,0.0,0.0}}, {{-0.125-0.5,-0.00125, 0.125-0.5,0.0,0.0,0.0}, {-0.125-0.5, 0.00125, 0.125-0.5,0.0,1.0,0.0}, { 0.125-0.5, 0.00125, 0.125-0.5,0.0,0.0,1.0}, { 0.125-0.5,-0.00125, 0.125-0.5,0.0,0.0,0.0}, {-0.125-0.5,-0.00125,-0.125-0.5,0.0,0.0,0.0}, {-0.125-0.5, 0.00125,-0.125-0.5,1.0,0.0,0.0}, { 0.125-0.5, 0.00125,-0.125-0.5,1.0,1.0,0.0}, { 0.125-0.5,-0.00125,-0.125-0.5,0.0,0.0,0.0}}, {{-0.125-0.5,-0.00125, 0.125+0.5,0.0,0.0,0.0}, {-0.125-0.5, 0.00125, 0.125+0.5,0.0,1.0,0.0}, { 0.125-0.5, 0.00125, 0.125+0.5,0.0,0.0,1.0}, { 0.125-0.5,-0.00125, 0.125+0.5,0.0,0.0,0.0}, {-0.125-0.5,-0.00125,-0.125+0.5,0.0,0.0,0.0}, {-0.125-0.5, 0.00125,-0.125+0.5,1.0,0.0,0.0}, { 0.125-0.5, 0.00125,-0.125+0.5,1.0,1.0,0.0}, { 0.125-0.5,-0.00125,-0.125+0.5,0.0,0.0,0.0}}, {{-0.125,-0.00125, 0.125+0.5,0.0,0.0,0.0}, {-0.125, 0.00125, 0.125+0.5,0.0,1.0,0.0}, { 0.125, 0.00125, 0.125+0.5,0.0,0.0,1.0}, { 0.125,-0.00125, 0.125+0.5,0.0,0.0,0.0}, {-0.125,-0.00125,-0.125+0.5,0.0,0.0,0.0}, {-0.125, 0.00125,-0.125+0.5,1.0,0.0,0.0}, { 0.125, 0.00125,-0.125+0.5,1.0,1.0,0.0}, { 0.125,-0.00125,-0.125+0.5,0.0,0.0,0.0}}, {{-0.125+0.5,-0.00125, 0.125-0.5,0.0,0.0,0.0}, {-0.125+0.5, 0.00125, 0.125-0.5,0.0,1.0,0.0}, { 0.125+0.5, 0.00125, 0.125-0.5,0.0,0.0,1.0}, { 0.125+0.5,-0.00125, 0.125-0.5,0.0,0.0,0.0}, {-0.125+0.5,-0.00125,-0.125-0.5,0.0,0.0,0.0}, {-0.125+0.5, 0.00125,-0.125-0.5,1.0,0.0,0.0}, { 0.125+0.5, 0.00125,-0.125-0.5,1.0,1.0,0.0}, { 0.125+0.5,-0.00125,-0.125-0.5,0.0,0.0,0.0}}, //reste de l'entourage {{-0.125+0.5,-0.00125, 0.125+0.25,0.0,0.0,0.0}, {-0.125+0.5, 0.00125, 0.125+0.25,0.0,1.0,0.0}, { 0.125+0.5, 0.00125, 0.125+0.25,0.0,0.0,1.0}, { 0.125+0.5,-0.00125, 0.125+0.25,0.0,0.0,0.0}, {-0.125+0.5,-0.00125,-0.125+0.25,0.0,0.0,0.0}, {-0.125+0.5, 0.00125,-0.125+0.25,1.0,0.0,0.0}, { 0.125+0.5, 0.00125,-0.125+0.25,1.0,1.0,0.0}, { 0.125+0.5,-0.00125,-0.125+0.25,0.0,0.0,0.0}}, {{-0.125-0.25,-0.00125, 0.125-0.5,0.0,0.0,0.0}, {-0.125-0.25, 0.00125, 0.125-0.5,0.0,1.0,0.0}, { 0.125-0.25, 0.00125, 0.125-0.5,0.0,0.0,1.0}, { 0.125-0.25,-0.00125, 0.125-0.5,0.0,0.0,0.0}, {-0.125-0.25,-0.00125,-0.125-0.5,0.0,0.0,0.0}, {-0.125-0.25, 0.00125,-0.125-0.5,1.0,0.0,0.0}, { 0.125-0.25, 0.00125,-0.125-0.5,1.0,1.0,0.0}, { 0.125-0.25,-0.00125,-0.125-0.5,0.0,0.0,0.0}}, {{-0.125+0.25,-0.00125, 0.125+0.5,0.0,0.0,0.0}, {-0.125+0.25, 0.00125, 0.125+0.5,0.0,1.0,0.0}, { 0.125+0.25, 0.00125, 0.125+0.5,0.0,0.0,1.0}, { 0.125+0.25,-0.00125, 0.125+0.5,0.0,0.0,0.0}, {-0.125+0.25,-0.00125,-0.125+0.5,0.0,0.0,0.0}, {-0.125+0.25, 0.00125,-0.125+0.5,1.0,0.0,0.0}, { 0.125+0.25, 0.00125,-0.125+0.5,1.0,1.0,0.0}, { 0.125+0.25,-0.00125,-0.125+0.5,0.0,0.0,0.0}}, {{-0.125+0.25,-0.00125, 0.125-0.5,0.0,0.0,0.0}, {-0.125+0.25, 0.00125, 0.125-0.5,0.0,1.0,0.0}, { 0.125+0.25, 0.00125, 0.125-0.5,0.0,0.0,1.0}, { 0.125+0.25,-0.00125, 0.125-0.5,0.0,0.0,0.0}, {-0.125+0.25,-0.00125,-0.125-0.5,0.0,0.0,0.0}, {-0.125+0.25, 0.00125,-0.125-0.5,1.0,0.0,0.0}, { 0.125+0.25, 0.00125,-0.125-0.5,1.0,1.0,0.0}, { 0.125+0.25,-0.00125,-0.125-0.5,0.0,0.0,0.0}}, {{-0.125-0.5,-0.00125, 0.125+0.25,0.0,0.0,0.0}, {-0.125-0.5, 0.00125, 0.125+0.25,0.0,1.0,0.0}, { 0.125-0.5, 0.00125, 0.125+0.25,0.0,0.0,1.0}, { 0.125-0.5,-0.00125, 0.125+0.25,0.0,0.0,0.0}, {-0.125-0.5,-0.00125,-0.125+0.25,0.0,0.0,0.0}, {-0.125-0.5, 0.00125,-0.125+0.25,1.0,0.0,0.0}, { 0.125-0.5, 0.00125,-0.125+0.25,1.0,1.0,0.0}, { 0.125-0.5,-0.00125,-0.125+0.25,0.0,0.0,0.0}}, {{-0.125-0.5,-0.00125, 0.125-0.25,0.0,0.0,0.0}, {-0.125-0.5, 0.00125, 0.125-0.25,0.0,1.0,0.0}, { 0.125-0.5, 0.00125, 0.125-0.25,0.0,0.0,1.0}, { 0.125-0.5,-0.00125, 0.125-0.25,0.0,0.0,0.0}, {-0.125-0.5,-0.00125,-0.125-0.25,0.0,0.0,0.0}, {-0.125-0.5, 0.00125,-0.125-0.25,1.0,0.0,0.0}, { 0.125-0.5, 0.00125,-0.125-0.25,1.0,1.0,0.0}, { 0.125-0.5,-0.00125,-0.125-0.25,0.0,0.0,0.0}}, {{-0.125-0.25,-0.00125, 0.125+0.5,0.0,0.0,0.0}, {-0.125-0.25, 0.00125, 0.125+0.5,0.0,1.0,0.0}, { 0.125-0.25, 0.00125, 0.125+0.5,0.0,0.0,1.0}, { 0.125-0.25,-0.00125, 0.125+0.5,0.0,0.0,0.0}, {-0.125-0.25,-0.00125,-0.125+0.5,0.0,0.0,0.0}, {-0.125-0.25, 0.00125,-0.125+0.5,1.0,0.0,0.0}, { 0.125-0.25, 0.00125,-0.125+0.5,1.0,1.0,0.0}, { 0.125-0.25,-0.00125,-0.125+0.5,0.0,0.0,0.0}}, {{-0.125+0.5,-0.00125, 0.125-0.25,0.0,0.0,0.0}, {-0.125+0.5, 0.00125, 0.125-0.25,0.0,1.0,0.0}, { 0.125+0.5, 0.00125, 0.125-0.25,0.0,0.0,1.0}, { 0.125+0.5,-0.00125, 0.125-0.25,0.0,0.0,0.0}, {-0.125+0.5,-0.00125,-0.125-0.25,0.0,0.0,0.0}, {-0.125+0.5, 0.00125,-0.125-0.25,1.0,0.0,0.0}, { 0.125+0.5, 0.00125,-0.125-0.25,1.0,1.0,0.0}, { 0.125+0.5,-0.00125,-0.125-0.25,0.0,0.0,0.0}} }; int f[6][4]={ {0,1,2,3}, {3,2,6,7}, {4,5,6,7}, {0,1,5,4}, {1,5,6,2}, {0,4,7,3}}; char presse; double anglex,angley,x,y,xold,yold; double force=300000; float hasard; bool rotation=true; void affichage(); void clavier(unsigned char touche,int x,int y); void reshape(int x,int y); void idle(int value); void mouse(int bouton,int etat,int x,int y); void mousemotion(int x,int y); void generation (); void init(); int main(int argc,char **argv) { angley=10; anglex=10; glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("cube"); glutFullScreen(); glClearColor(0.0,0.0,0.0,0.0); glColor3f(1.0,1.0,1.0); glPointSize(1.0); glEnable(GL_DEPTH_TEST); glutSetCursor(GLUT_CURSOR_CROSSHAIR); glutDisplayFunc(affichage); glutKeyboardFunc(clavier); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMotionFunc(mousemotion); glutTimerFunc(5,idle,0); glutMainLoop(); return 0; } void affichage() { int i,j,h; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(-angley,1.0,0.0,0.0); glRotatef(-anglex,0.0,1.0,0.0); for(h=0; h<25;h++) { for (i=0;i<6;i++) { glBegin(GL_QUADS); for (j=0;j<4;j++) { glColor3f(p[h][f[i][j]].r,p[h][f[i][j]].g,p[h][f[i][j]].b); glVertex3f(p[h][f[i][j]].x,p[h][f[i][j]].y-0.5,p[h][f[i][j]].z); } glEnd(); } } glutSwapBuffers(); } void clavier(unsigned char touche,int x,int y) { switch (touche) { case 'p': glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glutPostRedisplay(); break; case 'f': glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glutPostRedisplay(); break; case 's' : glPolygonMode(GL_FRONT_AND_BACK,GL_POINT); glutPostRedisplay(); break; case 'd': glEnable(GL_DEPTH_TEST); glutPostRedisplay(); break; case 'D': glDisable(GL_DEPTH_TEST); glutPostRedisplay(); break; case 'a' : generation(); break; case '+' : force=100000; break; case '-' : if(force>100000) force=30000;break; case '*' : force=100000; break; case '/' : force=1000000; break; case 27 : /*la touche echap permet de quitter le programme */ exit(0); } } void reshape(int x,int y) { if (x<y) glViewport(0,(y-x)/2,x,x); else glViewport((x-y)/2,0,y,y); } void mouse(int button, int state,int x,int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { presse = 1; xold = x; yold=y; } if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) { presse=0; } if (button == GLUT_RIGHT_BUTTON) { rotation=true; glutTimerFunc(50,idle,0); } if (button == GLUT_MIDDLE_BUTTON) { rotation=false; } } void mousemotion(int x,int y) { if (presse) { anglex=anglex+(x-xold); angley=angley+(y-yold); glutPostRedisplay(); } xold=x; yold=y; } void generation () { for(int h=0; h<25 ; h++) { for(int i=0; i<8; i++) { if(i==1 || i==2 || i==5 || i==6) { hasard=rand(); hasard=hasard/force; p[h][i].y+=hasard; } } } glutPostRedisplay(); } void idle(int value) { if(rotation==true) { anglex++; glutTimerFunc(50,idle,0); glutPostRedisplay(); } } //FRACTALE.CPP : // main.cpp #include <gl\glut.h> //inclusion des libraires openGL #include <stdio.h> //inclusion de la libraire standart entrée/sortie double _a = -2, _b = 2, _c = 1.2, _d = -1.2; //définitions de variables double (32bits) int L=1024, H=768; //variables définissant la hauteur et la largeur int choix_couleur=0; //non-utilisé pour l'instant double itermax=30; //nombre maximum d'itération (32bits) double pas_x, pas_y; //décalage d'affixe pour décalage d'un pixel double rouge=1,vert=0,bleu=0; //non-utilisé double force_du_zoom=0.04; //force du zoom :) double _x,_y; //variable pour gérer le zoom float temp; //variable flotante(non entière) //utilisée pour garder une variable en mémoire class complex_tpe //class qui gère les nombres complexes { //variable défini en public car le programme public : //doit avant tout être rapide double rel, img; //partie réelle et imaginaire double mod_carre; //module du complexe au carré }; complex_tpe nombre1, nombre2, nombre3, nombre4; //création de 4 nombres complexes void display(void) //fonction qui gère l'affichage { glClear(GL_COLOR_BUFFER_BIT); //On efface le tampon de profondeur glColor3f(1.0,1.0,1.0); //On défini le blanc comme couleur courante glPointSize(1.0); //on spécifie la taille d'un point (ici 1 pixel) glColor3d(rouge,vert,bleu); //si on a choisi une autre couleur que blanc, elle //devient couleur courante for(double i = _a , _i =0; i<= _b; i+= pas_x, _i++) //boucle qui s'exécute "nombre max d'itération" fois (30 par défaut) { for(double j = _d , _j=0 ; j<= _c; j+= pas_y, _j++) //boucle qui s'éxécute en faisant varier le pixel courant { nombre3.rel = i ; nombre3.img = j; //les coordonnées du point sont utilisées pour définir son affixe nombre2.rel = i ; nombre2.img = j; //idem mais pour un autre nombre complexe nombre1.rel = i ; nombre1.img = j; //idem mais pour un autre nombre complexe static double iter; //variable static utilisée pour connaitre le nombre d'itération déjà effectué for(iter = 0 ; iter<=itermax; iter++) //début de l'algorithme proprement dit { temp = nombre1.rel; nombre1.rel = (nombre1.rel * nombre1.rel) - (nombre1.img * nombre1.img); //on défini la partie réelle de Xn+1 nombre1.img = 2 * temp * nombre1.img; //puis sa partie imaginaire nombre4.rel = nombre1.rel + nombre2.rel; //on calcul Xn + C nombre4.img = nombre1.img + nombre2.img; //idem mais pour la partie imaginaire nombre1.rel = nombre4.rel; //on donne la valeur finale a Xn+1 nombre1.img = nombre4.img; //idem mais pour la partie imaginaire nombre1.mod_carre= (nombre1.rel * nombre1.rel) + (nombre1.img * nombre1.img); //on calcul le module if(nombre1.mod_carre>=4)break; //si le module est supérieur ou égal à 4, on arrête et on ne l'affiche pas } if(nombre1.mod_carre<=4) //si le module est inférieur ou égal à 4, on lance l'affichage d'un poiunt blanc { glColor3d(1.0,0.0,0.0); //on défini la couleur rouge pour afficher glBegin(GL_POINTS); //on commence le dessin glVertex3f(_i,_j,0.0); //on place le point sur l'ecran glEnd(); //on arrete le dessin } } choix_couleur = 0; //non-utilisé } glColor3d(0.0,0.0,1.0); //on place un point bleu au centre de l'écran glBegin(GL_POINTS); glVertex3f(L/2,H/2,0.0); //le centre a pour coordonnées (L/2 ; H/2) glEnd(); glFlush(); glutSwapBuffers(); //on échange l'image de profondeur avec l'image affichée sur l'ecran } void init( ) { glClearColor(0.0,0.0,0.0,0.0); //on définie la couleur d'effacement glMatrixMode(GL_PROJECTION); //on choisi une matrice en 2D glLoadIdentity( ); //on prend une matrice vierge gluOrtho2D(0,L,0,H); //on défini un repère printf("Bienvenue dans le generateur de fractale\n"); printf("D'Alexis, Maxime et Francois, realise dans\n"); printf("le cadre de leurs TPE de Terminale S2\n"); printf("**** Logiciel programme par Maxime ****\n"); pas_x = (_b - _a) / L; //on définie le pas courant en fonction de la taille de l'image //cout << pas_x << endl; pas_y = (_c - _d) / H; //idem mais pour le pas en ordonnée //cout << pas_y << endl; nombre1.rel = 0; //on attribue des valeurs nulles aux variables pour éviter les problemes :) nombre1.img = 0; nombre2.rel = 0; nombre2.img = 0; nombre3.rel = 0; nombre3.img = 0; } void clavier(unsigned char key, int x, int y) //fonction qui gère le clavier { switch(key) { case 27 : exit(0); //si l'utilisateur appuis sur echap : on quitte case 'e' : L=1024, H=768;init(); break; //pour régler l'affichage sur un ecran de résolution souhaitée case 'z' : L=800, H=600;init(); break; //idem case 'a' : L=640, H=480;init(); break; //idem case 'r' : L=1280, H=1024; init(); break; //idem case '/' : force_du_zoom=force_du_zoom/2; break; //pour modifier la force du zoom --> peu utile case '*' : force_du_zoom=force_du_zoom*2; break; //pour modifier la force du zoom --> peu utile case 't' : force_du_zoom = ((_b-_a)+(_d-_c)) / 4; break; //pour choisir automatiquement une force de zoom case 'v' : _a+=force_du_zoom ; _b-=force_du_zoom ;break; //pour gérer le zoom au clavier case 'b' : _a-=force_du_zoom ; _b+=force_du_zoom ;break; //pour gérer le zoom au clavier case 'f' : _d+=force_du_zoom ; _c-=force_du_zoom ;break; //pour gérer le zoom au clavier case 'g' : _d-=force_du_zoom ; _c+=force_du_zoom ;break; //pour gérer le zoom au clavier case'h':_a+=force_du_zoom ; _b-=force_du_zoom ;_d+=force_du_zoom ;_c-=force_du_zoom;break; //pour gérer le zoom au clavier case'j':_a-=force_du_zoom ; _b+=force_du_zoom ;_d-=force_du_zoom ;_c+=force_du_zoom;break; //pour gérer le zoom au clavier case ':' : temp = _a; _a = _a + ((_b-_a)/4); //pour gérer le zoom au clavier _b = _b - ((_b-temp)/4); temp = _d; _d = _d + ((_c-_d)/4); _c = _c - ((_c-temp)/4); force_du_zoom = force_du_zoom/2; break; case '!' : temp = _a; _a = _a - ((_b-_a)/4); //pour gérer le zoom au clavier _b = _b + ((_b-temp)/4); temp = _d; _d = _d - ((_c-_d)/4); _c = _c + ((_c-temp)/4); force_du_zoom = force_du_zoom*2; break; case 'o' : _d+=0.1; break; //pour gérer le zoom au clavier case 'p' : _d-=0.1; break; //pour gérer le zoom au clavier case 'l' : _c+=0.1; break; //pour gérer le zoom au clavier case 'm' : _c-=0.1; break; //pour gérer le zoom au clavier case '7' : if(rouge<=1) rouge += 1;glutPostRedisplay();break; //pour changer la couleur --> inutil pour l'instant case '4' : if(rouge>=1) rouge -= 1;glutPostRedisplay();break; //pour changer la couleur --> inutil pour l'instant case '8' : if(vert<=1)vert +=1;glutPostRedisplay();break; //pour changer la couleur --> inutil pour l'instant case '5' : if(vert>=1)vert -= 1;glutPostRedisplay();break; //pour changer la couleur --> inutil pour l'instant case '9' : if(bleu<=1)bleu += 1;glutPostRedisplay();break; //pour changer la couleur --> inutil pour l'instant case '6' : if(bleu>=1)bleu -= 1;glutPostRedisplay();break; //pour changer la couleur --> inutil pour l'instant case '+' : itermax++; break; //Pour augmenter le nombre d'itération case '-' : itermax--; break; //Pour diminuer le nombre d'itération case 'd' : _a = -2, _b = 2, _c = 1.2, _d = -1.2; break; //on revient aux valeurs par défaut } } void touches_speciales(int key, int x, int y) //fonction qui gere les touches haut, bas, gauche, droite du clavier --> inutile { switch(key) { case GLUT_KEY_UP : _d+=force_du_zoom; _c+=force_du_zoom;break; case GLUT_KEY_DOWN : _d-=force_du_zoom; _c-=force_du_zoom;break; case GLUT_KEY_LEFT : _a-=force_du_zoom; _b-=force_du_zoom;break; case GLUT_KEY_RIGHT : _a+=force_du_zoom; _b+=force_du_zoom;break; } } void relache_1(int key, int x, int y) //inutile pour l'instant { pas_x = (_b - _a) / L;pas_y = (_c - _d) / H; glutPostRedisplay(); } void relache_2(unsigned char key, int x, int y) //inutile pour l'instant { pas_x = (_b - _a) / L;pas_y = (_c - _d) / H; glutPostRedisplay(); } void souris (int bouton, int etat, int x, int y) //fonction qui gere la souris { switch(bouton) { case GLUT_LEFT_BUTTON : //impossible à commenter :D ~gere le zoom if(etat==GLUT_DOWN) { y = H-y; _x = _a + (((_b - _a)*x) / L); _y = _d + (((_c-_d)*y)/H); temp = _a; _a = _x - ((_b - _a)/2); _b = _x + ((_b - temp)/2); temp = _d; _d = _y - ((_c - _d)/2); _c = _y + ((_c - temp)/2); temp = _a; _a = _a + ((_b-_a)/4); _b = _b - ((_b-temp)/4); temp = _d; _d = _d + ((_c-_d)/4); _c = _c - ((_c-temp)/4); force_du_zoom = force_du_zoom/2; break; } case GLUT_RIGHT_BUTTON ://impossible à commenter :D ~gere le zoom if(etat==GLUT_DOWN) { y = H-y; _x = _a + (((_b - _a)*x) / L); _y = _d + (((_c-_d)*y)/H); temp = _a; _a = _x - ((_b - _a)/2); _b = _x + ((_b - temp)/2); temp = _d; _d = _y - ((_c - _d)/2); _c = _y + ((_c - temp)/2); temp = _a; _a = _a - ((_b-_a)/4); _b = _b + ((_b-temp)/4); temp = _d; _d = _d - ((_c-_d)/4); _c = _c + ((_c-temp)/4); force_du_zoom = force_du_zoom*2; break;} } pas_x = (_b - _a) / L;pas_y = (_c - _d) / H; glutPostRedisplay(); //on recalcul le pas, et on demande le réaffichage } // main int main(int argc, char** argv) //fonction principale d'entrée dans le programme { glutInit(&argc, argv); //initialisation openGL glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow("Tpe Fractale par Maxime Gauberti copyright 2005 -> 3012"); //touche finale du programmeur :D glutFullScreen(); //pour afficher en plein écran init( ); //on initialise en appelant la fonction void init() glutDisplayFunc(display); //callback d'affichage (fonction qui gere l'affichage) glutKeyboardFunc(clavier);//callback de getion du clavier glutSpecialFunc(touches_speciales);//callback de gestion de touches spéciales du clavier glutSpecialUpFunc(relache_1);//callback qui gere les touches spéciales relachées par l'utilisateur glutKeyboardUpFunc(relache_2);//callback qui gere les touches relachées par l'utilisateur glutMouseFunc(souris);//callback de gestion de souris glutMainLoop( ); //Boucle principale du programme return 0; }

Conclusion :


Aucun bug connu ;)

Sources compilables sous Linux (inclure glut et rajouter #include <stdlib.h>). Je n'ai pas mis le bin de linux, parce que la rapidité de l'execution dépend beaucoup de la configuration de votre ordinateur et le compilateur sera capable d'optimiser pour votre systeme.

//pas testé sous Unix

A voir également

Ajouter un commentaire

Commentaires

Messages postés
3006
Date d'inscription
dimanche 14 avril 2002
Statut
Membre
Dernière intervention
31 décembre 2008

bah non, t'aurais dû surcharger les opérateurs, voilà ce qu'il y a, et ça t'aurais fait une belle classe.
Messages postés
82
Date d'inscription
dimanche 3 août 2003
Statut
Membre
Dernière intervention
10 août 2007

Merci kirua :D

Pour la classe complexe je sais :D mais dans le cadre d'un TPE, il fallait que je créé moi même :)
En plus, j'ai fai une classe alors qu'une structure aurait été plus adaptée... mais bon, ca a suffit à éblouir mes deux examinateurs aujourd'hui :D
Messages postés
3006
Date d'inscription
dimanche 14 avril 2002
Statut
Membre
Dernière intervention
31 décembre 2008

"class complex_tpe"

inclus plutôt <complex>, c'est un en-tête standard, ça vaut tjs mieux. doc à foison sur google.

Sinon, j'aurais bien vu tout ça un peu plus orienté objet, modulaire quoi, mais sinon pr la sévérité t'as raison, je me suis contenté de commenter le tableau, alors que le résultat est beau, bravo.
Messages postés
82
Date d'inscription
dimanche 3 août 2003
Statut
Membre
Dernière intervention
10 août 2007

Vous êtes sévères ;)
Messages postés
3006
Date d'inscription
dimanche 14 avril 2002
Statut
Membre
Dernière intervention
31 décembre 2008

je suis assez d'accord pr l'histoire du tableau, c'est assez laid :/ soit tu le charges et tu fournis le programme qui génère les fichiers, soit tu laisses le code qui fait le précalcul au démarrage du programme, parce que c'est très peu évolutif :/
Afficher les 8 commentaires

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.