X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Fglx%2Flavalite.c;h=b1604a2212829b623850e7be27dde2a78cc2f831;hb=4361b69d3178d7fc98d0388f9a223af6c2651aba;hp=631c2e22b6d3e7bb77e1927007dd4c52971e939a;hpb=13dbc569cdc6e29019722c0ef9b932a925efbcad;p=xscreensaver diff --git a/hacks/glx/lavalite.c b/hacks/glx/lavalite.c index 631c2e22..b1604a22 100644 --- a/hacks/glx/lavalite.c +++ b/hacks/glx/lavalite.c @@ -1,6 +1,6 @@ /* lavalite --- 3D Simulation a Lava Lite, written by jwz. * - * This software Copyright (c) 2002 Jamie Zawinski + * This software Copyright (c) 2002-2017 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 @@ -64,54 +64,14 @@ * with depth buffering turned off? */ -#include - -extern XtAppContext app; - -#define PROGCLASS "LavaLite" -#define HACK_INIT init_lavalite -#define HACK_DRAW draw_lavalite -#define HACK_RESHAPE reshape_lavalite -#define HACK_HANDLE_EVENT lavalite_handle_event -#define EVENT_MASK PointerMotionMask -#define sws_opts xlockmore_opts - -#define DEF_SPIN "Z" -#define DEF_WANDER "False" -#define DEF_SPEED "0.003" -#define DEF_RESOLUTION "40" -#define DEF_SMOOTH "True" -#define DEF_COUNT "3" -#define DEF_STYLE "random" -#define DEF_IMPATIENT "False" -#define DEF_LCOLOR "#FF0000" /* lava */ -#define DEF_FCOLOR "#00AAFF" /* fluid */ -#define DEF_BCOLOR "#666666" /* base */ -#define DEF_TCOLOR "#000000" /*"#00FF00"*/ /* table */ - -#define DEF_FTEX "(none)" -#define DEF_BTEX "(none)" -#define DEF_TTEX "(none)" - -#define DEFAULTS "*delay: 10000 \n" \ +#define DEFAULTS "*delay: 30000 \n" \ "*showFPS: False \n" \ "*wireframe: False \n" \ + "*geometry: 600x900\n" \ "*count: " DEF_COUNT " \n" \ - "*style: " DEF_STYLE " \n" \ - "*speed: " DEF_SPEED " \n" \ - "*spin: " DEF_SPIN "\n" \ - "*wander: " DEF_WANDER "\n" \ - "*resolution: " DEF_RESOLUTION "\n" \ - "*smooth: " DEF_SMOOTH "\n" \ - "*impatient: " DEF_IMPATIENT " \n" \ - "*geometry: 600x900\n" \ - "*lavaColor: " DEF_LCOLOR "\n" \ - "*fluidColor: " DEF_FCOLOR "\n" \ - "*baseColor: " DEF_BCOLOR "\n" \ - "*tableColor: " DEF_TCOLOR "\n" \ - "*fluidTexture: " DEF_FTEX "\n" \ - "*baseTexture: " DEF_BTEX "\n" \ - "*tableTexture: " DEF_TTEX "\n" \ + +# define refresh_lavalite 0 +# define release_lavalite 0 #define BLOBS_PER_GROUP 4 @@ -137,8 +97,23 @@ extern XtAppContext app; #ifdef USE_GL /* whole file */ -#include +#define DEF_SPIN "Z" +#define DEF_WANDER "False" +#define DEF_SPEED "0.003" +#define DEF_RESOLUTION "40" +#define DEF_SMOOTH "True" +#define DEF_COUNT "3" +#define DEF_STYLE "random" +#define DEF_IMPATIENT "False" +#define DEF_LCOLOR "#FF0000" /* lava */ +#define DEF_FCOLOR "#00AAFF" /* fluid */ +#define DEF_BCOLOR "#666666" /* base */ +#define DEF_TCOLOR "#000000" /*"#00FF00"*/ /* table */ + +#define DEF_FTEX "(none)" +#define DEF_BTEX "(none)" +#define DEF_TTEX "(none)" typedef struct metaball metaball; @@ -171,7 +146,7 @@ typedef struct { GLfloat texture_elevation; } lamp_geometry; -static lamp_geometry classic_lamp[] = { +static const lamp_geometry classic_lamp[] = { { CAP, 1.16, 0.089, 0.00 }, { BOTTLE, 0.97, 0.120, 0.40 }, { BOTTLE, 0.13, 0.300, 0.87 }, @@ -182,7 +157,7 @@ static lamp_geometry classic_lamp[] = { { 0, 0, 0, 0 }, }; -static lamp_geometry giant_lamp[] = { +static const lamp_geometry giant_lamp[] = { { CAP, 1.12, 0.105, 0.00 }, { BOTTLE, 0.97, 0.130, 0.30 }, { BOTTLE, 0.20, 0.300, 0.87 }, @@ -193,7 +168,7 @@ static lamp_geometry giant_lamp[] = { { 0, 0, 0, 0 }, }; -static lamp_geometry cone_lamp[] = { +static const lamp_geometry cone_lamp[] = { { CAP, 1.35, 0.001, 0.00 }, { CAP, 1.35, 0.020, 0.00 }, { CAP, 1.30, 0.055, 0.05 }, @@ -205,7 +180,7 @@ static lamp_geometry cone_lamp[] = { { 0, 0, 0, 0 }, }; -static lamp_geometry rocket_lamp[] = { +static const lamp_geometry rocket_lamp[] = { { CAP, 1.35, 0.001, 0.00 }, { CAP, 1.34, 0.020, 0.00 }, { CAP, 1.30, 0.055, 0.05 }, @@ -225,7 +200,7 @@ static lamp_geometry rocket_lamp[] = { typedef struct { GLXContext *glx_context; lamp_style style; - lamp_geometry *model; + const lamp_geometry *model; rotator *rot; rotator *rot2; trackball_state *trackball; @@ -244,8 +219,6 @@ typedef struct { GLuint bottle_list; GLuint ball_list; - Bool spin_x, spin_y, spin_z; /* spin of the scene overall */ - int bottle_poly_count; /* polygons in the bottle only */ } lavalite_configuration; @@ -265,13 +238,14 @@ static char *lava_color_str, *fluid_color_str, *base_color_str, static char *fluid_tex, *base_tex, *table_tex; static GLfloat lava_color[4], fluid_color[4], base_color[4], table_color[4]; -static GLfloat lava_spec[4] = {1.0, 1.0, 1.0, 1.0}; -static GLfloat lava_shininess = 128.0; -static GLfloat foot_color[4] = {0.2, 0.2, 0.2, 1.0}; -static GLfloat light0_pos[4] = {-0.6, 0.0, 1.0, 0.0}; -static GLfloat light1_pos[4] = { 1.0, 0.0, 0.2, 0.0}; -static GLfloat light2_pos[4] = { 0.6, 0.0, 1.0, 0.0}; +static const GLfloat lava_spec[4] = {1.0, 1.0, 1.0, 1.0}; +static const GLfloat lava_shininess = 128.0; +static const GLfloat foot_color[4] = {0.2, 0.2, 0.2, 1.0}; + +static const GLfloat light0_pos[4] = {-0.6, 0.0, 1.0, 0.0}; +static const GLfloat light1_pos[4] = { 1.0, 0.0, 0.2, 0.0}; +static const GLfloat light2_pos[4] = { 0.6, 0.0, 1.0, 0.0}; @@ -299,30 +273,30 @@ static XrmOptionDescRec opts[] = { }; static argtype vars[] = { - {(caddr_t *) &do_style, "style", "Style", DEF_STYLE, t_String}, - {(caddr_t *) &do_spin, "spin", "Spin", DEF_SPIN, t_String}, - {(caddr_t *) &do_wander, "wander", "Wander", DEF_WANDER, t_Bool}, - {(caddr_t *) &speed, "speed", "Speed", DEF_SPEED, t_Float}, - {(caddr_t *) &resolution, "resolution", "Resolution", DEF_RESOLUTION, t_Int}, - {(caddr_t *) &do_smooth, "smooth", "Smooth", DEF_SMOOTH, t_Bool}, - {(caddr_t *) &do_impatient, "impatient", "Impatient", DEF_IMPATIENT, t_Bool}, - - {(caddr_t *) &lava_color_str, "lavaColor", "LavaColor", DEF_LCOLOR,t_String}, - {(caddr_t *) &fluid_color_str,"fluidColor","FluidColor",DEF_FCOLOR,t_String}, - {(caddr_t *) &base_color_str, "baseColor", "BaseColor", DEF_BCOLOR,t_String}, - {(caddr_t *) &table_color_str,"tableColor","TableColor",DEF_TCOLOR,t_String}, - - {(caddr_t *) &fluid_tex, "fluidTexture", "FluidTexture", DEF_FTEX, t_String}, - {(caddr_t *) &base_tex, "baseTexture", "BaseTexture", DEF_BTEX, t_String}, - {(caddr_t *) &table_tex, "tableTexture", "BaseTexture", DEF_TTEX, t_String}, + {&do_style, "style", "Style", DEF_STYLE, t_String}, + {&do_spin, "spin", "Spin", DEF_SPIN, t_String}, + {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool}, + {&speed, "speed", "Speed", DEF_SPEED, t_Float}, + {&resolution, "resolution", "Resolution", DEF_RESOLUTION, t_Int}, + {&do_smooth, "smooth", "Smooth", DEF_SMOOTH, t_Bool}, + {&do_impatient, "impatient", "Impatient", DEF_IMPATIENT, t_Bool}, + + {&lava_color_str, "lavaColor", "LavaColor", DEF_LCOLOR, t_String}, + {&fluid_color_str, "fluidColor", "FluidColor", DEF_FCOLOR, t_String}, + {&base_color_str, "baseColor", "BaseColor", DEF_BCOLOR, t_String}, + {&table_color_str, "tableColor", "TableColor", DEF_TCOLOR, t_String}, + + {&fluid_tex, "fluidTexture", "FluidTexture", DEF_FTEX, t_String}, + {&base_tex, "baseTexture", "BaseTexture", DEF_BTEX, t_String}, + {&table_tex, "tableTexture", "BaseTexture", DEF_TTEX, t_String}, }; -ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL}; +ENTRYPOINT ModeSpecOpt lavalite_opts = {countof(opts), opts, countof(vars), vars, NULL}; /* Window management, etc */ -void +ENTRYPOINT void reshape_lavalite (ModeInfo *mi, int width, int height) { GLfloat h = (GLfloat) height / (GLfloat) width; @@ -365,6 +339,7 @@ load_texture (ModeInfo *mi, const char *filename) } image = xpm_file_to_ximage (dpy, visual, cmap, filename); + if (!image) return False; clear_gl_error(); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, @@ -536,7 +511,7 @@ draw_table (GLfloat z, Bool wire) static int draw_wing (GLfloat w, GLfloat h, GLfloat d, Bool wire) { - static int coords[2][8][2] = { + static const int coords[2][8][2] = { { { 0, 0 }, { 10, 10 }, { 20, 23 }, @@ -559,7 +534,7 @@ draw_wing (GLfloat w, GLfloat h, GLfloat d, Bool wire) int polys = 0; int maxx = coords[0][countof(coords[0])-1][0]; int maxy = coords[0][countof(coords[0])-1][1]; - int x; + unsigned int x; for (x = 1; x < countof(coords[0]); x++) { @@ -637,9 +612,8 @@ generate_bottle (ModeInfo *mi) int wire = MI_IS_WIREFRAME(mi); int faces = resolution * 1.5; Bool smooth = do_smooth; - Bool have_texture = False; - lamp_geometry *top_slice = bp->model; + const lamp_geometry *top_slice = bp->model; const char *current_texture = 0; lamp_part last_part = 0; @@ -661,7 +635,7 @@ generate_bottle (ModeInfo *mi) while (1) { - lamp_geometry *bot_slice = top_slice + 1; + const lamp_geometry *bot_slice = top_slice + 1; const char *texture = 0; GLfloat *color = 0; @@ -686,11 +660,10 @@ generate_bottle (ModeInfo *mi) break; } - have_texture = False; if (!wire && texture && texture != current_texture) { current_texture = texture; - have_texture = load_texture (mi, current_texture); + load_texture (mi, current_texture); } /* Color the discs darker than the tube walls. */ @@ -709,7 +682,8 @@ generate_bottle (ModeInfo *mi) if ((top_slice->part == CAP && bot_slice->part == BOTTLE) || (top_slice->part == BASE && bot_slice->part == 0)) { - lamp_geometry *sl = (bot_slice->part == 0 ? top_slice : bot_slice); + const lamp_geometry *sl = (bot_slice->part == 0 + ? top_slice : bot_slice); bp->bottle_poly_count += draw_disc (sl->radius, sl->elevation, faces, False, wire); } @@ -761,7 +735,7 @@ generate_bottle (ModeInfo *mi) } - have_texture = !wire && load_texture (mi, table_tex); + if (!wire) load_texture (mi, table_tex); glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, table_color); bp->bottle_poly_count += draw_table (top_slice->elevation, wire); @@ -886,7 +860,7 @@ static GLfloat max_bottle_radius (lavalite_configuration *bp) { GLfloat r = 0; - lamp_geometry *slice; + const lamp_geometry *slice; for (slice = bp->model; slice->part != 0; slice++) { if (slice->part == BOTTLE && slice->radius > r) @@ -901,8 +875,8 @@ max_bottle_radius (lavalite_configuration *bp) static GLfloat bottle_radius_at (lavalite_configuration *bp, GLfloat z) { - GLfloat topz = -999, botz = -999, topr, botr; - lamp_geometry *slice; + GLfloat topz = -999, botz = -999, topr = 0, botr = 0; + const lamp_geometry *slice; GLfloat ratio; for (slice = bp->model; slice->part != 0; slice++) @@ -1239,7 +1213,10 @@ animate_lava (ModeInfo *mi) mi->polygon_count = 0; { - double s = 1.0/bp->grid_size; + double s; + if (bp->grid_size == 0) bp->grid_size = 1; /* first time through */ + s = 1.0/bp->grid_size; + glPushMatrix(); glTranslatef (-0.5, -0.5, 0); glScalef (s, s, s); @@ -1260,34 +1237,15 @@ animate_lava (ModeInfo *mi) /* Startup initialization */ -Bool +ENTRYPOINT Bool lavalite_handle_event (ModeInfo *mi, XEvent *event) { lavalite_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; - } - else if (event->xany.type == MotionNotify && - bp->button_down_p) - { - gltrackball_track (bp->trackball, - event->xmotion.x, event->xmotion.y, - MI_WIDTH (mi), MI_HEIGHT (mi)); - return True; - } + if (gltrackball_event_handler (event, bp->trackball, + MI_WIDTH (mi), MI_HEIGHT (mi), + &bp->button_down_p)) + return True; return False; } @@ -1297,7 +1255,7 @@ static void parse_color (ModeInfo *mi, const char *name, const char *s, GLfloat *a) { XColor c; - a[4] = 1.0; /* alpha */ + a[3] = 1.0; /* alpha */ if (! XParseColor (MI_DISPLAY(mi), MI_COLORMAP(mi), s, &c)) { @@ -1310,22 +1268,13 @@ parse_color (ModeInfo *mi, const char *name, const char *s, GLfloat *a) } -void +ENTRYPOINT void init_lavalite (ModeInfo *mi) { lavalite_configuration *bp; int wire = MI_IS_WIREFRAME(mi); - if (!bps) { - bps = (lavalite_configuration *) - calloc (MI_NUM_SCREENS(mi), sizeof (lavalite_configuration)); - if (!bps) { - fprintf(stderr, "%s: out of memory\n", progname); - exit(1); - } - - bp = &bps[MI_SCREEN(mi)]; - } + MI_INIT (mi, bps, NULL); bp = &bps[MI_SCREEN(mi)]; @@ -1334,24 +1283,7 @@ init_lavalite (ModeInfo *mi) reshape_lavalite (mi, MI_WIDTH(mi), MI_HEIGHT(mi)); { - char *s = do_spin; - while (*s) - { - if (*s == ' ' || *s == '\t') ; - else if (*s == 'x' || *s == 'X') bp->spin_x = 1; - else if (*s == 'y' || *s == 'Y') bp->spin_y = 1; - else if (*s == 'z' || *s == 'Z') bp->spin_z = 1; - else - { - fprintf (stderr, - "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n", - progname, do_spin); - exit (1); - } - s++; - } - - s = do_style; + char *s = do_style; if (!s || !*s || !strcasecmp (s, "classic")) bp->style = CLASSIC; else if (!strcasecmp (s, "giant")) bp->style = GIANT; else if (!strcasecmp (s, "cone")) bp->style = CONE; @@ -1414,6 +1346,7 @@ init_lavalite (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, @@ -1433,19 +1366,15 @@ init_lavalite (ModeInfo *mi) bp->rot2 = make_rotator (spin_speed, 0, 0, 1, 0.1, False); - bp->trackball = gltrackball_init (); - - /* move initial camera position up by around 15 degrees: - in other words, tilt the scene toward the viewer. */ - gltrackball_start (bp->trackball, 50, 50, 100, 100); - gltrackball_track (bp->trackball, 50, 5, 100, 100); - - /* Oh, but if it's the "Giant" model, tilt the scene away: make it - look like we're looking up at it instead of odwn at it! */ - if (bp->style == GIANT) - gltrackball_track (bp->trackball, 50, -12, 100, 100); - else if (bp->style == ROCKET) /* same for rocket, but not as much */ - gltrackball_track (bp->trackball, 50, -4, 100, 100); + bp->trackball = gltrackball_init (False); + + /* Tilt the scene a bit: lean the normal lamps toward the viewer, + and the huge lamps away. */ + gltrackball_reset (bp->trackball, + -0.3 + frand(0.6), + (bp->style == ROCKET || bp->style == GIANT + ? frand (0.2) + : -frand (0.6))); } switch (bp->style) @@ -1478,7 +1407,7 @@ init_lavalite (ModeInfo *mi) /* Render one frame */ -void +ENTRYPOINT void draw_lavalite (ModeInfo *mi) { lavalite_configuration *bp = &bps[MI_SCREEN(mi)]; @@ -1488,6 +1417,8 @@ draw_lavalite (ModeInfo *mi) if (!bp->glx_context) return; + glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context)); + glMatrixMode (GL_MODELVIEW); glPushMatrix (); @@ -1544,6 +1475,7 @@ draw_lavalite (ModeInfo *mi) #endif /* 0 */ glLoadIdentity(); + glRotatef(current_device_rotation(), 0, 0, 1); gluLookAt ((cx - 0.5) * 8, /* Position the camera */ (cy - 0.5) * 8, @@ -1553,6 +1485,8 @@ draw_lavalite (ModeInfo *mi) gltrackball_rotate (bp->trackball); /* Apply mouse-based camera position */ + glRotatef (-90, 1, 0, 0); /* Right side up */ + /* Place the lights relative to the object, before the object has been rotated or wandered within the scene. */ @@ -1594,4 +1528,6 @@ draw_lavalite (ModeInfo *mi) glXSwapBuffers(dpy, window); } +XSCREENSAVER_MODULE ("Lavalite", lavalite) + #endif /* USE_GL */