| 3. Enter libGDX - Camera

Step 3 - Set up view and perspective transformation matrices

Now that we've got our graphical details ready for rendering, we need to create a window into our virtual 3D space by creating a viewport and perspective projection. Here are the matrix functions initially used.


1
2
3
4
5
6
7
8
float ratio = (float) width / height;
float zNear = 0.1f;
float zFar = 1000;
float fov = 0.75f; // 0.2 to 1.0
float size = (float)( zNear * Math.tan(fov / 2) );
Matrix.setLookAtM( viewMatrix, 0, -13, 5, 10, 0, 0, 0, 0, 1, 0);
Matrix.frustumM( projectionMatrix, 0, -size, size, -size / ratio, size / ratio, zNear, zFar );
Matrix.multiplyMM( MVPMatrix, 0, projectionMatrix, 0, viewMatrix, 0 );

LibGDX accomplishes the above with its Camera class. By looking at the linked class, you can see how libGDX internally creates the equivalent of the above .setLookAtM(), .frustrumM(), and .multiplyMM() functions. We set up a libGDX camera like this:

1
2
3
4
5
6
cam = new PerspectiveCamera( 67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
cam.position.set( 0f, 0f, 5f );
cam.lookAt( 0, 0, 0 );
cam.near = 0.1f;
cam.far = 1000f;
cam.update();

And that's all there is to creating a window into our virtual space. If you'd like to dig into what matrices, frustrums and projection are, I recommend looking into basic linear algebra for matrix theory and checking out this link (also listed in 'Useful Reading' section) to see the math behind projection.

Next.