2 static const char sccsid[] = "@(#)b_draw.c 4.11 98/06/16 xlockmore";
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.
13 typedef struct draw_context {
14 /* The list of bubbles currently on the screen. */
18 /* When was the last time we created a new bubble? */
30 GLfloat mat_specular[] =
32 GLfloat mat_emission[] =
34 GLfloat mat_shininess[] =
38 GLfloat light_position[][4] =
43 GLfloat light_diffuse[][4] =
48 GLfloat light_specular[][4] =
54 /* Initialize the context. */
55 c = (struct draw_context *) malloc(sizeof (struct draw_context));
61 c->bubble_count = glb_config.create_bubbles_every;
63 /* Do some GL initialization. */
64 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, glb_config.bubble_colour);
65 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
66 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
67 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
69 if (glb_config.transparent_p)
70 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
72 glEnable(GL_LIGHTING);
77 if (glb_config.transparent_p)
80 glEnable(GL_DEPTH_TEST);
82 glEnable(GL_AUTO_NORMAL);
83 glEnable(GL_NORMALIZE);
85 if (glb_config.transparent_p)
86 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
88 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
89 glLightfv(GL_LIGHT0, GL_POSITION, light_position[0]);
90 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse[0]);
91 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular[0]);
92 glLightfv(GL_LIGHT1, GL_POSITION, light_position[1]);
93 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse[1]);
94 glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular[1]);
95 glLightfv(GL_LIGHT2, GL_POSITION, light_position[2]);
96 glLightfv(GL_LIGHT2, GL_DIFFUSE, light_diffuse[2]);
97 glLightfv(GL_LIGHT2, GL_SPECULAR, light_specular[2]);
99 c->d = glb_sphere_init();
105 delete_bubble(draw_context * c, int j)
109 glb_bubble_delete(c->bubble_list[j]);
111 for (i = j; i < c->nr_bubbles - 1; ++i)
112 c->bubble_list[i] = c->bubble_list[i + 1];
118 glb_draw_end(void *cc)
120 draw_context *c = (draw_context *) cc;
123 for (i = 0; i < c->nr_bubbles; ++i) {
128 glb_sphere_end (c->d);
130 (void) free((void *) c->bubble_list);
131 (void) free((void *) c);
135 create_new_bubbles(draw_context * c)
138 double r = glb_drand();
139 GLfloat size, speed, scale_incr, x, y, z;
141 void **old_bubble_list;
143 /* How many bubbles to make? */
144 if (r < glb_config.p_bubble_group[0])
146 else if (r < glb_config.p_bubble_group[1])
148 else if (r < glb_config.p_bubble_group[2])
153 /* Initial position of top-most bubble in group. */
154 x = glb_drand() * 4 - 2;
155 y = glb_config.screen_bottom;
156 z = glb_drand() * 2 - 2;
159 size = glb_config.min_size
160 + glb_drand() * (glb_config.max_size - glb_config.min_size);
163 speed = glb_config.min_speed
164 + glb_drand() * (glb_config.max_speed - glb_config.min_speed);
166 /* Work out the scaling increment. Bubbles should increase by scale_factor
167 * as they go from bottom to top of screen.
169 scale_incr = (size * glb_config.scale_factor - size)
170 / ((glb_config.screen_top - glb_config.screen_bottom) / speed);
172 /* Create the bubble(s). */
173 for (i = 0; i < n; ++i) {
174 if ((b[i] = glb_bubble_new(c->d, x, y, z, size, speed, scale_incr)) == 0) {
175 /* Out of memory - recover. */
178 glb_bubble_delete(b[i]);
181 /* Create the next bubble below the last bubble. */
185 /* Add the bubbles to the list. */
187 old_bubble_list = c->bubble_list;
188 if (c->bubble_list == 0) {
189 c->bubble_list = (void **) malloc(c->nr_bubbles * sizeof (void *));
191 c->bubble_list = (void **) realloc(c->bubble_list,
192 c->nr_bubbles * sizeof (void *));
195 if (c->bubble_list == 0) {
196 /* Out of memory - recover. */
197 for (i = 0; i < n; ++i)
198 glb_bubble_delete(b[i]);
199 c->bubble_list = old_bubble_list;
203 for (i = 0; i < n; ++i)
204 c->bubble_list[c->nr_bubbles - i - 1] = b[i];
210 glb_draw_step(void *cc)
212 draw_context *c = (draw_context *) cc;
215 /* Consider creating a new bubble or bubbles. */
216 if (c->nr_bubbles < glb_config.max_bubbles &&
217 c->bubble_count++ > glb_config.create_bubbles_every) {
218 if (create_new_bubbles(c))
221 /* Clear the display. */
222 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
224 /* XXX Draw the background here ... */
226 /* Draw all the bubbles on the display. */
227 for (i = 0; i < c->nr_bubbles; ++i) {
228 void *b = c->bubble_list[i];
231 glb_bubble_draw(c->d, b);
233 /* Has the bubble reached the top of the screen? */
234 if (glb_bubble_get_y(b) >= glb_config.screen_top) {