## The view matrix

Combining translation, rotation and scale creates a transformation matrix. Sometimes also called the model matrix, or model to world matrix. But we're editing the mode-view matrix! So what is the view matrix?

As discussed earlyer, the view matrix is the inverse of the cameras world position! There are two things you need to know about your camera in world space, where it is (it's position) and which what it's looking (it's orientation).

Setting the view matrix with the fixed function pipeline is challenging at best, this is mainly because there is no fixed function equivalent of interting a matrix!

This means if your camera is located at (90, 20, 30) and rotated to (7, 1, 0, 0) you would have to do the following commands:

// Negate the translation of the camera
GL.Translate(-90, -20, -30);
// Negate the rotation of the camera
GL.Rotate(-7, 1, 0, 0);


Well, that wasn't so bad... Was it? No, but your world position for the camera is NEVER going to be that simple. But there is an easy way to create the world matrix of the camera, we just won't cover it until later (we cover it in the custom matrices section).

Until then, copy/paste and use this function. It is defined as follows:

void LookAt(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ, float upX, float upY, float upZ);


LookAt is actually a pretty standard OpenGL function. It's not included in the core of OpenGL, but it is so useful every programmer knows how it works!

The functions first three arguments are the position of the camera. So, if the camra is at (5, 3, 1) then the first 3 argumetns would be 5, 2, 1.

The second 3 arguments are the target that the camra is looking at. So if the camera is looking at world coordinates (0, 0, 0) then those would be the second three arguments.

The last three arguments are up. Which direction is up? Since the camera is in world space, and in world space positive Y is up, the last 3 arguments are almost always going to be 0, 1 0.

## Example

using System;
using OpenTK.Graphics.OpenGL;

namespace GameApplication {
class LookAtExample : Game {
Grid grid = null;

protected static void LookAt(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ, float upX, float upY, float upZ) {
float len = (float)Math.Sqrt(upX * upX + upY * upY + upZ * upZ);
upX /= len; upY /= len; upZ /= len;

float[] f = { targetX - eyeX, targetY - eyeY, targetZ - eyeZ };
len = (float)Math.Sqrt(f * f + f * f + f * f);
f /= len; f /= len; f /= len;

float[] s = { 0f, 0f, 0f };
s = f * upZ - f * upY;
s = f * upX - f * upZ;
s = f * upY - f * upX;
len = (float)Math.Sqrt(s * s + s * s + s * s);
s /= len; s /= len; s /= len;

float[] u = { 0f, 0f, 0f };
u = s * f - s * f;
u = s * f - s * f;
u = s * f - s * f;
len = (float)Math.Sqrt(s * u + u * u + u * u);
u /= len; u /= len; u /= len;

float[] result = {
s, u, -f, 0.0f,
s, u, -f, 0.0f,
s, u, -f, 0.0f,
0.0f, 0.0f,  0.0f, 1.0f
};

GL.MultMatrix(result);
GL.Translate(-eyeX, -eyeY, -eyeZ);
}

public override void Initialize() {
grid = new Grid();
}

public override void Update(float dTime) {

}
public override void Render() {
GL.MatrixMode(MatrixMode.Modelview);
LookAt(0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

grid.Render();
}
public override void Shutdown() {

}
}
}


Running the example produces this screen: Cool, we can now see the grid with some perspective! Why are we so close? Well, we didn't set a projection matrix yet (we don't really know how to), so anything further than 1 will simply not render as our world projection is still in NDC. But hey, at least we can look at things with angles now!