176013f47ae3ab9960b46b2db24a4f442eef226d
[freeglut] / progs / demos / Fractals_random / fractals_random.c
1 /* fractals_random.c */
2 /* This demo shows a single-buffering "freeglut" example. */
3
4 /*
5  * Program to draw a fractal by Michael Barnsley's stochastic algorithm.
6  * Algorithm:
7  *  (1) Define the affine transformations (of the form r(i+1) = A r(i) + b )
8  *  (2) Find the stationary point for the first transformation
9  *  (3) To draw:
10  *        - Pick a random integer between 1 and the number of transformations (inclusive)
11  *        - Send the current point through the transformation to create the new current point
12  *        - Plot the new current point
13  */
14
15 /*
16  * User Commands:
17  *  PgUp, PgDn - increase/decrease scaling
18  *  Arrow keys - translate viewing section
19  *  r - reset view
20  *  Escape - quit
21  */
22
23 #include <GL/freeglut.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28 #ifdef _MSC_VER
29 /* DUMP MEMORY LEAKS */
30 #include <crtdbg.h>
31 #endif
32
33 typedef struct
34 {
35   double a00, a01, a10, a11 ;   /* Transformation matrix */
36   double b0, b1 ;               /* Constant vector added on */
37   double statx, staty ;         /* Coordinates of the stationary point */
38 }
39 AffineTrans ;
40
41 /* Number of levels to draw the fractal */
42 static int num_levels = 0 ;
43
44 /* The definition of the fractal */
45 static int num_trans ;
46 static AffineTrans *affine ;
47
48 /* the window title */
49 char window_title [ 80 ] ;
50
51 /* The amount the view is translated */
52 double xwin = 0.0, ywin = 0.0 ;
53 double scale_factor = 1.0 ;
54
55 /* The current point */
56 double current_x = 0.0, current_y = 0.0 ;
57
58 /* Signals when a glClear is needed */
59 static GLboolean needClear = GL_TRUE;
60
61 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
62 {
63   /* Draw a fractal transformed by "M", "N" as passed in */
64   int i ;
65
66   for ( i = 0; i < 10; i++ )
67   {
68     int random = ( rand( ) >> 10 ) % num_trans;
69     double new_x = affine[random].a00 * current_x + affine[random].a01 * current_y + affine[random].b0 ;
70     double new_y = affine[random].a10 * current_x + affine[random].a11 * current_y + affine[random].b1 ;
71     
72     glVertex2d ( new_x, new_y ) ;
73     current_x = new_x ;
74     current_y = new_y ;
75   }
76 }
77
78 static void 
79 Display(void)
80 {
81   if (needClear) {
82     glClear(GL_COLOR_BUFFER_BIT);
83     needClear = GL_FALSE;
84   }
85
86   /* the curve */
87   glPushMatrix();
88   glScaled(2.5, 2.5, 2.5);
89
90   glColor4f(0.0, 0.0, 0.0, 1.0);
91   glBegin ( GL_POINTS ) ;
92   draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
93   glEnd () ;
94
95   glPopMatrix();
96
97   glFlush();
98   glutPostRedisplay();  /* Needed so that this function will be called again */
99 }
100
101 static void 
102 Reshape(int width, int height)
103 {
104   float ar;
105   glViewport(0, 0, width, height);
106   glMatrixMode(GL_PROJECTION);
107   glLoadIdentity();
108   ar = (float) width / (float) height;
109   if( ar > 1 )
110       glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
111   else
112       glFrustum(-1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0);
113   glMatrixMode(GL_MODELVIEW);
114   glLoadIdentity();
115   xwin = -1.0 ;
116   ywin =  0.0 ;
117   glTranslated(xwin, ywin, -5.0);
118   needClear = GL_TRUE;
119 }
120
121 static void 
122 Key(unsigned char key, int x, int y)
123 {
124   int changed_settings = 1;
125   
126   switch (key) {
127   case 27:  /* Escape key */
128     glutLeaveMainLoop ();
129     break;
130
131   case 'r' :  case 'R' :
132     glMatrixMode(GL_MODELVIEW);
133     glLoadIdentity();
134     xwin = -1.0 ;
135     ywin = 0.0 ;
136     glTranslated(xwin, ywin, -5.0);
137     break ;
138
139   default:
140     changed_settings = 0;
141     break;
142   }
143   if (changed_settings)
144     needClear = GL_TRUE;
145   glutPostRedisplay();
146 }
147
148 static void 
149 Special(int key, int x, int y)
150 {
151   int changed_settings = 1;
152
153   switch (key) {
154   case GLUT_KEY_UP :
155     glMatrixMode(GL_MODELVIEW);
156     ywin += 0.1 * scale_factor ;
157     glTranslated(0.0, 0.1 * scale_factor, 0.0);
158     break ;
159
160   case GLUT_KEY_DOWN :
161     glMatrixMode(GL_MODELVIEW);
162     ywin -= 0.1 * scale_factor ;
163     glTranslated(0.0, -0.1 * scale_factor, 0.0);
164     break ;
165
166   case GLUT_KEY_LEFT :
167     glMatrixMode(GL_MODELVIEW);
168     xwin -= 0.1 * scale_factor ;
169     glTranslated(-0.1 * scale_factor, 0.0, 0.0);
170     break ;
171
172   case GLUT_KEY_RIGHT :
173     glMatrixMode(GL_MODELVIEW);
174     xwin += 0.1 * scale_factor ;
175     glTranslated(0.1 * scale_factor, 0.0, 0.0);
176     break ;
177
178   case GLUT_KEY_PAGE_UP :
179     glMatrixMode(GL_MODELVIEW);
180     glTranslated ( -xwin, -ywin, 0.0 ) ;
181     glScaled(1.25, 1.25, 1.25);
182     glTranslated ( xwin, ywin, 0.0 ) ;
183     scale_factor *= 0.8 ;
184     break ;
185
186   case GLUT_KEY_PAGE_DOWN :
187     glMatrixMode(GL_MODELVIEW);
188     glTranslated ( -xwin, -ywin, 0.0 ) ;
189     glScaled(0.8, 0.8, 0.8);
190     glTranslated ( xwin, ywin, 0.0 ) ;
191     scale_factor *= 1.25 ;
192     break ;
193
194   default:
195     changed_settings = 0;
196     break;
197   }
198   if (changed_settings)
199     needClear = GL_TRUE;
200
201   glutPostRedisplay();
202 }
203
204
205 static int mouse_x = 0, mouse_y = 0 ;
206 static int button_down = GLUT_DOWN ;
207
208 static void 
209 Mouse ( int button, int updown, int x, int y )
210 {
211   button_down = updown ;
212
213   if ( updown == GLUT_DOWN )
214   {
215     mouse_x = x ;
216     mouse_y = y ;
217   }
218 }
219
220 static void 
221 MouseMotion ( int x, int y )
222 {
223   int window_width  = glutGet ( GLUT_WINDOW_WIDTH  ) ;
224   int window_height = glutGet ( GLUT_WINDOW_HEIGHT ) ;
225   int window_size = ( window_width < window_height ) ? window_width : window_height ;
226
227   double delta_x = 5.0 * (double)(x - mouse_x) / (double)(window_size) ;
228   double delta_y = 5.0 * (double)(y - mouse_y) / (double)(window_size) ;
229
230   xwin += delta_x * scale_factor ;
231   ywin -= delta_y * scale_factor ;
232   glMatrixMode ( GL_MODELVIEW ) ;
233   glTranslated ( delta_x * scale_factor, -delta_y * scale_factor, 0.0 ) ;
234
235   needClear = GL_TRUE;
236   glutPostRedisplay();
237
238   mouse_x = x ;
239   mouse_y = y ;
240 }
241
242 static void 
243 MouseWheel ( int wheel_number, int direction, int x, int y )
244 {
245   double scale = ( direction > 0 ) ? 1.25 : 0.8 ;
246
247   glMatrixMode ( GL_MODELVIEW ) ;
248   glTranslated ( -xwin, -ywin, 0.0 ) ;
249   glScaled ( scale, scale, scale ) ;
250   glTranslated ( xwin, ywin, 0.0 ) ;
251   scale_factor /= scale ;
252
253   needClear = GL_TRUE;
254   glutPostRedisplay();
255 }
256
257
258 static void
259 checkedFGets ( char *s, int size, FILE *stream )
260 {
261   if ( fgets ( s, size, stream ) == NULL ) {
262     fprintf ( stderr, "fgets failed\n");
263     exit ( EXIT_FAILURE );
264   }
265 }
266
267
268 void readConfigFile ( char *fnme )
269 {
270   FILE *fptr = fopen ( fnme, "rt" ) ;
271   int i ;
272   char inputline [ 256 ] ;
273
274   if ( fptr )
275   {
276     /* Read a header line */
277     checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
278
279     /* Read a comment line */
280     checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
281
282     /* Read the window title */
283     checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
284     /* We assume here that this line will not exceed 79 characters plus a 
285        newline (window_title is 80 characters long). That'll cause a buffer 
286        overflow. For a simple program like  this, though, we're letting it 
287        slide! 
288     */
289     sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ; 
290
291     /* Read a comment line */
292     checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
293
294     /* Read the number of affine transformations */
295     checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
296     sscanf ( inputline, "%d", &num_trans ) ;
297
298     affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
299
300     /* Read a comment line */
301     checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
302
303     for ( i = 0; i < num_trans; i++ )
304     {
305       /* Read an affine transformation definition */
306       checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
307       sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
308                        &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
309     }
310   }
311   else  /* No data file, set a default */
312   {
313     printf ( "ERROR opening file <%s>\n", fnme ) ;
314     strcpy ( window_title, "Cantor Dust" ) ;
315     num_trans = 2 ;
316     affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
317     affine[0].a00 = 0.25 ;  affine[0].a01 = 0.00 ;  affine[0].a10 = 0.00 ;  affine[0].a11 = 0.25 ;
318     affine[0].b0 = 0.0 ;    affine[0].b1 = 0.0 ;
319     affine[1].a00 = 0.25 ;  affine[1].a01 = 0.00 ;  affine[1].a10 = 0.00 ;  affine[1].a11 = 0.25 ;
320     affine[1].b0 = 0.5 ;    affine[1].b1 = 0.0 ;
321   }
322
323   for ( i = 0; i < num_trans; i++ )
324   {
325     double m00, m01, m10, m11 ;  /* Matrix "I" minus "A" */
326     double determ ;              /* Determinant of this matrix */
327
328     /* Calculate the stationary point */
329
330     m00 = 1.0 - affine[i].a00 ;
331     m01 =     - affine[i].a01 ;
332     m10 =     - affine[i].a10 ;
333     m11 = 1.0 - affine[i].a11 ;
334
335     determ = m00 * m11 - m01 * m10 ;
336
337     if ( fabs ( determ ) > 1.e-6 )
338     {
339       affine[i].statx = (  m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
340       affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
341     }
342     else
343       affine[i].statx = affine[i].staty = 0.0 ;
344   }
345 }
346
347 int 
348 main(int argc, char *argv[])
349 {
350   int fractal_window ;
351
352   glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
353
354   glutInitWindowSize(500, 250);
355   glutInitWindowPosition ( 140, 140 ) ;
356   glutInit(&argc, argv);
357
358   if ( argc > 1 )
359     readConfigFile ( argv[1] ) ;
360   else
361     readConfigFile ( "fractals.dat" ) ;
362
363   fractal_window = glutCreateWindow( window_title );
364
365   glClearColor(1.0, 1.0, 1.0, 1.0);
366
367   glutReshapeFunc(Reshape);
368   glutKeyboardFunc(Key);
369   glutSpecialFunc(Special);
370   glutDisplayFunc(Display);
371   glutMouseFunc(Mouse);
372   glutMotionFunc(MouseMotion);
373   glutMouseWheelFunc(MouseWheel);
374
375   glutMainLoop();
376
377   printf ( "Back from the 'freeglut' main loop\n" ) ;
378
379   free ( affine ) ;
380
381 #ifdef _MSC_VER
382   /* DUMP MEMORY LEAK INFORMATION */
383   _CrtDumpMemoryLeaks () ;
384 #endif
385
386   return 0;             /* ANSI C requires main to return int. */
387 }