Added --disable-replace-glut option to autoconf configure.
[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   int need_redisplay = 1;
124   
125   switch (key) {
126   case 27:  /* Escape key */
127     glutLeaveMainLoop () ;
128     break;
129
130   case '+' :
131     ++num_levels ;
132     break ;
133
134   case '-' :
135     if ( num_levels > 0 )
136       --num_levels ;
137     break ;
138
139   case 'r' :  case 'R' :
140     glMatrixMode ( GL_MODELVIEW ) ;
141     glLoadIdentity();
142     xwin = -1.0 ;
143     ywin = 0.0 ;
144     glTranslated ( xwin, ywin, -5.0 ) ;
145     break ;
146
147   default:
148     need_redisplay = 0;
149     break;
150   }
151   if (need_redisplay)
152     glutPostRedisplay();
153 }
154
155 static void 
156 Special(int key, int x, int y)
157 {
158   int need_redisplay = 1;
159
160   switch (key) {
161   case GLUT_KEY_UP :
162     glMatrixMode ( GL_MODELVIEW ) ;
163     ywin += 0.1 * scale_factor ;
164     glTranslated ( 0.0, 0.1 * scale_factor, 0.0 ) ;
165     break ;
166
167   case GLUT_KEY_DOWN :
168     glMatrixMode ( GL_MODELVIEW ) ;
169     ywin -= 0.1 * scale_factor ;
170     glTranslated ( 0.0, -0.1 * scale_factor, 0.0 ) ;
171     break ;
172
173   case GLUT_KEY_LEFT :
174     glMatrixMode ( GL_MODELVIEW ) ;
175     xwin -= 0.1 * scale_factor ;
176     glTranslated ( -0.1 * scale_factor, 0.0, 0.0 ) ;
177     break ;
178
179   case GLUT_KEY_RIGHT :
180     glMatrixMode ( GL_MODELVIEW ) ;
181     xwin += 0.1 * scale_factor ;
182     glTranslated ( 0.1 * scale_factor, 0.0, 0.0 ) ;
183     break ;
184
185   case GLUT_KEY_PAGE_UP :
186     glMatrixMode ( GL_MODELVIEW ) ;
187     glTranslated ( -xwin, -ywin, 0.0 ) ;
188     glScaled ( 1.25, 1.25, 1.25 ) ;
189     glTranslated ( xwin, ywin, 0.0 ) ;
190     scale_factor *= 0.8 ;
191     break ;
192
193   case GLUT_KEY_PAGE_DOWN :
194     glMatrixMode ( GL_MODELVIEW ) ;
195     glTranslated ( -xwin, -ywin, 0.0 ) ;
196     glScaled ( 0.8, 0.8, 0.8 ) ;
197     glTranslated ( xwin, ywin, 0.0 ) ;
198     scale_factor *= 1.25 ;
199     break ;
200
201   default:
202     need_redisplay = 0;
203     break;
204   }
205   if (need_redisplay)
206     glutPostRedisplay();
207 }
208
209
210 void readConfigFile ( char *fnme )
211 {
212   FILE *fptr = fopen ( fnme, "rt" ) ;
213   int i ;
214   char inputline [ 256 ] ;
215
216   if ( fptr )
217   {
218     /* Read a header line */
219     fgets ( inputline, 256, fptr ) ;
220
221     /* Read a comment line */
222     fgets ( inputline, 256, fptr ) ;
223
224     /* Read the window title */
225     fgets ( inputline, 256, fptr ) ;
226     /* We assume here that this line will not exceed 79 characters plus a 
227        newline (window_title is 80 characters long). That'll cause a buffer 
228        overflow. For a simple program like  this, though, we're letting it 
229        slide! 
230     */
231     sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ; 
232
233     /* Read a comment line */
234     fgets ( inputline, 256, fptr ) ;
235
236     /* Read the number of affine transformations */
237     fgets ( inputline, 256, fptr ) ;
238     sscanf ( inputline, "%d", &num_trans ) ;
239
240     affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
241
242     /* Read a comment line */
243     fgets ( inputline, 256, fptr ) ;
244
245     for ( i = 0; i < num_trans; i++ )
246     {
247       /* Read an affine transformation definition */
248       fgets ( inputline, 256, fptr ) ;
249       sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
250                        &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
251     }
252   }
253   else  /* No data file, set a default */
254   {
255     printf ( "ERROR opening file <%s>\n", fnme ) ;
256     strcpy ( window_title, "Cantor Dust" ) ;
257     num_trans = 2 ;
258     affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
259     affine[0].a00 = 0.25 ;  affine[0].a01 = 0.00 ;  affine[0].a10 = 0.00 ;  affine[0].a11 = 0.25 ;
260     affine[0].b0 = 0.0 ;    affine[0].b1 = 0.0 ;
261     affine[1].a00 = 0.25 ;  affine[1].a01 = 0.00 ;  affine[1].a10 = 0.00 ;  affine[1].a11 = 0.25 ;
262     affine[1].b0 = 0.5 ;    affine[1].b1 = 0.0 ;
263   }
264
265   for ( i = 0; i < num_trans; i++ )
266   {
267     double m00, m01, m10, m11 ;  /* Matrix "I" minus "A" */
268     double determ ;              /* Determinant of this matrix */
269
270     /* Calculate the stationary point */
271
272     m00 = 1.0 - affine[i].a00 ;
273     m01 =     - affine[i].a01 ;
274     m10 =     - affine[i].a10 ;
275     m11 = 1.0 - affine[i].a11 ;
276
277     determ = m00 * m11 - m01 * m10 ;
278
279     if ( fabs ( determ ) > 1.e-6 )
280     {
281       affine[i].statx = (  m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
282       affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
283     }
284     else
285       affine[i].statx = affine[i].staty = 0.0 ;
286   }
287 }
288
289 int 
290 main(int argc, char *argv[])
291 {
292   int fractal_window ;
293
294   glutInitWindowSize(500, 250);
295   glutInitWindowPosition ( 140, 140 );
296   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
297   glutInit(&argc, argv);
298
299   if ( argc > 1 )
300     readConfigFile ( argv[1] ) ;
301   else
302     readConfigFile ( "fractals.dat" ) ;
303
304   fractal_window = glutCreateWindow( window_title );
305
306   glClearColor(1.0, 1.0, 1.0, 1.0);
307
308   glutReshapeFunc(Reshape);
309   glutKeyboardFunc(Key);
310   glutSpecialFunc(Special);
311   glutDisplayFunc(Display);
312
313   glutMainLoop();
314
315   printf ( "Back from the 'freeglut' main loop\n" ) ;
316
317   return 0;             /* ANSI C requires main to return int. */
318 }