From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / hacks / glx / b_lockglue.c
1 #if 0
2 static const char sccsid[] = "@(#)b_lockglue.c  4.11 98/06/16 xlockmore";
3 #endif
4
5 /*-
6  * BUBBLE3D (C) 1998 Richard W.M. Jones.
7  * b_lockglue.c: Glue to make this all work with xlockmore.
8  */
9
10 #include "bubble3d.h"
11
12 /* XXX This lot should eventually be made configurable using the
13  * options stuff below.
14  */
15 struct glb_config glb_config =
16 {
17         0,                      /* transparent_p */
18 #if GLB_SLOW_GL
19         2,                      /* subdivision_depth */
20 #else
21         3,                      /* subdivision_depth */
22 #endif
23         5,                      /* nr_nudge_axes */
24         0.01,                   /* nudge_angle_factor */
25         0.20,                   /* nudge_factor */
26         0.1,                    /* rotation_factor */
27         8,                      /* create_bubbles_every */
28         8,                      /* max_bubbles */
29         {0.7, 0.8, 0.9, 1.0},   /* p_bubble_group */
30         0.5,                    /* max_size */
31         0.1,                    /* min_size */
32         0.03,                   /* max_speed */
33         0.005,                  /* min_speed */
34         1.5,                    /* scale_factor */
35         -4,                     /* screen_bottom */
36         4,                      /* screen_top */
37         {0.0, 0.0, 0.7, 0.3}    /* bubble_colour */
38 };
39
40 #ifdef STANDALONE
41 # define DEFAULTS       "*delay:        10000   \n"     \
42                         "*showFPS:      False   \n"
43
44 # define refresh_bubble3d 0
45 # define bubble3d_handle_event 0
46 #include "xlockmore.h"
47 #else
48 #include "xlock.h"
49 #include "vis.h"
50 #endif
51
52 #ifdef USE_GL
53
54
55 #define DEF_TRANSPARENT "True"
56 #define DEF_BUBBLECOLOR "random"
57
58 static Bool transparent_p;
59 static char *bubble_color_str;
60
61 #undef countof
62 #define countof(x) (sizeof((x))/sizeof((*x)))
63
64 static XrmOptionDescRec opts[] = {
65   { "-transparent",  ".transparent",   XrmoptionNoArg, "True" },
66   { "+transparent",  ".transparent",   XrmoptionNoArg, "False" },
67   { "-color",        ".bubblecolor",   XrmoptionSepArg, 0 },
68 };
69
70 static argtype vars[] = {
71   {&transparent_p,   "transparent", "Transparent", DEF_TRANSPARENT, t_Bool},
72   {&bubble_color_str,        "bubblecolor", "BubbleColor", DEF_BUBBLECOLOR, t_String},
73 };
74
75 ENTRYPOINT ModeSpecOpt bubble3d_opts = {countof(opts), opts, countof(vars), vars, NULL};
76
77 #ifdef USE_MODULES
78 ModStruct   bubbles3d_description =
79 {"bubbles3d",
80  "init_bubble3d",
81  "draw_bubble3d",
82  "release_bubble3d",
83  "change_bubble3d",
84  "init_bubble3d",
85  NULL,
86  &bubble3d_opts,
87  1000, 1, 2, 1, 64, 1.0, "",
88  "Richard Jones's GL bubbles",
89  0,
90  NULL
91 };
92
93 #endif /* USE_MODULES */
94
95 struct context {
96         GLXContext *glx_context;
97         void       *draw_context;
98 };
99
100 static struct context *contexts = 0;
101
102 static void
103 parse_color (ModeInfo *mi, const char *name, const char *s, GLfloat *a)
104 {
105   XColor c;
106
107   if (! XParseColor (MI_DISPLAY(mi), MI_COLORMAP(mi), s, &c))
108     {
109       fprintf (stderr, "%s: can't parse %s color %s", progname, name, s);
110       exit (1);
111     }
112   a[0] = c.red   / 65536.0;
113   a[1] = c.green / 65536.0;
114   a[2] = c.blue  / 65536.0;
115 }
116
117 static void
118 init_colors(ModeInfo *mi)
119 {
120         if (strncasecmp(bubble_color_str, "auto", strlen("auto")) == 0) {
121                 glb_config.bubble_colour[0] = ((float) (NRAND(100)) / 100.0);
122                 glb_config.bubble_colour[1] = ((float) (NRAND(100)) / 100.0);
123                 /* I keep more blue */
124                 glb_config.bubble_colour[2] = ((float) (NRAND(50)) / 100.0) + 0.50;
125         } else if (strncasecmp(bubble_color_str, "random", strlen("random")) == 0) {
126             glb_config.bubble_colour[0] = -1.0;
127         } else {
128                 parse_color(mi, "bubble", bubble_color_str, glb_config.bubble_colour);
129         }
130 }
131
132 static void
133 init(struct context *c)
134 {
135         glb_config.transparent_p = transparent_p;
136         glb_sphere_init();
137         c->draw_context = glb_draw_init();
138 }
139
140 ENTRYPOINT void
141 reshape_bubble3d(ModeInfo *mi, int w, int h)
142 {
143         glViewport(0, 0, (GLsizei) w, (GLsizei) h);
144         glMatrixMode(GL_PROJECTION);
145         glLoadIdentity();
146         gluPerspective(45, (GLdouble) w / (GLdouble) h, 3, 8);
147         glMatrixMode(GL_MODELVIEW);
148         glLoadIdentity();
149         glTranslatef(0, 0, -5);
150 }
151
152 static void
153 do_display(struct context *c)
154 {
155         glb_draw_step(c->draw_context);
156 }
157
158 ENTRYPOINT void
159 init_bubble3d(ModeInfo * mi)
160 {
161         Display    *display = MI_DISPLAY(mi);
162         Window      window = MI_WINDOW(mi);
163         int         screen = MI_SCREEN(mi);
164         struct context *c;
165
166         if (contexts == 0) {
167                 contexts = (struct context *) calloc(sizeof (struct context), MI_NUM_SCREENS(mi));
168
169                 if (contexts == 0)
170                         return;
171         }
172         c = &contexts[screen];
173         c->glx_context = init_GL(mi);
174         init_colors(mi);
175         if (c->glx_context != 0) {
176                 init(c);
177                 reshape_bubble3d(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
178                 do_display(c);
179                 glFinish();
180                 glXSwapBuffers(display, window);
181         } else
182                 MI_CLEARWINDOW(mi);
183 }
184
185 ENTRYPOINT void
186 draw_bubble3d(ModeInfo * mi)
187 {
188         struct context *c = &contexts[MI_SCREEN(mi)];
189         Display    *display = MI_DISPLAY(mi);
190         Window      window = MI_WINDOW(mi);
191
192         MI_IS_DRAWN(mi) = True;
193
194         if (!c->glx_context)
195                 return;
196
197         glXMakeCurrent(display, window, *(c->glx_context));
198
199         glb_config.polygon_count = 0;
200         glPushMatrix();
201
202 # ifdef HAVE_MOBILE     /* Keep it the same relative size when rotated. */
203         {
204           GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
205           int o = (int) current_device_rotation();
206           if (o != 0 && o != 180 && o != -180)
207             glScalef (1/h, 1/h, 1/h);
208           glRotatef(o, 0, 0, 1);
209         }
210 # endif
211
212         do_display(c);
213         glPopMatrix();
214         mi->polygon_count = glb_config.polygon_count;
215
216         if (mi->fps_p) do_fps (mi);
217         glFinish();
218         glXSwapBuffers(display, window);
219 }
220
221 #ifndef STANDALONE
222 ENTRYPOINT void
223 change_bubble3d(ModeInfo * mi)
224 {
225         /* nothing */
226 }
227 #endif /* !STANDALONE */
228
229 ENTRYPOINT void
230 release_bubble3d(ModeInfo * mi)
231 {
232   if (contexts) {
233     int screen;
234     for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
235       struct context *c = &contexts[screen];
236       if (c->draw_context)
237         glb_draw_end(c->draw_context);
238     }
239     free (contexts);
240     contexts = 0;
241   }
242   FreeAllGL(mi);
243 }
244
245 XSCREENSAVER_MODULE ("Bubble3D", bubble3d)
246
247 #endif /* USE_GL */