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 #include <crtdbg.h> // DUMP MEMORY LEAKS
34 double a00, a01, a10, a11 ; /* Transformation matrix */
35 double b0, b1 ; /* Constant vector added on */
36 double statx, staty ; /* Coordinates of the stationary point */
40 /* Number of levels to draw the fractal */
41 static int num_levels = 0 ;
43 /* The definition of the fractal */
44 static int num_trans ;
45 static AffineTrans *affine ;
47 /* the window title */
48 char window_title [ 80 ] ;
50 /* The amount the view is translated */
51 double xwin = 0.0, ywin = 0.0 ;
52 double scale_factor = 1.0 ;
54 /* The current point */
55 double current_x = 0.0, current_y = 0.0 ;
57 /* Signals when a glClear is needed */
58 static GLboolean needClear = GL_TRUE;
60 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
62 /* Draw a fractal transformed by "M", "N" as passed in */
65 for ( i = 0; i < 10; i++ )
67 int random = ( rand( ) >> 10 ) % num_trans;
68 double new_x = affine[random].a00 * current_x + affine[random].a01 * current_y + affine[random].b0 ;
69 double new_y = affine[random].a10 * current_x + affine[random].a11 * current_y + affine[random].b1 ;
71 glVertex2d ( new_x, new_y ) ;
81 glClear(GL_COLOR_BUFFER_BIT);
87 glScaled(2.5, 2.5, 2.5);
89 glColor4f(0.0, 0.0, 0.0, 1.0);
90 glBegin ( GL_POINTS ) ;
91 draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
97 glutPostRedisplay(); /* Needed so that this function will be called again */
101 Reshape(int width, int height)
104 glViewport(0, 0, width, height);
105 glMatrixMode(GL_PROJECTION);
107 ar = (float) width / (float) height;
109 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
111 glFrustum(-1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0);
112 glMatrixMode(GL_MODELVIEW);
116 glTranslated(xwin, ywin, -5.0);
121 Key(unsigned char key, int x, int y)
123 int changed_settings = 1;
126 case 27: /* Escape key */
127 glutLeaveMainLoop ();
130 case 'r' : case 'R' :
131 glMatrixMode(GL_MODELVIEW);
135 glTranslated(xwin, ywin, -5.0);
139 changed_settings = 0;
142 if (changed_settings)
148 Special(int key, int x, int y)
150 int changed_settings = 1;
154 glMatrixMode(GL_MODELVIEW);
155 ywin += 0.1 * scale_factor ;
156 glTranslated(0.0, 0.1 * scale_factor, 0.0);
160 glMatrixMode(GL_MODELVIEW);
161 ywin -= 0.1 * scale_factor ;
162 glTranslated(0.0, -0.1 * scale_factor, 0.0);
166 glMatrixMode(GL_MODELVIEW);
167 xwin -= 0.1 * scale_factor ;
168 glTranslated(-0.1 * scale_factor, 0.0, 0.0);
171 case GLUT_KEY_RIGHT :
172 glMatrixMode(GL_MODELVIEW);
173 xwin += 0.1 * scale_factor ;
174 glTranslated(0.1 * scale_factor, 0.0, 0.0);
177 case GLUT_KEY_PAGE_UP :
178 glMatrixMode(GL_MODELVIEW);
179 glTranslated ( -xwin, -ywin, 0.0 ) ;
180 glScaled(1.25, 1.25, 1.25);
181 glTranslated ( xwin, ywin, 0.0 ) ;
182 scale_factor *= 0.8 ;
185 case GLUT_KEY_PAGE_DOWN :
186 glMatrixMode(GL_MODELVIEW);
187 glTranslated ( -xwin, -ywin, 0.0 ) ;
188 glScaled(0.8, 0.8, 0.8);
189 glTranslated ( xwin, ywin, 0.0 ) ;
190 scale_factor *= 1.25 ;
194 changed_settings = 0;
197 if (changed_settings)
204 static int mouse_x = 0, mouse_y = 0 ;
205 static int button_down = GLUT_DOWN ;
208 Mouse ( int button, int updown, int x, int y )
210 button_down = updown ;
212 if ( updown == GLUT_DOWN )
220 MouseMotion ( int x, int y )
222 int window_width = glutGet ( GLUT_WINDOW_WIDTH ) ;
223 int window_height = glutGet ( GLUT_WINDOW_HEIGHT ) ;
224 int window_size = ( window_width < window_height ) ? window_width : window_height ;
226 double delta_x = 5.0 * (double)(x - mouse_x) / (double)(window_size) ;
227 double delta_y = 5.0 * (double)(y - mouse_y) / (double)(window_size) ;
229 xwin += delta_x * scale_factor ;
230 ywin -= delta_y * scale_factor ;
231 glMatrixMode ( GL_MODELVIEW ) ;
232 glTranslated ( delta_x * scale_factor, -delta_y * scale_factor, 0.0 ) ;
242 MouseWheel ( int wheel_number, int direction, int x, int y )
244 double scale = ( direction > 0 ) ? 1.25 : 0.8 ;
246 glMatrixMode ( GL_MODELVIEW ) ;
247 glTranslated ( -xwin, -ywin, 0.0 ) ;
248 glScaled ( scale, scale, scale ) ;
249 glTranslated ( xwin, ywin, 0.0 ) ;
250 scale_factor /= scale ;
257 void readConfigFile ( char *fnme )
259 FILE *fptr = fopen ( fnme, "rt" ) ;
261 char inputline [ 256 ] ;
265 /* Read a header line */
266 fgets ( inputline, 256, fptr ) ;
268 /* Read a comment line */
269 fgets ( inputline, 256, fptr ) ;
271 /* Read the window title */
272 fgets ( inputline, 256, fptr ) ;
273 /* We assume here that this line will not exceed 79 characters plus a
274 newline (window_title is 80 characters long). That'll cause a buffer
275 overflow. For a simple program like this, though, we're letting it
278 sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
280 /* Read a comment line */
281 fgets ( inputline, 256, fptr ) ;
283 /* Read the number of affine transformations */
284 fgets ( inputline, 256, fptr ) ;
285 sscanf ( inputline, "%d", &num_trans ) ;
287 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
289 /* Read a comment line */
290 fgets ( inputline, 256, fptr ) ;
292 for ( i = 0; i < num_trans; i++ )
294 /* Read an affine transformation definition */
295 fgets ( inputline, 256, fptr ) ;
296 sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
297 &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
300 else /* No data file, set a default */
302 printf ( "ERROR opening file <%s>\n", fnme ) ;
303 strcpy ( window_title, "Cantor Dust" ) ;
305 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
306 affine[0].a00 = 0.25 ; affine[0].a01 = 0.00 ; affine[0].a10 = 0.00 ; affine[0].a11 = 0.25 ;
307 affine[0].b0 = 0.0 ; affine[0].b1 = 0.0 ;
308 affine[1].a00 = 0.25 ; affine[1].a01 = 0.00 ; affine[1].a10 = 0.00 ; affine[1].a11 = 0.25 ;
309 affine[1].b0 = 0.5 ; affine[1].b1 = 0.0 ;
312 for ( i = 0; i < num_trans; i++ )
314 double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
315 double determ ; /* Determinant of this matrix */
317 /* Calculate the stationary point */
319 m00 = 1.0 - affine[i].a00 ;
320 m01 = - affine[i].a01 ;
321 m10 = - affine[i].a10 ;
322 m11 = 1.0 - affine[i].a11 ;
324 determ = m00 * m11 - m01 * m10 ;
326 if ( fabs ( determ ) > 1.e-6 )
328 affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
329 affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
332 affine[i].statx = affine[i].staty = 0.0 ;
337 main(int argc, char *argv[])
341 glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
343 glutInitWindowSize(500, 250);
344 glutInitWindowPosition ( 140, 140 ) ;
345 glutInit(&argc, argv);
348 readConfigFile ( argv[1] ) ;
350 readConfigFile ( "fractals.dat" ) ;
352 fractal_window = glutCreateWindow( window_title );
354 glClearColor(1.0, 1.0, 1.0, 1.0);
356 glutReshapeFunc(Reshape);
357 glutKeyboardFunc(Key);
358 glutSpecialFunc(Special);
359 glutDisplayFunc(Display);
360 glutMouseFunc(Mouse);
361 glutMotionFunc(MouseMotion);
362 glutMouseWheelFunc(MouseWheel);
366 printf ( "Back from the 'freeglut' main loop\n" ) ;
371 _CrtDumpMemoryLeaks () ; // DUMP MEMORY LEAK INFORMATION
374 return 0; /* ANSI C requires main to return int. */