ftp://ftp.krokus.ru/pub/OpenBSD/distfiles/xscreensaver-4.22.tar.gz
[xscreensaver] / hacks / glx / b_draw.c
1 #if 0
2 static const char sccsid[] = "@(#)b_draw.c  4.11 98/06/16 xlockmore";
3 #endif
4
5 /*-
6  * BUBBLE3D (C) 1998 Richard W.M. Jones.
7  * b_draw.c: This code creates new bubbles, manages them and controls
8  * them as they are drawn on the screen.
9  */
10
11 #include "bubble3d.h"
12
13 typedef struct draw_context {
14         /* The list of bubbles currently on the screen. */
15         void      **bubble_list;
16         int         nr_bubbles;
17
18         /* When was the last time we created a new bubble? */
19         int         bubble_count;
20 } draw_context;
21
22 void       *
23 glb_draw_init(void)
24 {
25         draw_context *c;
26
27         GLfloat     mat_specular[] =
28         {1, 1, 1, 1};
29         GLfloat     mat_emission[] =
30         {0, 0, 0, 1};
31         GLfloat     mat_shininess[] =
32         {100};
33         GLfloat     ambient[] =
34         {0.5, 0.5, 0.5, 1.0};
35         GLfloat     light_position[][4] =
36         {
37                 {0, -1, 0, 0},
38                 {1, 1, 0, 0},
39                 {-1, 0, 1, 0}};
40         GLfloat     light_diffuse[][4] =
41         {
42                 {1, 1, 1, 1},
43                 {1, 1, 1, 1},
44                 {1, 1, 1, 1}};
45         GLfloat     light_specular[][4] =
46         {
47                 {1, 1, 1, 1},
48                 {1, 1, 1, 1},
49                 {1, 1, 1, 1}};
50
51         /* Initialize the context. */
52         c = (struct draw_context *) malloc(sizeof (struct draw_context));
53
54         if (c == 0)
55                 return 0;
56         c->bubble_list = 0;
57         c->nr_bubbles = 0;
58         c->bubble_count = glb_config.create_bubbles_every;
59
60         /* Do some GL initialization. */
61         glClearColor(glb_config.bg_colour[0],
62                      glb_config.bg_colour[1],
63                      glb_config.bg_colour[2],
64                      glb_config.bg_colour[3]);
65
66         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, glb_config.bubble_colour);
67         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
68         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
69         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
70
71         if (glb_config.transparent_p)
72           glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
73
74         glEnable(GL_LIGHTING);
75         glEnable(GL_LIGHT0);
76         glEnable(GL_LIGHT1);
77         glEnable(GL_LIGHT2);
78
79         if (glb_config.transparent_p)
80           glEnable(GL_BLEND);
81         else
82           glEnable(GL_DEPTH_TEST);
83
84         glEnable(GL_AUTO_NORMAL);
85         glEnable(GL_NORMALIZE);
86
87         if (glb_config.transparent_p)
88           glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
89
90         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
91         glLightfv(GL_LIGHT0, GL_POSITION, light_position[0]);
92         glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse[0]);
93         glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular[0]);
94         glLightfv(GL_LIGHT1, GL_POSITION, light_position[1]);
95         glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse[1]);
96         glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular[1]);
97         glLightfv(GL_LIGHT2, GL_POSITION, light_position[2]);
98         glLightfv(GL_LIGHT2, GL_DIFFUSE, light_diffuse[2]);
99         glLightfv(GL_LIGHT2, GL_SPECULAR, light_specular[2]);
100
101         return c;
102 }
103
104 static void
105 delete_bubble(draw_context * c, int j)
106 {
107         int         i;
108
109         glb_bubble_delete(c->bubble_list[j]);
110
111         for (i = j; i < c->nr_bubbles - 1; ++i)
112                 c->bubble_list[i] = c->bubble_list[i + 1];
113
114         c->nr_bubbles--;
115 }
116
117 void
118 glb_draw_end(void *cc)
119 {
120         draw_context *c = (draw_context *) cc;
121         int         i;
122
123         for (i = 0; i < c->nr_bubbles; ++i) {
124                 delete_bubble(c, i);
125                 i--;
126         }
127         (void) free((void *) c->bubble_list);
128         (void) free((void *) c);
129 }
130
131 static int
132 create_new_bubbles(draw_context * c)
133 {
134         int         n, i;
135         double      r = glb_drand();
136         GLfloat     size, speed, scale_incr, x, y, z;
137         void       *b[4];
138         void      **old_bubble_list;
139
140         /* How many bubbles to make? */
141         if (r < glb_config.p_bubble_group[0])
142                 n = 1;
143         else if (r < glb_config.p_bubble_group[1])
144                 n = 2;
145         else if (r < glb_config.p_bubble_group[2])
146                 n = 3;
147         else
148                 n = 4;
149
150         /* Initial position of top-most bubble in group. */
151         x = glb_drand() * 4 - 2;
152         y = glb_config.screen_bottom;
153         z = glb_drand() * 2 - 2;
154
155         /* What size? */
156         size = glb_config.min_size
157                 + glb_drand() * (glb_config.max_size - glb_config.min_size);
158
159         /* What speed? */
160         speed = glb_config.min_speed
161                 + glb_drand() * (glb_config.max_speed - glb_config.min_speed);
162
163         /* Work out the scaling increment. Bubbles should increase by scale_factor
164          * as they go from bottom to top of screen.
165          */
166         scale_incr = (size * glb_config.scale_factor - size)
167                 / ((glb_config.screen_top - glb_config.screen_bottom) / speed);
168
169         /* Create the bubble(s). */
170         for (i = 0; i < n; ++i) {
171                 if ((b[i] = glb_bubble_new(x, y, z, size, speed, scale_incr)) == 0) {
172                         /* Out of memory - recover. */
173                         i--;
174                         while (i >= 0)
175                                 glb_bubble_delete(b[i]);
176                         return 0;
177                 }
178                 /* Create the next bubble below the last bubble. */
179                 y -= size * 3;
180         }
181
182         /* Add the bubbles to the list. */
183         c->nr_bubbles += n;
184         old_bubble_list = c->bubble_list;
185         if (c->bubble_list == 0) {
186                 c->bubble_list = (void **) malloc(c->nr_bubbles * sizeof (void *));
187         } else {
188                 c->bubble_list = (void **) realloc(c->bubble_list,
189                                             c->nr_bubbles * sizeof (void *));
190         }
191
192         if (c->bubble_list == 0) {
193                 /* Out of memory - recover. */
194                 for (i = 0; i < n; ++i)
195                         glb_bubble_delete(b[i]);
196                 c->bubble_list = old_bubble_list;
197                 c->nr_bubbles -= n;
198                 return 0;
199         }
200         for (i = 0; i < n; ++i)
201                 c->bubble_list[c->nr_bubbles - i - 1] = b[i];
202
203         return 1;
204 }
205
206 void
207 glb_draw_step(void *cc)
208 {
209         draw_context *c = (draw_context *) cc;
210         int         i;
211
212         /* Consider creating a new bubble or bubbles. */
213         if (c->nr_bubbles < glb_config.max_bubbles &&
214             c->bubble_count++ > glb_config.create_bubbles_every) {
215                 if (create_new_bubbles(c))
216                         c->bubble_count = 0;
217         }
218         /* Clear the display. */
219         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
220
221         /* XXX Draw the background here ... */
222
223         /* Draw all the bubbles on the display. */
224         for (i = 0; i < c->nr_bubbles; ++i) {
225                 void       *b = c->bubble_list[i];
226
227                 glb_bubble_step(b);
228                 glb_bubble_draw(b);
229
230                 /* Has the bubble reached the top of the screen? */
231                 if (glb_bubble_get_y(b) >= glb_config.screen_top) {
232                         delete_bubble(c, i);
233                         i--;
234                 }
235         }
236 }