Calculating The ModelView Matrix For OpenGL

Sometimes its very useful to create a modelview matrix without modifying the OpenGL matrix stack. Methods that will modify the matrix stack are for example gluLookAt or similar methods.

I wrote my own method that calculates the modelview matrix for java but i guess it can be easily adapted. Calculating a modelview matrix in this way allows one to pass the generated matrix to a glsl shader using a simple matrix uniform. Using vertex processing in a vertex shader is even the preferred way of dealing with modelview transformations in OpenGL 3.1.

A full example can be found <a href="">here</a>.

	public float[] calculateModelViewMatrix(Vector3f eye, Vector3f center,
			Vector3f up) {

		// Create a new matrix that will hold the modelview matrix
		Matrix4fe mv = new Matrix4fe();

		// Set the identity matrix

		// Calculate the forward vector
		Vector3f forward = new Vector3f();
		forward.sub(center, eye);

		// Calculate the side vector by calculating the cross product of the
		// forward and up vector
		Vector3f side = new Vector3f();
		side.cross(forward, up);

		// Recalculate up
		up.cross(side, forward);

		// Set the matrix fields
		mv.setElement(0, 0, side.getX());
		mv.setElement(0, 1, side.getY());
		mv.setElement(0, 2, side.getZ());

		mv.setElement(1, 0, up.getX());
		mv.setElement(1, 1, up.getY());
		mv.setElement(1, 2, up.getZ());

		mv.setElement(2, 0, -forward.getX());
		mv.setElement(2, 1, -forward.getY());
		mv.setElement(2, 2, -forward.getZ());

		// Negate the eye vector and create a translation matrix
		Matrix4f translationMatrix = new Matrix4f();

		// Apply the translation

		return mv.getFloatArray();