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