# Difference between revisions of "GluLookAt code"

GLU - the OpenGL Utility library is an additional library that contains a handful of functions for additional tasks.
It is traditional and can be found in a lot of tutorials and examples.
gluLookAt's source code is very simple and can be replaced easily.
The code here comes from glh library (OpenGL Helper Library), LGPL license https://sourceforge.net/projects/glhlib

This function computes a matrix and multiplies it with whatever is in *matrix.
eyePosition3D is a XYZ position. This is where you are (your eye is).
center3D is the XYZ position where you want to look at.
upVector3D is a XYZ normalized vector. Quite often 0.0, 1.0, 0.0

The code presented here is slightly modified (but functional and correct) from the one in glhlib.

``` void glhLookAtf2(float *matrix, float *eyePosition3D, float *center3D, float *upVector3D)
{
float forward, side, up;
float matrix2, resultMatrix;
//------------------
forward = center3D - eyePosition3D;
forward = center3D - eyePosition3D;
forward = center3D - eyePosition3D;
NormalizeVector(forward);
//------------------
//Side = forward x up
ComputeNormalOfPlane(side, forward, upVector3D);
NormalizeVector(side);
//------------------
//Recompute up as: up = side x forward
ComputeNormalOfPlane(up, side, forward);
//------------------
matrix2 = side;
matrix2 = side;
matrix2 = side;
matrix2 = 0.0;
//------------------
matrix2 = up;
matrix2 = up;
matrix2 = up;
matrix2 = 0.0;
//------------------
matrix2 = -forward;
matrix2 = -forward;
matrix2 = -forward;
matrix2 = 0.0;
//------------------
matrix2 = matrix2 = matrix2 = 0.0;
matrix2 = 1.0;
//------------------
MultiplyMatrices4by4OpenGL_FLOAT(resultMatrix, matrix, matrix2);
glhTranslatef2(resultMatrix, -eyePosition3D, -eyePosition3D, -eyePosition3D);
//------------------
memcpy(matrix, resultMatrix, 16*sizeof(float));
}
```

The matrix you get can be uploaded with a call to glLoadMatrixf(matrix) or if you are using shaders, use glUniformMatrix4fv(location, 1, GL_FALSE, matrix).