ds2.9a12/bin/
ds2.9a12/extra/
ds2.9a12/extra/crat/
ds2.9a12/extra/creremote/
ds2.9a12/extra/mingw/
ds2.9a12/extra/wolfpaw/
ds2.9a12/fluffos-2.14-ds13/
ds2.9a12/fluffos-2.14-ds13/Win32/
ds2.9a12/fluffos-2.14-ds13/compat/
ds2.9a12/fluffos-2.14-ds13/compat/simuls/
ds2.9a12/fluffos-2.14-ds13/include/
ds2.9a12/fluffos-2.14-ds13/testsuite/
ds2.9a12/fluffos-2.14-ds13/testsuite/clone/
ds2.9a12/fluffos-2.14-ds13/testsuite/command/
ds2.9a12/fluffos-2.14-ds13/testsuite/data/
ds2.9a12/fluffos-2.14-ds13/testsuite/etc/
ds2.9a12/fluffos-2.14-ds13/testsuite/include/
ds2.9a12/fluffos-2.14-ds13/testsuite/inherit/
ds2.9a12/fluffos-2.14-ds13/testsuite/inherit/master/
ds2.9a12/fluffos-2.14-ds13/testsuite/log/
ds2.9a12/fluffos-2.14-ds13/testsuite/single/
ds2.9a12/fluffos-2.14-ds13/testsuite/single/tests/compiler/
ds2.9a12/fluffos-2.14-ds13/testsuite/single/tests/efuns/
ds2.9a12/fluffos-2.14-ds13/testsuite/single/tests/operators/
ds2.9a12/fluffos-2.14-ds13/testsuite/u/
ds2.9a12/lib/cmds/admins/
ds2.9a12/lib/cmds/common/
ds2.9a12/lib/cmds/creators/include/
ds2.9a12/lib/daemon/services/
ds2.9a12/lib/daemon/tmp/
ds2.9a12/lib/doc/
ds2.9a12/lib/doc/bguide/
ds2.9a12/lib/doc/efun/all/
ds2.9a12/lib/doc/efun/arrays/
ds2.9a12/lib/doc/efun/buffers/
ds2.9a12/lib/doc/efun/compile/
ds2.9a12/lib/doc/efun/floats/
ds2.9a12/lib/doc/efun/functions/
ds2.9a12/lib/doc/efun/general/
ds2.9a12/lib/doc/efun/mixed/
ds2.9a12/lib/doc/efun/numbers/
ds2.9a12/lib/doc/efun/parsing/
ds2.9a12/lib/doc/hbook/
ds2.9a12/lib/doc/help/classes/
ds2.9a12/lib/doc/help/races/
ds2.9a12/lib/doc/lfun/
ds2.9a12/lib/doc/lfun/all/
ds2.9a12/lib/doc/lfun/lib/abilities/
ds2.9a12/lib/doc/lfun/lib/armor/
ds2.9a12/lib/doc/lfun/lib/bank/
ds2.9a12/lib/doc/lfun/lib/bot/
ds2.9a12/lib/doc/lfun/lib/clay/
ds2.9a12/lib/doc/lfun/lib/clean/
ds2.9a12/lib/doc/lfun/lib/clerk/
ds2.9a12/lib/doc/lfun/lib/client/
ds2.9a12/lib/doc/lfun/lib/combat/
ds2.9a12/lib/doc/lfun/lib/connect/
ds2.9a12/lib/doc/lfun/lib/container/
ds2.9a12/lib/doc/lfun/lib/corpse/
ds2.9a12/lib/doc/lfun/lib/creator/
ds2.9a12/lib/doc/lfun/lib/daemon/
ds2.9a12/lib/doc/lfun/lib/damage/
ds2.9a12/lib/doc/lfun/lib/deterioration/
ds2.9a12/lib/doc/lfun/lib/donate/
ds2.9a12/lib/doc/lfun/lib/door/
ds2.9a12/lib/doc/lfun/lib/equip/
ds2.9a12/lib/doc/lfun/lib/file/
ds2.9a12/lib/doc/lfun/lib/fish/
ds2.9a12/lib/doc/lfun/lib/fishing/
ds2.9a12/lib/doc/lfun/lib/flashlight/
ds2.9a12/lib/doc/lfun/lib/follow/
ds2.9a12/lib/doc/lfun/lib/ftp_client/
ds2.9a12/lib/doc/lfun/lib/ftp_data_connection/
ds2.9a12/lib/doc/lfun/lib/fuel/
ds2.9a12/lib/doc/lfun/lib/furnace/
ds2.9a12/lib/doc/lfun/lib/genetics/
ds2.9a12/lib/doc/lfun/lib/holder/
ds2.9a12/lib/doc/lfun/lib/id/
ds2.9a12/lib/doc/lfun/lib/interactive/
ds2.9a12/lib/doc/lfun/lib/lamp/
ds2.9a12/lib/doc/lfun/lib/leader/
ds2.9a12/lib/doc/lfun/lib/light/
ds2.9a12/lib/doc/lfun/lib/limb/
ds2.9a12/lib/doc/lfun/lib/living/
ds2.9a12/lib/doc/lfun/lib/load/
ds2.9a12/lib/doc/lfun/lib/look/
ds2.9a12/lib/doc/lfun/lib/manipulate/
ds2.9a12/lib/doc/lfun/lib/meal/
ds2.9a12/lib/doc/lfun/lib/messages/
ds2.9a12/lib/doc/lfun/lib/player/
ds2.9a12/lib/doc/lfun/lib/poison/
ds2.9a12/lib/doc/lfun/lib/position/
ds2.9a12/lib/doc/lfun/lib/post_office/
ds2.9a12/lib/doc/lfun/lib/potion/
ds2.9a12/lib/doc/lfun/lib/room/
ds2.9a12/lib/doc/lfun/lib/server/
ds2.9a12/lib/doc/lfun/lib/spell/
ds2.9a12/lib/doc/lfun/lib/torch/
ds2.9a12/lib/doc/lfun/lib/vendor/
ds2.9a12/lib/doc/lfun/lib/virt_sky/
ds2.9a12/lib/doc/lfun/lib/weapon/
ds2.9a12/lib/doc/lfun/lib/worn_storage/
ds2.9a12/lib/doc/lpc/basic/
ds2.9a12/lib/doc/lpc/concepts/
ds2.9a12/lib/doc/lpc/constructs/
ds2.9a12/lib/doc/lpc/etc/
ds2.9a12/lib/doc/lpc/intermediate/
ds2.9a12/lib/doc/lpc/types/
ds2.9a12/lib/doc/misc/
ds2.9a12/lib/doc/old/
ds2.9a12/lib/domains/
ds2.9a12/lib/domains/Praxis/adm/
ds2.9a12/lib/domains/Praxis/attic/
ds2.9a12/lib/domains/Praxis/cemetery/mon/
ds2.9a12/lib/domains/Praxis/data/
ds2.9a12/lib/domains/Praxis/death/
ds2.9a12/lib/domains/Praxis/mountains/
ds2.9a12/lib/domains/Praxis/obj/armour/
ds2.9a12/lib/domains/Praxis/obj/magic/
ds2.9a12/lib/domains/Praxis/obj/weapon/
ds2.9a12/lib/domains/Praxis/orc_valley/
ds2.9a12/lib/domains/Ylsrim/
ds2.9a12/lib/domains/Ylsrim/adm/
ds2.9a12/lib/domains/Ylsrim/armor/
ds2.9a12/lib/domains/Ylsrim/broken/
ds2.9a12/lib/domains/Ylsrim/fish/
ds2.9a12/lib/domains/Ylsrim/meal/
ds2.9a12/lib/domains/Ylsrim/npc/
ds2.9a12/lib/domains/Ylsrim/obj/
ds2.9a12/lib/domains/Ylsrim/virtual/
ds2.9a12/lib/domains/Ylsrim/weapon/
ds2.9a12/lib/domains/campus/adm/
ds2.9a12/lib/domains/campus/etc/
ds2.9a12/lib/domains/campus/meals/
ds2.9a12/lib/domains/campus/save/
ds2.9a12/lib/domains/campus/txt/ai/charles/
ds2.9a12/lib/domains/campus/txt/ai/charles/bak2/
ds2.9a12/lib/domains/campus/txt/ai/charles/bak2/bak1/
ds2.9a12/lib/domains/campus/txt/ai/charly/
ds2.9a12/lib/domains/campus/txt/ai/charly/bak/
ds2.9a12/lib/domains/campus/txt/jenny/
ds2.9a12/lib/domains/cave/doors/
ds2.9a12/lib/domains/cave/etc/
ds2.9a12/lib/domains/cave/meals/
ds2.9a12/lib/domains/cave/weap/
ds2.9a12/lib/domains/default/creator/
ds2.9a12/lib/domains/default/doors/
ds2.9a12/lib/domains/default/etc/
ds2.9a12/lib/domains/default/vehicles/
ds2.9a12/lib/domains/default/virtual/
ds2.9a12/lib/domains/default/weap/
ds2.9a12/lib/domains/town/txt/shame/
ds2.9a12/lib/domains/town/virtual/
ds2.9a12/lib/domains/town/virtual/bottom/
ds2.9a12/lib/domains/town/virtual/space/
ds2.9a12/lib/estates/
ds2.9a12/lib/ftp/
ds2.9a12/lib/lib/comp/
ds2.9a12/lib/lib/daemons/
ds2.9a12/lib/lib/daemons/include/
ds2.9a12/lib/lib/lvs/
ds2.9a12/lib/lib/user/
ds2.9a12/lib/lib/virtual/
ds2.9a12/lib/log/
ds2.9a12/lib/log/adm/
ds2.9a12/lib/log/archive/
ds2.9a12/lib/log/chan/
ds2.9a12/lib/log/errors/
ds2.9a12/lib/log/law/adm/
ds2.9a12/lib/log/law/email/
ds2.9a12/lib/log/law/names/
ds2.9a12/lib/log/law/sites-misc/
ds2.9a12/lib/log/law/sites-register/
ds2.9a12/lib/log/law/sites-tempban/
ds2.9a12/lib/log/law/sites-watch/
ds2.9a12/lib/log/open/
ds2.9a12/lib/log/reports/
ds2.9a12/lib/log/router/
ds2.9a12/lib/log/secure/
ds2.9a12/lib/log/watch/
ds2.9a12/lib/obj/book_source/
ds2.9a12/lib/obj/include/
ds2.9a12/lib/powers/prayers/
ds2.9a12/lib/powers/spells/
ds2.9a12/lib/realms/template/adm/
ds2.9a12/lib/realms/template/area/armor/
ds2.9a12/lib/realms/template/area/npc/
ds2.9a12/lib/realms/template/area/obj/
ds2.9a12/lib/realms/template/area/room/
ds2.9a12/lib/realms/template/area/weap/
ds2.9a12/lib/realms/template/bak/
ds2.9a12/lib/realms/template/cmds/
ds2.9a12/lib/save/kills/o/
ds2.9a12/lib/secure/cfg/classes/
ds2.9a12/lib/secure/cmds/builders/
ds2.9a12/lib/secure/cmds/creators/include/
ds2.9a12/lib/secure/cmds/players/
ds2.9a12/lib/secure/cmds/players/include/
ds2.9a12/lib/secure/daemon/imc2server/
ds2.9a12/lib/secure/daemon/include/
ds2.9a12/lib/secure/lib/
ds2.9a12/lib/secure/lib/include/
ds2.9a12/lib/secure/lib/net/include/
ds2.9a12/lib/secure/lib/std/
ds2.9a12/lib/secure/log/adm/
ds2.9a12/lib/secure/log/bak/
ds2.9a12/lib/secure/log/intermud/
ds2.9a12/lib/secure/log/network/
ds2.9a12/lib/secure/modules/
ds2.9a12/lib/secure/npc/
ds2.9a12/lib/secure/obj/include/
ds2.9a12/lib/secure/room/
ds2.9a12/lib/secure/save/
ds2.9a12/lib/secure/save/backup/
ds2.9a12/lib/secure/save/boards/
ds2.9a12/lib/secure/tmp/
ds2.9a12/lib/secure/upgrades/files/
ds2.9a12/lib/secure/verbs/creators/
ds2.9a12/lib/std/board/
ds2.9a12/lib/std/lib/
ds2.9a12/lib/tmp/
ds2.9a12/lib/verbs/admins/include/
ds2.9a12/lib/verbs/builders/
ds2.9a12/lib/verbs/common/
ds2.9a12/lib/verbs/common/include/
ds2.9a12/lib/verbs/creators/
ds2.9a12/lib/verbs/creators/include/
ds2.9a12/lib/verbs/rooms/
ds2.9a12/lib/verbs/rooms/include/
ds2.9a12/lib/www/client/
ds2.9a12/lib/www/errors/
ds2.9a12/lib/www/images/
ds2.9a12/lib/www/lpmuds/downloads_files/
ds2.9a12/lib/www/lpmuds/intermud_files/
ds2.9a12/lib/www/lpmuds/links_files/
ds2.9a12/win32/
/*
 *  matrix.c -- matrix efuns.
 *              2-93 : Dwayne Fontenot : original coding.
 */

