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