[comp.sys.sgi] lookat

thant@horus.sgi.com (Thant Tessman) (08/30/89)

Howdy,

The 'lookat' command on the IRIS assumes that 'y' is up.  Most people 
want 'z' to be up.  I think the PHIGS+ specification allows any vector
to be up.  So, I've written a routine that works like the 'lookat'
command, except that instead of twist, you specify the x, y, and z 
components of an up vector.

The results are undefined when the up vector and the look along vector
are parallel.

Be sure to prototype the function at the top of the file you wish to
use it in.

Enjoy--

thant@sgi.com


=============================== cut here =============================


#include "math.h"
#include "gl.h"

#define X 0
#define Y 1
#define Z 2

void normalize(float v*);
void cross(float *result, float *v1, float *v2);

void upat(float vx, float vy, float vz,
          float px, float py, float pz,
          float ux, float uy, float uz) {

    int i;
    float forward[3], side[3], up[3];
    float m[4][4];

    forward[X] = px - vx;
    forward[Y] = py - vy;
    forward[Z] = pz - vz;

    up[X] = ux;	    /* temporarily use view-up to hold world-up */
    up[Y] = uy;
    up[Z] = uz;

    normalize(forward);

    /* make side from view forward and world up */
    cross(side, forward, up);
    normalize(side);

    /* make view up from view forward and view side */
    cross(up, side, forward);

    m[0][0] = side[X];
    m[1][0] = side[Y];
    m[2][0] = side[Z];
    m[3][0] = 0.0;

    m[0][1] = up[X];
    m[1][1] = up[Y];
    m[2][1] = up[Z];
    m[3][1] = 0.0;

    m[0][2] = -forward[X];
    m[1][2] = -forward[Y];
    m[2][2] = -forward[Z];
    m[3][2] = 0.0;

    m[0][3] = 0.0;
    m[1][3] = 0.0;
    m[2][3] = 0.0;
    m[3][3] = 1.0;

    multmatrix(m);
    translate(-vx, -vy, -vz);

}


void normalize(float *v) {

    float r;

    r = sqrt( v[X]*v[X] + v[Y]*v[Y] + v[Z]*v[Z] );

    v[X] /= r;
    v[Y] /= r;
    v[Z] /= r;
}


void cross(float *result, float *v1, float *v2) {

    result[X] = v1[Y]*v2[Z] - v1[Z]*v2[Y];
    result[Y] = v1[Z]*v2[X] - v1[X]*v2[Z];
    result[Z] = v1[X]*v2[Y] - v1[Y]*v2[X];
}
--
-----------------------------------------------------------------------------
Let us say, then, to summarize, that a mythology is an organization of images
conceived as a rendition of the sense of life, and that this sense is to be
apprehended in two ways, namely: 1) the way of thought, and 2) the way of 
experience.  As thought mythology approaches -or is a primative prelude to-
science; and as expierience it is precicely art.  -  Joseph Campbell
-----------------------------------------------------------------------------