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