#include <math.h>

#ifdef LATTICE
#include "/lpc_incl.h"
#else
#include "../lpc_incl.h"
#include "../efun_protos.h"
#endif

#include "matrix.h"

static Matrix identity =
{1., 0., 0., 0.,
 0., 1., 0., 0.,
 0., 0., 1., 0.,
 0., 0., 0., 1.};

static void print_matrix (Matrix, char *);
static void print_array (Vector *, char *);
static Vector *normalize_array (Vector *);
static Vector *cross_product (Vector *, Vector *, Vector *);
static Vector *points_to_array (Vector *, Vector *, Vector *);

void f_id_matrix (void)
{
    array_t *matrix;
    int i;

    matrix = allocate_empty_array(16);
    for (i = 0; i < 16; i++) {
        matrix->item[i].type = T_REAL;
        matrix->item[i].u.real = identity[i];
    }
    push_refed_array(matrix);
}

void f_translate (void)
{
    array_t *matrix;
    double x, y, z;
    Matrix current_matrix;
    Matrix trans_matrix;
    Matrix final_matrix;
    int i;

    if ((sp - 1)->type != T_REAL) {
	bad_arg(3, F_TRANSLATE);
    }
    if (sp->type != T_REAL) {
	bad_arg(4, F_TRANSLATE);
    }
    /*
     * get arguments from stack.
     */
    matrix = (sp - 3)->u.arr;
    x = (sp - 2)->u.real;
    y = (sp - 1)->u.real;
    z = sp->u.real;
    sp -= 3;

    /*
     * convert vec matrix to float matrix.
     */
    for (i = 0; i < 16; i++) {
	current_matrix[i] = matrix->item[i].u.real;
    }
    /*
     * create translation matrix.
     */
    translate_matrix(x, y, z, trans_matrix);
    /*
     * compute transformed matrix.
     */
    mult_matrix(current_matrix, trans_matrix, final_matrix);
    /*
     * convert float matrix to vec matrix.
     */
    for (i = 0; i < 16; i++) {
	matrix->item[i].u.real = final_matrix[i];
    }
}

