animation - Animating a Fractal Via OpenGL -


i working on animating fractal (triangle fractal) frame-by-frame. know need use call-back method this, not sure how implement it. wish working via left-mouse click have code:

void mouse(int button, int state, int x, int y){   if(button == glut_right_button && state == glut_down){      glclear(gl_color_buffer_bit);     divide_triangle(v[0], v[1], v[2], n);   }   //closes window on right button   if(button == glut_right_button && state == glut_down){   exit(0);   } } 

this mouse click call back, doesn't re-animate triangle each time triangle drawn. how can show process of drawing each time recurse through triangle drawing?

void divide_triangle(point2 a, point2 b, point2 c, int m) {   /* triangle subdivision using vertex coordinates */   point2 v0, v1, v2;   int j;   if(m>0){     for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;     for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;     for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;     divide_triangle(a, v0, v1, m-1);     divide_triangle(c, v1, v2, m-1);     divide_triangle(b, v2, v0, m-1);   }   else(triangle(a,b,c));   glutpostredisplay(); /* draw triangle @ end of recursion */ } 

i want redisplay once draw new triangle here, , make appear animation, guess need delay between redisplays. how can set up? want make can draw without animating everytime, i.e. click specific mouse key: 'f1' or unreserved key , display final recursive triangle.

here full code viewing:

#ifdef __apple__ //for use os x #include <glut/glut.h> #else            //linux #include <gl/glut.h> #endif  #include <stdlib.h> #include <gl/glut.h>  typedef glfloat point2[2]; /* initial triangle – global variables */ point2 v[]={{-2.0, -1.5}, {2.0, -1.5}, {0.0, 1.5}}; int n; /* number of recursive steps */ int windowx, windowy; //window size parameters.  float red = .25; float green = .25;  float blue = .70; bool color_state = true;  void mouse(int button, int state, int x, int y); void triangle( point2 a, point2 b, point2 c); void divide_triangle(point2 a, point2 b, point2 c, int m); void swap_colors();  void display(void){   glclear(gl_color_buffer_bit);   divide_triangle(v[0], v[1], v[2], n);   glflush(); }  void init(){   glmatrixmode(gl_projection);   glloadidentity();   gluortho2d(-2.0, 2.0, -2.0, 2.0);   glmatrixmode(gl_modelview);   glclearcolor (0.10, 0.10, 0.10 ,1.0);   glcolor3f(red, green, blue); }  int main(int argc, char **argv){   if(argc <= 1){     windowx = 500;     windowy = 500;     n = 4;   }    else if(argc > 1){     windowx = atoi(argv[1]);  //atoi converts char int     windowy = atoi(argv[2]);     n = atoi(argv[3]);   }   glutinit(&argc, argv);   glutinitdisplaymode(glut_single|glut_rgb);   glutinitwindowsize(windowx, windowy);   glutcreatewindow("n-force");   glutdisplayfunc(display);   gluttimerfunc(30, recurse, -1)   glutmousefunc(mouse);   init();   glutmainloop(); }  void mouse(int button, int state, int x, int y){   if(button == glut_right_button && state == glut_down){ //swap colors     glclear(gl_color_buffer_bit);     divide_triangle(v[0], v[1], v[2], n);   }   //closes window on right button   if(button == glut_right_button && state == glut_down){   exit(0);   } }  void divide_triangle(point2 a, point2 b, point2 c, int m) {   /* triangle subdivision using vertex coordinates */   point2 v0, v1, v2;   int j;   if(m>0){     for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;     for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;     for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;     divide_triangle(a, v0, v1, m-1);     divide_triangle(c, v1, v2, m-1);     divide_triangle(b, v2, v0, m-1);   }   else(triangle(a,b,c));   glutpostredisplay(); /* draw triangle @ end of recursion */ }  void triangle( point2 a, point2 b, point2 c){   glbegin(gl_triangles);   glvertex2fv(a);   glvertex2fv(b);   glvertex2fv(c);   glend(); }  void swap_colors(){  //maybe add parameters x , y change colors based on coordinates   if(color_state == true){     red = blue = green = .1;     color_state = false;   }   else{     red = 0.25;     green = .25;     blue = 0.70;     color_state = true;   } }   /* do:     1. add cmd line args, (width, depth, , recursive depth) (check)    2. add color swap    3. add exit callback (check> right mouse)    4. idle callback animation  */ 

use gluttimerfunc() callback boolean flag increment recursion depth value , post redisplays:

bool animating = false; unsigned int n = 4; void timer( int value ) {     if( !animating )         return;      n++;     if( n > 6 )         n = 0;      gluttimerfunc( 200, timer, 0 );     glutpostredisplay(); }  void mouse(int button, int state, int x, int y) {     if(button == glut_right_button && state == glut_down)     {         animating = !animating;         gluttimerfunc( 0, timer, 0 );     } } 

all together:

#include <gl/glut.h>  bool animating = false; unsigned int n = 4; void timer( int value ) {     if( !animating )         return;      n++;     if( n > 6 )         n = 0;      gluttimerfunc( 200, timer, 0 );     glutpostredisplay(); }  void mouse(int button, int state, int x, int y) {     if(button == glut_right_button && state == glut_down)     {         animating = !animating;         gluttimerfunc( 0, timer, 0 );     } }  typedef glfloat point2[2]; void divide_triangle(point2 a, point2 b, point2 c, int m) {     /* triangle subdivision using vertex coordinates */     if(m>0)     {         point2 v0, v1, v2;         for( int j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;         for( int j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;         for( int j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;         divide_triangle(a, v0, v1, m-1);         divide_triangle(c, v1, v2, m-1);         divide_triangle(b, v2, v0, m-1);     }     else     {         glvertex2fv(a);         glvertex2fv(b);         glvertex2fv(c);     } }  void display(void) {     glclearcolor (0.10, 0.10, 0.10 ,1.0);     glclear(gl_color_buffer_bit);      glmatrixmode(gl_projection);     glloadidentity();     gluortho2d(-2.0, 2.0, -2.0, 2.0);     glmatrixmode(gl_modelview);     glloadidentity();      float red = .25;     float green = .25;      float blue = .70;     glcolor3f(red, green, blue);      /* initial triangle – global variables */     point2 v[]={{-2.0, -1.5}, {2.0, -1.5}, {0.0, 1.5}};     glbegin(gl_triangles);     divide_triangle(v[0], v[1], v[2], n);     glend();      glflush(); }  int main(int argc, char **argv) {     glutinit(&argc, argv);     glutinitdisplaymode(glut_single|glut_rgb);     glutinitwindowsize(500, 500);     glutcreatewindow("n-force");     glutdisplayfunc(display);     glutmousefunc(mouse);     glutmainloop(); } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -