Blame test/trackball.c

Packit 328d5c
/*
Packit 328d5c
 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
Packit 328d5c
 * ALL RIGHTS RESERVED
Packit 328d5c
 * Permission to use, copy, modify, and distribute this software for
Packit 328d5c
 * any purpose and without fee is hereby granted, provided that the above
Packit 328d5c
 * copyright notice appear in all copies and that both the copyright notice
Packit 328d5c
 * and this permission notice appear in supporting documentation, and that
Packit 328d5c
 * the name of Silicon Graphics, Inc. not be used in advertising
Packit 328d5c
 * or publicity pertaining to distribution of the software without specific,
Packit 328d5c
 * written prior permission.
Packit 328d5c
 *
Packit 328d5c
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
Packit 328d5c
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
Packit 328d5c
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
Packit 328d5c
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
Packit 328d5c
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
Packit 328d5c
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
Packit 328d5c
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
Packit 328d5c
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
Packit 328d5c
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
Packit 328d5c
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
Packit 328d5c
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
Packit 328d5c
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
Packit 328d5c
 *
Packit 328d5c
 * US Government Users Restricted Rights
Packit 328d5c
 * Use, duplication, or disclosure by the Government is subject to
Packit 328d5c
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
Packit 328d5c
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
Packit 328d5c
 * clause at DFARS 252.227-7013 and/or in similar or successor
Packit 328d5c
 * clauses in the FAR or the DOD or NASA FAR Supplement.
Packit 328d5c
 * Unpublished-- rights reserved under the copyright laws of the
Packit 328d5c
 * United States.  Contractor/manufacturer is Silicon Graphics,
Packit 328d5c
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
Packit 328d5c
 *
Packit 328d5c
 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
Packit 328d5c
 */
Packit 328d5c
/*
Packit 328d5c
 * Trackball code:
Packit 328d5c
 *
Packit 328d5c
 * Implementation of a virtual trackball.
Packit 328d5c
 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
Packit 328d5c
 *   the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
Packit 328d5c
 *
Packit 328d5c
 * Vector manip code:
Packit 328d5c
 *
Packit 328d5c
 * Original code from:
Packit 328d5c
 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
Packit 328d5c
 *
Packit 328d5c
 * Much mucking with by:
Packit 328d5c
 * Gavin Bell
Packit 328d5c
 */
Packit 328d5c
#include <math.h>
Packit 328d5c
#include "trackball.h"
Packit 328d5c
Packit 328d5c
/*
Packit 328d5c
 * This size should really be based on the distance from the center of
Packit 328d5c
 * rotation to the point on the object underneath the mouse.  That
Packit 328d5c
 * point would then track the mouse as closely as possible.  This is a
Packit 328d5c
 * simple example, though, so that is left as an Exercise for the
Packit 328d5c
 * Programmer.
Packit 328d5c
 */
Packit 328d5c
#define TRACKBALLSIZE  (0.8)
Packit 328d5c
Packit 328d5c
/*
Packit 328d5c
 * Local function prototypes (not defined in trackball.h)
Packit 328d5c
 */
