Make "gcc -Wall -pedantic -Werror" happy.
[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 <string.h>
27 #include <math.h>
28 #ifdef WIN32
29 /* DUMP MEMORY LEAKS */
30 #include <crtdbg.h>
31 #endif
32
33 typedef struct
34 {
35   double a00, a01, a10, a11 ;   /* Transformation matrix */
36   double b0, b1 ;               /* Constant vector added on */
37   double statx, staty ;         /* Coordinates of the stationary point */
38 }
39 AffineTrans ;
40
41 /* Number of levels to draw the fractal */
42 static int num_levels = 0 ;
43
44 /* The definition of the fractal */
45 static int num_trans ;
46 static AffineTrans *affine ;
47
48 /* the window title */
49 char window_title [ 80 ] ;
50
51 /* The amount the view is translated */
52 double xwin = 0.0, ywin = 0.0 ;
53 double scale_factor = 1.0 ;
54
55 /* The current point */
56 double current_x = 0.0, current_y = 0.0 ;
57
58 /* Signals when a glClear is needed */
59 static GLboolean needClear = GL_TRUE;
60
61 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
62 {
63   /* Draw a fractal transformed by "M", "N" as passed in */
64   int i ;
65
66   for ( i = 0; i < 10; i++ )
67   {
68     int random = ( rand( ) >> 10 ) % num_trans;
69     double new_x = affine[random].a00 * current_x + affine[random].a01 * current_y + affine[random].b0 ;
70     double new_y = affine[random].a10 * current_x + affine[random].a11 * current_y + affine[random].b1 ;
71     
72     glVertex2d ( new_x, new_y ) ;
73     current_x = new_x ;
74     current_y = new_y ;
75   }
76 }
77
78 static void 
79 Display(void)
80 {
81   if (needClear) {
82     glClear(GL_COLOR_BUFFER_BIT);
83     needClear = GL_FALSE;
84   }
85
86   /* the curve */
87   glPushMatrix();
88   glScaled(2.5, 2.5, 2.5);
89
90   glColor4f(0.0, 0.0, 0.0, 1.0);
91   glBegin ( GL_POINTS ) ;
92   draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
93   glEnd () ;
94
95   glPopMatrix();
96
97   glFlush();
98   glutPostRedisplay();  /* Needed so that this function will be called again */
99 }
100
101 static void 
102 Reshape(int width, int height)
103 {
104   float ar;
105   glViewport(0, 0, width, height);
106   glMatrixMode(GL_PROJECTION);
107   glLoadIdentity();
108   ar = (float) width / (float) height;
109   if( ar > 1 )
110       glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
111   else
112       glFrustum(-1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0);
113   glMatrixMode(GL_MODELVIEW);
114   glLoadIdentity();
115   xwin = -1.0 ;
116   ywin =  0.0 ;
117   glTranslated(xwin, ywin, -5.0);
118   needClear = GL_TRUE;
119 }
120
121 static void 
122 Key(unsigned char key, int x, int y)
123 {
124   int changed_settings = 1;
125   
126   switch (key) {
127   case 27:  /* Escape key */
128     glutLeaveMainLoop ();
129     break;
130
131   case 'r' :  case 'R' :
132     glMatrixMode(GL_MODELVIEW);
133     glLoadIdentity();
134     xwin = -1.0 ;
135     ywin = 0.0 ;
136     glTranslated(xwin, ywin, -5.0);
137     break ;
138
139   default:
140     changed_settings = 0;
141     break;
142   }
143   if (changed_settings)
144     needClear = GL_TRUE;
145   glutPostRedisplay();
146 }
147
148 static void 
149 Special(int key, int x, int y)
150 {
151   int changed_settings = 1;
152
153   switch (key) {
154   case GLUT_KEY_UP :
155     glMatrixMode(GL_MODELVIEW);
156     ywin += 0.1 * scale_factor ;
157     glTranslated(0.0, 0.1 * scale_factor, 0.0);
158     break ;
159
160   case GLUT_KEY_DOWN :
161     glMatrixMode(GL_MODELVIEW);
162     ywin -= 0.1 * scale_factor ;
163     glTranslated(0.0, -0.1 * scale_factor, 0.0);
164     break ;
165
166   case GLUT_KEY_LEFT :
167     glMatrixMode(GL_MODELVIEW);
168     xwin -= 0.1 * scale_factor ;
169     glTranslated(-0.1 * scale_factor, 0.0, 0.0);
170     break ;
171
172   case GLUT_KEY_RIGHT :
173     glMatrixMode(GL_MODELVIEW);
174     xwin += 0.1 * scale_factor ;
175     glTranslated(0.1 * scale_factor, 0.0, 0.0);
176     break ;
177
178   case GLUT_KEY_PAGE_UP :
179     glMatrixMode(GL_MODELVIEW);
180     glTranslated ( -xwin, -ywin, 0.0 ) ;
181     glScaled(1.25, 1.25, 1.25);
182     glTranslated ( xwin, ywin, 0.0 ) ;
183     scale_factor *= 0.8 ;
184     break ;
185
186   case GLUT_KEY_PAGE_DOWN :
187     glMatrixMode(GL_MODELVIEW);
188     glTranslated ( -xwin, -ywin, 0.0 ) ;
189     glScaled(0.8, 0.8, 0.8);
190     glTranslated ( xwin, ywin, 0.0 ) ;
191     scale_factor *= 1.25 ;
192     break ;
193
194   default:
195     changed_settings = 0;
196     break;
197   }
198   if (changed_settings)
199     needClear = GL_TRUE;
200
201   glutPostRedisplay();
202 }
203
204
205 static int mouse_x = 0, mouse_y = 0 ;
206 static int button_down = GLUT_DOWN ;
207
208 static void 
209 Mouse ( int button, int updown, int x, int y )
210 {
211   button_down = updown ;
212
213   if ( updown == GLUT_DOWN )
214   {
215     mouse_x = x ;
216     mouse_y = y ;
217   }
218 }
219
220 static void 
221 MouseMotion ( int x, int y )
222 {
223   int window_width  = glutGet ( GLUT_WINDOW_WIDTH  ) ;
224   int window_height = glutGet ( GLUT_WINDOW_HEIGHT ) ;
225   int window_size = ( window_width < window_height ) ? window_width : window_height ;
226
227   double delta_x = 5.0 * (double)(x - mouse_x) / (double)(window_size) ;
228   double delta_y = 5.0 * (double)(y - mouse_y) / (double)(window_size) ;
229
230   xwin += delta_x * scale_factor ;
231   ywin -= delta_y * scale_factor ;
232   glMatrixMode ( GL_MODELVIEW ) ;
233   glTranslated ( delta_x * scale_factor, -delta_y * scale_factor, 0.0 ) ;
234
235   needClear = GL_TRUE;
236   glutPostRedisplay();
237
238   mouse_x = x ;
239   mouse_y = y ;
240 }
241
242 static void 
243 MouseWheel ( int wheel_number, int direction, int x, int y )
244 {
245   double scale = ( direction > 0 ) ? 1.25 : 0.8 ;
246
247   glMatrixMode ( GL_MODELVIEW ) ;
248   glTranslated ( -xwin, -ywin, 0.0 ) ;
249   glScaled ( scale, scale, scale ) ;
250   glTranslated ( xwin, ywin, 0.0 ) ;
251   scale_factor /= scale ;
252
253   needClear = GL_TRUE;
254   glutPostRedisplay();
255 }
256
257
258 void readConfigFile ( char *fnme )
259 {
260   FILE *fptr = fopen ( fnme, "rt" ) ;
261   int i ;
262   char inputline [ 256 ] ;
263
264   if ( fptr )
265   {
266     /* Read a header line */
267     fgets ( inputline, 256, fptr ) ;
268
269     /* Read a comment line */
270     fgets ( inputline, 256, fptr ) ;
271
272     /* Read the window title */
273     fgets ( inputline, 256, fptr ) ;
274     /* We assume here that this line will not exceed 79 characters plus a 
275        newline (window_title is 80 characters long). That'll cause a buffer 
276        overflow. For a simple program like  this, though, we're letting it 
277        slide! 
278     */
279     sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ; 
280
281     /* Read a comment line */
282     fgets ( inputline, 256, fptr ) ;
283
284     /* Read the number of affine transformations */
285     fgets ( inputline, 256, fptr ) ;
286     sscanf ( inputline, "%d", &num_trans ) ;
287
288     affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
289
290     /* Read a comment line */
291     fgets ( inputline, 256, fptr ) ;
292
293     for ( i = 0; i < num_trans; i++ )
294     {
295       /* Read an affine transformation definition */
296       fgets ( inputline, 256, fptr ) ;
297       sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
298                        &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
299     }
300   }
301   else  /* No data file, set a default */
302   {
303     printf ( "ERROR opening file <%s>\n", fnme ) ;
304     strcpy ( window_title, "Cantor Dust" ) ;
305     num_trans = 2 ;
306     affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
307     affine[0].a00 = 0.25 ;  affine[0].a01 = 0.00 ;  affine[0].a10 = 0.00 ;  affine[0].a11 = 0.25 ;
308     affine[0].b0 = 0.0 ;    affine[0].b1 = 0.0 ;
309     affine[1].a00 = 0.25 ;  affine[1].a01 = 0.00 ;  affine[1].a10 = 0.00 ;  affine[1].a11 = 0.25 ;
310     affine[1].b0 = 0.5 ;    affine[1].b1 = 0.0 ;
311   }
312
313   for ( i = 0; i < num_trans; i++ )
314   {
315     double m00, m01, m10, m11 ;  /* Matrix "I" minus "A" */
316     double determ ;              /* Determinant of this matrix */
317
318     /* Calculate the stationary point */
319
320     m00 = 1.0 - affine[i].a00 ;
321     m01 =     - affine[i].a01 ;
322     m10 =     - affine[i].a10 ;
323     m11 = 1.0 - affine[i].a11 ;
324
325     determ = m00 * m11 - m01 * m10 ;
326
327     if ( fabs ( determ ) > 1.e-6 )
328     {
329       affine[i].statx = (  m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
330       affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
331     }
332     else
333       affine[i].statx = affine[i].staty = 0.0 ;
334   }
335 }
336
337 int 
338 main(int argc, char *argv[])
339 {
340   int fractal_window ;
341
342   glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
343
344   glutInitWindowSize(500, 250);
345   glutInitWindowPosition ( 140, 140 ) ;
346   glutInit(&argc, argv);
347
348   if ( argc > 1 )
349     readConfigFile ( argv[1] ) ;
350   else
351     readConfigFile ( "fractals.dat" ) ;
352
353   fractal_window = glutCreateWindow( window_title );
354
355   glClearColor(1.0, 1.0, 1.0, 1.0);
356
357   glutReshapeFunc(Reshape);
358   glutKeyboardFunc(Key);
359   glutSpecialFunc(Special);
360   glutDisplayFunc(Display);
361   glutMouseFunc(Mouse);
362   glutMotionFunc(MouseMotion);
363   glutMouseWheelFunc(MouseWheel);
364
365   glutMainLoop();
366
367   printf ( "Back from the 'freeglut' main loop\n" ) ;
368
369   free ( affine ) ;
370
371 #ifdef WIN32
372   /* DUMP MEMORY LEAK INFORMATION */
373   _CrtDumpMemoryLeaks () ;
374 #endif
375
376   return 0;             /* ANSI C requires main to return int. */
377 }