void f_scale (void)
{
    array_t *matrix;
    double x, y, z;
    Matrix current_matrix;
    Matrix scaling_matrix;
    Matrix final_matrix;
    int i;

    if ((sp - 1)->type != T_REAL) {
	bad_arg(3, F_SCALE);
    }
    if (sp->type != T_REAL) {
	bad_arg(4, F_SCALE);
    }
    /*
     * get arguments from stack.
     */
    matrix = (sp - 3)->u.arr;
    x = (sp - 2)->u.real;
    y = (sp - 1)->u.real;
    z = sp->u.real;
    sp -= 3;
    /*
     * convert vec matrix to float matrix.
     */
    for (i = 0; i < 16; i++) {
	current_matrix[i] = matrix->item[i].u.real;
    }
    /*
     * create scaling matrix.
     */
    scale_matrix(x, y, z, scaling_matrix);
    /*
     * compute transformed matrix.
     */
    mult_matrix(current_matrix, scaling_matrix, final_matrix);
    /*
     * convert float matrix to vec matrix.
     */
    for (i = 0; i < 16; i++) {
	matrix->item[i].u.real = final_matrix[i];
    }
}

void f_rotate_x (void)
{
    array_t *matrix;
    double angle;
    Matrix current_matrix;
    Matrix rot_matrix;
    Matrix final_matrix;
    int i;

    /*
     * get arguments from stack.
     */
    matrix = (sp - 1)->u.arr;
    angle = (sp--)->u.real;
    /*
     * convert vec matrix to float matrix.
     */
    for (i = 0; i < 16; i++) {
	current_matrix[i] = matrix->item[i].u.real;
    }
    /*
     * create x rotation matrix.
     */
    rotate_x_matrix(angle, rot_matrix);
    /*
     * compute transformed matrix.
     */
    mult_matrix(current_matrix, rot_matrix, final_matrix);
    /*
     * convert float matrix to vec matrix.
     */
    for (i = 0; i < 16; i++) {
	matrix->item[i].u.real = final_matrix[i];
    }
}

