Applied John's updated ReadConfigFile() changes to the fractals demos.
[freeglut] / progs / demos / Fractals / fractals.c
1 /* fractals.c */
2 /*
3  * Program to draw a fractal by Michael Barnsley's deterministic algorithm.
4  * Algorithm:
5  *  (1) Define the affine transformations (of the form r(i+1) = A r(i) + b )
6  *  (2) Find the stationary point for each transformation
7  *  (3) To draw:
8  *        - If you are at the lowest level, draw lines connecting all the stationary points
9  *        - If not, call the draw function recursively with each affine transformation applied
10  */
11
12 /*
13  * User Commands:
14  *  +,- - increment/decrement number of levels
15  *  PgUp, PgDn - increase/decrease scaling
16  *  Arrow keys - translate viewing section
17  *  r - reset view
18  *  Escape - quit
19  */
20
21 #include <GL/freeglut.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <math.h>
25
26 typedef struct
27 {
28   double a00, a01, a10, a11 ;   /* Transformation matrix */
29   double b0, b1 ;               /* Constant vector added on */
30   double statx, staty ;         /* Coordinates of the stationary point */
31 }
32 AffineTrans ;
33
34 /* Number of levels to draw the fractal */
35 static int num_levels = 0 ;
36
37 /* The definition of the fractal */
38 static int num_trans ;
39 static AffineTrans *affine ;
40
41 /* the window title */
42 char window_title [ 80 ] ;
43
44 /* The amount the view is translated and scaled */
45 double xwin = 0.0, ywin = 0.0 ;
46 double scale_factor = 1.0 ;
47
48 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
49 {
50   /* Draw a fractal transformed by "M", "N" as passed in */
51   int i ;
52
53   if ( num == 0 )
54   {
55     double x0 = m00 * affine[0].statx + m01 * affine[0].staty + n0 ;
56     double y0 = m10 * affine[0].statx + m11 * affine[0].staty + n1 ;
57
58     for ( i = 1; i < num_trans; i++ )
59     {
60       double x1 = m00 * affine[i].statx + m01 * affine[i].staty + n0 ;
61       double y1 = m10 * affine[i].statx + m11 * affine[i].staty + n1 ;
62
63       glVertex2d ( x0, y0 ) ;
64       glVertex2d ( x1, y1 ) ;
65
66       x0 = x1 ;
67       y0 = y1 ;
68     }
69   }
70   else
71   {
72     /* Map each affine transformation in the fractal through the one passed in and call "draw_level" */
73
74     for ( i = 0; i < num_trans; i++ )
75     {
76       draw_level ( num-1, m00*affine[i].a00+m01*affine[i].a10,     m00*affine[i].a01+m01*affine[i].a11,
77                           m10*affine[i].a00+m11*affine[i].a10,     m10*affine[i].a01+m11*affine[i].a11,
78                           m00*affine[i].b0 +m01*affine[i].b1 + n0, m10*affine[i].b0 +m11*affine[i].b1  + n1 ) ;
79     }
80   }
81 }
82
83 static void 
84 Display(void)
85 {
86   glClear( GL_COLOR_BUFFER_BIT );
87
88   /* the curve */
89   glPushMatrix();
90   glScalef(2.5, 2.5, 2.5);
91
92   glColor4f(0.0, 0.0, 0.0, 1.0);
93   glBegin ( GL_LINES ) ;
94   draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
95   glEnd () ;
96
97   glPopMatrix();
98   glutSwapBuffers();
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 }
119
120 static void 
121 Key(unsigned char key, int x, int y)
122 {
123   switch (key) {
124   case 27:  /* Escape key */
125     glutLeaveMainLoop () ;
126     break;
127
128   case '+' :
129     ++num_levels ;
130     break ;
131
132   case '-' :
133     if ( num_levels > 0 )
134       --num_levels ;
135     break ;
136
137   case 'r' :  case 'R' :
138     glMatrixMode ( GL_MODELVIEW ) ;
139     glLoadIdentity();
140     xwin = -1.0 ;
141     ywin = 0.0 ;
142     glTranslated ( xwin, ywin, -5.0 ) ;
143
144     break ;
145   }
146
147   glutPostRedisplay();
148 }
149
150 static void 
151 Special(int key, int x, int y)
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
195   glutPostRedisplay();
196 }
197
198
199 void readConfigFile ( char *fnme )
200 {
201   FILE *fptr = fopen ( fnme, "rt" ) ;
202   int i ;
203   char inputline [ 256 ] ;
204
205   if ( fptr )
206   {
207     /* Read a header line */
208     fgets ( inputline, 256, fptr ) ;
209
210     /* Read a comment line */
211     fgets ( inputline, 256, fptr ) ;
212
213     /* Read the window title */
214     fgets ( inputline, 256, fptr ) ;
215     /* We assume here that this line will not exceed 79 characters plus a 
216        newline (window_title is 80 characters long). That'll cause a buffer 
217        overflow. For a simple program like  this, though, we're letting it 
218        slide! 
219     */
220     sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ; 
221
222     /* Read a comment line */
223     fgets ( inputline, 256, fptr ) ;
224
225     /* Read the number of affine transformations */
226     fgets ( inputline, 256, fptr ) ;
227     sscanf ( inputline, "%d", &num_trans ) ;
228
229     affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
230
231     /* Read a comment line */
232     fgets ( inputline, 256, fptr ) ;
233
234     for ( i = 0; i < num_trans; i++ )
235     {
236       /* Read an affine transformation definition */
237       fgets ( inputline, 256, fptr ) ;
238       sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
239                        &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
240     }
241   }
242   else  /* No data file, set a default */
243   {
244     printf ( "ERROR opening file <%s>\n", fnme ) ;
245     strcpy ( window_title, "Cantor Dust" ) ;
246     num_trans = 2 ;
247     affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
248     affine[0].a00 = 0.25 ;  affine[0].a01 = 0.00 ;  affine[0].a10 = 0.00 ;  affine[0].a11 = 0.25 ;
249     affine[0].b0 = 0.0 ;    affine[0].b1 = 0.0 ;
250     affine[1].a00 = 0.25 ;  affine[1].a01 = 0.00 ;  affine[1].a10 = 0.00 ;  affine[1].a11 = 0.25 ;
251     affine[1].b0 = 0.5 ;    affine[1].b1 = 0.0 ;
252   }
253
254   for ( i = 0; i < num_trans; i++ )
255   {
256     double m00, m01, m10, m11 ;  /* Matrix "I" minus "A" */
257     double determ ;              /* Determinant of this matrix */
258
259     /* Calculate the stationary point */
260
261     m00 = 1.0 - affine[i].a00 ;
262     m01 =     - affine[i].a01 ;
263     m10 =     - affine[i].a10 ;
264     m11 = 1.0 - affine[i].a11 ;
265
266     determ = m00 * m11 - m01 * m10 ;
267
268     if ( fabs ( determ ) > 1.e-6 )
269     {
270       affine[i].statx = (  m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
271       affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
272     }
273     else
274       affine[i].statx = affine[i].staty = 0.0 ;
275   }
276 }
277
278 int 
279 main(int argc, char *argv[])
280 {
281   int fractal_window ;
282
283   glutInitWindowSize(500, 250);
284   glutInitWindowPosition ( 140, 140 );
285   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
286   glutInit(&argc, argv);
287
288   if ( argc > 1 )
289     readConfigFile ( argv[1] ) ;
290   else
291     readConfigFile ( "fractals.dat" ) ;
292
293   fractal_window = glutCreateWindow( window_title );
294
295   glClearColor(1.0, 1.0, 1.0, 1.0);
296
297   glutReshapeFunc(Reshape);
298   glutKeyboardFunc(Key);
299   glutSpecialFunc(Special);
300   glutDisplayFunc(Display);
301
302   glutMainLoop();
303
304   printf ( "Back from the 'freeglut' main loop\n" ) ;
305
306   return 0;             /* ANSI C requires main to return int. */
307 }