Add an idle function to ensure that the screen gets drawn even when callbacks are...
[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
28 typedef struct
29 {
30   double a00, a01, a10, a11 ;   /* Transformation matrix */
31   double b0, b1 ;               /* Constant vector added on */
32   double statx, staty ;         /* Coordinates of the stationary point */
33 }
34 AffineTrans ;
35
36 /* Number of levels to draw the fractal */
37 static int num_levels = 0 ;
38
39 /* The definition of the fractal */
40 static int num_trans ;
41 static AffineTrans *affine ;
42
43 /* the window title */
44 char window_title [ 80 ] ;
45
46 /* The amount the view is translated */
47 float xwin = 0.0, ywin = 0.0 ;
48 float scale_factor = 1.0 ;
49
50 /* The current point */
51 float current_x = 0.0, current_y = 0.0 ;
52
53 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
54 {
55   /* Draw a fractal transformed by "M", "N" as passed in */
56   int i ;
57
58   for ( i = 0; i < 10; i++ )
59   {
60     int random = (rand() >> 10) % num_trans;
61     float new_x = affine[random].a00 * current_x + affine[random].a01 * current_y + affine[random].b0 ;
62     float new_y = affine[random].a10 * current_x + affine[random].a11 * current_y + affine[random].b1 ;
63
64     glVertex2f ( new_x, new_y ) ;
65     current_x = new_x ;
66     current_y = new_y ;
67   }
68 }
69
70 static void 
71 Display(void)
72 {
73   /* the curve */
74   glPushMatrix();
75   glScalef(2.5, 2.5, 2.5);
76
77   glColor4f(0.0, 0.0, 0.0, 1.0);
78   glBegin ( GL_POINTS ) ;
79   draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
80   glEnd () ;
81
82   glPopMatrix();
83
84   //  glutSwapBuffers(); /* Should NOT be here... This is a Single-Buffered Program! */
85   glutPostRedisplay();  /* Needed so that this function will be called again */
86 }
87
88 static void 
89 Reshape(int width, int height)
90 {
91   float ar;
92   glViewport(0, 0, width, height);
93   glMatrixMode(GL_PROJECTION);
94   glLoadIdentity();
95   ar = (float) width / (float) height;
96   glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
97   glMatrixMode(GL_MODELVIEW);
98   glLoadIdentity();
99   xwin = -1.0 ;
100   ywin =  0.0 ;
101   glTranslatef(xwin, ywin, -5.0);
102 }
103
104 static void 
105 Key(unsigned char key, int x, int y)
106 {
107   switch (key) {
108   case 27:  /* Escape key */
109     glutLeaveMainLoop ();
110     break;
111
112   case 'r' :  case 'R' :
113     glMatrixMode(GL_MODELVIEW);
114     glLoadIdentity();
115     xwin = -1.0 ;
116     ywin = 0.0 ;
117     glTranslatef(xwin, ywin, -5.0);
118
119     break ;
120   }
121
122   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
123
124   glutPostRedisplay();
125 }
126
127 static void 
128 Special(int key, int x, int y)
129 {
130   switch (key) {
131   case GLUT_KEY_UP :
132     glMatrixMode(GL_MODELVIEW);
133     ywin += 0.1 * scale_factor ;
134     glTranslatef(0.0, 0.1 * scale_factor, 0.0);
135     break ;
136
137   case GLUT_KEY_DOWN :
138     glMatrixMode(GL_MODELVIEW);
139     ywin -= 0.1 * scale_factor ;
140     glTranslatef(0.0, -0.1 * scale_factor, 0.0);
141     break ;
142
143   case GLUT_KEY_LEFT :
144     glMatrixMode(GL_MODELVIEW);
145     xwin -= 0.1 * scale_factor ;
146     glTranslatef(-0.1 * scale_factor, 0.0, 0.0);
147     break ;
148
149   case GLUT_KEY_RIGHT :
150     glMatrixMode(GL_MODELVIEW);
151     xwin += 0.1 * scale_factor ;
152     glTranslatef(0.1 * scale_factor, 0.0, 0.0);
153     break ;
154
155   case GLUT_KEY_PAGE_UP :
156     glMatrixMode(GL_MODELVIEW);
157     glTranslatef ( -xwin, -ywin, 0.0 ) ;
158     glScalef(1.25, 1.25, 1.25);
159     glTranslatef ( xwin, ywin, 0.0 ) ;
160     scale_factor *= 0.8 ;
161     break ;
162
163   case GLUT_KEY_PAGE_DOWN :
164     glMatrixMode(GL_MODELVIEW);
165     glTranslatef ( -xwin, -ywin, 0.0 ) ;
166     glScalef(0.8, 0.8, 0.8);
167     glTranslatef ( xwin, ywin, 0.0 ) ;
168     scale_factor *= 1.25 ;
169     break ;
170   }
171
172   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
173
174   glutPostRedisplay();
175 }
176
177 void readConfigFile ( char *fnme )
178 {
179   FILE *fptr = fopen ( fnme, "rt" ) ;
180   int i ;
181   char inputline [ 256 ] ;
182
183   /* Read a header line */
184   fgets ( inputline, 256, fptr ) ;
185
186   /* Read a comment line */
187   fgets ( inputline, 256, fptr ) ;
188
189   /* Read the window title */
190   fgets ( inputline, 256, fptr ) ;
191   /* We assume here that this line will not exceed 79 characters plus a 
192      newline (window_title is 80 characters long). That'll cause a buffer 
193      overflow. For a simple program like  this, though, we're letting it 
194      slide! 
195   */
196   sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
197
198   /* Read a comment line */
199   fgets ( inputline, 256, fptr ) ;
200
201   /* Read the number of affine transformations */
202   fgets ( inputline, 256, fptr ) ;
203   sscanf ( inputline, "%d", &num_trans ) ;
204
205   affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
206
207   /* Read a comment line */
208   fgets ( inputline, 256, fptr ) ;
209
210   for ( i = 0; i < num_trans; i++ )
211   {
212     double m00, m01, m10, m11 ;  /* Matrix "I" minus "A" */
213     double determ ;              /* Determinant of this matrix */
214
215     /* Read an affine transformation definition */
216     fgets ( inputline, 256, fptr ) ;
217     sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
218                      &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
219
220     /* Calculate the stationary point */
221
222     m00 = 1.0 - affine[i].a00 ;
223     m01 =     - affine[i].a01 ;
224     m10 =     - affine[i].a10 ;
225     m11 = 1.0 - affine[i].a11 ;
226
227     determ = m00 * m11 - m01 * m10 ;
228
229     if ( fabs ( determ ) > 1.e-6 )
230     {
231       affine[i].statx = (  m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
232       affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
233     }
234     else
235       affine[i].statx = affine[i].staty = 0.0 ;
236   }
237 }
238
239 void Idle(void) {
240     return;
241 }
242
243 int 
244 main(int argc, char *argv[])
245 {
246   int fractal_window ;
247
248   if ( argc > 1 )
249     readConfigFile ( argv[1] ) ;
250   else
251     readConfigFile ( "fractals.dat" ) ;
252
253   glutInit(&argc, argv);
254   glutInitWindowSize(500, 250);
255   glutInitWindowPosition ( 140, 140 ) ;
256
257   glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
258
259   fractal_window = glutCreateWindow( window_title );
260
261   glClearColor(1.0, 1.0, 1.0, 1.0);
262
263   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
264
265   glutReshapeFunc(Reshape);
266   glutKeyboardFunc(Key);
267   glutSpecialFunc(Special);
268   glutDisplayFunc(Display);
269   glutIdleFunc(Idle);
270
271   glutMainLoop();
272
273   printf ( "Back from the 'freeglut' main loop\n" ) ;
274
275   return 0;             /* ANSI C requires main to return int. */
276 }