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