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>
28 #include <crtdbg.h> // DUMP MEMORY LEAKS
33 double a00, a01, a10, a11 ; /* Transformation matrix */
34 double b0, b1 ; /* Constant vector added on */
35 double statx, staty ; /* Coordinates of the stationary point */
39 /* Number of levels to draw the fractal */
40 static int num_levels = 0 ;
42 /* The definition of the fractal */
43 static int num_trans ;
44 static AffineTrans *affine ;
46 /* the window title */
47 char window_title [ 80 ] ;
49 /* The amount the view is translated */
50 double xwin = 0.0, ywin = 0.0 ;
51 double scale_factor = 1.0 ;
53 /* The current point */
54 double current_x = 0.0, current_y = 0.0 ;
56 /* Signals when a glClear is needed */
57 static GLboolean needClear = GL_TRUE;
59 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
61 /* Draw a fractal transformed by "M", "N" as passed in */
64 for ( i = 0; i < 10; i++ )
66 int random = ( rand( ) >> 10 ) % num_trans;
67 double new_x = affine[random].a00 * current_x + affine[random].a01 * current_y + affine[random].b0 ;
68 double new_y = affine[random].a10 * current_x + affine[random].a11 * current_y + affine[random].b1 ;
70 glVertex2d ( new_x, new_y ) ;
80 glClear(GL_COLOR_BUFFER_BIT);
86 glScaled(2.5, 2.5, 2.5);
88 glColor4f(0.0, 0.0, 0.0, 1.0);
89 glBegin ( GL_POINTS ) ;
90 draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
96 glutPostRedisplay(); /* Needed so that this function will be called again */
100 Reshape(int width, int height)
103 glViewport(0, 0, width, height);
104 glMatrixMode(GL_PROJECTION);
106 ar = (float) width / (float) height;
108 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
110 glFrustum(-1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0);
111 glMatrixMode(GL_MODELVIEW);
115 glTranslated(xwin, ywin, -5.0);
120 Key(unsigned char key, int x, int y)
122 int changed_settings = 1;
125 case 27: /* Escape key */
126 glutLeaveMainLoop ();
129 case 'r' : case 'R' :
130 glMatrixMode(GL_MODELVIEW);
134 glTranslated(xwin, ywin, -5.0);
138 changed_settings = 0;
141 if (changed_settings)
147 Special(int key, int x, int y)
149 int changed_settings = 1;
153 glMatrixMode(GL_MODELVIEW);
154 ywin += 0.1 * scale_factor ;
155 glTranslated(0.0, 0.1 * scale_factor, 0.0);
159 glMatrixMode(GL_MODELVIEW);
160 ywin -= 0.1 * scale_factor ;
161 glTranslated(0.0, -0.1 * scale_factor, 0.0);
165 glMatrixMode(GL_MODELVIEW);
166 xwin -= 0.1 * scale_factor ;
167 glTranslated(-0.1 * scale_factor, 0.0, 0.0);
170 case GLUT_KEY_RIGHT :
171 glMatrixMode(GL_MODELVIEW);
172 xwin += 0.1 * scale_factor ;
173 glTranslated(0.1 * scale_factor, 0.0, 0.0);
176 case GLUT_KEY_PAGE_UP :
177 glMatrixMode(GL_MODELVIEW);
178 glTranslated ( -xwin, -ywin, 0.0 ) ;
179 glScaled(1.25, 1.25, 1.25);
180 glTranslated ( xwin, ywin, 0.0 ) ;
181 scale_factor *= 0.8 ;
184 case GLUT_KEY_PAGE_DOWN :
185 glMatrixMode(GL_MODELVIEW);
186 glTranslated ( -xwin, -ywin, 0.0 ) ;
187 glScaled(0.8, 0.8, 0.8);
188 glTranslated ( xwin, ywin, 0.0 ) ;
189 scale_factor *= 1.25 ;
193 changed_settings = 0;
196 if (changed_settings)
203 static int mouse_x = 0, mouse_y = 0 ;
204 static int button_down = GLUT_DOWN ;
207 Mouse ( int button, int updown, int x, int y )
209 button_down = updown ;
211 if ( updown == GLUT_DOWN )
219 MouseMotion ( int x, int y )
221 int window_width = glutGet ( GLUT_WINDOW_WIDTH ) ;
222 int window_height = glutGet ( GLUT_WINDOW_HEIGHT ) ;
223 int window_size = ( window_width < window_height ) ? window_width : window_height ;
225 double delta_x = 5.0 * (double)(x - mouse_x) / (double)(window_size) ;
226 double delta_y = 5.0 * (double)(y - mouse_y) / (double)(window_size) ;
228 xwin += delta_x * scale_factor ;
229 ywin -= delta_y * scale_factor ;
230 glMatrixMode ( GL_MODELVIEW ) ;
231 glTranslated ( delta_x * scale_factor, -delta_y * scale_factor, 0.0 ) ;
241 MouseWheel ( int wheel_number, int direction, int x, int y )
243 double scale = ( direction > 0 ) ? 1.25 : 0.8 ;
245 glMatrixMode ( GL_MODELVIEW ) ;
246 glTranslated ( -xwin, -ywin, 0.0 ) ;
247 glScaled ( scale, scale, scale ) ;
248 glTranslated ( xwin, ywin, 0.0 ) ;
249 scale_factor /= scale ;
256 void readConfigFile ( char *fnme )
258 FILE *fptr = fopen ( fnme, "rt" ) ;
260 char inputline [ 256 ] ;
264 /* Read a header line */
265 fgets ( inputline, 256, fptr ) ;
267 /* Read a comment line */
268 fgets ( inputline, 256, fptr ) ;
270 /* Read the window title */
271 fgets ( inputline, 256, fptr ) ;
272 /* We assume here that this line will not exceed 79 characters plus a
273 newline (window_title is 80 characters long). That'll cause a buffer
274 overflow. For a simple program like this, though, we're letting it
277 sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
279 /* Read a comment line */
280 fgets ( inputline, 256, fptr ) ;
282 /* Read the number of affine transformations */
283 fgets ( inputline, 256, fptr ) ;
284 sscanf ( inputline, "%d", &num_trans ) ;
286 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
288 /* Read a comment line */
289 fgets ( inputline, 256, fptr ) ;
291 for ( i = 0; i < num_trans; i++ )
293 /* Read an affine transformation definition */
294 fgets ( inputline, 256, fptr ) ;
295 sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
296 &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
299 else /* No data file, set a default */
301 printf ( "ERROR opening file <%s>\n", fnme ) ;
302 strcpy ( window_title, "Cantor Dust" ) ;
304 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
305 affine[0].a00 = 0.25 ; affine[0].a01 = 0.00 ; affine[0].a10 = 0.00 ; affine[0].a11 = 0.25 ;
306 affine[0].b0 = 0.0 ; affine[0].b1 = 0.0 ;
307 affine[1].a00 = 0.25 ; affine[1].a01 = 0.00 ; affine[1].a10 = 0.00 ; affine[1].a11 = 0.25 ;
308 affine[1].b0 = 0.5 ; affine[1].b1 = 0.0 ;
311 for ( i = 0; i < num_trans; i++ )
313 double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
314 double determ ; /* Determinant of this matrix */
316 /* Calculate the stationary point */
318 m00 = 1.0 - affine[i].a00 ;
319 m01 = - affine[i].a01 ;
320 m10 = - affine[i].a10 ;
321 m11 = 1.0 - affine[i].a11 ;
323 determ = m00 * m11 - m01 * m10 ;
325 if ( fabs ( determ ) > 1.e-6 )
327 affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
328 affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
331 affine[i].statx = affine[i].staty = 0.0 ;
336 main(int argc, char *argv[])
340 glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
342 glutInitWindowSize(500, 250);
343 glutInitWindowPosition ( 140, 140 ) ;
344 glutInit(&argc, argv);
347 readConfigFile ( argv[1] ) ;
349 readConfigFile ( "fractals.dat" ) ;
351 fractal_window = glutCreateWindow( window_title );
353 glClearColor(1.0, 1.0, 1.0, 1.0);
355 glutReshapeFunc(Reshape);
356 glutKeyboardFunc(Key);
357 glutSpecialFunc(Special);
358 glutDisplayFunc(Display);
359 glutMouseFunc(Mouse);
360 glutMotionFunc(MouseMotion);
361 glutMouseWheelFunc(MouseWheel);
365 printf ( "Back from the 'freeglut' main loop\n" ) ;
370 _CrtDumpMemoryLeaks () ; // DUMP MEMORY LEAK INFORMATION
373 return 0; /* ANSI C requires main to return int. */