changes for make dist
[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 () * num_trans / RAND_MAX ;
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   glutPostRedisplay();  /* Needed so that this function will be called again */
84 }
85
86 static void 
87 Reshape(int width, int height)
88 {
89   float ar;
90   glViewport(0, 0, width, height);
91   glMatrixMode(GL_PROJECTION);
92   glLoadIdentity();
93   ar = (float) width / (float) height;
94   glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
95   glMatrixMode(GL_MODELVIEW);
96   glLoadIdentity();
97   xwin = -1.0 ;
98   ywin =  0.0 ;
99   glTranslatef(xwin, ywin, -5.0);
100 }
101
102 static void 
103 Key(unsigned char key, int x, int y)
104 {
105   switch (key) {
106   case 27:  /* Escape key */
107     glutLeaveMainLoop ();
108     break;
109
110   case 'r' :  case 'R' :
111     glMatrixMode(GL_MODELVIEW);
112     glLoadIdentity();
113     xwin = -1.0 ;
114     ywin = 0.0 ;
115     glTranslatef(xwin, ywin, -5.0);
116
117     break ;
118   }
119
120   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
121
122   glutPostRedisplay();
123 }
124
125 static void 
126 Special(int key, int x, int y)
127 {
128   switch (key) {
129   case GLUT_KEY_UP :
130     glMatrixMode(GL_MODELVIEW);
131     ywin += 0.1 * scale_factor ;
132     glTranslatef(0.0, 0.1 * scale_factor, 0.0);
133     break ;
134
135   case GLUT_KEY_DOWN :
136     glMatrixMode(GL_MODELVIEW);
137     ywin -= 0.1 * scale_factor ;
138     glTranslatef(0.0, -0.1 * scale_factor, 0.0);
139     break ;
140
141   case GLUT_KEY_LEFT :
142     glMatrixMode(GL_MODELVIEW);
143     xwin -= 0.1 * scale_factor ;
144     glTranslatef(-0.1 * scale_factor, 0.0, 0.0);
145     break ;
146
147   case GLUT_KEY_RIGHT :
148     glMatrixMode(GL_MODELVIEW);
149     xwin += 0.1 * scale_factor ;
150     glTranslatef(0.1 * scale_factor, 0.0, 0.0);
151     break ;
152
153   case GLUT_KEY_PAGE_UP :
154     glMatrixMode(GL_MODELVIEW);
155     glTranslatef ( -xwin, -ywin, 0.0 ) ;
156     glScalef(1.25, 1.25, 1.25);
157     glTranslatef ( xwin, ywin, 0.0 ) ;
158     scale_factor *= 0.8 ;
159     break ;
160
161   case GLUT_KEY_PAGE_DOWN :
162     glMatrixMode(GL_MODELVIEW);
163     glTranslatef ( -xwin, -ywin, 0.0 ) ;
164     glScalef(0.8, 0.8, 0.8);
165     glTranslatef ( xwin, ywin, 0.0 ) ;
166     scale_factor *= 1.25 ;
167     break ;
168   }
169
170   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
171
172   glutPostRedisplay();
173 }
174
175 void readConfigFile ( char *fnme )
176 {
177   FILE *fptr = fopen ( fnme, "rt" ) ;
178   int i ;
179   char inputline [ 256 ] ;
180
181   /* Read a header line */
182   fgets ( inputline, 256, fptr ) ;
183
184   /* Read a comment line */
185   fgets ( inputline, 256, fptr ) ;
186
187   /* Read the window title */
188   fgets ( inputline, 256, fptr ) ;
189   sscanf ( inputline, "%s", window_title ) ;
190
191   /* Read a comment line */
192   fgets ( inputline, 256, fptr ) ;
193
194   /* Read the number of affine transformations */
195   fgets ( inputline, 256, fptr ) ;
196   sscanf ( inputline, "%d", &num_trans ) ;
197
198   affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
199
200   /* Read a comment line */
201   fgets ( inputline, 256, fptr ) ;
202
203   for ( i = 0; i < num_trans; i++ )
204   {
205     double m00, m01, m10, m11 ;  /* Matrix "I" minus "A" */
206     double determ ;              /* Determinant of this matrix */
207
208     /* Read an affine transformation definition */
209     fgets ( inputline, 256, fptr ) ;
210     sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
211                      &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
212
213     /* Calculate the stationary point */
214
215     m00 = 1.0 - affine[i].a00 ;
216     m01 =     - affine[i].a01 ;
217     m10 =     - affine[i].a10 ;
218     m11 = 1.0 - affine[i].a11 ;
219
220     determ = m00 * m11 - m01 * m10 ;
221
222     if ( fabs ( determ ) > 1.e-6 )
223     {
224       affine[i].statx = (  m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
225       affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
226     }
227     else
228       affine[i].statx = affine[i].staty = 0.0 ;
229   }
230 }
231
232 int 
233 main(int argc, char *argv[])
234 {
235   int fractal_window ;
236
237   if ( argc > 1 )
238     readConfigFile ( argv[1] ) ;
239   else
240     readConfigFile ( "fractals.dat" ) ;
241
242   glutInit(&argc, argv);
243   glutInitWindowSize(500, 250);
244   glutInitWindowPosition ( 140, 140 ) ;
245
246   glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
247
248   fractal_window = glutCreateWindow( window_title );
249
250   glClearColor(1.0, 1.0, 1.0, 1.0);
251
252   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
253
254   glutReshapeFunc(Reshape);
255   glutKeyboardFunc(Key);
256   glutSpecialFunc(Special);
257   glutDisplayFunc(Display);
258
259   glutMainLoop();
260
261   printf ( "Back from the 'freeglut' main loop\n" ) ;
262
263   return 0;             /* ANSI C requires main to return int. */
264 }