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++ )
69 random = (((random >> 10)) & 2) + (((random >> 20) ) & 1);
70 new_x = affine[random].a00 * current_x + affine[random].a01 * current_y + affine[random].b0 ;
71 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 ;
259 void readConfigFile ( char *fnme )
261 FILE *fptr = fopen ( fnme, "rt" ) ;
263 char inputline [ 256 ] ;
267 /* Read a header line */
268 fgets ( inputline, 256, fptr ) ;
270 /* Read a comment line */
271 fgets ( inputline, 256, fptr ) ;
273 /* Read the window title */
274 fgets ( inputline, 256, fptr ) ;
275 /* We assume here that this line will not exceed 79 characters plus a
276 newline (window_title is 80 characters long). That'll cause a buffer
277 overflow. For a simple program like this, though, we're letting it
280 sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
282 /* Read a comment line */
283 fgets ( inputline, 256, fptr ) ;
285 /* Read the number of affine transformations */
286 fgets ( inputline, 256, fptr ) ;
287 sscanf ( inputline, "%d", &num_trans ) ;
289 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
291 /* Read a comment line */
292 fgets ( inputline, 256, fptr ) ;
294 for ( i = 0; i < num_trans; i++ )
296 /* Read an affine transformation definition */
297 fgets ( inputline, 256, fptr ) ;
298 sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
299 &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
302 else /* No data file, set a default */
304 printf ( "ERROR opening file <%s>\n", fnme ) ;
305 strcpy ( window_title, "Cantor Dust" ) ;
307 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
308 affine[0].a00 = 0.25 ; affine[0].a01 = 0.00 ; affine[0].a10 = 0.00 ; affine[0].a11 = 0.25 ;
309 affine[0].b0 = 0.0 ; affine[0].b1 = 0.0 ;
310 affine[1].a00 = 0.25 ; affine[1].a01 = 0.00 ; affine[1].a10 = 0.00 ; affine[1].a11 = 0.25 ;
311 affine[1].b0 = 0.5 ; affine[1].b1 = 0.0 ;
314 for ( i = 0; i < num_trans; i++ )
316 double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
317 double determ ; /* Determinant of this matrix */
319 /* Calculate the stationary point */
321 m00 = 1.0 - affine[i].a00 ;
322 m01 = - affine[i].a01 ;
323 m10 = - affine[i].a10 ;
324 m11 = 1.0 - affine[i].a11 ;
326 determ = m00 * m11 - m01 * m10 ;
328 if ( fabs ( determ ) > 1.e-6 )
330 affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
331 affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
334 affine[i].statx = affine[i].staty = 0.0 ;
339 main(int argc, char *argv[])
343 glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
345 glutInitWindowSize(500, 250);
346 glutInitWindowPosition ( 140, 140 ) ;
347 glutInit(&argc, argv);
350 readConfigFile ( argv[1] ) ;
352 readConfigFile ( "fractals.dat" ) ;
354 fractal_window = glutCreateWindow( window_title );
356 glClearColor(1.0, 1.0, 1.0, 1.0);
358 glutReshapeFunc(Reshape);
359 glutKeyboardFunc(Key);
360 glutSpecialFunc(Special);
361 glutDisplayFunc(Display);
362 glutMouseFunc(Mouse);
363 glutMotionFunc(MouseMotion);
364 glutMouseWheelFunc(MouseWheel);
368 printf ( "Back from the 'freeglut' main loop\n" ) ;
373 _CrtDumpMemoryLeaks () ; // DUMP MEMORY LEAK INFORMATION
376 return 0; /* ANSI C requires main to return int. */