1 /* fractals_random.c */
2 /* This demo shows a single-buffering "freeglut" example. */
5 * Program to draw a fractal by Michael Barnsley's stochastic 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
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
17 * PgUp, PgDn - increase/decrease scaling
18 * Arrow keys - translate viewing section
23 #include <GL/freeglut.h>
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 */
36 /* Number of levels to draw the fractal */
37 static int num_levels = 0 ;
39 /* The definition of the fractal */
40 static int num_trans ;
41 static AffineTrans *affine ;
43 /* the window title */
44 char window_title [ 80 ] ;
46 /* The amount the view is translated */
47 float xwin = 0.0, ywin = 0.0 ;
48 float scale_factor = 1.0 ;
50 /* The current point */
51 float current_x = 0.0, current_y = 0.0 ;
53 /* Signals when a glClear is needed */
54 static GLboolean needClear = GL_TRUE;
56 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
58 /* Draw a fractal transformed by "M", "N" as passed in */
61 for ( i = 0; i < 10; i++ )
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 ;
67 glVertex2f ( new_x, new_y ) ;
77 glClear(GL_COLOR_BUFFER_BIT);
83 glScalef(2.5, 2.5, 2.5);
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 );
93 glutPostRedisplay(); /* Needed so that this function will be called again */
97 Reshape(int width, int height)
100 glViewport(0, 0, width, height);
101 glMatrixMode(GL_PROJECTION);
103 ar = (float) width / (float) height;
105 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
107 glFrustum(-1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0);
108 glMatrixMode(GL_MODELVIEW);
112 glTranslatef(xwin, ywin, -5.0);
117 Key(unsigned char key, int x, int y)
120 case 27: /* Escape key */
121 glutLeaveMainLoop ();
124 case 'r' : case 'R' :
125 glMatrixMode(GL_MODELVIEW);
129 glTranslatef(xwin, ywin, -5.0);
140 Special(int key, int x, int y)
144 glMatrixMode(GL_MODELVIEW);
145 ywin += 0.1 * scale_factor ;
146 glTranslatef(0.0, 0.1 * scale_factor, 0.0);
150 glMatrixMode(GL_MODELVIEW);
151 ywin -= 0.1 * scale_factor ;
152 glTranslatef(0.0, -0.1 * scale_factor, 0.0);
156 glMatrixMode(GL_MODELVIEW);
157 xwin -= 0.1 * scale_factor ;
158 glTranslatef(-0.1 * scale_factor, 0.0, 0.0);
161 case GLUT_KEY_RIGHT :
162 glMatrixMode(GL_MODELVIEW);
163 xwin += 0.1 * scale_factor ;
164 glTranslatef(0.1 * scale_factor, 0.0, 0.0);
167 case GLUT_KEY_PAGE_UP :
168 glMatrixMode(GL_MODELVIEW);
169 glTranslatef ( -xwin, -ywin, 0.0 ) ;
170 glScalef(1.25, 1.25, 1.25);
171 glTranslatef ( xwin, ywin, 0.0 ) ;
172 scale_factor *= 0.8 ;
175 case GLUT_KEY_PAGE_DOWN :
176 glMatrixMode(GL_MODELVIEW);
177 glTranslatef ( -xwin, -ywin, 0.0 ) ;
178 glScalef(0.8, 0.8, 0.8);
179 glTranslatef ( xwin, ywin, 0.0 ) ;
180 scale_factor *= 1.25 ;
190 void readConfigFile ( char *fnme )
192 FILE *fptr = fopen ( fnme, "rt" ) ;
194 char inputline [ 256 ] ;
198 /* Read a header line */
199 fgets ( inputline, 256, fptr ) ;
201 /* Read a comment line */
202 fgets ( inputline, 256, fptr ) ;
204 /* Read the window title */
205 fgets ( inputline, 256, fptr ) ;
206 /* We assume here that this line will not exceed 79 characters plus a
207 newline (window_title is 80 characters long). That'll cause a buffer
208 overflow. For a simple program like this, though, we're letting it
211 sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
213 /* Read a comment line */
214 fgets ( inputline, 256, fptr ) ;
216 /* Read the number of affine transformations */
217 fgets ( inputline, 256, fptr ) ;
218 sscanf ( inputline, "%d", &num_trans ) ;
220 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
222 /* Read a comment line */
223 fgets ( inputline, 256, fptr ) ;
225 for ( i = 0; i < num_trans; i++ )
227 /* Read an affine transformation definition */
228 fgets ( inputline, 256, fptr ) ;
229 sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
230 &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
233 else /* No data file, set a default */
235 printf ( "ERROR opening file <%s>\n", fnme ) ;
236 strcpy ( window_title, "Cantor Dust" ) ;
238 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
239 affine[0].a00 = 0.25 ; affine[0].a01 = 0.00 ; affine[0].a10 = 0.00 ; affine[0].a11 = 0.25 ;
240 affine[0].b0 = 0.0 ; affine[0].b1 = 0.0 ;
241 affine[1].a00 = 0.25 ; affine[1].a01 = 0.00 ; affine[1].a10 = 0.00 ; affine[1].a11 = 0.25 ;
242 affine[1].b0 = 0.5 ; affine[1].b1 = 0.0 ;
245 for ( i = 0; i < num_trans; i++ )
247 double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
248 double determ ; /* Determinant of this matrix */
250 /* Calculate the stationary point */
252 m00 = 1.0 - affine[i].a00 ;
253 m01 = - affine[i].a01 ;
254 m10 = - affine[i].a10 ;
255 m11 = 1.0 - affine[i].a11 ;
257 determ = m00 * m11 - m01 * m10 ;
259 if ( fabs ( determ ) > 1.e-6 )
261 affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
262 affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
265 affine[i].statx = affine[i].staty = 0.0 ;
270 main(int argc, char *argv[])
274 glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
276 glutInitWindowSize(500, 250);
277 glutInitWindowPosition ( 140, 140 ) ;
278 glutInit(&argc, argv);
281 readConfigFile ( argv[1] ) ;
283 readConfigFile ( "fractals.dat" ) ;
285 fractal_window = glutCreateWindow( window_title );
287 glClearColor(1.0, 1.0, 1.0, 1.0);
289 glutReshapeFunc(Reshape);
290 glutKeyboardFunc(Key);
291 glutSpecialFunc(Special);
292 glutDisplayFunc(Display);
296 printf ( "Back from the 'freeglut' main loop\n" ) ;
298 return 0; /* ANSI C requires main to return int. */