void f_rotate_y (void)
{
    array_t *matrix;
    double angle;
    Matrix current_matrix;
    Matrix rot_matrix;
    Matrix final_matrix;
    int i;

    /*
     * get arguments from stack.
     */
    matrix = (sp - 1)->u.arr;
    angle = (sp--)->u.real;
    /*
     * convert vec matrix to float matrix.
     */
    for (i = 0; i < 16; i++) {
	current_matrix[i] = matrix->item[i].u.real;
    }
    /*
     * create y rotation matrix.
     */
    rotate_y_matrix(angle, rot_matrix);
    /*
     * compute transformed matrix.
     */
    mult_matrix(current_matrix, rot_matrix, final_matrix);
    /*
     * convert float matrix to vec matrix.
     */
    for (i = 0; i < 16; i++) {
	matrix->item[i].u.real = final_matrix[i];
    }
}

void f_rotate_z (void)
{
    array_t *matrix;
    double angle;
    Matrix current_matrix;
    Matrix rot_matrix;
    Matrix final_matrix;
    int i;

    /*
     * get arguments from stack.
     */
    matrix = (sp - 1)->u.arr;
    angle = (sp--)->u.real;
    /*
     * convert vec matrix to float matrix.
     */
    for (i = 0; i < 16; i++) {
	current_matrix[i] = matrix->item[i].u.real;
    }
    /*
     * create z rotation matrix.
     */
    rotate_z_matrix(angle, rot_matrix);
    /*
     * compute transformed matrix.
     */
    mult_matrix(current_matrix, rot_matrix, final_matrix);
    /*
     * convert float matrix to vec matrix.
     */
    for (i = 0; i < 16; i++) {
	matrix->item[i].u.real = final_matrix[i];
    }
}

