X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?p=xscreensaver;a=blobdiff_plain;f=hacks%2Fglx%2Fspheremonics.c;h=29177c290a0f197f969d50a4ca5e15d2ebd3da81;hp=efc4d46a2b806ab43bc56c617b700415c2d47703;hb=6b1c86cf395f59389e4ece4ea8f4bea2c332745b;hpb=e4fa2ac140f7bc56571373a7b7eb585fa4500e38 diff --git a/hacks/glx/spheremonics.c b/hacks/glx/spheremonics.c index efc4d46a..29177c29 100644 --- a/hacks/glx/spheremonics.c +++ b/hacks/glx/spheremonics.c @@ -1,4 +1,4 @@ -/* xscreensaver, Copyright (c) 2002 Jamie Zawinski +/* xscreensaver, Copyright (c) 2002-2008 Jamie Zawinski * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that @@ -60,43 +60,19 @@ * surfaces would be too hard. */ -#include - -extern XtAppContext app; - -#define PROGCLASS "Spheremonics" -#define HACK_INIT init_spheremonics -#define HACK_DRAW draw_spheremonics -#define HACK_RESHAPE reshape_spheremonics -#define HACK_HANDLE_EVENT spheremonics_handle_event -#define EVENT_MASK PointerMotionMask -#define ccs_opts xlockmore_opts - -#define DEF_DURATION "100" -#define DEF_SPIN "XYZ" -#define DEF_WANDER "False" -#define DEF_RESOLUTION "64" -#define DEF_BBOX "False" -#define DEF_GRID "True" -#define DEF_SMOOTH "True" -#define DEF_PARMS "(default)" - -#define DEFAULTS "*delay: 30000 \n" \ - "*resolution: " DEF_RESOLUTION "\n" \ - "*showFPS: False \n" \ - "*wireframe: False \n" \ - "*duration: " DEF_DURATION "\n" \ - "*spin: " DEF_SPIN "\n" \ - "*wander: " DEF_WANDER "\n" \ - "*bbox: " DEF_BBOX "\n" \ - "*grid: " DEF_GRID "\n" \ - "*smooth: " DEF_SMOOTH "\n" \ - "*parameters: " DEF_PARMS "\n" \ +#define DEFAULTS "*delay: 30000 \n" \ + "*showFPS: False \n" \ + "*wireframe: False \n" \ + "*labelfont: -*-times-bold-r-normal-*-180-*\n" +# define refresh_spheremonics 0 +# define release_spheremonics 0 #undef countof #define countof(x) (sizeof((x))/sizeof((*x))) #include "xlockmore.h" +#include "glxfonts.h" +#include "normals.h" #include "colors.h" #include "rotator.h" #include "gltrackball.h" @@ -104,11 +80,14 @@ extern XtAppContext app; #ifdef USE_GL /* whole file */ -#include - -typedef struct { - double x,y,z; -} XYZ; +#define DEF_DURATION "100" +#define DEF_SPIN "XYZ" +#define DEF_WANDER "False" +#define DEF_RESOLUTION "64" +#define DEF_BBOX "False" +#define DEF_GRID "True" +#define DEF_SMOOTH "True" +#define DEF_PARMS "(default)" typedef struct { GLXContext *glx_context; @@ -134,6 +113,8 @@ typedef struct { XFontStruct *font; GLuint font_list; + int change_tick; + int done_once; } spheremonics_configuration; @@ -175,12 +156,12 @@ static argtype vars[] = { {&static_parms, "parameters", "Parameters", DEF_PARMS, t_String}, }; -ModeSpecOpt ccs_opts = {countof(opts), opts, countof(vars), vars, NULL}; +ENTRYPOINT ModeSpecOpt spheremonics_opts = {countof(opts), opts, countof(vars), vars, NULL}; /* Window management, etc */ -void +ENTRYPOINT void reshape_spheremonics (ModeInfo *mi, int width, int height) { GLfloat h = (GLfloat) height / (GLfloat) width; @@ -207,7 +188,7 @@ gl_init (ModeInfo *mi) /* spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)]; */ int wire = MI_IS_WIREFRAME(mi); - static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0}; + static const GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0}; glEnable(GL_NORMALIZE); @@ -238,7 +219,7 @@ gl_init (ModeInfo *mi) lighting effect. */ glDisable(GL_CULL_FACE); - glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, TRUE); + glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, True); } } @@ -265,46 +246,6 @@ sphere_eval (double theta, double phi, int *m) } -/* Normalise a vector */ -static void -normalize (XYZ *p) -{ - double length; - length = sqrt(p->x * p->x + p->y * p->y + p->z * p->z); - if (length != 0) { - p->x /= length; - p->y /= length; - p->z /= length; - } else { - p->x = 0; - p->y = 0; - p->z = 0; - } -} - -/*------------------------------------------------------------------------- - Calculate the unit normal at p given two other points - p1,p2 on the surface. The normal points in the direction - of p1 crossproduct p2 - */ -static XYZ -calc_normal (XYZ p, XYZ p1, XYZ p2) -{ - XYZ n, pa, pb; - pa.x = p1.x - p.x; - pa.y = p1.y - p.y; - pa.z = p1.z - p.z; - pb.x = p2.x - p.x; - pb.y = p2.y - p.y; - pb.z = p2.z - p.z; - n.x = pa.y * pb.z - pa.z * pb.y; - n.y = pa.z * pb.x - pa.x * pb.z; - n.z = pa.x * pb.y - pa.y * pb.x; - normalize (&n); - return (n); -} - - static void do_color (int i, XColor *colors) { @@ -363,8 +304,8 @@ draw_bounding_box (ModeInfo *mi) { spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)]; - static GLfloat c1[4] = { 0.2, 0.2, 0.6, 1.0 }; - static GLfloat c2[4] = { 1.0, 0.0, 0.0, 1.0 }; + static const GLfloat c1[4] = { 0.2, 0.2, 0.6, 1.0 }; + static const GLfloat c2[4] = { 1.0, 0.0, 0.0, 1.0 }; int wire = MI_IS_WIREFRAME(mi); GLfloat x1 = cc->bbox[0].x; @@ -383,6 +324,7 @@ draw_bounding_box (ModeInfo *mi) { glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c1); glFrontFace(GL_CCW); + glEnable(GL_CULL_FACE); glBegin(wire ? GL_LINE_LOOP : GL_QUADS); glNormal3f(0, 1, 0); @@ -414,6 +356,7 @@ draw_bounding_box (ModeInfo *mi) glVertex3f(x2, y1, z1); glVertex3f(x2, y1, z2); glVertex3f(x2, y2, z2); glVertex3f(x2, y2, z1); glEnd(); + glDisable(GL_CULL_FACE); } glPushAttrib (GL_LIGHTING); @@ -477,7 +420,7 @@ do_tracer (ModeInfo *mi) if (s > 0.001) { - static GLfloat c[4] = { 0.6, 0.5, 1.0, 1.0 }; + static const GLfloat c[4] = { 0.6, 0.5, 1.0, 1.0 }; glPushAttrib (GL_LIGHTING); glDisable (GL_LIGHTING); @@ -709,14 +652,11 @@ generate_spheremonics (ModeInfo *mi) tweak_parameters (mi); - { - static Bool done = False; - if (!done || (0 == (random() % 20))) - { - init_colors (mi); - done = True; - } - } + if (!cc->done_once || (0 == (random() % 20))) + { + init_colors (mi); + cc->done_once = True; + } { glNewList(cc->dlist, GL_COMPILE); @@ -738,67 +678,7 @@ generate_spheremonics (ModeInfo *mi) -static void -load_font (ModeInfo *mi, char *res, XFontStruct **fontP, GLuint *dlistP) -{ - const char *font = get_string_resource (res, "Font"); - XFontStruct *f; - Font id; - int first, last; - - if (!font) font = "-*-times-bold-r-normal-*-140-*"; - - f = XLoadQueryFont(mi->dpy, font); - if (!f) f = XLoadQueryFont(mi->dpy, "fixed"); - - id = f->fid; - first = f->min_char_or_byte2; - last = f->max_char_or_byte2; - - clear_gl_error (); - *dlistP = glGenLists ((GLuint) last+1); - check_gl_error ("glGenLists"); - glXUseXFont(id, first, last-first+1, *dlistP + first); - check_gl_error ("glXUseXFont"); - - *fontP = f; -} - -static void -draw_label (ModeInfo *mi, const char *s) -{ - spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)]; - unsigned int i; - - glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT); - glDisable(GL_LIGHTING); - glDisable(GL_DEPTH_TEST); - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - gluOrtho2D(0, mi->xgwa.width, 0, mi->xgwa.height); - glColor3f(1.0, 1.0, 0.0); - - glRasterPos2f (10, - (mi->xgwa.height - - 10 - - (cc->font->ascent + cc->font->descent))); - for (i = 0; i < strlen(s); i++) - glCallList (cc->font_list + (int)s[i]); - - glPopMatrix(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glPopAttrib(); -} - - - - -void +ENTRYPOINT void init_spheremonics (ModeInfo *mi) { spheremonics_configuration *cc; @@ -840,6 +720,7 @@ init_spheremonics (ModeInfo *mi) if (*s == 'x' || *s == 'X') spinx = True; else if (*s == 'y' || *s == 'Y') spiny = True; else if (*s == 'z' || *s == 'Z') spinz = True; + else if (*s == '0') ; else { fprintf (stderr, @@ -864,7 +745,7 @@ init_spheremonics (ModeInfo *mi) cc->resolution = res; - load_font (mi, "labelfont", &cc->font, &cc->font_list); + load_font (mi->dpy, "labelfont", &cc->font, &cc->font_list); cc->dlist = glGenLists(1); cc->dlist2 = glGenLists(1); @@ -884,13 +765,13 @@ init_spheremonics (ModeInfo *mi) } -Bool +ENTRYPOINT Bool spheremonics_handle_event (ModeInfo *mi, XEvent *event) { spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)]; if (event->xany.type == ButtonPress && - event->xbutton.button & Button1) + event->xbutton.button == Button1) { cc->button_down_p = True; gltrackball_start (cc->trackball, @@ -899,11 +780,33 @@ spheremonics_handle_event (ModeInfo *mi, XEvent *event) return True; } else if (event->xany.type == ButtonRelease && - event->xbutton.button & Button1) + event->xbutton.button == Button1) { cc->button_down_p = False; return True; } + else if (event->xany.type == ButtonPress && + (event->xbutton.button == Button4 || + event->xbutton.button == Button5 || + event->xbutton.button == Button6 || + event->xbutton.button == Button7)) + { + gltrackball_mousewheel (cc->trackball, event->xbutton.button, 10, + !!event->xbutton.state); + return True; + } + else if (event->xany.type == KeyPress) + { + KeySym keysym; + char c = 0; + XLookupString (&event->xkey, &c, 1, &keysym, 0); + + if (c == ' ' || c == '\t' || c == '\r' || c == '\n') + { + cc->change_tick = duration; + return True; + } + } else if (event->xany.type == MotionNotify && cc->button_down_p) { @@ -917,7 +820,7 @@ spheremonics_handle_event (ModeInfo *mi, XEvent *event) } -void +ENTRYPOINT void draw_spheremonics (ModeInfo *mi) { spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)]; @@ -927,6 +830,8 @@ draw_spheremonics (ModeInfo *mi) if (!cc->glx_context) return; + glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cc->glx_context)); + glShadeModel(GL_SMOOTH); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -978,16 +883,20 @@ draw_spheremonics (ModeInfo *mi) : "%d %d %d %d %d %d %d %d"), cc->m[0], cc->m[1], cc->m[2], cc->m[3], cc->m[4], cc->m[5], cc->m[6], cc->m[7]); - draw_label (mi, buf); + + glColor3f(1.0, 1.0, 0.0); + print_gl_string (mi->dpy, cc->font, cc->font_list, + mi->xgwa.width, mi->xgwa.height, + 10, mi->xgwa.height - 10, + buf); } if (!static_parms) { - static int tick = 0; - if (tick++ >= duration && !cc->button_down_p) + if (cc->change_tick++ >= duration && !cc->button_down_p) { generate_spheremonics(mi); - tick = 0; + cc->change_tick = 0; cc->mesher = -1; /* turn off the mesh when switching objects */ } } @@ -1000,4 +909,6 @@ draw_spheremonics (ModeInfo *mi) glXSwapBuffers(dpy, window); } +XSCREENSAVER_MODULE ("Spheremonics", spheremonics) + #endif /* USE_GL */