+/* Put the labels on the atoms.
+ This can't be a part of the display list because of the games
+ we play with the translation matrix.
+ */
+static void
+draw_labels (ModeInfo *mi)
+{
+ molecule_configuration *mc = &mcs[MI_SCREEN(mi)];
+ int wire = MI_IS_WIREFRAME(mi);
+ molecule *m = &mc->molecules[mc->which];
+# ifdef HAVE_GLBITMAP
+ XFontStruct *xfont = (mc->scale_down ? mc->xfont2 : mc->xfont1);
+ GLuint font_dlist = (mc->scale_down ? mc->font2_dlist : mc->font1_dlist);
+# else
+ texture_font_data *font_data = mc->font1_data; /* don't scale down */
+# endif
+ int i;
+
+ if (!do_labels)
+ return;
+
+ if (!wire)
+ glDisable (GL_LIGHTING); /* don't light fonts */
+
+ for (i = 0; i < m->natoms; i++)
+ {
+ molecule_atom *a = &m->atoms[i];
+ GLfloat size = atom_size (a);
+ GLfloat m[4][4];
+
+ glPushMatrix();
+
+ if (!wire)
+ set_atom_color (mi, a, True, 1);
+
+ /* First, we translate the origin to the center of the atom.
+
+ Then we retrieve the prevailing modelview matrix (which
+ includes any rotation, wandering, and user-trackball-rolling
+ of the scene.
+
+ We set the top 3x3 cells of that matrix to be the identity
+ matrix. This removes all rotation from the matrix, while
+ leaving the translation alone. This has the effect of
+ leaving the prevailing coordinate system perpendicular to
+ the camera view: were we to draw a square face, it would
+ be in the plane of the screen.
+
+ Now we translate by `size' toward the viewer -- so that the
+ origin is *just in front* of the ball.
+
+ Then we draw the label text, allowing the depth buffer to
+ do its work: that way, labels on atoms will be occluded
+ properly when other atoms move in front of them.
+
+ This technique (of neutralizing rotation relative to the
+ observer, after both rotations and translations have been
+ applied) is known as "billboarding".
+ */
+
+ glTranslatef(a->x, a->y, a->z); /* get matrix */
+ glGetFloatv (GL_MODELVIEW_MATRIX, &m[0][0]); /* load rot. identity */
+ 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(); /* reset modelview */
+ glMultMatrixf (&m[0][0]); /* replace with ours */
+
+ glTranslatef (0, 0, (size * 1.1)); /* move toward camera */
+
+ glRotatef (current_device_rotation(), 0, 0, 1); /* right side up */
+
+# ifdef HAVE_GLBITMAP
+ glRasterPos3f (0, 0, 0); /* draw text here */
+
+ /* Before drawing the string, shift the origin to center
+ the text over the origin of the sphere. */
+ glBitmap (0, 0, 0, 0,
+ -string_width (xfont, a->label, 0) / 2,
+ -xfont->descent,
+ NULL);
+ {
+ int j;
+ for (j = 0; j < strlen(a->label); j++)
+ glCallList (font_dlist + (int)(a->label[j]));
+ }
+# else
+ {
+ int h;
+ int w = texture_string_width (font_data, a->label, &h);
+ GLfloat s = 1.0 / h;
+ GLfloat max = 18; /* max point size to avoid pixellated text */
+ if (h > max) s *= max/h;
+ glScalef (s, s, 1);
+ glTranslatef (-w/2, h*2/3, 0);
+ print_gl_string (mi->dpy,
+# ifdef HAVE_GLBITMAP
+ xfont, font_dlist,
+# else
+ font_data,
+# endif
+ 0, 0, 0, 0,
+ a->label, False);
+ }
+# endif
+
+ glPopMatrix();
+ }
+
+ /* More efficient to always call glEnable() with correct values
+ than to call glPushAttrib()/glPopAttrib(), since reading
+ attributes from GL does a round-trip and stalls the pipeline.
+ */
+ if (!wire)
+ glEnable (GL_LIGHTING);
+}
+
+
+static void
+pick_new_molecule (ModeInfo *mi, time_t last)
+{
+ molecule_configuration *mc = &mcs[MI_SCREEN(mi)];
+
+ if (mc->nmolecules == 1)
+ {
+ if (last != 0) return;
+ mc->which = 0;
+ }
+ else if (last == 0)
+ {
+ mc->which = random() % mc->nmolecules;
+ }
+ else
+ {
+ int n = mc->which;
+ while (n == mc->which)
+ n = random() % mc->nmolecules;
+ mc->which = n;
+ }
+
+ if (verbose_p)
+ {
+ char *name = strdup (mc->molecules[mc->which].label);
+ char *s = strpbrk (name, "\r\n");
+ if (s) *s = 0;
+ fprintf (stderr, "%s: drawing %s (%d)\n", progname, name, mc->which);
+ free (name);
+ }
+
+ mc->polygon_count = 0;
+
+ glNewList (mc->molecule_dlist, GL_COMPILE);
+ ensure_bounding_box_visible (mi);
+
+ do_labels = orig_do_labels;
+ do_atoms = orig_do_atoms;
+ do_bonds = orig_do_bonds;
+ do_shells = orig_do_shells;
+ MI_IS_WIREFRAME(mi) = orig_wire;
+
+ if (mc->molecule_size > mc->no_label_threshold)
+ do_labels = 0;
+ if (mc->molecule_size > mc->wireframe_threshold)
+ MI_IS_WIREFRAME(mi) = 1;
+
+ if (MI_IS_WIREFRAME(mi))
+ do_bonds = 1, do_shells = 0;
+
+ if (!do_bonds)
+ do_shells = 0;
+
+ if (! (do_bonds || do_atoms || do_labels))
+ {
+ /* Make sure *something* shows up! */
+ MI_IS_WIREFRAME(mi) = 1;
+ do_bonds = 1;
+ }
+
+ build_molecule (mi, False);
+ glEndList();
+
+ if (do_shells)
+ {
+ glNewList (mc->shell_dlist, GL_COMPILE);
+ ensure_bounding_box_visible (mi);
+
+ do_labels = 0;
+ do_atoms = 1;
+ do_bonds = 0;
+
+ build_molecule (mi, True);
+
+ glEndList();
+ do_bonds = orig_do_bonds;
+ do_atoms = orig_do_atoms;
+ do_labels = orig_do_labels;
+ }
+}
+
+
+ENTRYPOINT void