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 #define FGH_PI 3.14159265358979323846
30 /* DUMP MEMORY LEAKS */
36 double a00, a01, a10, a11 ; /* Transformation matrix */
37 double b0, b1 ; /* Constant vector added on */
38 double statx, staty ; /* Coordinates of the stationary point */
42 /* Number of levels to draw the fractal */
43 static int num_levels = 0 ;
45 /* The definition of the fractal */
46 static int num_trans ;
47 static AffineTrans *affine ;
49 /* the window title */
50 char window_title [ 80 ] ;
52 /* The amount the view is translated */
53 double xwin = 0.0, ywin = 0.0 ;
54 double scale_factor = 1.0 ;
56 /* The current point */
57 double current_x = 0.0, current_y = 0.0 ;
59 /* Signals when a glClear is needed */
60 static GLboolean needClear = GL_TRUE;
62 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
64 /* Draw a fractal transformed by "M", "N" as passed in */
67 for ( i = 0; i < 10; i++ )
69 int random = ( rand( ) >> 10 ) % num_trans;
70 double new_x = affine[random].a00 * current_x + affine[random].a01 * current_y + affine[random].b0 ;
71 double new_y = affine[random].a10 * current_x + affine[random].a11 * current_y + affine[random].b1 ;
73 glVertex2d ( new_x, new_y ) ;
83 glClear(GL_COLOR_BUFFER_BIT);
89 glScaled(2.5, 2.5, 2.5);
91 glColor4f(0.0, 0.0, 0.0, 1.0);
92 glBegin ( GL_POINTS ) ;
93 draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
99 glutPostRedisplay(); /* Needed so that this function will be called again */
103 Reshape(int width, int height)
106 glViewport(0, 0, width, height);
107 glMatrixMode(GL_PROJECTION);
109 ar = (float) width / (float) height;
111 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
113 glFrustum(-1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0);
114 glMatrixMode(GL_MODELVIEW);
118 glTranslated(xwin, ywin, -5.0);
123 Key(unsigned char key, int x, int y)
125 int changed_settings = 1;
128 case 27: /* Escape key */
129 glutLeaveMainLoop ();
132 case 'r' : case 'R' :
133 glMatrixMode(GL_MODELVIEW);
137 glTranslated(xwin, ywin, -5.0);
141 changed_settings = 0;
144 if (changed_settings)
150 Special(int key, int x, int y)
152 int changed_settings = 1;
156 glMatrixMode(GL_MODELVIEW);
157 ywin += 0.1 * scale_factor ;
158 glTranslated(0.0, 0.1 * scale_factor, 0.0);
162 glMatrixMode(GL_MODELVIEW);
163 ywin -= 0.1 * scale_factor ;
164 glTranslated(0.0, -0.1 * scale_factor, 0.0);
168 glMatrixMode(GL_MODELVIEW);
169 xwin -= 0.1 * scale_factor ;
170 glTranslated(-0.1 * scale_factor, 0.0, 0.0);
173 case GLUT_KEY_RIGHT :
174 glMatrixMode(GL_MODELVIEW);
175 xwin += 0.1 * scale_factor ;
176 glTranslated(0.1 * scale_factor, 0.0, 0.0);
179 case GLUT_KEY_PAGE_UP :
180 glMatrixMode(GL_MODELVIEW);
181 glTranslated ( -xwin, -ywin, 0.0 ) ;
182 glScaled(1.25, 1.25, 1.25);
183 glTranslated ( xwin, ywin, 0.0 ) ;
184 scale_factor *= 0.8 ;
187 case GLUT_KEY_PAGE_DOWN :
188 glMatrixMode(GL_MODELVIEW);
189 glTranslated ( -xwin, -ywin, 0.0 ) ;
190 glScaled(0.8, 0.8, 0.8);
191 glTranslated ( xwin, ywin, 0.0 ) ;
192 scale_factor *= 1.25 ;
196 changed_settings = 0;
199 if (changed_settings)
206 static int mouse_x = 0, mouse_y = 0 ;
207 static int button_down = GLUT_DOWN ;
210 Mouse ( int button, int updown, int x, int y )
212 button_down = updown ;
214 if ( updown == GLUT_DOWN )
222 MouseMotion ( int x, int y )
224 int window_width = glutGet ( GLUT_WINDOW_WIDTH ) ;
225 int window_height = glutGet ( GLUT_WINDOW_HEIGHT ) ;
226 int window_size = ( window_width < window_height ) ? window_width : window_height ;
228 double delta_x = 5.0 * (double)(x - mouse_x) / (double)(window_size) ;
229 double delta_y = 5.0 * (double)(y - mouse_y) / (double)(window_size) ;
231 xwin += delta_x * scale_factor ;
232 ywin -= delta_y * scale_factor ;
233 glMatrixMode ( GL_MODELVIEW ) ;
234 glTranslated ( delta_x * scale_factor, -delta_y * scale_factor, 0.0 ) ;
244 MouseWheel ( int wheel_number, int direction, int x, int y )
246 double scale = ( direction > 0 ) ? 1.25 : 0.8 ;
248 glMatrixMode ( GL_MODELVIEW ) ;
249 glTranslated ( -xwin, -ywin, 0.0 ) ;
250 glScaled ( scale, scale, scale ) ;
251 glTranslated ( xwin, ywin, 0.0 ) ;
252 scale_factor /= scale ;
260 checkedFGets ( char *s, int size, FILE *stream )
262 if ( fgets ( s, size, stream ) == NULL ) {
263 fprintf ( stderr, "fgets failed\n");
264 exit ( EXIT_FAILURE );
269 void readConfigFile ( char *fnme )
271 FILE *fptr = fopen ( fnme, "rt" ) ;
273 char inputline [ 256 ] ;
277 /* Read a header line */
278 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
280 /* Read a comment line */
281 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
283 /* Read the window title */
284 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
285 /* We assume here that this line will not exceed 79 characters plus a
286 newline (window_title is 80 characters long). That'll cause a buffer
287 overflow. For a simple program like this, though, we're letting it
290 sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
292 /* Read a comment line */
293 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
295 /* Read the number of affine transformations */
296 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
297 sscanf ( inputline, "%d", &num_trans ) ;
299 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
301 /* Read a comment line */
302 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
304 for ( i = 0; i < num_trans; i++ )
306 /* Read an affine transformation definition */
307 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
308 sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
309 &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
312 else /* No data file, set a default */
314 printf ( "ERROR opening file <%s>\n", fnme ) ;
315 strcpy ( window_title, "Koch Snowflake" ) ;
317 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
318 affine[0].a00 = 1/3. ; affine[0].a01 = 0.00 ; affine[0].a10 = 0.00 ; affine[0].a11 = 1/3. ;
319 affine[0].b0 = 0.0 ; affine[0].b1 = 0.0 ;
321 affine[1].a00 = 1/6. ; affine[1].a01 = -1/3.*sin(FGH_PI/3.) ; affine[1].a10 = 1/3.*sin(FGH_PI/3.) ; affine[1].a11 = 1/6. ;
322 affine[1].b0 = 1/3. ; affine[1].b1 = 0.0 ;
324 affine[2].a00 = 1/6. ; affine[2].a01 = -1/3.*sin(-FGH_PI/3.) ; affine[2].a10 = 1/3.*sin(-FGH_PI/3.) ; affine[2].a11 = 1/6. ;
325 affine[2].b0 = 0.5 ; affine[2].b1 = sqrt(3)/6. ;
327 affine[3].a00 = 1/3. ; affine[3].a01 = 0.00 ; affine[3].a10 = 0.00 ; affine[3].a11 = 1/3. ;
328 affine[3].b0 = 2/3. ; affine[3].b1 = 0.0 ;
331 for ( i = 0; i < num_trans; i++ )
333 double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
334 double determ ; /* Determinant of this matrix */
336 /* Calculate the stationary point */
338 m00 = 1.0 - affine[i].a00 ;
339 m01 = - affine[i].a01 ;
340 m10 = - affine[i].a10 ;
341 m11 = 1.0 - affine[i].a11 ;
343 determ = m00 * m11 - m01 * m10 ;
345 if ( fabs ( determ ) > 1.e-6 )
347 affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
348 affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
351 affine[i].statx = affine[i].staty = 0.0 ;
356 main(int argc, char *argv[])
360 glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
362 glutInitWindowSize(500, 250);
363 glutInitWindowPosition ( 140, 140 ) ;
364 glutInit(&argc, argv);
367 readConfigFile ( argv[1] ) ;
369 readConfigFile ( "fractals.dat" ) ;
371 fractal_window = glutCreateWindow( window_title );
373 glClearColor(1.0, 1.0, 1.0, 1.0);
375 glutReshapeFunc(Reshape);
376 glutKeyboardFunc(Key);
377 glutSpecialFunc(Special);
378 glutDisplayFunc(Display);
379 glutMouseFunc(Mouse);
380 glutMotionFunc(MouseMotion);
381 glutMouseWheelFunc(MouseWheel);
385 printf ( "Back from the 'freeglut' main loop\n" ) ;
390 /* DUMP MEMORY LEAK INFORMATION */
391 _CrtDumpMemoryLeaks () ;
394 return 0; /* ANSI C requires main to return int. */