-/* winduprobot, Copyright (c) 2014 Jamie Zawinski <jwz@jwz.org>
+/* winduprobot, Copyright (c) 2014-2017 Jamie Zawinski <jwz@jwz.org>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* came for us to throw the Cocktail Robotics Grand Challenge at DNA Lounge, I
* photographed this robot (holding a tiny martini glass) to make a flyer for
* the event. You can see that photo here:
- * http://www.dnalounge.com/flyers/2014/09/14.html
+ * https://www.dnalounge.com/flyers/2014/09/14.html
*
* Then I decided to try and make award statues for the contest by modeling
* this robot and 3D-printing it (a robot on a post, with the DNA Lounge
* - Clean up the model a little bit more;
* - Export to DXF with "Millimeters", "Triangles", using
* http://www.guitar-list.com/download-software/convert-sketchup-skp-files-dxf-or-stl
+ *
+ * We did eventually end up with robotic award statues, but we constructed
+ * them out of mass-produced wind-up robots, rather than 3D printing them:
+ * https://www.dnalounge.com/gallery/2014/09-14/045.html
+ * https://www.youtube.com/watch?v=EZF4ZAAy49g
*/
-#define LABEL_FONT "-*-helvetica-bold-r-normal-*-240-*"
+#define LABEL_FONT "-*-helvetica-bold-r-normal-*-*-240-*-*-*-*-*-*"
#define DEFAULTS "*delay: 20000 \n" \
"*count: 25 \n" \
"*program: xscreensaver-text\n" \
"*usePty: False\n"
-#define DEBUG
+#undef DEBUG
#define WORDBUBBLES
-# define refresh_robot 0
+# define release_robot 0
#undef countof
#define countof(x) (sizeof((x))/sizeof((*x)))
#ifdef WORDBUBBLES
# include "textclient.h"
-# include "glxfonts.h"
+# include "texfont.h"
#endif
#include <ctype.h>
reshape_robot (ModeInfo *mi, int width, int height)
{
GLfloat h = (GLfloat) height / (GLfloat) width;
+ int y = 0;
+
+ if (width > height * 5) { /* tiny window: show middle */
+ height = width * 9/16;
+ y = -height/2;
+ h = height / (GLfloat) width;
+ }
- glViewport (0, 0, (GLint) width, (GLint) height);
+ glViewport (0, y, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
- gluLookAt( 0, 20, 30,
+ gluLookAt( 0, 0, 30,
0, 0, 0,
0, 1, 0);
robot_configuration *bp;
int wire = MI_IS_WIREFRAME(mi);
int i;
- if (!bps) {
- bps = (robot_configuration *)
- calloc (MI_NUM_SCREENS(mi), sizeof (robot_configuration));
- if (!bps) {
- fprintf(stderr, "%s: out of memory\n", progname);
- exit(1);
- }
- }
+ MI_INIT (mi, bps);
bp = &bps[MI_SCREEN(mi)];
char *key = 0;
GLfloat spec1[4] = {1.00, 1.00, 1.00, 1.0};
GLfloat spec2[4] = {0.40, 0.40, 0.70, 1.0};
- GLfloat *spec = spec1;
+ GLfloat *spec = 0;
GLfloat shiny = 20;
glNewList (bp->dlists[i], GL_COMPILE);
# endif /* WORDBUBBLES */
- /* Let's tilt the floor a little. */
# ifdef DEBUG
if (!debug_p)
# endif
- {
- gltrackball_start (bp->user_trackball, 0, 500, 1000, 1000);
- gltrackball_track (bp->user_trackball,
- 0, 500 + (random() % 200) - 100,
- 1000, 1000);
- }
+ /* Let's tilt the floor a little. */
+ gltrackball_reset (bp->user_trackball,
+ -0.6 + frand(1.2),
+ -0.6 + frand(0.2));
}
/* Draw a robot standing in the right place, 1 unit tall.
*/
static int
-draw_walker (ModeInfo *mi, walker *f)
+draw_walker (ModeInfo *mi, walker *f, const char *tag)
{
+ robot_configuration *bp = &bps[MI_SCREEN(mi)];
+ int wire = MI_IS_WIREFRAME(mi);
int count = 0;
glPushMatrix();
/* Draw these last, and outer shell first, to make transparency work.
The order in which things hit the depth buffer matters.
*/
- if (f->body_transparency >= 0.001)
- {
- count += draw_arm (mi, f, True, f->hand_rot[0], f->hand_pos[0]);
- count += draw_arm (mi, f, False, f->hand_rot[1], f->hand_pos[1]);
- count += draw_body (mi, f, False);
- count += draw_body (mi, f, True);
- count += draw_dome (mi, f);
- }
+ if (f->body_transparency >= 0.001)
+ {
+ count += draw_arm (mi, f, True, f->hand_rot[0], f->hand_pos[0]);
+ count += draw_arm (mi, f, False, f->hand_rot[1], f->hand_pos[1]);
+ count += draw_body (mi, f, False);
+ count += draw_body (mi, f, True);
+ count += draw_dome (mi, f);
+ }
+
+ if (tag) /* For debugging depth sorting: label each robot */
+ {
+ GLfloat m[4][4];
+ if (! wire) glDisable (GL_DEPTH_TEST);
+ glColor3f (1, 1, 1);
+ glPushMatrix();
+
+ /* Billboard rotation */
+ glGetFloatv (GL_MODELVIEW_MATRIX, &m[0][0]);
+ m[0][0] = 1; m[1][0] = 0; m[2][0] = 0;
+ m[0][1] = 0; m[1][1] = 1; m[2][1] = 0;
+ m[0][2] = 0; m[1][2] = 0; m[2][2] = 1;
+ glLoadIdentity();
+ glMultMatrixf (&m[0][0]);
+ glScalef (0.04, 0.04, 0.04);
+
+ print_texture_string (bp->font_data, tag);
+ glPopMatrix();
+ if (! wire) glEnable (GL_DEPTH_TEST);
+ }
glPopMatrix();
return count;
draw_ground (ModeInfo *mi, GLfloat color[4])
{
int wire = MI_IS_WIREFRAME(mi);
- GLfloat i;
- GLfloat cell_size = 0.9;
- int cells = 1000 * size;
+ GLfloat i, j, k;
+
+ /* When using fog, iOS apparently doesn't like lines or quads that are
+ really long, and extend very far outside of the scene. Maybe? If the
+ length of the line (cells * cell_size) is greater than 25 or so, lines
+ that are oriented steeply away from the viewer tend to disappear
+ (whether implemented as GL_LINES or as GL_QUADS).
+
+ So we do a bunch of smaller grids instead of one big one.
+ */
+ int cells = 30;
+ GLfloat cell_size = 0.8;
int points = 0;
+ int grids = 12;
# ifdef DEBUG
if (debug_p) return 0;
{
GLfloat fog_color[4] = { 0, 0, 0, 1 };
- glLineWidth (2);
+ glLineWidth (4);
glEnable (GL_LINE_SMOOTH);
glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glFogi (GL_FOG_MODE, GL_EXP2);
glFogfv (GL_FOG_COLOR, fog_color);
- glFogf (GL_FOG_DENSITY, 0.017);
- glFogf (GL_FOG_START, -cells/2 * cell_size);
-# ifndef USE_IPHONE /* #### Not working on iOS for some reason */
+ glFogf (GL_FOG_DENSITY, 0.015);
+ glFogf (GL_FOG_START, -cells/2 * cell_size * grids);
glEnable (GL_FOG);
-# endif
}
glColor4fv (color);
glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
- glBegin (GL_LINES);
- for (i = -cells/2; i < cells/2; i++)
+ glTranslatef (-cells * grids * cell_size / 2,
+ -cells * grids * cell_size / 2, 0);
+
+ for (j = 0; j < grids; j++)
{
- GLfloat a = i * cell_size;
- GLfloat b = cells/2 * cell_size;
- glVertex3f (a, -b, 0); glVertex3f (a, b, 0); points++;
- glVertex3f (-b, a, 0); glVertex3f (b, a, 0); points++;
+ glPushMatrix();
+ for (k = 0; k < grids; k++)
+ {
+ glBegin (GL_LINES);
+ for (i = -cells/2; i < cells/2; i++)
+ {
+ GLfloat a = i * cell_size;
+ GLfloat b = cells/2 * cell_size;
+ glVertex3f (a, -b, 0); glVertex3f (a, b, 0); points++;
+ glVertex3f (-b, a, 0); glVertex3f (b, a, 0); points++;
+ }
+ glEnd();
+ glTranslatef (cells * cell_size, 0, 0);
+ }
+ glPopMatrix();
+ glTranslatef (0, cells * cell_size, 0);
}
- glEnd();
if (!wire)
{
glRotatef (current_device_rotation(), 0, 0, 1); /* right side up */
{
- int cw, ch, w, h;
+ XCharStruct e;
+ int cw, ch, w, h, ascent, descent;
GLfloat s;
GLfloat max = 24; /* max point size to avoid pixellated text */
if (mi->xgwa.height <= 640 || mi->xgwa.width <= 640)
max *= 3;
- cw = texture_string_width (bp->font_data, "X", &ch); /* line height */
+ texture_string_metrics (bp->font_data, "X", &e, &ascent, &descent);
+ cw = e.width;
+ ch = ascent + descent;
s = 1.0 / ch;
if (ch > max) s *= max/ch;
s *= scale;
- w = texture_string_width (bp->font_data, label, &h);
+ texture_string_metrics (bp->font_data, label, &e, 0, 0);
+ w = e.width;
+ h = e.ascent + e.descent;
glScalef (s, s, 1);
glTranslatef (-w/2, h*2/3 + (cw * 7), 0);
glPushMatrix();
- glTranslatef (0, -h - ch/4, -0.1);
+ glTranslatef (0, -h + (ch * 1.2), -0.1);
draw_bubble_box (mi, w, h,
ch * 2, /* corner radius */
ch * 2.5, /* arrow height */
glPopMatrix();
glColor4fv (bp->text_color);
- print_gl_string (mi->dpy, bp->font_data,
- 0, 0, 0, 0,
- label, False);
+ glTranslatef (0, ch/2, 0);
+ print_texture_string (bp->font_data, label);
}
glPopMatrix();
+typedef struct {
+ int i;
+ GLdouble d;
+} depth_sorter;
+
+static int
+cmp_depth_sorter (const void *aa, const void *bb)
+{
+ const depth_sorter *a = (depth_sorter *) aa;
+ const depth_sorter *b = (depth_sorter *) bb;
+ return (a->d == b->d ? 0 : a->d < b->d ? -1 : 1);
+}
+
+
ENTRYPOINT void
draw_robot (ModeInfo *mi)
{
Display *dpy = MI_DISPLAY(mi);
Window window = MI_WINDOW(mi);
GLfloat robot_size;
+ depth_sorter *sorted;
int i;
if (!bp->glx_context)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
- glRotatef(current_device_rotation(), 0, 0, 1);
+
+# ifdef HAVE_MOBILE
+ glRotatef (current_device_rotation(), 0, 0, 1); /* right side up */
+# endif
+
gltrackball_rotate (bp->user_trackball);
+ glTranslatef (0, -20, 0); /* Move the horizon down the screen */
+
robot_size = size * 7;
glScalef (robot_size, robot_size, robot_size);
look_at_center (mi);
# endif /* DEBUG */
+ /* For transparency to work right, we have to draw the robots from
+ back to front, from the perspective of the observer. So project
+ the origin of the robot to screen coordinates, and sort that by Z.
+ */
+ sorted = (depth_sorter *) calloc (bp->nwalkers, sizeof (*sorted));
+ {
+ GLdouble m[16], p[16];
+ GLint v[4];
+ glGetDoublev (GL_MODELVIEW_MATRIX, m);
+ glGetDoublev (GL_PROJECTION_MATRIX, p);
+ glGetIntegerv (GL_VIEWPORT, v);
+
+ for (i = 0; i < bp->nwalkers; i++)
+ {
+ GLdouble x, y, z;
+ walker *f = &bp->walkers[i];
+ gluProject (f->y, f->z, f->x, m, p, v, &x, &y, &z);
+ sorted[i].i = i;
+ sorted[i].d = -z;
+ }
+ qsort (sorted, i, sizeof(*sorted), cmp_depth_sorter);
+ }
+
+
mi->polygon_count = 0;
for (i = 0; i < bp->nwalkers; i++)
{
- walker *f = &bp->walkers[i];
- int i, ticks = 22 * speed * f->speed;
+ int ii = sorted[i].i;
+ walker *f = &bp->walkers[ii];
+ int ticks = 22 * speed * f->speed;
int max = 180;
+ char tag[1024];
+ *tag = 0;
if (ticks < 1) ticks = 1;
if (ticks > max) ticks = max;
- mi->polygon_count += draw_walker (mi, f);
-
# ifdef DEBUG
if (debug_p)
+ sprintf (tag, "%.4f, %.4f, %.4f",
+ bp->debug_x, bp->debug_y, bp->debug_z);
+ else
{
- char s[1024];
- sprintf (s, "%.4f, %.4f, %.4f",
- bp->debug_x, bp->debug_y, bp->debug_z);
- glColor3f (1, 1, 1);
- draw_label (mi, f, -0.3, 1, s);
+# if 1
+ /* sprintf (tag + strlen(tag), " %d\n", ii); */
+ sprintf (tag + strlen(tag), " %d\n", bp->nwalkers - i - 1);
+ /* sprintf (tag + strlen(tag), "%.03f\n", sorted[i].d); */
+# endif
}
+
# endif /* DEBUG */
- for (i = 0; i < ticks; i++)
+ mi->polygon_count += draw_walker (mi, f, tag);
+
+ for (ii = 0; ii < ticks; ii++)
tick_walker (mi, f);
}
-
+ free (sorted);
glPopMatrix ();
}
ENTRYPOINT void
-release_robot (ModeInfo *mi)
+free_robot (ModeInfo *mi)
{
# ifdef WORDBUBBLES
robot_configuration *bp = &bps[MI_SCREEN(mi)];
- textclient_close (bp->tc);
+ if (bp->tc)
+ textclient_close (bp->tc);
# endif
}