void f_lookat_rotate (void)
{
    array_t *matrix;
    double x, y, z;
    Matrix current_matrix;
    Matrix lookat_matrix;
    int i;

    if ((sp - 1)->type != T_REAL) {
	bad_arg(3, F_LOOKAT_ROTATE);
    }
    if (sp->type != T_REAL) {
	bad_arg(4, F_LOOKAT_ROTATE);
    }
    /*
     * get arguments from stack.
     */
    matrix = (sp - 3)->u.arr;
    x = (sp - 2)->u.real;
    y = (sp - 1)->u.real;
    z = sp->u.real;
    sp -= 3;
    /*
     * convert vec matrix to float matrix.
     */
    for (i = 0; i < 16; i++) {
	current_matrix[i] = matrix->item[i].u.real;
    }
    /*
     * create new viewing transformation matrix.
     */
    lookat_rotate(current_matrix, x, y, z, lookat_matrix);
    /*
     * convert float matrix to vec matrix.
     */
    for (i = 0; i < 16; i++) {
	matrix->item[i].u.real = lookat_matrix[i];
    }
}

#ifdef F_LOOKAT_ROTATE2
void f_lookat_rotate2 (void)
{
    array_t *matrix;
    double ex, ey, ez, lx, ly, lz;
    Matrix current_matrix;
    Matrix lookat_matrix;
    int i, j;

    for (j = 4; j >= 0; j--) {
	if ((sp - j)->type != T_REAL) {
	    bad_arg(7 - j, F_LOOKAT_ROTATE2);
	}
    }
    /*
     * get arguments from stack.
     */
    matrix = (sp - 6)->u.arr;
    ex = (sp - 5)->u.real;
    ey = (sp - 4)->u.real;
    ez = (sp - 3)->u.real;
    lx = (sp - 2)->u.real;
    ly = (sp - 1)->u.real;
    lz = sp->u.real;
    sp -= 5;
    free_array((sp--)->u.arr);

    /*
     * convert vec matrix to float matrix.
     */
    for (i = 0; i < 16; i++) {
	current_matrix[i] = matrix->item[i].u.real;
    }
    /*
     * create new viewing transformation matrix.
     */
    lookat_rotate2(ex, ey, ez, lx, ly, lz, lookat_matrix);
    /*
     * convert float matrix to vec matrix.
     */
    for (i = 0; i < 16; i++) {
	matrix->item[i].u.real = lookat_matrix[i];
    }
}
#endif