Packit 328d5c
static float tb_project_to_sphere(float, float, float);
Packit 328d5c
static void normalize_quat(float [4]);
Packit 328d5c
Packit 328d5c
void
Packit 328d5c
vzero(float *v)
Packit 328d5c
{
Packit 328d5c
    v[0] = 0.0;
Packit 328d5c
    v[1] = 0.0;
Packit 328d5c
    v[2] = 0.0;
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
void
Packit 328d5c
vset(float *v, float x, float y, float z)
Packit 328d5c
{
Packit 328d5c
    v[0] = x;
Packit 328d5c
    v[1] = y;
Packit 328d5c
    v[2] = z;
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
void
Packit 328d5c
vsub(const float *src1, const float *src2, float *dst)
Packit 328d5c
{
Packit 328d5c
    dst[0] = src1[0] - src2[0];
Packit 328d5c
    dst[1] = src1[1] - src2[1];
Packit 328d5c
    dst[2] = src1[2] - src2[2];
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
void
Packit 328d5c
vcopy(const float *v1, float *v2)
Packit 328d5c
{
Packit 328d5c
    int i;
Packit 328d5c
    for (i = 0 ; i < 3 ; i++)
Packit 328d5c
        v2[i] = v1[i];
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
void
Packit 328d5c
vcross(const float *v1, const float *v2, float *cross)
Packit 328d5c
{
Packit 328d5c
    float temp[3];
Packit 328d5c
Packit 328d5c
    temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
Packit 328d5c
    temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
Packit 328d5c
    temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
Packit 328d5c
    vcopy(temp, cross);
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
float
Packit 328d5c
vlength(const float *v)
Packit 328d5c
{
Packit 328d5c
    return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
void
Packit 328d5c
vscale(float *v, float div)
Packit 328d5c
{
Packit 328d5c
    v[0] *= div;
Packit 328d5c
    v[1] *= div;
Packit 328d5c
    v[2] *= div;
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
void
Packit 328d5c
vnormal(float *v)
Packit 328d5c
{
Packit 328d5c
    vscale(v,1.0/vlength(v));
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
float
Packit 328d5c
vdot(const float *v1, const float *v2)
Packit 328d5c
{
Packit 328d5c
    return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
void
Packit 328d5c
vadd(const float *src1, const float *src2, float *dst)
Packit 328d5c
{
Packit 328d5c
    dst[0] = src1[0] + src2[0];
Packit 328d5c
    dst[1] = src1[1] + src2[1];
Packit 328d5c
    dst[2] = src1[2] + src2[2];
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
/*
Packit 328d5c
 * Ok, simulate a track-ball.  Project the points onto the virtual
Packit 328d5c
 * trackball, then figure out the axis of rotation, which is the cross
Packit 328d5c
 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
Packit 328d5c
 * Note:  This is a deformed trackball-- is a trackball in the center,
Packit 328d5c
 * but is deformed into a hyperbolic sheet of rotation away from the
Packit 328d5c
 * center.  This particular function was chosen after trying out
Packit 328d5c
 * several variations.
Packit 328d5c
 *
Packit 328d5c
 * It is assumed that the arguments to this routine are in the range
Packit 328d5c
 * (-1.0 ... 1.0)
Packit 328d5c
 */
Packit 328d5c
void
Packit 328d5c
trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
Packit 328d5c
{
Packit 328d5c
    float a[3]; /* Axis of rotation */
Packit 328d5c
    float phi;  /* how much to rotate about axis */
Packit 328d5c
    float p1[3], p2[3], d[3];
Packit 328d5c
    float t;
Packit 328d5c
Packit 328d5c
    if (p1x == p2x && p1y == p2y) {
Packit 328d5c
        /* Zero rotation */
Packit 328d5c
        vzero(q);
Packit 328d5c
        q[3] = 1.0;
Packit 328d5c
        return;
Packit 328d5c
    }
Packit 328d5c
Packit 328d5c
    /*
Packit 328d5c
     * First, figure out z-coordinates for projection of P1 and P2 to
Packit 328d5c
     * deformed sphere
Packit 328d5c
     */
Packit 328d5c
    vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
Packit 328d5c
    vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
Packit 328d5c
Packit 328d5c
    /*
Packit 328d5c
     *  Now, we want the cross product of P1 and P2
Packit 328d5c
     */
Packit 328d5c
    vcross(p2,p1,a);
Packit 328d5c
Packit 328d5c
    /*
Packit 328d5c
     *  Figure out how much to rotate around that axis.
Packit 328d5c
     */
Packit 328d5c
    vsub(p1,p2,d);
Packit 328d5c
    t = vlength(d) / (2.0*TRACKBALLSIZE);
Packit 328d5c
Packit 328d5c
    /*
Packit 328d5c
     * Avoid problems with out-of-control values...
Packit 328d5c
     */
Packit 328d5c
    if (t > 1.0) t = 1.0;
Packit 328d5c
    if (t < -1.0) t = -1.0;
Packit 328d5c
    phi = 2.0 * asin(t);
Packit 328d5c
Packit 328d5c
    axis_to_quat(a,phi,q);
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
/*
Packit 328d5c
 *  Given an axis and angle, compute quaternion.
Packit 328d5c
 */
Packit 328d5c
void
Packit 328d5c
axis_to_quat(float a[3], float phi, float q[4])
Packit 328d5c
{
Packit 328d5c
    vnormal(a);
Packit 328d5c
    vcopy(a,q);
Packit 328d5c
    vscale(q,sin(phi/2.0));
Packit 328d5c
    q[3] = cos(phi/2.0);
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
/*
Packit 328d5c
 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
Packit 328d5c
 * if we are away from the center of the sphere.
Packit 328d5c
 */
Packit 328d5c
static float
Packit 328d5c
tb_project_to_sphere(float r, float x, float y)
Packit 328d5c
{
Packit 328d5c
    float d, t, z;
Packit 328d5c
Packit 328d5c
    d = sqrt(x*x + y*y);
Packit 328d5c
    if (d < r * 0.70710678118654752440) {    /* Inside sphere */
Packit 328d5c
        z = sqrt(r*r - d*d);
Packit 328d5c
    } else {           /* On hyperbola */
Packit 328d5c
        t = r / 1.41421356237309504880;
Packit 328d5c
        z = t*t / d;
Packit 328d5c
    }
Packit 328d5c
    return z;
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
/*
Packit 328d5c
 * Given two rotations, e1 and e2, expressed as quaternion rotations,
Packit 328d5c
 * figure out the equivalent single rotation and stuff it into dest.
Packit 328d5c
 *
Packit 328d5c
 * This routine also normalizes the result every RENORMCOUNT times it is
Packit 328d5c
 * called, to keep error from creeping in.
Packit 328d5c
 *
Packit 328d5c
 * NOTE: This routine is written so that q1 or q2 may be the same
Packit 328d5c
 * as dest (or each other).
Packit 328d5c
 */
Packit 328d5c
Packit 328d5c
#define RENORMCOUNT 97
Packit 328d5c
Packit 328d5c
void
Packit 328d5c
add_quats(float q1[4], float q2[4], float dest[4])
Packit 328d5c
{
Packit 328d5c
    static int count=0;
Packit 328d5c
    float t1[4], t2[4], t3[4];
Packit 328d5c
    float tf[4];
Packit 328d5c
Packit 328d5c
    vcopy(q1,t1);
Packit 328d5c
    vscale(t1,q2[3]);
Packit 328d5c
Packit 328d5c
    vcopy(q2,t2);
Packit 328d5c
    vscale(t2,q1[3]);
Packit 328d5c
Packit 328d5c
    vcross(q2,q1,t3);
Packit 328d5c
    vadd(t1,t2,tf);
Packit 328d5c
    vadd(t3,tf,tf);
Packit 328d5c
    tf[3] = q1[3] * q2[3] - vdot(q1,q2);
Packit 328d5c
Packit 328d5c
    dest[0] = tf[0];
Packit 328d5c
    dest[1] = tf[1];
Packit 328d5c
    dest[2] = tf[2];
Packit 328d5c
    dest[3] = tf[3];
Packit 328d5c
Packit 328d5c
    if (++count > RENORMCOUNT) {
Packit 328d5c
        count = 0;
Packit 328d5c
        normalize_quat(dest);
Packit 328d5c
    }
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
/*
Packit 328d5c
 * Quaternions always obey:  a^2 + b^2 + c^2 + d^2 = 1.0
Packit 328d5c
 * If they don't add up to 1.0, dividing by their magnitued will
Packit 328d5c
 * renormalize them.
Packit 328d5c
 *
Packit 328d5c
 * Note: See the following for more information on quaternions:
Packit 328d5c
 *
Packit 328d5c
 * - Shoemake, K., Animating rotation with quaternion curves, Computer
Packit 328d5c
 *   Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
Packit 328d5c
 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
Packit 328d5c
 *   graphics, The Visual Computer 5, 2-13, 1989.
Packit 328d5c
 */
Packit 328d5c
static void
Packit 328d5c
normalize_quat(float q[4])
Packit 328d5c
{
Packit 328d5c
    int i;
Packit 328d5c
    float mag;
Packit 328d5c
Packit 328d5c
    mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
Packit 328d5c
    for (i = 0; i < 4; i++) q[i] /= mag;
Packit 328d5c
}
Packit 328d5c
Packit 328d5c
/*
Packit 328d5c
 * Build a rotation matrix, given a quaternion rotation.
Packit 328d5c
 *
Packit 328d5c
 */
Packit 328d5c
void
Packit 328d5c
build_rotmatrix(float m[4][4], float q[4])
Packit 328d5c
{
Packit 328d5c
    m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
Packit 328d5c
    m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
Packit 328d5c
    m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
Packit 328d5c
    m[0][3] = 0.0;
Packit 328d5c
Packit 328d5c
    m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
Packit 328d5c
    m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
Packit 328d5c
    m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
Packit 328d5c
    m[1][3] = 0.0;
Packit 328d5c
Packit 328d5c
    m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
Packit 328d5c
    m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
Packit 328d5c
    m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
Packit 328d5c
    m[2][3] = 0.0;
Packit 328d5c
Packit 328d5c
    m[3][0] = 0.0;
Packit 328d5c
    m[3][1] = 0.0;
Packit 328d5c
    m[3][2] = 0.0;
Packit 328d5c
    m[3][3] = 1.0;
Packit 328d5c
}
Packit 328d5c