# comp.graphics.algorithms

## Subject: Re: 3D camera question

> Thanks for your reply. The mathematical notation is a little confusing to
> me, but let me see if I understand this correctly.
>
> I should have a 4x4 matrix representing the location and rotation of the
> camera. Do I plug the numbers representing the location and rotation
> directly into positions in the matrix or should I use the standard
> rotate/translate way of altering the matrix?

Directly. Assuming column vectors the matrix is:

[ Xx Yx Zx Px ]
C = [ Xy Yy Zy Py ]
[ Xz Yz Zz Pz ]
[ 0 0 0 1 ]

where the camera axes are:
X = (Xx, Xy, Xz)
Y = (Yx, Yy, Yz)
Z = (Zx, Zy, Zz)
and the camera location is:
P = (Px, Py, Pz)

If Q is of the form [Qx, Qy, Qz, 1]^T, then it represents a point and C
transforms Q to Q' of the form [Q'x, Q'y, Q'z, 1]^T (another point).
If V is of the form [Vx, Vy, Vz, 0]^T, then it represents a vector and C
transforms V to V' of the form [V'x, V'y, V'z, 0]^T (another vector).

> Once I have that matrix, I then invert it. Once inverted I multiply any
> existing world matrixes of each object by the camera matrix and that should
> be it?

Yes, but not permanently, just for the matter of vertex transforming.

> Any code/psuedocode would be really helpful.

Okay, the following is not tested. Assume matrices are initialized into
identity matrices.

Vector4 cubeLocal[8] = {
Vector4(-1, -1, -1, 1), Vector4(-1, 1, -1, 1),
Vector4(1, 1, -1, 1), Vector4(1, -1, -1, 1),
Vector4(-1, -1, 1, 1), Vector4(-1, 1, 1, 1),
Vector4(1, 1, 1, 1), Vector4(1, -1, 1, 1)};

Matrix4 cubeWorld;
scale(cubeWorld, 2, 3, 4);
rotate(cubeWorld, 0.2, 0.3, 0.5);
translate(cubeWorld, 10, 15, 100);

Matrix4 view;

Matrix4 viewInversion(invert(view));
Vector4 cubeView[8];

{
// Transform from local coordinates to view coordinates
Matrix4 localToView(viewInversion * cubeWorld);
for (int i = 0;i < 8;++i)
{
cubeView[i] = localToView * cubeLocal[i];
}
// Transform from view coordinates to screen coordinates
// Render
// Animate camera and the object
rotate(cubeWorld, 0.1, 0,2, -0.1);
rotate(view, -0.1, 0.04, 0.07);
}

--
Kalle Rutanen
http://kaba.hilvi.org