+
+static void sball_motion(int x, int y, int z)
+{
+ sball_pos.x += (float)x * 0.001;
+ sball_pos.y += (float)y * 0.001;
+ sball_pos.z -= (float)z * 0.001;
+ sball_update_pending = true;
+}
+
+static void sball_rotate(int x, int y, int z)
+{
+ Vec3 axis = Vec3(x, y, -z);
+ float axis_len = length(axis);
+ if(axis_len > 0.0f) {
+ Quat q;
+ q.set_rotation(axis / axis_len, axis_len * 0.001);
+ sball_rot = q * sball_rot;
+ sball_update_pending = true;
+ }
+}
+
+static void sball_button(int bn, int st)
+{
+ if(st != GLUT_DOWN) return;
+
+ sball_pos = Vec3(0, 0, 0);
+ sball_rot = Quat::identity;
+ sball_xform = Mat4::identity;
+}
+
+static unsigned int gen_grad_tex(int sz, const Vec3 &c0, const Vec3 &c1)
+{
+ unsigned char *pixels = new unsigned char[sz * 3];
+ for(int i=0; i<sz; i++) {
+ float t = (float)i / (float)(sz - 1);
+ Vec3 color = c0 + (c1 - c0) * t;
+ pixels[i * 3] = color.x * 255;
+ pixels[i * 3 + 1] = color.y * 255;
+ pixels[i * 3 + 2] = color.z * 255;
+ }
+
+ unsigned int tex;
+ glGenTextures(1, &tex);
+ glBindTexture(GL_TEXTURE_1D, tex);
+
+ glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+
+ glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, sz, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
+
+ delete [] pixels;
+
+ return tex;
+}
+
+static void draw_text(const char *text, int x, int y, float sz, const Vec3 &color)
+{
+ glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POINT_BIT);
+
+ glDisable(GL_DEPTH_TEST);
+ glDisable(GL_LIGHTING);
+ glLineWidth(3);
+ glPointSize(3);
+
+ glMatrixMode(GL_PROJECTION);
+ glPushMatrix();
+ glLoadIdentity();
+ glOrtho(0, win_width, 0, win_height, -1, 1);
+
+ glMatrixMode(GL_MODELVIEW);
+ glPushMatrix();
+ glLoadIdentity();
+ glTranslatef(x, y, 0);
+ glScalef(0.1 * sz, 0.1 * sz, 1);
+
+ glColor3f(color.x, color.y, color.z);
+ while(*text != '\0') {
+ glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, *text);
+ text++;
+ }
+
+ glMatrixMode(GL_PROJECTION);
+ glPopMatrix();
+
+ glMatrixMode(GL_MODELVIEW);
+ glPopMatrix();
+
+ glPopAttrib();
+}
+
+static void update_sball_matrix()
+{
+ Mat4 rot = sball_rot.calc_matrix();
+ //rot.transpose();
+
+ Mat4 trans;
+ trans.translation(sball_pos);
+
+ sball_xform = rot * trans;
+}