#ifdef DEBUG
static void print_matrix (Matrix m, char * label)
{
    int i;
    int j;

    fprintf(stderr, "%s:\n", label);
    for (i = 0; i < 4; i++) {
	for (j = 0; j < 4; j++) {
	    fprintf(stderr, "%f\t", m[i * 4 + j]);
	}
	fprintf(stderr, "\n");
    }
}

static void print_array (Vector * v, char * label)
{
    fprintf(stderr, "%s:\t%f\t%f\t%f\n", label, v->x, v->y, v->z);
}
#endif

static Vector *normalize_array (Vector * v)
{
    double xx, yy, zz, mm, m;

    xx = v->x * v->x;
    yy = v->y * v->y;
    zz = v->z * v->z;
    mm = xx + yy + zz;
    m = sqrt(mm);
    if (m) {
	v->x /= m;
	v->y /= m;
	v->z /= m;
    }
    return (v);
}

static Vector *cross_product (Vector * v, Vector * va, Vector * vb)
{
    v->x = (va->y * vb->z) - (va->z * vb->y);
    v->y = (va->z * vb->x) - (va->x * vb->z);
    v->z = (va->x * vb->y) - (va->y * vb->x);
    return (v);
}

static Vector *points_to_array (Vector * v, Vector * pa, Vector * pb)
{
    v->x = pa->x - pb->x;
    v->y = pa->y - pb->y;
    v->z = pa->z - pb->z;
    return (v);
}

void lookat_rotate (Matrix T, double x, double y, double z, Matrix M)
{
    static Vector N, V, U;
    static Vector ep, lp;

    lp.x = x;
    lp.y = y;
    lp.z = z;
    ep.x = T[12];
    ep.y = T[13];
    ep.z = T[14];
    points_to_array(&N, &lp, &ep);
    normalize_array(&N);

    U.x = T[0];
    U.y = T[4];
    U.z = T[8];
    cross_product(&V, &N, &U);
    normalize_array(&V);

    cross_product(&U, &V, &N);
    normalize_array(&U);

    M[0] = U.x;
    M[1] = V.x;
    M[2] = N.x;
    M[3] = 0.;
    M[4] = U.y;
    M[5] = V.y;
    M[6] = N.y;
    M[7] = 0.;
    M[8] = U.z;
    M[9] = V.z;
    M[10] = N.z;
    M[11] = 0.;
#if 0
    M[12] = ep.x;
    M[13] = ep.y;
    M[14] = ep.z;
    M[15] = 1.;
#endif
#if 0
    M[12] = -U.x * ep.x - U.y * ep.y - U.z * ep.z;
    M[13] = -V.x * ep.x - V.y * ep.y - V.z * ep.z;
    M[14] = -N.x * ep.x - N.y * ep.y - N.z * ep.z;
#endif
    M[12] = ((U.x * ep.x) + (U.y * ep.y) + (U.z * ep.z));
    M[13] = ((V.x * ep.x) + (V.y * ep.y) + (V.z * ep.z));
    M[14] = ((N.x * ep.x) + (N.y * ep.y) + (N.z * ep.z));
    M[15] = 1.;

#ifdef DEBUG
    print_array(&lp, "look point");
    print_array(&ep, "eye point");
    print_array(&N, "normal array");
    print_array(&V, "V = N x U");
    print_array(&U, "U = V x N");
    print_matrix(M, "final matrix");
#endif				/* DEBUG */
}

