 # Please double check my object positioning/orienting code

This code is suppose to setup a matrix, that when multiplied using glMultMatrix, will set up the model coordinates for a new model, with the given position as the model’s origin, the look up vector as the model’s y-axis, and the look at vector as the model’s z-axis. How can I improve it?

OOGLE_MATRIXF is a 16 element array. The matrix is as follows:

00 01 02 03
04 05 06 07
08 09 10 11
12 13 14 15

OOGLE_POSITIONF is a record/structure as follows:
OOGLE_POSITIONF = record
vertex, lookat, lookup: OOGLE_VECTORF
end;

// Modified version of Mesa 3.21 gluLookAt function
procedure OoglePositionObject(var outmatrix: OOGLE_MATRIXF; const pos: OOGLE_POSITIONF); overload;
var
m: OOGLE_MATRIXF absolute outmatrix;
x, y, z: array[0…2] of Single;

procedure m2(row, col: Integer; v: Single);
begin
m[col*4+row] := v;
end;

procedure norm(v: array of Single);
var
mag: Single;
begin
mag := sqrt(sqr(v) + sqr(v) + sqr(v));
if mag = 0 then Exit;
v := v / mag;
v := v / mag;
v := v / mag;
end;
begin
// Z vector
z := -pos.lookat.x;
z := -pos.lookat.y;
z := -pos.lookat.z;
norm(z);

// Y vector
y := pos.lookup.x;
y := pos.lookup.y;
y := pos.lookup.z;
norm(y);

// X vector = Y cross Z
x := y * z - y * z;
x := -y * z + y * z;
x := y * z - y * z;

// Recompute Y = Z cross X
y := z * x - z * x;
y := -z * x + z * x;
y := z * x - z * x;

{ Cross product gives area of parallelogram, which
is < 1 for non-perpendicular unit-length vectors,
so normalize x and y here }
norm(x);
norm(y);

// Set matrix values
m2(0, 0, x);
m2(0, 1, x);
m2(0, 2, x);
m2(0, 3, 0);

m2(1, 0, y);
m2(1, 1, y);
m2(1, 2, y);
m2(1, 3, 0);

m2(2, 0, z);
m2(2, 1, z);
m2(2, 2, z);
m2(2, 3, 0);

m2(3, 0, pos.vertex.x);
m2(3, 1, pos.vertex.y);
m2(3, 2, pos.vertex.z);
m2(3, 3, 1);
end;