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