void lookat_rotate2 (double ex, double ey, double ez, double lx, double ly, double lz, Matrix M)
{
    static Vector N, V, U;
    static Vector ep, lp;

    ep.x = ex;
    ep.y = ey;
    ep.z = ez;
    lp.x = lx;
    lp.y = ly;
    lp.z = lz;
    points_to_array(&N, &lp, &ep);
    normalize_array(&N);

    U.x = 0.;
    U.y = 1.;
    U.z = 0.;
    cross_product(&V, &N, &U);
    normalize_array(&V);

    cross_product(&U, &V, &N);
    normalize_array(&U);

    M[0] = U.x;
    M[1] = V.x;
    M[2] = N.x;
    M[3] = 0.;
    M[4] = U.y;
    M[5] = V.y;
    M[6] = N.y;
    M[7] = 0.;
    M[8] = U.z;
    M[9] = V.z;
    M[10] = N.z;
    M[11] = 0.;
#if 0
    M[12] = ep.x;
    M[13] = ep.y;
    M[14] = ep.z;
    M[15] = 1.;
#endif
#if 0
    M[12] = -U.x * ep.x - U.y * ep.y - U.z * ep.z;
    M[13] = -V.x * ep.x - V.y * ep.y - V.z * ep.z;
    M[14] = -N.x * ep.x - N.y * ep.y - N.z * ep.z;
#endif
    M[12] = ((U.x * ep.x) + (U.y * ep.y) + (U.z * ep.z));
    M[13] = ((V.x * ep.x) + (V.y * ep.y) + (V.z * ep.z));
    M[14] = ((N.x * ep.x) + (N.y * ep.y) + (N.z * ep.z));
    M[15] = 1.;

#ifdef DEBUG
    print_array(&lp, "look point");
    print_array(&ep, "eye point");
    print_array(&N, "normal array");
    print_array(&V, "V = N x U");
    print_array(&U, "U = V x N");
    print_matrix(M, "final matrix");
#endif				/* DEBUG */
}

void translate_matrix (double x, double y, double z, Matrix m)
{
    m[0] = 1.;
    m[1] = 0.;
    m[2] = 0.;
    m[3] = 0.;
    m[4] = 0.;
    m[5] = 1.;
    m[6] = 0.;
    m[7] = 0.;
    m[8] = 0.;
    m[9] = 0.;
    m[10] = 1.;
    m[11] = 0.;
    m[12] = x;
    m[13] = y;
    m[14] = z;
    m[15] = 1.;
}

void scale_matrix (double x, double y, double z, Matrix m)
{
    m[0] = x;
    m[1] = 0.;
    m[2] = 0.;
    m[3] = 0.;
    m[4] = 0.;
    m[5] = y;
    m[6] = 0.;
    m[7] = 0.;
    m[8] = 0.;
    m[9] = 0.;
    m[10] = z;
    m[11] = 0.;
    m[12] = 0.;
    m[13] = 0.;
    m[14] = 0.;
    m[15] = 1.;
}

