644b33724a63690d332d09458944e89489e00d62
[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 | GL_DEPTH_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   glFrustum ( -ar, ar, -1.0, 1.0, 2.0, 100.0 ) ;
110   glMatrixMode ( GL_MODELVIEW ) ;
111   glLoadIdentity () ;
112   xwin = -1.0 ;
113   ywin =  0.0 ;
114   glTranslated ( xwin, ywin, -5.0 ) ;
115 }
116
117 static void 
118 Key(unsigned char key, int x, int y)
119 {
120   switch (key) {
121   case 27:  /* Escape key */
122     glutLeaveMainLoop () ;
123     break;
124
125   case '+' :
126     ++num_levels ;
127     break ;
128
129   case '-' :
130     if ( num_levels > 0 )
131       --num_levels ;
132     break ;
133
134   case 'r' :  case 'R' :
135     glMatrixMode ( GL_MODELVIEW ) ;
136     glLoadIdentity();
137     xwin = -1.0 ;
138     ywin = 0.0 ;
139     glTranslated ( xwin, ywin, -5.0 ) ;
140
141     break ;
142   }
143
144   glutPostRedisplay();
145 }
146
147 static void 
148 Special(int key, int x, int y)
149 {
150   switch (key) {
151   case GLUT_KEY_UP :
152     glMatrixMode ( GL_MODELVIEW ) ;
153     ywin += 0.1 * scale_factor ;
154     glTranslated ( 0.0, 0.1 * scale_factor, 0.0 ) ;
155     break ;
156
157   case GLUT_KEY_DOWN :
158     glMatrixMode ( GL_MODELVIEW ) ;
159     ywin -= 0.1 * scale_factor ;
160     glTranslated ( 0.0, -0.1 * scale_factor, 0.0 ) ;
161     break ;
162
163   case GLUT_KEY_LEFT :
164     glMatrixMode ( GL_MODELVIEW ) ;
165     xwin -= 0.1 * scale_factor ;
166     glTranslated ( -0.1 * scale_factor, 0.0, 0.0 ) ;
167     break ;
168
169   case GLUT_KEY_RIGHT :
170     glMatrixMode ( GL_MODELVIEW ) ;
171     xwin += 0.1 * scale_factor ;
172     glTranslated ( 0.1 * scale_factor, 0.0, 0.0 ) ;
173     break ;
174
175   case GLUT_KEY_PAGE_UP :
176     glMatrixMode ( GL_MODELVIEW ) ;
177     glTranslated ( -xwin, -ywin, 0.0 ) ;
178     glScaled ( 1.25, 1.25, 1.25 ) ;
179     glTranslated ( xwin, ywin, 0.0 ) ;
180     scale_factor *= 0.8 ;
181     break ;
182
183   case GLUT_KEY_PAGE_DOWN :
184     glMatrixMode ( GL_MODELVIEW ) ;
185     glTranslated ( -xwin, -ywin, 0.0 ) ;
186     glScaled ( 0.8, 0.8, 0.8 ) ;
187     glTranslated ( xwin, ywin, 0.0 ) ;
188     scale_factor *= 1.25 ;
189     break ;
190   }
191
192   glutPostRedisplay();
193 }
194
195 void readConfigFile ( char *fnme )
196 {
197   FILE *fptr = fopen ( fnme, "rt" ) ;
198   int i ;
199   char inputline [ 256 ] ;
200
201   /* Read a header line */
202   fgets ( inputline, 256, fptr ) ;
203
204   /* Read a comment line */
205   fgets ( inputline, 256, fptr ) ;
206
207   /* Read the window title */
208   fgets ( inputline, 256, fptr ) ;
209   sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
210
211   /* Read a comment line */
212   fgets ( inputline, 256, fptr ) ;
213
214   /* Read the number of affine transformations */
215   fgets ( inputline, 256, fptr ) ;
216   sscanf ( inputline, "%d", &num_trans ) ;
217
218   affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
219
220   /* Read a comment line */
221   fgets ( inputline, 256, fptr ) ;
222
223   for ( i = 0; i < num_trans; i++ )
224   {
225     double m00, m01, m10, m11 ;  /* Matrix "I" minus "A" */
226     double determ ;              /* Determinant of this matrix */
227
228     /* Read an affine transformation definition */
229     fgets ( inputline, 256, fptr ) ;
230     sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
231                      &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
232
233     /* Calculate the stationary point */
234
235     m00 = 1.0 - affine[i].a00 ;
236     m01 =     - affine[i].a01 ;
237     m10 =     - affine[i].a10 ;
238     m11 = 1.0 - affine[i].a11 ;
239
240     determ = m00 * m11 - m01 * m10 ;
241
242     if ( fabs ( determ ) > 1.e-6 )
243     {
244       affine[i].statx = (  m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
245       affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
246     }
247     else
248       affine[i].statx = affine[i].staty = 0.0 ;
249   }
250 }
251
252 int 
253 main(int argc, char *argv[])
254 {
255   int fractal_window ;
256
257   if ( argc > 1 )
258     readConfigFile ( argv[1] ) ;
259   else
260     readConfigFile ( "fractals.dat" ) ;
261
262   glutInit(&argc, argv);
263   glutInitWindowSize(500, 250);
264   glutInitWindowPosition ( 140, 140 ) ;
265
266   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
267
268   fractal_window = glutCreateWindow( window_title );
269
270   glClearColor(1.0, 1.0, 1.0, 1.0);
271
272   glutReshapeFunc(Reshape);
273   glutKeyboardFunc(Key);
274   glutSpecialFunc(Special);
275   glutDisplayFunc(Display);
276
277   glutMainLoop();
278
279   printf ( "Back from the 'freeglut' main loop\n" ) ;
280
281   return 0;             /* ANSI C requires main to return int. */
282 }