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 # define DEFAULTS "*delay: 20000 \n" \
17 # define refresh_klein 0
18 # include "xlockmore.h" /* from the xscreensaver distribution */
19 #else /* !STANDALONE */
20 # include "xlock.h" /* from the xlockmore distribution */
21 #endif /* !STANDALONE */
25 #define DEF_SPIN "True"
26 #define DEF_WANDER "False"
27 #define DEF_RAND "True"
28 #define DEF_SPEED "150"
31 #include "gltrackball.h"
34 #define countof(x) (sizeof((x))/sizeof((*x)))
36 /* surfaces being drawn */
49 /* primitives to draw with
50 * note that we skip the polygons and
51 * triangle fans -- too slow
53 * also removed triangle_strip and quads --
54 * just doesn't look good enough
68 static Bool do_wander;
70 static XrmOptionDescRec opts[] = {
71 {"-speed", ".speed", XrmoptionSepArg, 0 },
72 { "-spin", ".spin", XrmoptionNoArg, "True" },
73 { "+spin", ".spin", XrmoptionNoArg, "False" },
74 { "-wander", ".wander", XrmoptionNoArg, "True" },
75 { "+wander", ".wander", XrmoptionNoArg, "False" },
76 { "-random", ".rand", XrmoptionNoArg, "True" },
77 { "+random", ".rand", XrmoptionNoArg, "False" },
80 static argtype vars[] = {
81 {&rand, "rand", "Random", DEF_RAND, t_Bool},
82 {&do_spin, "spin", "Spin", DEF_SPIN, t_Bool},
83 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
84 {&speed, "speed", "Speed", DEF_SPEED, t_Int},
88 ENTRYPOINT ModeSpecOpt klein_opts = {countof(opts), opts, countof(vars), vars, NULL};
99 GLXContext *glx_context;
102 trackball_state *trackball;
115 static kleinstruct *klein = NULL;
121 kleinstruct *kp = &klein[MI_SCREEN(mi)];
125 mi->polygon_count = 0;
126 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
128 glEnable(GL_DEPTH_TEST);
129 glEnable(GL_NORMALIZE);
130 glEnable(GL_CULL_FACE);
136 get_position (kp->rot, &x, &y, &z, !kp->button_down_p);
137 glTranslatef((x - 0.5) * 10,
141 gltrackball_rotate (kp->trackball);
143 get_rotation (kp->rot, &x, &y, &z, !kp->button_down_p);
144 glRotatef (x * 360, 1.0, 0.0, 0.0);
145 glRotatef (y * 360, 0.0, 1.0, 0.0);
146 glRotatef (z * 360, 0.0, 0.0, 1.0);
149 glScalef( 4.0, 4.0, 4.0 );
152 switch(kp->surface) {
154 for(u = -M_PI; u < M_PI; u+=kp->du){
155 for(v = -M_PI; v < M_PI; v+=kp->dv){
156 coord[0] = cos(u)*(kp->a + sin(v)*cos(u/2) -
157 sin(2*v)*sin(u/2)/2);
158 coord[1] = sin(u)*(kp->a + sin(v)*cos(u/2) -
159 sin(2*v)*sin(u/2)/2);
160 coord[2] = sin(u/2)*sin(v) + cos(u/2)*sin(2*v)/2;
161 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
168 for(u = -M_PI; u < M_PI; u+=kp->du){
169 for(v = -M_PI; v < M_PI; v+=kp->dv){
170 coord[0] = kp->a*cos(u)*sin(v);
171 coord[1] = kp->a*sin(u)*sin(v);
172 coord[2] = kp->a*(cos(v) + sin(tan((v/2))))+0.2*u;
173 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
180 for(u = -M_PI; u < M_PI; u+=kp->du){
181 for(v = -M_PI; v < M_PI; v+=kp->dv){
182 coord[0] = kp->a*(u-(u*u*u/3)+u*v*v);
183 coord[1] = kp->b*(v-(v*v*v/3)+u*u*v);
185 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
192 for(u = -M_PI; u < M_PI; u+=kp->du){
193 for(v = -M_PI; v < M_PI; v+=kp->dv){
194 coord[0] = 2*(cos(u)+u*sin(u))*sin(v)/(1+u*u*sin(v)*sin(v));
195 coord[1] = 2*(sin(u)-u*cos(u))*sin(v)/(1+u*u*sin(v)*sin(v));
196 coord[2] = sin(tan(v/2))+2*cos(v)/(1+u*u*sin(v)*sin(v));
198 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
205 for(u = -M_PI; u < M_PI; u+=kp->du){
206 for(v = -M_PI; v < M_PI; v+=kp->dv){
207 coord[0] = cos(u)+v*cos(u/2)*cos(u);
208 coord[1] = sin(u)+v*cos(u/2)*sin(u);
209 coord[2] = v*sin(u/2);
210 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
217 for(u = 0; u < 2*M_PI; u+=kp->du){
218 for(v = 0; v < 2*M_PI; v+=kp->dv){
219 coord[0] = kp->a*(1-v/(2*M_PI))*cos(2*v)*(1+cos(u))+sin(kp->c+=0.00001)*cos(2*v);
220 coord[1] = kp->a*(1-v/(2*M_PI))*sin(2*v)*(1+cos(u))+cos(kp->c+=0.00001)*sin(2*v);
221 coord[2] = sin(kp->b+=0.00001)*v/(2*M_PI)+kp->a*(1-v/(2*M_PI))*sin(u);
222 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
229 for(u = -M_PI; u < M_PI; u+=kp->du){
230 for(v = -M_PI; v < M_PI; v+=kp->dv){
231 coord[0] = u*pow(v,2) + 3*pow(v,4);
232 coord[1] = -2*u*v - 4*pow(v,3);
234 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
241 for(u = -M_PI; u < M_PI; u+=kp->du){
242 for(v = -M_PI; v < M_PI; v+=kp->dv){
243 coord[0] = kp->a*cos(u);
244 coord[1] = 1.5*cos(v) + kp->a*sin(u);
246 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] = sin(u)*kp->a;
256 coord[1] = cos(u)*kp->a;
257 coord[2] = sin(u/2)*cos(v) + cos(u/2)*sin(v);
258 glColor3f(coord[0]+0.7, coord[1]+0.7, coord[2]+0.7);
268 if (kp->render == GL_LINES)
269 mi->polygon_count /= 2;
271 kp->a = sin(kp->draw_step+=0.01);
272 kp->b = cos(kp->draw_step+=0.01);
276 /* new window size or exposure */
278 reshape_klein(ModeInfo *mi, int width, int height)
280 GLfloat h = (GLfloat) height / (GLfloat) width;
282 glViewport(0, 0, (GLint) width, (GLint) height);
283 glMatrixMode(GL_PROJECTION);
285 gluPerspective (30.0, 1/h, 1.0, 100.0);
287 glMatrixMode(GL_MODELVIEW);
289 gluLookAt( 0.0, 0.0, 30.0,
293 glClear(GL_COLOR_BUFFER_BIT);
298 klein_handle_event (ModeInfo *mi, XEvent *event)
300 kleinstruct *kp = &klein[MI_SCREEN(mi)];
302 if (event->xany.type == ButtonPress && event->xbutton.button == Button1) {
303 kp->button_down_p = True;
304 gltrackball_start (kp->trackball, event->xbutton.x, event->xbutton.y, MI_WIDTH (mi), MI_HEIGHT (mi));
306 } else if (event->xany.type == ButtonRelease && event->xbutton.button == Button1) {
307 kp->button_down_p = False;
309 } else if (event->xany.type == ButtonPress &&
310 (event->xbutton.button == Button4 ||
311 event->xbutton.button == Button5 ||
312 event->xbutton.button == Button6 ||
313 event->xbutton.button == Button7)) {
314 gltrackball_mousewheel (kp->trackball, event->xbutton.button, 10,
315 !!event->xbutton.state);
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 XSCREENSAVER_MODULE ("Klein", klein)
424 /*********************************************************/