22c723a338ac8daa82a6d3461bfd2f548f0aebab
[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 #if 0
38         {0.1, 0.0, 0.4, 0.0},   /* bg_colour */
39 #else
40         {0.0, 0.0, 0.0, 0.0},   /* bg_colour */
41 #endif
42 #if 0
43         {0.7, 0.7, 0.0, 0.3}    /* bubble_colour */
44 #else
45         {0.0, 0.0, 0.7, 0.3}    /* bubble_colour */
46 #endif
47 };
48
49 #ifdef STANDALONE
50 # define DEFAULTS       "*delay:        10000   \n"     \
51                         "*showFPS:      False   \n"
52
53 # define refresh_bubble3d 0
54 # define bubble3d_handle_event 0
55 #include "xlockmore.h"
56 #else
57 #include "xlock.h"
58 #include "vis.h"
59 #endif
60
61 #ifdef USE_GL
62
63
64 #define DEF_TRANSPARENT "True"
65 #define DEF_COLOR "random"
66
67 static Bool transparent_p;
68 static char *bubble_color_str;
69
70 #undef countof
71 #define countof(x) (sizeof((x))/sizeof((*x)))
72
73 static XrmOptionDescRec opts[] = {
74   { "-transparent",  ".transparent",   XrmoptionNoArg, "True" },
75   { "+transparent",  ".transparent",   XrmoptionNoArg, "False" },
76   { "-color",    ".bubble3d.bubblecolor", XrmoptionSepArg, 0 },
77 };
78
79 static argtype vars[] = {
80   {&transparent_p,   "transparent", "Transparent", DEF_TRANSPARENT, t_Bool},
81   {&bubble_color_str,        "bubblecolor", "BubbleColor", DEF_COLOR, t_String},
82 };
83
84 ENTRYPOINT ModeSpecOpt bubble3d_opts = {countof(opts), opts, countof(vars), vars, NULL};
85
86 #ifdef USE_MODULES
87 ModStruct   bubbles3d_description =
88 {"bubbles3d",
89  "init_bubble3d",
90  "draw_bubble3d",
91  "release_bubble3d",
92  "change_bubble3d",
93  "init_bubble3d",
94  NULL,
95  &bubble3d_opts,
96  1000, 1, 2, 1, 64, 1.0, "",
97  "Richard Jones's GL bubbles",
98  0,
99  NULL
100 };
101
102 #endif /* USE_MODULES */
103
104 struct context {
105         GLXContext *glx_context;
106         void       *draw_context;
107 };
108
109 static struct context *contexts = 0;
110
111 static void
112 parse_color (ModeInfo *mi, const char *name, const char *s, GLfloat *a)
113 {
114   XColor c;
115
116   if (! XParseColor (MI_DISPLAY(mi), MI_COLORMAP(mi), s, &c))
117     {
118       fprintf (stderr, "%s: can't parse %s color %s", progname, name, s);
119       exit (1);
120     }
121   a[0] = c.red   / 65536.0;
122   a[1] = c.green / 65536.0;
123   a[2] = c.blue  / 65536.0;
124 }
125
126 static void
127 init_colors(ModeInfo *mi)
128 {
129         if (strncasecmp(bubble_color_str, "auto", strlen("auto")) == 0) {
130                 glb_config.bubble_colour[0] = ((float) (NRAND(100)) / 100.0);
131                 glb_config.bubble_colour[1] = ((float) (NRAND(100)) / 100.0);
132                 /* I keep more blue */
133                 glb_config.bubble_colour[2] = ((float) (NRAND(50)) / 100.0) + 0.50;
134         } else if (strncasecmp(bubble_color_str, "random", strlen("random")) == 0) {
135             glb_config.bubble_colour[0] = -1.0;
136         } else {
137                 parse_color(mi, "bubble", bubble_color_str, glb_config.bubble_colour);
138         }
139 }
140
141 static void
142 init(struct context *c)
143 {
144         glb_config.transparent_p = transparent_p;
145         glb_sphere_init();
146         c->draw_context = glb_draw_init();
147 }
148
149 ENTRYPOINT void
150 reshape_bubble3d(ModeInfo *mi, int w, int h)
151 {
152         glViewport(0, 0, (GLsizei) w, (GLsizei) h);
153         glMatrixMode(GL_PROJECTION);
154         glLoadIdentity();
155         gluPerspective(45, (GLdouble) w / (GLdouble) h, 3, 8);
156         glMatrixMode(GL_MODELVIEW);
157         glLoadIdentity();
158         glTranslatef(0, 0, -5);
159 }
160
161 static void
162 do_display(struct context *c)
163 {
164         glb_draw_step(c->draw_context);
165 }
166
167 ENTRYPOINT void
168 init_bubble3d(ModeInfo * mi)
169 {
170         Display    *display = MI_DISPLAY(mi);
171         Window      window = MI_WINDOW(mi);
172         int         screen = MI_SCREEN(mi);
173         struct context *c;
174
175         if (contexts == 0) {
176                 contexts = (struct context *) calloc(sizeof (struct context), MI_NUM_SCREENS(mi));
177
178                 if (contexts == 0)
179                         return;
180         }
181         c = &contexts[screen];
182         c->glx_context = init_GL(mi);
183         init_colors(mi);
184         if (c->glx_context != 0) {
185                 init(c);
186                 reshape_bubble3d(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
187                 do_display(c);
188                 glFinish();
189                 glXSwapBuffers(display, window);
190         } else
191                 MI_CLEARWINDOW(mi);
192 }
193
194 ENTRYPOINT void
195 draw_bubble3d(ModeInfo * mi)
196 {
197         struct context *c = &contexts[MI_SCREEN(mi)];
198         Display    *display = MI_DISPLAY(mi);
199         Window      window = MI_WINDOW(mi);
200
201         MI_IS_DRAWN(mi) = True;
202
203         if (!c->glx_context)
204                 return;
205
206         glXMakeCurrent(display, window, *(c->glx_context));
207
208         glb_config.polygon_count = 0;
209         do_display(c);
210         mi->polygon_count = glb_config.polygon_count;
211
212         if (mi->fps_p) do_fps (mi);
213         glFinish();
214         glXSwapBuffers(display, window);
215 }
216
217 #ifndef STANDALONE
218 ENTRYPOINT void
219 change_bubble3d(ModeInfo * mi)
220 {
221         /* nothing */
222 }
223 #endif /* !STANDALONE */
224
225 ENTRYPOINT void
226 release_bubble3d(ModeInfo * mi)
227 {
228   if (contexts) {
229     int screen;
230     for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
231       struct context *c = &contexts[screen];
232       if (c->draw_context)
233         glb_draw_end(c->draw_context);
234     }
235     free (contexts);
236     contexts = 0;
237   }
238   FreeAllGL(mi);
239 }
240
241 XSCREENSAVER_MODULE ("Bubble3D", bubble3d)
242
243 #endif /* USE_GL */