X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Fglx%2Fpolyhedra-gl.c;h=57a7938787cd0b4c65522fc79b5dcbafb15c9a5f;hb=6afd6db0ae9396cd7ff897ade597cd5483f49b0e;hp=cdc7888632ef4a05869ebd50ba1df5b307c7628e;hpb=ffd8c0873576a9e3065696a624dce6b766b77062;p=xscreensaver diff --git a/hacks/glx/polyhedra-gl.c b/hacks/glx/polyhedra-gl.c index cdc78886..57a79387 100644 --- a/hacks/glx/polyhedra-gl.c +++ b/hacks/glx/polyhedra-gl.c @@ -1,4 +1,4 @@ -/* polyhedra, Copyright (c) 2004 Jamie Zawinski +/* polyhedra, Copyright (c) 2004-2014 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 @@ -15,53 +15,59 @@ * is in "polyhedra.c". */ -#include - -extern XtAppContext app; - -#define PROGCLASS "Polyhedra" -#define HACK_INIT init_polyhedra -#define HACK_DRAW draw_polyhedra -#define HACK_RESHAPE reshape_polyhedra -#define HACK_HANDLE_EVENT polyhedra_handle_event -#define EVENT_MASK PointerMotionMask -#define sws_opts xlockmore_opts - -#define DEF_SPIN "True" -#define DEF_WANDER "True" -#define DEF_SPEED "1.0" -#define DEF_TITLES "True" -#define DEF_DURATION "12" -#define DEF_WHICH "-1" - #define DEFAULTS "*delay: 30000 \n" \ "*showFPS: False \n" \ "*wireframe: False \n" \ - "*speed: " DEF_SPEED "\n" \ - "*spin: " DEF_SPIN "\n" \ - "*wander: " DEF_WANDER "\n" \ - "*duration: " DEF_DURATION "\n" \ - "*which: " DEF_WHICH "\n" \ - "*titleFont: -*-times-bold-r-normal-*-180-*\n" \ - "*titleFont2: -*-times-bold-r-normal-*-120-*\n" \ - "*titleFont3: -*-times-bold-r-normal-*-80-*\n" \ + "*titleFont: -*-helvetica-medium-r-normal-*-140-*\n" \ + "*titleFont2: -*-helvetica-medium-r-normal-*-100-*\n" \ + "*titleFont3: -*-helvetica-medium-r-normal-*-80-*\n" \ +# define refresh_polyhedra 0 +# define release_polyhedra 0 #undef countof #define countof(x) (sizeof((x))/sizeof((*x))) #include "xlockmore.h" -#include +#ifdef HAVE_COCOA +# include "jwxyz.h" +#else +# include +# include +# include +#endif + +#ifdef HAVE_JWZGLES +# include "jwzgles.h" +#endif /* HAVE_JWZGLES */ + +#define DEF_SPIN "True" +#define DEF_WANDER "True" +#define DEF_SPEED "1.0" +#define DEF_TITLES "True" +#define DEF_DURATION "12" +#define DEF_WHICH "random" + +#include "glxfonts.h" +#include "normals.h" #include "polyhedra.h" #include "colors.h" #include "rotator.h" #include "gltrackball.h" -#include +#include "teapot.h" -#ifdef USE_GL /* whole file */ +#ifndef HAVE_COCOA +# define XK_MISCELLANY +# include +#endif -#include +#ifndef HAVE_JWZGLES +# define HAVE_TESS +#endif + + +#ifdef USE_GL /* whole file */ typedef struct { GLXContext *glx_context; @@ -83,8 +89,10 @@ typedef struct { int ncolors; XColor *colors; - XFontStruct *xfont1, *xfont2, *xfont3; - GLuint font1_dlist, font2_dlist, font3_dlist; + texture_font_data *font1_data, *font2_data, *font3_data; + + time_t last_change_time; + int change_tick; } polyhedra_configuration; @@ -119,56 +127,10 @@ static argtype vars[] = { {&do_which_str,"which", "Which", DEF_WHICH, t_String}, }; -ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL}; +ENTRYPOINT ModeSpecOpt polyhedra_opts = {countof(opts), opts, countof(vars), vars, NULL}; -typedef struct { - double x,y,z; -} XYZ; - -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); -} - - /* Calculate the normals at each vertex of a face, and use the sum to decide which normal to assign to the entire face. This also solves problems caused by nonconvex faces, in most (but not all) cases. @@ -177,7 +139,7 @@ static void kludge_normal (int n, const int *indices, const point *points) { XYZ normal = { 0, 0, 0 }; - XYZ p; + XYZ p = { 0, 0, 0 }; int i; for (i = 0; i < n; ++i) { @@ -196,7 +158,7 @@ kludge_normal (int n, const int *indices, const point *points) normal.z += p.z; } - normalize(&normal); + /*normalize(&normal);*/ if (normal.x == 0 && normal.y == 0 && normal.z == 0) { glNormal3f (p.x, p.y, p.z); } else { @@ -204,136 +166,16 @@ kludge_normal (int n, const int *indices, const point *points) } } -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-*-180-*"; - - 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 load_fonts (ModeInfo *mi) { polyhedra_configuration *bp = &bps[MI_SCREEN(mi)]; - load_font (mi, "titleFont", &bp->xfont1, &bp->font1_dlist); - load_font (mi, "titleFont2", &bp->xfont2, &bp->font2_dlist); - load_font (mi, "titleFont3", &bp->xfont3, &bp->font3_dlist); -} - - -static int -string_width (XFontStruct *f, const char *c) -{ - int w = 0; - while (*c) - { - int cc = *((unsigned char *) c); - w += (f->per_char - ? f->per_char[cc-f->min_char_or_byte2].rbearing - : f->min_bounds.rbearing); - c++; - } - return w; + bp->font1_data = load_texture_font (mi->dpy, "titleFont"); + bp->font2_data = load_texture_font (mi->dpy, "titleFont2"); + bp->font3_data = load_texture_font (mi->dpy, "titleFont3"); } -static void -print_title_string (ModeInfo *mi, const char *string, - GLfloat x, GLfloat y, - XFontStruct *font, int font_dlist) -{ - GLfloat line_height = font->ascent + font->descent; - GLfloat sub_shift = (line_height * 0.3); - int cw = string_width (font, "m"); - int tabs = cw * 7; - - y -= line_height; - - glPushAttrib (GL_TRANSFORM_BIT | /* for matrix contents */ - GL_ENABLE_BIT); /* for various glDisable calls */ - glDisable (GL_LIGHTING); - glDisable (GL_DEPTH_TEST); - { - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - { - glLoadIdentity(); - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - { - int i; - int x2 = x; - Bool sub_p = False; - glLoadIdentity(); - - gluOrtho2D (0, mi->xgwa.width, 0, mi->xgwa.height); - - glColor3f (0.8, 0.8, 0); - - glRasterPos2f (x, y); - for (i = 0; i < strlen(string); i++) - { - char c = string[i]; - if (c == '\n') - { - glRasterPos2f (x, (y -= line_height)); - x2 = x; - } - else if (c == '\t') - { - x2 -= x; - x2 = ((x2 + tabs) / tabs) * tabs; /* tab to tab stop */ - x2 += x; - glRasterPos2f (x2, y); - } - else if (c == '[' && (isdigit (string[i+1]))) - { - sub_p = True; - glRasterPos2f (x2, (y -= sub_shift)); - } - else if (c == ']' && sub_p) - { - sub_p = False; - glRasterPos2f (x2, (y += sub_shift)); - } - else - { - glCallList (font_dlist + (int)(c)); - x2 += (font->per_char - ? font->per_char[c - font->min_char_or_byte2].width - : font->min_bounds.width); - } - } - } - glPopMatrix(); - } - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - } - glPopAttrib(); - - glMatrixMode(GL_MODELVIEW); -} static void @@ -341,10 +183,16 @@ startup_blurb (ModeInfo *mi) { polyhedra_configuration *bp = &bps[MI_SCREEN(mi)]; const char *s = "Computing polyhedra..."; - print_title_string (mi, s, - mi->xgwa.width - (string_width (bp->xfont1, s) + 40), - 10 + bp->xfont1->ascent + bp->xfont1->descent, - bp->xfont1, bp->font1_dlist); + texture_font_data *f = bp->font1_data; + + glColor3f (0.8, 0.8, 0); + print_gl_string (mi->dpy, bp->font1_data, + mi->xgwa.width, mi->xgwa.height, + mi->xgwa.width - ( + texture_string_width (f, s, 0) + + 40), + mi->xgwa.height - 10, + s, False); glFinish(); glXSwapBuffers(MI_DISPLAY(mi), MI_WINDOW(mi)); } @@ -355,7 +203,7 @@ startup_blurb (ModeInfo *mi) */ static void new_label (ModeInfo *mi); -void +ENTRYPOINT void reshape_polyhedra (ModeInfo *mi, int width, int height) { GLfloat h = (GLfloat) height / (GLfloat) width; @@ -379,26 +227,15 @@ reshape_polyhedra (ModeInfo *mi, int width, int height) } -Bool +ENTRYPOINT Bool polyhedra_handle_event (ModeInfo *mi, XEvent *event) { polyhedra_configuration *bp = &bps[MI_SCREEN(mi)]; - if (event->xany.type == ButtonPress && - event->xbutton.button & Button1) - { - bp->button_down_p = True; - gltrackball_start (bp->trackball, - event->xbutton.x, event->xbutton.y, - MI_WIDTH (mi), MI_HEIGHT (mi)); - return True; - } - else if (event->xany.type == ButtonRelease && - event->xbutton.button & Button1) - { - bp->button_down_p = False; - return True; - } + if (gltrackball_event_handler (event, bp->trackball, + MI_WIDTH (mi), MI_HEIGHT (mi), + &bp->button_down_p)) + return True; else if (event->xany.type == KeyPress) { KeySym keysym; @@ -408,21 +245,23 @@ polyhedra_handle_event (ModeInfo *mi, XEvent *event) bp->change_to = -1; if (c == ' ' || c == '\t' || c == '\r' || c == '\n') bp->change_to = random() % bp->npolyhedra; - else if (c == '>' || c == '.' || c == '+' || c == '=') + else if (c == '>' || c == '.' || c == '+' || c == '=' || + keysym == XK_Right || keysym == XK_Up || keysym == XK_Next) bp->change_to = (bp->which + 1) % bp->npolyhedra; else if (c == '<' || c == ',' || c == '-' || c == '_' || - c == '\010' || c == '\177') + c == '\010' || c == '\177' || + keysym == XK_Left || keysym == XK_Down || keysym == XK_Prior) bp->change_to = (bp->which + bp->npolyhedra - 1) % bp->npolyhedra; + else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event)) + goto DEF; if (bp->change_to != -1) return True; } - else if (event->xany.type == MotionNotify && - bp->button_down_p) + else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event)) { - gltrackball_track (bp->trackball, - event->xmotion.x, event->xmotion.y, - MI_WIDTH (mi), MI_HEIGHT (mi)); + DEF: + bp->change_to = random() % bp->npolyhedra; return True; } @@ -463,31 +302,37 @@ new_label (ModeInfo *mi) p->density, (p->chi < 0 ? "" : " "), p->chi); { - XFontStruct *f; - GLuint fl; + GLfloat color[4] = { 0.8, 0.8, 0.8, 1 }; + texture_font_data *f; if (MI_WIDTH(mi) >= 500 && MI_HEIGHT(mi) >= 375) - f = bp->xfont1, fl = bp->font1_dlist; /* big font */ + f = bp->font1_data; else if (MI_WIDTH(mi) >= 350 && MI_HEIGHT(mi) >= 260) - f = bp->xfont2, fl = bp->font2_dlist; /* small font */ + f = bp->font2_data; /* small font */ else - f = bp->xfont3, fl = bp->font3_dlist; /* tiny font */ - - print_title_string (mi, label, - 10, mi->xgwa.height - 10, - f, fl); + f = bp->font3_data; /* tiny font */ + + glColor4fv (color); + glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color); + print_gl_string (mi->dpy, f, + mi->xgwa.width, mi->xgwa.height, + 10, mi->xgwa.height - 10, + label, False); } } glEndList (); } +#ifdef HAVE_TESS static void tess_error (GLenum errorCode) { fprintf (stderr, "%s: tesselation error: %s\n", progname, gluErrorString(errorCode)); - exit (0); + abort(); } +#endif /* HAVE_TESS */ + static void new_polyhedron (ModeInfo *mi) @@ -495,17 +340,18 @@ new_polyhedron (ModeInfo *mi) polyhedra_configuration *bp = &bps[MI_SCREEN(mi)]; polyhedron *p; int wire = MI_IS_WIREFRAME(mi); - static GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0}; int i; /* Use the GLU polygon tesselator so that nonconvex faces are displayed correctly (e.g., for the "pentagrammic concave deltohedron"). */ +# ifdef HAVE_TESS GLUtesselator *tobj = gluNewTess(); - gluTessCallback (tobj, GLU_TESS_BEGIN, (_GLUfuncptr) &glBegin); - gluTessCallback (tobj, GLU_TESS_END, (_GLUfuncptr) &glEnd); - gluTessCallback (tobj, GLU_TESS_VERTEX, (_GLUfuncptr) &glVertex3dv); - gluTessCallback (tobj, GLU_TESS_ERROR, (_GLUfuncptr) &tess_error); + gluTessCallback (tobj, GLU_TESS_BEGIN, (void (*) (void)) &glBegin); + gluTessCallback (tobj, GLU_TESS_END, (void (*) (void)) &glEnd); + gluTessCallback (tobj, GLU_TESS_VERTEX, (void (*) (void)) &glVertex3dv); + gluTessCallback (tobj, GLU_TESS_ERROR, (void (*) (void)) &tess_error); +# endif /* HAVE_TESS */ mi->polygon_count = 0; @@ -526,48 +372,113 @@ new_polyhedron (ModeInfo *mi) new_label (mi); + if (wire) + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + glNewList (bp->object_list, GL_COMPILE); - for (i = 0; i < p->nfaces; i++) + if (bp->which == bp->npolyhedra-1) { - int j; - face *f = &p->faces[i]; - - if (f->color > 64 || f->color < 0) abort(); + GLfloat bcolor[4]; + bcolor[0] = bp->colors[0].red / 65536.0; + bcolor[1] = bp->colors[0].green / 65536.0; + bcolor[2] = bp->colors[0].blue / 65536.0; + bcolor[3] = 1.0; if (wire) glColor3f (0, 1, 0); else + glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, bcolor); + + glScalef (0.8, 0.8, 0.8); + p->nfaces = unit_teapot (6, wire); + p->nedges = p->nfaces * 3 / 2; + p->npoints = p->nfaces * 3; + p->logical_faces = p->nfaces; + p->logical_vertices = p->npoints; + } + else + { + glFrontFace (GL_CCW); + for (i = 0; i < p->nfaces; i++) { - bcolor[0] = bp->colors[f->color].red / 65536.0; - bcolor[1] = bp->colors[f->color].green / 65536.0; - bcolor[2] = bp->colors[f->color].blue / 65536.0; - glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, bcolor); - } + int j; + face *f = &p->faces[i]; - kludge_normal (f->npoints, f->points, p->points); + if (f->color > 64 || f->color < 0) abort(); + if (wire) + glColor3f (0, 1, 0); + else + { + GLfloat bcolor[4]; + bcolor[0] = bp->colors[f->color].red / 65536.0; + bcolor[1] = bp->colors[f->color].green / 65536.0; + bcolor[2] = bp->colors[f->color].blue / 65536.0; + bcolor[3] = 1.0; + glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, bcolor); + } + + kludge_normal (f->npoints, f->points, p->points); - gluTessBeginPolygon (tobj, 0); - gluTessBeginContour (tobj); - for (j = 0; j < f->npoints; j++) - { - point *pp = &p->points[f->points[j]]; - gluTessVertex (tobj, &pp->x, &pp->x); +# ifdef HAVE_TESS + gluTessBeginPolygon (tobj, 0); + gluTessBeginContour (tobj); + for (j = 0; j < f->npoints; j++) + { + point *pp = &p->points[f->points[j]]; + gluTessVertex (tobj, &pp->x, &pp->x); + } + gluTessEndContour (tobj); + gluTessEndPolygon (tobj); +# else /* !HAVE_TESS */ + glBegin (wire ? GL_LINE_LOOP : + f->npoints == 3 ? GL_TRIANGLES : + f->npoints == 4 ? GL_QUADS : + GL_POLYGON); + for (j = 0; j < f->npoints; j++) + { + point *pp = &p->points[f->points[j]]; + glVertex3f (pp->x, pp->y, pp->z); + } + glEnd(); +# endif /* !HAVE_TESS */ } - gluTessEndContour (tobj); - gluTessEndPolygon (tobj); } glEndList (); mi->polygon_count += p->nfaces; +# ifdef HAVE_TESS gluDeleteTess (tobj); +# endif +} + + +static void +construct_teapot (ModeInfo *mi) +{ + polyhedra_configuration *bp = &bps[MI_SCREEN(mi)]; + int n = bp->npolyhedra-1; + polyhedron *p = (polyhedron *) calloc (1, sizeof(*p)); + p->number = n; + p->wythoff = strdup("X00398|1984"); + p->name = strdup("Teapot"); + p->dual = strdup(""); + p->config = strdup("Melitta"); + p->group = strdup("Teapotahedral (Newell[1975])"); + p->class = strdup("Utah Teapotahedron"); + bp->polyhedra[n] = p; } -void +ENTRYPOINT void init_polyhedra (ModeInfo *mi) { polyhedra_configuration *bp; int wire = MI_IS_WIREFRAME(mi); +# ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */ + MI_IS_WIREFRAME(mi) = 0; + wire = 0; +# endif + if (!bps) { bps = (polyhedra_configuration *) calloc (MI_NUM_SCREENS(mi), sizeof (polyhedra_configuration)); @@ -575,8 +486,6 @@ init_polyhedra (ModeInfo *mi) fprintf(stderr, "%s: out of memory\n", progname); exit(1); } - - bp = &bps[MI_SCREEN(mi)]; } bp = &bps[MI_SCREEN(mi)]; @@ -587,8 +496,6 @@ init_polyhedra (ModeInfo *mi) load_fonts (mi); startup_blurb (mi); - reshape_polyhedra (mi, MI_WIDTH(mi), MI_HEIGHT(mi)); - if (!wire) { GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0}; @@ -623,10 +530,11 @@ init_polyhedra (ModeInfo *mi) spin_accel, do_wander ? wander_speed : 0, True); - bp->trackball = gltrackball_init (); + bp->trackball = gltrackball_init (True); } bp->npolyhedra = construct_polyhedra (&bp->polyhedra); + construct_teapot (mi); bp->object_list = glGenLists (1); bp->title_list = glGenLists (1); @@ -636,8 +544,17 @@ init_polyhedra (ModeInfo *mi) int x; char c; do_which = -1; - if (1 == sscanf (do_which_str, " %d %c", &x, &c)) - do_which = x; + if (!strcasecmp (do_which_str, "random")) + ; + else if (1 == sscanf (do_which_str, " %d %c", &x, &c)) + { + if (x >= 0 && x < bp->npolyhedra) + do_which = x; + else + fprintf (stderr, + "%s: polyhedron %d does not exist: there are only %d.\n", + progname, x, bp->npolyhedra-1); + } else if (*do_which_str) { char *s; @@ -646,6 +563,7 @@ init_polyhedra (ModeInfo *mi) for (x = 0; x < bp->npolyhedra; x++) if (!strcasecmp (do_which_str, bp->polyhedra[x]->name) || + !strcasecmp (do_which_str, bp->polyhedra[x]->class) || !strcasecmp (do_which_str, bp->polyhedra[x]->wythoff) || !strcasecmp (do_which_str, bp->polyhedra[x]->config)) { @@ -662,42 +580,43 @@ init_polyhedra (ModeInfo *mi) } new_polyhedron (mi); + reshape_polyhedra (mi, MI_WIDTH(mi), MI_HEIGHT(mi)); + clear_gl_error(); /* WTF? sometimes "invalid op" from glViewport! */ + } -void +ENTRYPOINT void draw_polyhedra (ModeInfo *mi) { polyhedra_configuration *bp = &bps[MI_SCREEN(mi)]; Display *dpy = MI_DISPLAY(mi); Window window = MI_WINDOW(mi); - static time_t last_time = 0; - - static GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0}; - static GLfloat bshiny = 128.0; + static const GLfloat bspec[4] = {1.0, 1.0, 1.0, 1.0}; + GLfloat bshiny = 128.0; if (!bp->glx_context) return; + glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context)); + if (bp->mode == 0 && do_which >= 0 && bp->change_to < 0) ; else if (bp->mode == 0) { - static int tick = 0; - if (bp->change_to >= 0) - tick = 999, last_time = 1; - if (tick++ > 10) + bp->change_tick = 999, bp->last_change_time = 1; + if (bp->change_tick++ > 10) { time_t now = time((time_t *) 0); - if (last_time == 0) last_time = now; - tick = 0; - if (!bp->button_down_p && now - last_time >= duration) + if (bp->last_change_time == 0) bp->last_change_time = now; + bp->change_tick = 0; + if (!bp->button_down_p && now - bp->last_change_time >= duration) { bp->mode = 1; /* go out */ bp->mode_tick = 20 * speed; - last_time = now; + bp->last_change_time = now; } } } @@ -768,4 +687,6 @@ draw_polyhedra (ModeInfo *mi) glXSwapBuffers(dpy, window); } +XSCREENSAVER_MODULE ("Polyhedra", polyhedra) + #endif /* USE_GL */