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>
29 /* DUMP MEMORY LEAKS */
35 double a00, a01, a10, a11 ; /* Transformation matrix */
36 double b0, b1 ; /* Constant vector added on */
37 double statx, staty ; /* Coordinates of the stationary point */
41 /* Number of levels to draw the fractal */
42 static int num_levels = 0 ;
44 /* The definition of the fractal */
45 static int num_trans ;
46 static AffineTrans *affine ;
48 /* the window title */
49 char window_title [ 80 ] ;
51 /* The amount the view is translated */
52 double xwin = 0.0, ywin = 0.0 ;
53 double scale_factor = 1.0 ;
55 /* The current point */
56 double current_x = 0.0, current_y = 0.0 ;
58 /* Signals when a glClear is needed */
59 static GLboolean needClear = GL_TRUE;
61 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
63 /* Draw a fractal transformed by "M", "N" as passed in */
66 for ( i = 0; i < 10; i++ )
68 int random = ( rand( ) >> 10 ) % num_trans;
69 double new_x = affine[random].a00 * current_x + affine[random].a01 * current_y + affine[random].b0 ;
70 double new_y = affine[random].a10 * current_x + affine[random].a11 * current_y + affine[random].b1 ;
72 glVertex2d ( new_x, new_y ) ;
82 glClear(GL_COLOR_BUFFER_BIT);
88 glScaled(2.5, 2.5, 2.5);
90 glColor4f(0.0, 0.0, 0.0, 1.0);
91 glBegin ( GL_POINTS ) ;
92 draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
98 glutPostRedisplay(); /* Needed so that this function will be called again */
102 Reshape(int width, int height)
105 glViewport(0, 0, width, height);
106 glMatrixMode(GL_PROJECTION);
108 ar = (float) width / (float) height;
110 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
112 glFrustum(-1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0);
113 glMatrixMode(GL_MODELVIEW);
117 glTranslated(xwin, ywin, -5.0);
122 Key(unsigned char key, int x, int y)
124 int changed_settings = 1;
127 case 27: /* Escape key */
128 glutLeaveMainLoop ();
131 case 'r' : case 'R' :
132 glMatrixMode(GL_MODELVIEW);
136 glTranslated(xwin, ywin, -5.0);
140 changed_settings = 0;
143 if (changed_settings)
149 Special(int key, int x, int y)
151 int changed_settings = 1;
155 glMatrixMode(GL_MODELVIEW);
156 ywin += 0.1 * scale_factor ;
157 glTranslated(0.0, 0.1 * scale_factor, 0.0);
161 glMatrixMode(GL_MODELVIEW);
162 ywin -= 0.1 * scale_factor ;
163 glTranslated(0.0, -0.1 * scale_factor, 0.0);
167 glMatrixMode(GL_MODELVIEW);
168 xwin -= 0.1 * scale_factor ;
169 glTranslated(-0.1 * scale_factor, 0.0, 0.0);
172 case GLUT_KEY_RIGHT :
173 glMatrixMode(GL_MODELVIEW);
174 xwin += 0.1 * scale_factor ;
175 glTranslated(0.1 * scale_factor, 0.0, 0.0);
178 case GLUT_KEY_PAGE_UP :
179 glMatrixMode(GL_MODELVIEW);
180 glTranslated ( -xwin, -ywin, 0.0 ) ;
181 glScaled(1.25, 1.25, 1.25);
182 glTranslated ( xwin, ywin, 0.0 ) ;
183 scale_factor *= 0.8 ;
186 case GLUT_KEY_PAGE_DOWN :
187 glMatrixMode(GL_MODELVIEW);
188 glTranslated ( -xwin, -ywin, 0.0 ) ;
189 glScaled(0.8, 0.8, 0.8);
190 glTranslated ( xwin, ywin, 0.0 ) ;
191 scale_factor *= 1.25 ;
195 changed_settings = 0;
198 if (changed_settings)
205 static int mouse_x = 0, mouse_y = 0 ;
206 static int button_down = GLUT_DOWN ;
209 Mouse ( int button, int updown, int x, int y )
211 button_down = updown ;
213 if ( updown == GLUT_DOWN )
221 MouseMotion ( int x, int y )
223 int window_width = glutGet ( GLUT_WINDOW_WIDTH ) ;
224 int window_height = glutGet ( GLUT_WINDOW_HEIGHT ) ;
225 int window_size = ( window_width < window_height ) ? window_width : window_height ;
227 double delta_x = 5.0 * (double)(x - mouse_x) / (double)(window_size) ;
228 double delta_y = 5.0 * (double)(y - mouse_y) / (double)(window_size) ;
230 xwin += delta_x * scale_factor ;
231 ywin -= delta_y * scale_factor ;
232 glMatrixMode ( GL_MODELVIEW ) ;
233 glTranslated ( delta_x * scale_factor, -delta_y * scale_factor, 0.0 ) ;
243 MouseWheel ( int wheel_number, int direction, int x, int y )
245 double scale = ( direction > 0 ) ? 1.25 : 0.8 ;
247 glMatrixMode ( GL_MODELVIEW ) ;
248 glTranslated ( -xwin, -ywin, 0.0 ) ;
249 glScaled ( scale, scale, scale ) ;
250 glTranslated ( xwin, ywin, 0.0 ) ;
251 scale_factor /= scale ;
258 void readConfigFile ( char *fnme )
260 FILE *fptr = fopen ( fnme, "rt" ) ;
262 char inputline [ 256 ] ;
266 /* Read a header line */
267 fgets ( inputline, 256, fptr ) ;
269 /* Read a comment line */
270 fgets ( inputline, 256, fptr ) ;
272 /* Read the window title */
273 fgets ( inputline, 256, fptr ) ;
274 /* We assume here that this line will not exceed 79 characters plus a
275 newline (window_title is 80 characters long). That'll cause a buffer
276 overflow. For a simple program like this, though, we're letting it
279 sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
281 /* Read a comment line */
282 fgets ( inputline, 256, fptr ) ;
284 /* Read the number of affine transformations */
285 fgets ( inputline, 256, fptr ) ;
286 sscanf ( inputline, "%d", &num_trans ) ;
288 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
290 /* Read a comment line */
291 fgets ( inputline, 256, fptr ) ;
293 for ( i = 0; i < num_trans; i++ )
295 /* Read an affine transformation definition */
296 fgets ( inputline, 256, fptr ) ;
297 sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
298 &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
301 else /* No data file, set a default */
303 printf ( "ERROR opening file <%s>\n", fnme ) ;
304 strcpy ( window_title, "Cantor Dust" ) ;
306 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
307 affine[0].a00 = 0.25 ; affine[0].a01 = 0.00 ; affine[0].a10 = 0.00 ; affine[0].a11 = 0.25 ;
308 affine[0].b0 = 0.0 ; affine[0].b1 = 0.0 ;
309 affine[1].a00 = 0.25 ; affine[1].a01 = 0.00 ; affine[1].a10 = 0.00 ; affine[1].a11 = 0.25 ;
310 affine[1].b0 = 0.5 ; affine[1].b1 = 0.0 ;
313 for ( i = 0; i < num_trans; i++ )
315 double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
316 double determ ; /* Determinant of this matrix */
318 /* Calculate the stationary point */
320 m00 = 1.0 - affine[i].a00 ;
321 m01 = - affine[i].a01 ;
322 m10 = - affine[i].a10 ;
323 m11 = 1.0 - affine[i].a11 ;
325 determ = m00 * m11 - m01 * m10 ;
327 if ( fabs ( determ ) > 1.e-6 )
329 affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
330 affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
333 affine[i].statx = affine[i].staty = 0.0 ;
338 main(int argc, char *argv[])
342 glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
344 glutInitWindowSize(500, 250);
345 glutInitWindowPosition ( 140, 140 ) ;
346 glutInit(&argc, argv);
349 readConfigFile ( argv[1] ) ;
351 readConfigFile ( "fractals.dat" ) ;
353 fractal_window = glutCreateWindow( window_title );
355 glClearColor(1.0, 1.0, 1.0, 1.0);
357 glutReshapeFunc(Reshape);
358 glutKeyboardFunc(Key);
359 glutSpecialFunc(Special);
360 glutDisplayFunc(Display);
361 glutMouseFunc(Mouse);
362 glutMotionFunc(MouseMotion);
363 glutMouseWheelFunc(MouseWheel);
367 printf ( "Back from the 'freeglut' main loop\n" ) ;
372 /* DUMP MEMORY LEAK INFORMATION */
373 _CrtDumpMemoryLeaks () ;
376 return 0; /* ANSI C requires main to return int. */