1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* Klein --- Klein Bottle, Moebius and other parametric surfaces
7 * 2000: written by Andrey Mirtchovski <mirtchov@cpsc.ucalgary.ca
9 * 01-Mar-2003 mirtchov modified as a xscreensaver hack
14 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
15 * otherwise caddr_t is not defined correctly
18 #include <X11/Intrinsic.h>
21 # define PROGCLASS "Klein"
22 # define HACK_INIT init_klein
23 # define HACK_DRAW draw_klein
24 # define HACK_RESHAPE reshape_klein
25 # define HACK_HANDLE_EVENT klein_handle_event
26 # define EVENT_MASK PointerMotionMask
27 # define klein_opts xlockmore_opts
30 #define DEF_SPIN "True"
31 #define DEF_WANDER "False"
32 #define DEF_RANDOM "False"
33 #define DEF_SPEED "150"
35 # define DEFAULTS "*delay: 20000 \n" \
36 "*showFPS: False \n" \
37 "*wireframe: False \n" \
38 "*random: " DEF_RANDOM "\n" \
39 "*speed: " DEF_SPEED "\n" \
40 "*spin: " DEF_SPIN "\n" \
41 "*wander: " DEF_WANDER "\n" \
43 # include "xlockmore.h" /* from the xscreensaver distribution */
44 #else /* !STANDALONE */
45 # include "xlock.h" /* from the xlockmore distribution */
46 #endif /* !STANDALONE */
52 #include "gltrackball.h"
55 #define countof(x) (sizeof((x))/sizeof((*x)))
57 /* surfaces being drawn */
70 /* primitives to draw with
71 * note that we skip the polygons and
72 * triangle fans -- too slow
74 * also removed triangle_strip and quads --
75 * just doesn't look good enough
89 static Bool do_wander;
91 static XrmOptionDescRec opts[] = {
92 {"-speed", ".speed", XrmoptionSepArg, (caddr_t) 0 },
93 { "-spin", ".spin", XrmoptionNoArg, "True" },
94 { "+spin", ".spin", XrmoptionNoArg, "False" },
95 { "-wander", ".wander", XrmoptionNoArg, "True" },
96 { "+wander", ".wander", XrmoptionNoArg, "False" },
97 { "-random", ".rand", XrmoptionNoArg, "True" },
98 { "+random", ".rand", XrmoptionNoArg, "False" },
101 static argtype vars[] = {
102 {(caddr_t *) &rand, "rand", "Random", DEF_RANDOM, t_Bool},
103 {(caddr_t *) &do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
104 {(caddr_t *) &do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
105 {(caddr_t *) &speed, "speed", "Speed", DEF_SPEED, t_Int},
109 ModeSpecOpt klein_opts = {countof(opts), opts, countof(vars), vars, NULL};
120 GLXContext *glx_context;
123 trackball_state *trackball;
134 static kleinstruct *klein = NULL;
140 kleinstruct *kp = &klein[MI_SCREEN(mi)];
141 static float step = 0.0;
145 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
147 glEnable(GL_DEPTH_TEST);
148 glEnable(GL_NORMALIZE);
149 glEnable(GL_CULL_FACE);
155 get_position (kp->rot, &x, &y, &z, !kp->button_down_p);
156 glTranslatef((x - 0.5) * 10,
160 gltrackball_rotate (kp->trackball);
162 get_rotation (kp->rot, &x, &y, &z, !kp->button_down_p);
163 glRotatef (x * 360, 1.0, 0.0, 0.0);
164 glRotatef (y * 360, 0.0, 1.0, 0.0);
165 glRotatef (z * 360, 0.0, 0.0, 1.0);
168 glScalef( 4.0, 4.0, 4.0 );
171 switch(kp->surface) {
173 for(u = -M_PI; u < M_PI; u+=kp->du){
174 for(v = -M_PI; v < M_PI; v+=kp->dv){
175 coord[0] = cos(u)*(kp->a + sin(v)*cos(u/2) -
176 sin(2*v)*sin(u/2)/2);
177 coord[1] = sin(u)*(kp->a + sin(v)*cos(u/2) -
178 sin(2*v)*sin(u/2)/2);
179 coord[2] = sin(u/2)*sin(v) + cos(u/2)*sin(2*v)/2;
180 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
186 for(u = -M_PI; u < M_PI; u+=kp->du){
187 for(v = -M_PI; v < M_PI; v+=kp->dv){
188 coord[0] = kp->a*cos(u)*sin(v);
189 coord[1] = kp->a*sin(u)*sin(v);
190 coord[2] = kp->a*(cos(v) + sin(tan((v/2))))+0.2*u;
191 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
197 for(u = -M_PI; u < M_PI; u+=kp->du){
198 for(v = -M_PI; v < M_PI; v+=kp->dv){
199 coord[0] = kp->a*(u-(u*u*u/3)+u*v*v);
200 coord[1] = kp->b*(v-(v*v*v/3)+u*u*v);
202 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
208 for(u = -M_PI; u < M_PI; u+=kp->du){
209 for(v = -M_PI; v < M_PI; v+=kp->dv){
210 coord[0] = 2*(cos(u)+u*sin(u))*sin(v)/(1+u*u*sin(v)*sin(v));
211 coord[1] = 2*(sin(u)-u*cos(u))*sin(v)/(1+u*u*sin(v)*sin(v));
212 coord[2] = sin(tan(v/2))+2*cos(v)/(1+u*u*sin(v)*sin(v));
214 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
220 for(u = -M_PI; u < M_PI; u+=kp->du){
221 for(v = -M_PI; v < M_PI; v+=kp->dv){
222 coord[0] = cos(u)+v*cos(u/2)*cos(u);
223 coord[1] = sin(u)+v*cos(u/2)*sin(u);
224 coord[2] = v*sin(u/2);
225 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
231 for(u = 0; u < 2*M_PI; u+=kp->du){
232 for(v = 0; v < 2*M_PI; v+=kp->dv){
233 coord[0] = kp->a*(1-v/(2*M_PI))*cos(2*v)*(1+cos(u))+sin(kp->c+=0.00001)*cos(2*v);
234 coord[1] = kp->a*(1-v/(2*M_PI))*sin(2*v)*(1+cos(u))+cos(kp->c+=0.00001)*sin(2*v);
235 coord[2] = sin(kp->b+=0.00001)*v/(2*M_PI)+kp->a*(1-v/(2*M_PI))*sin(u);
236 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
242 for(u = -M_PI; u < M_PI; u+=kp->du){
243 for(v = -M_PI; v < M_PI; v+=kp->dv){
244 coord[0] = u*pow(v,2) + 3*pow(v,4);
245 coord[1] = -2*u*v - 4*pow(v,3);
247 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
253 for(u = -M_PI; u < M_PI; u+=kp->du){
254 for(v = -M_PI; v < M_PI; v+=kp->dv){
255 coord[0] = kp->a*cos(u);
256 coord[1] = 1.5*cos(v) + kp->a*sin(u);
258 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
264 for(u = -M_PI; u < M_PI; u+=kp->du){
265 for(v = -M_PI; v < M_PI; v+=kp->dv){
266 coord[0] = sin(u)*kp->a;
267 coord[1] = cos(u)*kp->a;
268 coord[2] = sin(u/2)*cos(v) + cos(u/2)*sin(v);
269 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
279 kp->a = sin(step+=0.01);
280 kp->b = cos(step+=0.01);
284 /* new window size or exposure */
286 reshape_klein(ModeInfo *mi, int width, int height)
288 GLfloat h = (GLfloat) height / (GLfloat) width;
290 glViewport(0, 0, (GLint) width, (GLint) height);
291 glMatrixMode(GL_PROJECTION);
293 gluPerspective (30.0, 1/h, 1.0, 100.0);
295 glMatrixMode(GL_MODELVIEW);
297 gluLookAt( 0.0, 0.0, 30.0,
301 glClear(GL_COLOR_BUFFER_BIT);
306 klein_handle_event (ModeInfo *mi, XEvent *event)
308 kleinstruct *kp = &klein[MI_SCREEN(mi)];
310 if (event->xany.type == ButtonPress && event->xbutton.button & Button1) {
311 kp->button_down_p = True;
312 gltrackball_start (kp->trackball, event->xbutton.x, event->xbutton.y, MI_WIDTH (mi), MI_HEIGHT (mi));
314 } else if (event->xany.type == ButtonRelease && event->xbutton.button & Button1) {
315 kp->button_down_p = False;
317 } else if (event->xany.type == MotionNotify && kp->button_down_p) {
318 gltrackball_track (kp->trackball, event->xmotion.x, event->xmotion.y, MI_WIDTH (mi), MI_HEIGHT (mi));
327 init_klein(ModeInfo *mi)
329 int screen = MI_SCREEN(mi);
333 if ((klein = (kleinstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (kleinstruct))) == NULL)
338 kp->window = MI_WINDOW(mi);
341 double spin_speed = 1.0;
342 double wander_speed = 0.03;
343 kp->rot = make_rotator (do_spin ? spin_speed : 0,
344 do_spin ? spin_speed : 0,
345 do_spin ? spin_speed : 0,
347 do_wander ? wander_speed : 0,
349 kp->trackball = gltrackball_init ();
353 render = random() % MY_PRIM_LAST;
354 kp->surface = random() % SURFACE_LAST;
356 render = MY_LINE_LOOP;
361 case MY_POINTS: kp->render = GL_POINTS; break;
362 case MY_LINES: kp->render = GL_LINES; break;
363 case MY_LINE_LOOP: kp->render = GL_LINE_LOOP; break;
365 kp->render = GL_LINE_LOOP;
367 /*kp->render=GL_TRIANGLE_FAN;*/
368 /*kp->render=GL_POLYGON;*/
376 if ((kp->glx_context = init_GL(mi)) != NULL) {
377 reshape_klein(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
384 draw_klein(ModeInfo * mi)
386 kleinstruct *kp = &klein[MI_SCREEN(mi)];
387 Display *display = MI_DISPLAY(mi);
388 Window window = MI_WINDOW(mi);
390 if (!kp->glx_context) return;
392 glDrawBuffer(GL_BACK);
394 glXMakeCurrent(display, window, *(kp->glx_context));
396 if (mi->fps_p) do_fps (mi);
398 glXSwapBuffers(display, window);
402 release_klein(ModeInfo * mi)
407 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
408 kleinstruct *kp = &klein[screen];
410 if (kp->glx_context) {
411 /* Display lists MUST be freed while their glXContext is current. */
412 glXMakeCurrent(MI_DISPLAY(mi), kp->window, *(kp->glx_context));
415 (void) free((void *) klein);
422 /*********************************************************/