New demo from John: CallbackMaker.
[freeglut] / progs / demos / Fractals_random / fractals_random.c
1 /* fractals_random.c */
2 /* This demo shows a single-buffering "freeglut" example. */
3
4 /*
5  * Program to draw a fractal by Michael Barnsley's stochastic algorithm.
6  * 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
9  *  (3) To draw:
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
13  */
14
15 /*
16  * User Commands:
17  *  PgUp, PgDn - increase/decrease scaling
18  *  Arrow keys - translate viewing section
19  *  r - reset view
20  *  Escape - quit
21  */
22
23 #include <GL/freeglut.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #ifdef WIN32
28 #include <crtdbg.h>  // DUMP MEMORY LEAKS
29 #endif
30
31 typedef struct
32 {
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 */
36 }
37 AffineTrans ;
38
39 /* Number of levels to draw the fractal */
40 static int num_levels = 0 ;
41
42 /* The definition of the fractal */
43 static int num_trans ;
44 static AffineTrans *affine ;
45
46 /* the window title */
47 char window_title [ 80 ] ;
48
49 /* The amount the view is translated */
50 double xwin = 0.0, ywin = 0.0 ;
51 double scale_factor = 1.0 ;
52
53 /* The current point */
54 double current_x = 0.0, current_y = 0.0 ;
55
56 /* Signals when a glClear is needed */
57 static GLboolean needClear = GL_TRUE;
58
59 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
60 {
61   /* Draw a fractal transformed by "M", "N" as passed in */
62   int i ;
63
64   for ( i = 0; i < 10; i++ )
65   {
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 ;
69     
70     glVertex2d ( new_x, new_y ) ;
71     current_x = new_x ;
72     current_y = new_y ;
73   }
74 }
75
76 static void 
77 Display(void)
78 {
79   if (needClear) {
80     glClear(GL_COLOR_BUFFER_BIT);
81     needClear = GL_FALSE;
82   }
83
84   /* the curve */
85   glPushMatrix();
86   glScaled(2.5, 2.5, 2.5);
87
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 );
91   glEnd () ;
92
93   glPopMatrix();
94
95   glFlush();
96   glutPostRedisplay();  /* Needed so that this function will be called again */
97 }
98
99 static void 
100 Reshape(int width, int height)
101 {
102   float ar;
103   glViewport(0, 0, width, height);
104   glMatrixMode(GL_PROJECTION);
105   glLoadIdentity();
106   ar = (float) width / (float) height;
107   if( ar > 1 )
108       glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
109   else
110       glFrustum(-1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0);
111   glMatrixMode(GL_MODELVIEW);
112   glLoadIdentity();
113   xwin = -1.0 ;
114   ywin =  0.0 ;
115   glTranslated(xwin, ywin, -5.0);
116   needClear = GL_TRUE;
117 }
118
119 static void 
120 Key(unsigned char key, int x, int y)
121 {
122   int changed_settings = 1;
123   
124   switch (key) {
125   case 27:  /* Escape key */
126     glutLeaveMainLoop ();
127     break;
128
129   case 'r' :  case 'R' :
130     glMatrixMode(GL_MODELVIEW);
131     glLoadIdentity();
132     xwin = -1.0 ;
133     ywin = 0.0 ;
134     glTranslated(xwin, ywin, -5.0);
135     break ;
136
137   default:
138     changed_settings = 0;
139     break;
140   }
141   if (changed_settings)
142     needClear = GL_TRUE;
143   glutPostRedisplay();
144 }
145
146 static void 
147 Special(int key, int x, int y)
148 {
149   int changed_settings = 1;
150
151   switch (key) {
152   case GLUT_KEY_UP :
153     glMatrixMode(GL_MODELVIEW);
154     ywin += 0.1 * scale_factor ;
155     glTranslated(0.0, 0.1 * scale_factor, 0.0);
156     break ;
157
158   case GLUT_KEY_DOWN :
159     glMatrixMode(GL_MODELVIEW);
160     ywin -= 0.1 * scale_factor ;
161     glTranslated(0.0, -0.1 * scale_factor, 0.0);
162     break ;
163
164   case GLUT_KEY_LEFT :
165     glMatrixMode(GL_MODELVIEW);
166     xwin -= 0.1 * scale_factor ;
167     glTranslated(-0.1 * scale_factor, 0.0, 0.0);
168     break ;
169
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);
174     break ;
175
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 ;
182     break ;
183
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 ;
190     break ;
191
192   default:
193     changed_settings = 0;
194     break;
195   }
196   if (changed_settings)
197     needClear = GL_TRUE;
198
199   glutPostRedisplay();
200 }
201
202
203 static int mouse_x = 0, mouse_y = 0 ;
204 static int button_down = GLUT_DOWN ;
205
206 static void 
207 Mouse ( int button, int updown, int x, int y )
208 {
209   button_down = updown ;
210
211   if ( updown == GLUT_DOWN )
212   {
213     mouse_x = x ;
214     mouse_y = y ;
215   }
216 }
217
218 static void 
219 MouseMotion ( int x, int y )
220 {
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 ;
224
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) ;
227
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 ) ;
232
233   needClear = GL_TRUE;
234   glutPostRedisplay();
235
236   mouse_x = x ;
237   mouse_y = y ;
238 }
239
240 static void 
241 MouseWheel ( int wheel_number, int direction, int x, int y )
242 {
243   double scale = ( direction > 0 ) ? 1.25 : 0.8 ;
244
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 ;
250
251   needClear = GL_TRUE;
252   glutPostRedisplay();
253 }
254
255
256 void readConfigFile ( char *fnme )
257 {
258   FILE *fptr = fopen ( fnme, "rt" ) ;
259   int i ;
260   char inputline [ 256 ] ;
261
262   if ( fptr )
263   {
264     /* Read a header line */
265     fgets ( inputline, 256, fptr ) ;
266
267     /* Read a comment line */
268     fgets ( inputline, 256, fptr ) ;
269
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 
275        slide! 
276     */
277     sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ; 
278
279     /* Read a comment line */
280     fgets ( inputline, 256, fptr ) ;
281
282     /* Read the number of affine transformations */
283     fgets ( inputline, 256, fptr ) ;
284     sscanf ( inputline, "%d", &num_trans ) ;
285
286     affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
287
288     /* Read a comment line */
289     fgets ( inputline, 256, fptr ) ;
290
291     for ( i = 0; i < num_trans; i++ )
292     {
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 ) ;
297     }
298   }
299   else  /* No data file, set a default */
300   {
301     printf ( "ERROR opening file <%s>\n", fnme ) ;
302     strcpy ( window_title, "Cantor Dust" ) ;
303     num_trans = 2 ;
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 ;
309   }
310
311   for ( i = 0; i < num_trans; i++ )
312   {
313     double m00, m01, m10, m11 ;  /* Matrix "I" minus "A" */
314     double determ ;              /* Determinant of this matrix */
315
316     /* Calculate the stationary point */
317
318     m00 = 1.0 - affine[i].a00 ;
319     m01 =     - affine[i].a01 ;
320     m10 =     - affine[i].a10 ;
321     m11 = 1.0 - affine[i].a11 ;
322
323     determ = m00 * m11 - m01 * m10 ;
324
325     if ( fabs ( determ ) > 1.e-6 )
326     {
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 ;
329     }
330     else
331       affine[i].statx = affine[i].staty = 0.0 ;
332   }
333 }
334
335 int 
336 main(int argc, char *argv[])
337 {
338   int fractal_window ;
339
340   glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
341
342   glutInitWindowSize(500, 250);
343   glutInitWindowPosition ( 140, 140 ) ;
344   glutInit(&argc, argv);
345
346   if ( argc > 1 )
347     readConfigFile ( argv[1] ) ;
348   else
349     readConfigFile ( "fractals.dat" ) ;
350
351   fractal_window = glutCreateWindow( window_title );
352
353   glClearColor(1.0, 1.0, 1.0, 1.0);
354
355   glutReshapeFunc(Reshape);
356   glutKeyboardFunc(Key);
357   glutSpecialFunc(Special);
358   glutDisplayFunc(Display);
359   glutMouseFunc(Mouse);
360   glutMotionFunc(MouseMotion);
361   glutMouseWheelFunc(MouseWheel);
362
363   glutMainLoop();
364
365   printf ( "Back from the 'freeglut' main loop\n" ) ;
366
367   free ( affine ) ;
368
369 #ifdef WIN32
370   _CrtDumpMemoryLeaks () ;  // DUMP MEMORY LEAK INFORMATION
371 #endif
372
373   return 0;             /* ANSI C requires main to return int. */
374 }