void rotate_x_matrix (double a, Matrix m)
{
    double a_rad;
    double c, s;

    a_rad = (double) (a * RADIANS_PER_DEGREE);
    c = cos(a_rad);
    s = sin(a_rad);
    m[0] = 1.;
    m[1] = 0.;
    m[2] = 0.;
    m[3] = 0.;
    m[4] = 0.;
    m[5] = c;
    m[6] = s;
    m[7] = 0.;
    m[8] = 0.;
    m[9] = -s;
    m[10] = c;
    m[11] = 0.;
    m[12] = 0.;
    m[13] = 0.;
    m[14] = 0.;
    m[15] = 1.;
}

void rotate_y_matrix (double a, Matrix m)
{
    double a_rad;
    double c, s;

    a_rad = (double) (a * RADIANS_PER_DEGREE);
    c = cos(a_rad);
    s = sin(a_rad);
    m[0] = c;
    m[1] = 0.;
    m[2] = -s;
    m[3] = 0.;
    m[4] = 0.;
    m[5] = 1.;
    m[6] = 0.;
    m[7] = 0.;
    m[8] = s;
    m[9] = 0.;
    m[10] = c;
    m[11] = 0.;
    m[12] = 0.;
    m[13] = 0.;
    m[14] = 0.;
    m[15] = 1.;
}

void rotate_z_matrix (double a, Matrix m)
{
    double a_rad;
    double c, s;

    a_rad = (double) (a * RADIANS_PER_DEGREE);
    c = cos(a_rad);
    s = sin(a_rad);
    m[0] = c;
    m[1] = s;
    m[2] = 0.;
    m[3] = 0.;
    m[4] = -s;
    m[5] = c;
    m[6] = 0.;
    m[7] = 0.;
    m[8] = 0.;
    m[9] = 0.;
    m[10] = 1.;
    m[11] = 0.;
    m[12] = 0.;
    m[13] = 0.;
    m[14] = 0.;
    m[15] = 1.;
}

void mult_matrix (Matrix ma, Matrix mb, Matrix m)
{
    m[0] = ma[0] * mb[0] + ma[1] * mb[4] + ma[2] * mb[8] + ma[3] * mb[12];

    m[1] = ma[0] * mb[1] + ma[1] * mb[5] + ma[2] * mb[9] + ma[3] * mb[13];

    m[2] = ma[0] * mb[2] + ma[1] * mb[6] + ma[2] * mb[10] + ma[3] * mb[14];

    m[3] = ma[0] * mb[3] + ma[1] * mb[7] + ma[2] * mb[11] + ma[3] * mb[15];

    m[4] = ma[4] * mb[0] + ma[5] * mb[4] + ma[6] * mb[8] + ma[7] * mb[12];

    m[5] = ma[4] * mb[1] + ma[5] * mb[5] + ma[6] * mb[9] + ma[7] * mb[13];

    m[6] = ma[4] * mb[2] + ma[5] * mb[6] + ma[6] * mb[10] + ma[7] * mb[14];

    m[7] = ma[4] * mb[3] + ma[5] * mb[7] + ma[6] * mb[11] + ma[7] * mb[15];

    m[8] = ma[8] * mb[0] + ma[9] * mb[4] + ma[10] * mb[8] + ma[11] * mb[12];

    m[9] = ma[8] * mb[1] + ma[9] * mb[5] + ma[10] * mb[9] + ma[11] * mb[13];

    m[10] = ma[8] * mb[2] + ma[9] * mb[6] + ma[10] * mb[10] + ma[11] * mb[14];

    m[11] = ma[8] * mb[3] + ma[9] * mb[7] + ma[10] * mb[11] + ma[11] * mb[15];

    m[12] = ma[12] * mb[0] + ma[13] * mb[4] + ma[14] * mb[8] + ma[15] * mb[12];

    m[13] = ma[12] * mb[1] + ma[13] * mb[5] + ma[14] * mb[9] + ma[15] * mb[13];

    m[14] = ma[12] * mb[2] + ma[13] * mb[6] + ma[14] * mb[10] + ma[15] * mb[14];

    m[15] = ma[12] * mb[3] + ma[13] * mb[7] + ma[14] * mb[11] + ma[15] * mb[15];
}